Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prod deploy #84

Merged
merged 20 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/huma-sdk/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ in Huma's pools that can be drawn down by the borrower.</p>
* [.getCreditEventsForUser(userAddress, chainId, poolName, poolType, event)](#SubgraphService.getCreditEventsForUser) ⇒ <code>Promise.&lt;Array.&lt;CreditEventPayload&gt;&gt;</code>
* [.getLastFactorizedAmountFromPool(userAddress, chainId, poolName, poolType)](#SubgraphService.getLastFactorizedAmountFromPool) ⇒ <code>Promise.&lt;number&gt;</code>
* [.getRWReceivableInfo(userAddress, chainId, poolName, poolType, pagination)](#SubgraphService.getRWReceivableInfo) ⇒ <code>Promise.&lt;RealWorldReceivableInfoBase&gt;</code>
* [.getPoolStats(chainId, pool)](#SubgraphService.getPoolStats) ⇒ <code>Promise.&lt;{PoolStats}&gt;</code>

<a name="SubgraphService.getSubgraphUrlForChainId"></a>

Expand Down Expand Up @@ -534,6 +535,19 @@ in Huma's pools that can be drawn down by the borrower.</p>
| poolType | <code>POOL\_TYPE</code> | <p>The type of the pool.</p> |
| pagination | [<code>Pagination</code>](#Pagination) | <p>The pagination option.</p> |

<a name="SubgraphService.getPoolStats"></a>

### SubgraphService.getPoolStats(chainId, pool) ⇒ <code>Promise.&lt;{PoolStats}&gt;</code>
<p>Returns the pool's stats.</p>

**Kind**: static method of [<code>SubgraphService</code>](#SubgraphService)
**Returns**: <code>Promise.&lt;{PoolStats}&gt;</code> - <p>The pool's stats info.</p>

| Param | Type | Description |
| --- | --- | --- |
| chainId | <code>number</code> | <p>The ID of the chain.</p> |
| pool | <code>string</code> | <p>The address of the pool.</p> |

<a name="defaultWrapper"></a>

## defaultWrapper()
Expand Down
51 changes: 51 additions & 0 deletions packages/huma-sdk/src/services/SubgraphService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,56 @@ function getRWReceivableInfo(
})
}

type PoolStats = {
id: string
totalPoolAssets: number
amountCreditOriginated: number
amountCreditRepaid: number
amountCreditDefaulted: number
}

/**
* Returns the pool's stats.
*
* @memberof SubgraphService
* @param {number} chainId - The ID of the chain.
* @param {string} pool - The address of the pool.
* @returns {Promise<{PoolStats}>} The pool's stats info.
*/
function getPoolStats(
chainId: number,
pool: string,
): Promise<PoolStats | undefined> {
const url = PoolSubgraphMap[chainId]?.subgraph
if (!url) {
return Promise.resolve(undefined)
}

const PoolStatsQuery = `
query {
poolStat(id:"${pool?.toLowerCase()}") {
id
amountCreditOriginated
amountCreditRepaid
amountCreditDefaulted
totalPoolAssets
}
}`

return requestPost<{
errors?: unknown
data: { poolStat: PoolStats }
}>(url, JSON.stringify({ query: PoolStatsQuery }), {
withCredentials: false,
}).then((res) => {
if (res.errors) {
console.error(res.errors)
return undefined
}
return res.data.poolStat
})
}

/**
* An object that contains functions to interact with Huma's Subgraph storage.
* @namespace SubgraphService
Expand All @@ -205,4 +255,5 @@ export const SubgraphService = {
getCreditEventsForUser,
getLastFactorizedAmountFromPool,
getRWReceivableInfo,
getPoolStats,
}
44 changes: 44 additions & 0 deletions packages/huma-sdk/tests/services/SubgraphService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,47 @@ describe('getRWReceivableInfo', () => {
expect(result).toStrictEqual(rwreceivables)
})
})

describe('getPoolStats', () => {
beforeEach(() => {
jest.resetAllMocks()
})

it('should return undefined if no subgraph url is found', async () => {
const chainId = 12 // ChainId without receivables Subgraph url
const pool = '0xc866A11cf6A3D178624Ff46B8A49202206A7c51B'

const result = await SubgraphService.getPoolStats(chainId, pool)
expect(result).toStrictEqual(undefined)
})

it('should return undefined if requestPost returns error', async () => {
;(requestPost as jest.Mock).mockResolvedValue({ errors: 'errors' })

const chainId = ChainEnum.Goerli
const pool = '0xc866A11cf6A3D178624Ff46B8A49202206A7c51B'

const result = await SubgraphService.getPoolStats(chainId, pool)
expect(result).toStrictEqual(undefined)
})

it('should return pool stats', async () => {
const pool = '0xc866A11cf6A3D178624Ff46B8A49202206A7c51B'
const poolStat = {
id: pool,
amountCreditOriginated: 300,
amountCreditRepaid: 400,
amountCreditDefaulted: 500,
}
;(requestPost as jest.Mock).mockResolvedValue({
data: {
poolStat,
},
})

const chainId = ChainEnum.Goerli

const result = await SubgraphService.getPoolStats(chainId, pool)
expect(result).toStrictEqual(poolStat)
})
})
2 changes: 1 addition & 1 deletion packages/huma-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@mui/system": "^5.0.6",
"@mui/x-date-pickers": "^5.0.7",
"@reduxjs/toolkit": "^1.8.6",
"@requestnetwork/multi-format": "^0.15.10",
"@requestnetwork/multi-format": "0.15.10",
"@types/utf8": "^3.0.1",
"@walletconnect/ethereum-provider": "1.8.0",
"@walletconnect/jsonrpc-http-connection": "^1.0.3",
Expand Down
25 changes: 16 additions & 9 deletions packages/huma-shared/src/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { BigNumber, ethers } from 'ethers'
import { isEmpty } from './common'
import { scientificToDecimal } from './scientificToDecimal'

const moneyFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
})

const numberFormatter = new Intl.NumberFormat('en-US')

export const formatMoney = (num: number | string | undefined) => {
export const formatMoney = (
num: number | string | undefined,
notation?: Intl.NumberFormatOptions['notation'],
) => {
if (isEmpty(num) || Number.isNaN(num)) {
return num
}
Expand All @@ -20,6 +17,14 @@ export const formatMoney = (num: number | string | undefined) => {
if (numCast > 1_000) {
numCast = Math.round(numCast)
}

const moneyFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
notation,
})

return moneyFormatter.format(numCast)
}

Expand Down Expand Up @@ -55,13 +60,15 @@ export const downScale = <T = string>(
}

export const upScale = <T = string>(
num: string | number,
num: string | number | BigNumber,
decimals?: number,
): T => {
if (isEmpty(num) || isEmpty(decimals)) {
return num as T
}
const result = Number(num) * 10 ** decimals!
const result = BigNumber.isBigNumber(num)
? num.mul(BigNumber.from(10).pow(decimals!))
: Number(num) * 10 ** decimals!
if (typeof num === 'string') {
return String(result) as T
}
Expand Down
76 changes: 61 additions & 15 deletions packages/huma-shared/src/utils/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum POOL_NAME {
BSOS = 'BSOS',
ImpactMarket = 'ImpactMarket',
Symplifi = 'Symplifi',
Raincards = 'Raincards',
}

export enum POOL_TYPE {
Expand Down Expand Up @@ -64,8 +65,9 @@ export type PoolInfoType = {
}
extra?: {
subgraph?: string
superTokens?: { id: string; symbol: string; decimals: number }[]
superToken?: { id: string; symbol: string; decimals: number }
borrower?: string // For single borrower pools
rwrUploader?: string // For single borrower pools where receivables are uploaded by a different wallet
hidden?: boolean // For pools that shouldn't be displayed in the UI
order?: number // Ordering in the pool list. Null values are sorted last.
disableBorrow?: boolean
Expand Down Expand Up @@ -138,6 +140,14 @@ export const PoolMap: PoolMapType = {
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
estAPY: '20%',
},
[POOL_NAME.Raincards]: {
name: 'Raincards Pool',
borrowDesc:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
lendDesc:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
estAPY: '20%',
},
},
[POOL_TYPE.Invoice]: {
[POOL_NAME.RequestNetwork]: {
Expand Down Expand Up @@ -313,6 +323,39 @@ export const PoolContractMap: PoolContractMapType = {
},
},
},
[POOL_TYPE.Stream]: {
[POOL_NAME.Superfluid]: {
basePoolConfig: '0x22C024496036A8e97F93E14efa0d8379192bb22c',
pool: '0xF713B5203Cb6f3223830De218c2ed89Ee654b94B',
poolProcessor: '0x6E2f33b6d3F1E2048d078984f7FFF847C0Ed3bEd',
poolFeeManager: '0xd5FD3F917cf8901BeB102d81504033C748c87F19',
poolUnderlyingToken: {
address: '0x2791bca1f2de4661ed88a30c99a7a9449aa84174',
symbol: 'USDC',
decimals: 6,
icon: 'USDC',
},
assetAddress: '0xa8B0362cfE0c8e4fd1D74c3512348d6f48d71080',
poolName: POOL_NAME.Superfluid,
poolType: POOL_TYPE.Stream,
poolAbi: STEAM_FACTORING_POOL_ABI,
basePoolConfigAbi: BASE_POOL_CONFIG_ABI,
poolAssetAbi: TRADABLE_STREAM_ABI,
HDT: {
address: '0x30b1f6bca8c6c742ede0ccb4eceb22af4cba58ef',
abi: HDT_ABI,
},
extra: {
subgraph:
'https://api.thegraph.com/subgraphs/name/superfluid-finance/protocol-v1-matic',
superToken: {
id: '0xcaa7349cea390f89641fe306d93591f87595dc1f',
symbol: 'USDCx',
decimals: 18,
},
},
},
},
},
[ChainEnum.Goerli]: {
[POOL_TYPE.CreditLine]: {
Expand Down Expand Up @@ -458,6 +501,7 @@ export const PoolContractMap: PoolContractMapType = {
disableBorrow: true,
detailsPage: true,
borrower: '0x10FB65dc26a7aCC7CFB4eA3b6E007c8C77591486',
rwrUploader: '0x4c6388346f2a3af2d64461339a5cdd3a3d63ccf5',
},
},
[POOL_NAME.ArfCreditPool1]: {
Expand All @@ -479,6 +523,8 @@ export const PoolContractMap: PoolContractMapType = {
abi: HDT_ABI,
},
extra: {
borrower: '0xea57a8a51377752ffddaa3db4d13ce8f97677f2d',
rwrUploader: '0xea57a8a51377752ffddaa3db4d13ce8f97677f2d',
disableBorrow: true,
detailsPage: true,
},
Expand Down Expand Up @@ -511,33 +557,31 @@ export const PoolContractMap: PoolContractMapType = {
extra: {
subgraph:
'https://api.thegraph.com/subgraphs/name/superfluid-finance/protocol-v1-mumbai',
superTokens: [
{
id: '0x42bb40bf79730451b11f6de1cba222f17b87afd7',
symbol: 'fUSDCx',
decimals: 18,
},
],
superToken: {
id: '0x42bb40bf79730451b11f6de1cba222f17b87afd7',
symbol: 'fUSDCx',
decimals: 18,
},
},
},
},
[POOL_TYPE.CreditLine]: {
[POOL_NAME.ArfUSDCMigrationTest]: {
basePoolConfig: '0xe8338a5e3e58b425249f82594c82b42c2df4c5e9',
pool: '0xbb1b50e1ec5835b3c58944e820e7a5e136141ddc',
poolFeeManager: '0x9f667f613C16542aC8b1e502F4D796774F623D86',
[POOL_NAME.Raincards]: {
basePoolConfig: '0x10b7CBe54178eB6C81b2D84Ac073747BcA744F6C',
pool: '0xf8065dA82cC990325059c436939c6a90C322E9Dd',
poolFeeManager: '0x87534B96FD15EbD6Aa0456F45045B541e5E8889a',
poolUnderlyingToken: {
address: '0x9999f7fea5938fd3b1e26a12c3f2fb024e194f97',
address: '0xb961c37ABDDA55929327fa9d20eBDE6BB8B1348E',
symbol: 'USDC',
decimals: 6,
icon: 'USDC',
},
poolName: POOL_NAME.ArfUSDCMigrationTest,
poolName: POOL_NAME.Raincards,
poolType: POOL_TYPE.CreditLine,
poolAbi: BASE_CREDIT_POOL_ABI,
basePoolConfigAbi: BASE_POOL_CONFIG_ABI,
HDT: {
address: '0x8bce02521622222Ee13D1Ce2c5E4CCab52ce24Bb',
address: '0x8Ec8f8AFE179032e2929C49eF4f8Ea2d18245B9a',
abi: HDT_ABI,
},
},
Expand Down Expand Up @@ -599,6 +643,8 @@ export const SupplementaryContractsMap: {
}
} = {
[ChainEnum.Polygon]: {
[SupplementaryContracts.MultiSend]:
'0xDa21D2Be30353EC6Aa5AcD37999806cCefaa4C6A',
[SupplementaryContracts.RealWorldReceivable]:
'0xCf67CcEaC38b5E1880d62b5DB531Ab1E77614E3D',
},
Expand Down
8 changes: 8 additions & 0 deletions packages/huma-shared/tests/utils/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('upScale', () => {
expect(upScale('123.45', 2)).toBe('12345')
expect(upScale(123.45, 2)).toBe(12345)
})

it('returns the upscaled number if the input is a valid number and decimals would cause overflow', () => {
expect(
upScale<BigNumber>(BigNumber.from(10), 18).eq(
BigNumber.from('10000000000000000000'),
),
).toBe(true)
})
})

describe('toBigNumber', () => {
Expand Down
11 changes: 7 additions & 4 deletions packages/huma-shared/tests/utils/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
} from '../../src/utils/pool'

describe('getPoolInfo', () => {
it('returns null if chainId is undefined', () => {
expect(
getPoolInfo(undefined, POOL_TYPE.Stream, POOL_NAME.Superfluid),
).toBeNull()
it('returns default if chainId is undefined', () => {
const poolInfo = getPoolInfo(
undefined,
POOL_TYPE.Stream,
POOL_NAME.Superfluid,
)
expect(poolInfo?.pool).toBe('0xF713B5203Cb6f3223830De218c2ed89Ee654b94B')
})

it('returns null if poolType or poolName is not found', () => {
Expand Down
Loading