Skip to content

Commit

Permalink
Merge pull request #125 from embrio-tech/main
Browse files Browse the repository at this point in the history
20230327 Sprint 29
  • Loading branch information
hieronx authored Mar 28, 2024
2 parents ccf7eca + 9d921c7 commit 1589211
Show file tree
Hide file tree
Showing 24 changed files with 547 additions and 328 deletions.
4 changes: 2 additions & 2 deletions .envrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export COMPOSE_PROFILES=subql-cfg
export COMPOSE_PROFILES=subql-eth
export DB_USER=postgres
export DB_PASS=postgres
export DB_DATABASE=postgres
export DB_HOST=localhost
export DB_PORT=5432
export SUBQL_DB_USER=$DB_USER
export SUBQL_DB_PASS=$DB_PASS
export CHAIN_ID="demo"
export CHAIN_ID="centrifuge"
2 changes: 1 addition & 1 deletion chains-cfg/development-embrio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ network:
file: ./dist/chaintypes.js
dataSources:
- kind: substrate/Runtime
startBlock: 1548180 #5th October 2023
startBlock: 2080500 #1549700
7 changes: 5 additions & 2 deletions docker/pg-Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
FROM postgres:12-alpine
FROM postgres:16-alpine

# Variables needed at runtime to configure postgres and run the initdb scripts
ENV POSTGRES_DB 'postgres'
ENV POSTGRES_USER 'postgres'
ENV POSTGRES_PASSWORD 'postgres'

# Copy in the load-extensions script
COPY docker/load-extensions.sh /docker-entrypoint-initdb.d/
COPY docker/load-extensions.sh /docker-entrypoint-initdb.d/

# Convert line endings to LF
RUN sed -i 's/\r$//' /docker-entrypoint-initdb.d/load-extensions.sh && chmod +x /docker-entrypoint-initdb.d/load-extensions.sh
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@subql/testing": "latest",
"@subql/types": "latest",
"@types/jest": "^29.1.2",
"@types/node-fetch": "^2.6.11",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"abab": "^2.0.6",
Expand Down
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Pool @entity {
# It's not possible to simply retrieve all entities, but it is supported
# to get all entities by a field. Hence we add the same type field to all
# pools, so we can do Pool.getByType("POOL").
blockchain: Blockchain! @index
type: String! @index
isActive: Boolean! @index

Expand All @@ -17,6 +18,11 @@ type Pool @entity {
currency: Currency
metadata: String

name: String
assetClass: String
assetSubclass: String
icon: String

minEpochTime: Int
maxPortfolioValuationAge: Int

Expand Down Expand Up @@ -102,6 +108,7 @@ type PoolSnapshot @entity {

type Tranche @entity {
id: ID! #poolId-trancheId
blockchain: Blockchain! @index
type: String! @index
pool: Pool! @index
trancheId: String!
Expand Down Expand Up @@ -331,13 +338,15 @@ type Asset @entity {
id: ID! # poolId - assetId
createdAt: Date!

blockchain: Blockchain! @index
type: AssetType!
valuationMethod: AssetValuationMethod!

collateralNftClassId: BigInt
collateralNftItemId: BigInt

metadata: String
name: String

advanceRate: BigInt
collateralValue: BigInt
Expand Down Expand Up @@ -469,11 +478,14 @@ type PoolFee @entity {
type: PoolFeeType!
status: PoolFeeStatus!
isActive: Boolean! @index
blockchain: Blockchain! @index

createdAt: Date
createdAtBlockNumber: Int
createdAtEpoch: Int

name: String

pool: Pool! @index

sumChargedAmount: BigInt #Applies to Fixed ONLY
Expand Down
99 changes: 74 additions & 25 deletions src/chaintypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,47 @@ import type { OverrideBundleDefinition } from '@polkadot/types/types'

/* eslint-disable sort-keys */

const latestTypes = {
ActiveLoanInfoV2: {
activeLoan: 'PalletLoansEntitiesLoansActiveLoan',
presentValue: 'Balance',
outstandingPrincipal: 'Balance',
outstandingInterest: 'Balance',
currentPrice: 'Option<Balance>',
},
ActiveLoanInfoV1: {
activeLoan: 'PalletLoansEntitiesLoansActiveLoan',
presentValue: 'Balance',
outstandingPrincipal: 'Balance',
outstandingInterest: 'Balance',
},
InvestmentPortfolio: {
poolCurrencyId: 'CfgTypesTokensCurrencyId',
pendingInvestCurrency: 'Balance',
claimableTrancheTokens: 'Balance',
freeTrancheTokens: 'Balance',
reservedTrancheTokens: 'Balance',
pendingRedeemTrancheTokens: 'Balance',
claimableCurrency: 'Balance',
},
PoolNav: {
navAum: 'Balance',
navFees: 'Balance',
reserve: 'Balance',
total: 'Balance',
},
PoolFeesList: 'Vec<PoolFeesOfBucket>',
PoolFeesOfBucket: {
bucket: 'CfgTraitsFeePoolFeeBucket',
fees: 'Vec<PoolFee>',
},
}

const definitions: OverrideBundleDefinition = {
types: [
{
minmax: [undefined, undefined],
types: {
ActiveLoanInfo: {
activeLoan: 'PalletLoansEntitiesLoansActiveLoan',
presentValue: 'Balance',
outstandingPrincipal: 'Balance',
outstandingInterest: 'Balance',
},
PoolNav: {
navAum: 'Balance',
navFees: 'Balance',
reserve: 'Balance',
total: 'Balance',
},
PoolFeesOfBucket: {
bucket: 'CfgTraitsFeePoolFeeBucket',
fees: ' Vec<CfgTypesPoolsPoolFee>',
},
PoolFeesList: 'Vec<PoolFeesOfBucket>',
},
types: latestTypes,
},
],
runtime: {
Expand All @@ -39,7 +57,7 @@ const definitions: OverrideBundleDefinition = {
type: 'u64',
},
],
type: 'Vec<(u64, ActiveLoanInfo)>',
type: 'Vec<(u64, ActiveLoanInfoV2)>',
},
portfolio_loan: {
description: 'Get active pool loan',
Expand All @@ -58,6 +76,35 @@ const definitions: OverrideBundleDefinition = {
},
version: 2,
},
{
methods: {
portfolio: {
description: 'Get active pool loan',
params: [
{
name: 'pool_id',
type: 'u64',
},
],
type: 'Vec<(u64, ActiveLoanInfoV1)>',
},
portfolio_loan: {
description: 'Get active pool loan',
params: [
{
name: 'pool_id',
type: 'u64',
},
{
name: 'loan_id',
type: 'u64',
},
],
type: 'Option<PalletLoansEntitiesLoansActiveLoan>',
},
},
version: 1,
},
],
PoolsApi: [
{
Expand Down Expand Up @@ -139,10 +186,12 @@ const definitions: OverrideBundleDefinition = {
},
}

// Fix for LoansApi old runtime v1
const loansApiRuntime = definitions['runtime']['LoansApi']
loansApiRuntime.push({ ...loansApiRuntime[0], version: 1 })

export default {
typesBundle: { spec: { 'centrifuge-devel': definitions, altair: definitions, centrifuge: definitions } },
typesBundle: {
spec: {
'centrifuge-devel': definitions,
altair: definitions,
centrifuge: definitions,
},
},
}
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { bnToBn } from '@polkadot/util'

export const IPFS_NODE = 'https://centrifuge.mypinata.cloud'
export const SNAPSHOT_INTERVAL_SECONDS = 3600 * 24
export const WAD_DIGITS = 18
export const WAD = bnToBn(10).pow(bnToBn(WAD_DIGITS))
Expand All @@ -8,6 +9,7 @@ export const RAY = bnToBn(10).pow(bnToBn(RAY_DIGITS))
export const CPREC = (digits: number) => bnToBn(10).pow(bnToBn(digits))
export const DAIMainnetAddress = '0x6b175474e89094c44da98b954eedeac495271d0f'
export const multicallAddress = '0xeefba1e63905ef1d7acba5a8513c70307c1ce441'

export const tinlakePools = [
{
id: '0x09e43329552c9d81cf205fd5f44796fbc40c822e',
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/ipfsFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { IPFS_NODE } from '../config'

export const cid = new RegExp(
'(Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})$'
)

export async function readIpfs<T extends Record<string, unknown>>(ipfsId: string): Promise<T> {
const uri = `${IPFS_NODE}/ipfs/${ipfsId}`
logger.info(`Fetching ${uri}`)
const response = await fetch(uri, { method: 'GET' })
return await response.json()
}
27 changes: 13 additions & 14 deletions src/helpers/paginatedGetter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { errorHandler } from './errorHandler'
import type { Entity } from '@subql/types-core'
import type { Entity, FieldsExpression } from '@subql/types-core'

type StoreArgs = Parameters<typeof store.getByField>

async function _paginatedGetter(entity: StoreArgs[0], field: StoreArgs[1], value: StoreArgs[2]): Promise<Entity[]> {
let results: Entity[] = []
export async function paginatedGetter<T extends Entity>(
entity: T['_name'],
filter: FieldsExpression<T>[]
): Promise<T[]> {
const results: T[] = []
const batch = 100
let amount = 0
let entities: Entity[]
let entities: T[]
do {
entities = (await store.getByField(entity, field, value, {
entities = (await store.getByField(entity, filter[0][0] as string, filter[0][2], {
// TODO: Revert back to getByFields
offset: amount,
limit: batch,
})) as Entity[]
results = results.concat(entities)
amount += entities.length
} while (entities.length > 0)
return results
})) as T[]
amount = results.push(...entities)
} while (entities.length === batch)
return filter.length > 1 ? results.filter( entity => entity[filter[1][0]] === filter[1][2]) : results
}
export const paginatedGetter = errorHandler(_paginatedGetter)
5 changes: 3 additions & 2 deletions src/helpers/stateSnapshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SubstrateBlock } from '@subql/types'
import { PoolService } from '../mappings/services/poolService'
import { substrateStateSnapshotter } from './stateSnapshot'
import { Pool, PoolSnapshot } from '../types'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const getByField = store.getByField as jest.Mock
Expand Down Expand Up @@ -45,15 +46,15 @@ describe('Given a populated pool,', () => {
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
await substrateStateSnapshotter('Pool', 'PoolSnapshot', block, undefined, 'active', true)
await substrateStateSnapshotter<Pool,PoolSnapshot>('Pool', 'PoolSnapshot', block, 'isActive', true)
expect(store.getByField).toHaveBeenNthCalledWith(1, 'Pool', 'active', true, expect.anything())
})

test('when a foreigh key is set, then the correct foreign key value is set on the snapshot', async () => {
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
await substrateStateSnapshotter('Pool', 'PoolSnapshot', block, 'poolId')
await substrateStateSnapshotter<Pool, PoolSnapshot>('Pool', 'PoolSnapshot', block, 'type', 'ALL', 'poolId')
expect(store.set).toHaveBeenNthCalledWith(
2,
'PoolSnapshot',
Expand Down
Loading

0 comments on commit 1589211

Please sign in to comment.