Skip to content

Commit

Permalink
165 implement transfer inout mechanism (#178)
Browse files Browse the repository at this point in the history
* feat: implement transfer in/out mechanism
Fixes #165

* feat: implement transfer in/out mechanism
Fixes #165

* chore: implement transfer in/out mechanism
Fixes #165

* feat: adapt data model to be chain independent

* fix: end of line

* feat: implement transfer in/out mechanism
Fixes #165

* feat: implement new currency model for orml tokens

* fix: implement transfer in/out mechanism
Fixes #165

* fix: implement transfer in/out mechanism
Fixes #165

* fix: implement transfer in/out mechanism
Fixes #165

* fix: implement transfer in/out mechanism
Fixes #165

* fix: update tests
  • Loading branch information
filo87 authored Dec 19, 2023
1 parent 5f48272 commit 91207d6
Show file tree
Hide file tree
Showing 30 changed files with 1,048 additions and 992 deletions.
30 changes: 15 additions & 15 deletions chains-cfg/base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ dataSources:
filter:
module: investments
method: RedeemOrdersCollected
- handler: handleProxyAdded
kind: substrate/EventHandler
filter:
module: proxy
method: ProxyAdded
- handler: handleProxyRemoved
kind: substrate/EventHandler
filter:
module: proxy
method: ProxyRemoved
- handler: handleProxyPureCreated
kind: substrate/EventHandler
filter:
module: proxy
method: PureCreated
# - handler: handleProxyAdded
# kind: substrate/EventHandler
# filter:
# module: proxy
# method: ProxyAdded
# - handler: handleProxyRemoved
# kind: substrate/EventHandler
# filter:
# module: proxy
# method: ProxyRemoved
# - handler: handleProxyPureCreated
# kind: substrate/EventHandler
# filter:
# module: proxy
# method: PureCreated
- handler: handleLoanCreated
kind: substrate/EventHandler
filter:
Expand Down
4 changes: 2 additions & 2 deletions chains-cfg/development-embrio.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: 'centrifuge-subql'
repository: 'https://github.com/embrio-tech/centrifuge-subql'
network:
endpoint: wss://fullnode.demo.k-f.dev/public-ws
endpoint: wss://fullnode.demo.k-f.dev/public-ws #wss://node-7118620155331796992.gx.onfinality.io/ws?apikey=00538f2d-6297-44e3-8812-4b9d579524b2
chainId: '0xe0e40f62affe742eb92d75c07830671ef3e0cc6efc1ecaf81cf34a28148e91f0'
chaintypes:
file: ./dist/chaintypes.js
dataSources:
- kind: substrate/Runtime
startBlock: 1534900 #1st October 2023
startBlock: 1548180 #5th October 2023
2 changes: 1 addition & 1 deletion chains-evm/development-embrio.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ name: 'centrifuge-subql'
repository: 'https://github.com/embrio-tech/centrifuge-subql'
network:
chainId: '5' #Goerli
endpoint: "https://goerli.infura.io/v3/${ETH_NODE_API_KEY}"
endpoint: "https://goerli.infura.io/v3/a4ba76cd4be643618572e7467a444e3a"
dictionary: "https://dict-tyk.subquery.network/query/eth-goerli"
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "subql build",
"build:dev": "subql build --mode=dev",
"prepack": "rm -rf dist && subql build",
"test": "jest",
"codegen": "subql codegen",
Expand All @@ -31,24 +32,24 @@
"@jest/globals": "^29.2.0",
"@polkadot/api": "^10",
"@polkadot/typegen": "^10",
"@subql/cli": "^4.1.0",
"@subql/cli": "latest",
"@subql/testing": "latest",
"@subql/types": "^3.2.0",
"@subql/types": "latest",
"@types/jest": "^29.1.2",
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.0.1",
"husky": "^7.0.0",
"jest": "^29.2.0",
"lint-staged": "^13.2.0",
"node-fetch": "2.6.7",
"prettier": "^2.7.0",
"prettier-eslint": "^15.0.1",
"prettier": "^3.1.1",
"prettier-eslint": "^16.2.0",
"ts-jest": "^29.0.3",
"ts-node": "^8.6.2",
"typescript": "^5.2.2"
"typescript": "^4.1.3"
},
"lint-staged": {
"src/**/*.{js,ts}": [
Expand Down
110 changes: 59 additions & 51 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,30 @@ type Pool @entity {
# to get all entities by a field. Hence we add the same type field to all
# pools, so we can do Pool.getByType("POOL").
type: String! @index
isActive: Boolean! @index

createdAt: Date!
createdAtBlockNumber: Int!

currency: Currency!
metadata: String!
createdAt: Date
createdAtBlockNumber: Int

minEpochTime: Int!
maxPortfolioValuationAge: Int!
currency: Currency
metadata: String

minEpochTime: Int
maxPortfolioValuationAge: Int

# This cannot reference Pool directly, since this leads to a cyclic dependency.
currentEpoch: Int!
currentEpoch: Int
lastEpochClosed: Int
lastEpochExecuted: Int

#States
portfolioValuation: BigInt!
totalReserve: BigInt!
availableReserve: BigInt!
maxReserve: BigInt!
sumDebt: BigInt!
value: BigInt!
portfolioValuation: BigInt
totalReserve: BigInt
availableReserve: BigInt
maxReserve: BigInt
sumDebt: BigInt
value: BigInt

# Aggregated transaction data over the last period
sumBorrowedAmountByPeriod: BigInt
Expand All @@ -39,9 +41,9 @@ type Pool @entity {
sumRedeemedAmountByPeriod: BigInt
sumNumberOfLoansByPeriod: BigInt

sumNumberOfActiveLoans: BigInt!
sumDebtOverdue: BigInt!
sumDebtWrittenOffByPeriod: BigInt!
sumNumberOfActiveLoans: BigInt
sumDebtOverdue: BigInt
sumDebtWrittenOffByPeriod: BigInt

# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
Expand All @@ -61,12 +63,12 @@ type PoolSnapshot @entity {
periodStart: Date! @index

#States
portfolioValuation: BigInt!
totalReserve: BigInt!
availableReserve: BigInt!
maxReserve: BigInt!
sumDebt: BigInt!
value: BigInt!
portfolioValuation: BigInt
totalReserve: BigInt
availableReserve: BigInt
maxReserve: BigInt
sumDebt: BigInt
value: BigInt

# Aggregated transaction data over the last period
sumBorrowedAmountByPeriod: BigInt
Expand All @@ -75,7 +77,7 @@ type PoolSnapshot @entity {
sumRedeemedAmountByPeriod: BigInt
sumNumberOfLoansByPeriod: BigInt

sumNumberOfActiveLoans: BigInt!
sumNumberOfActiveLoans: BigInt

# Cumulated transaction data since pool creation
sumBorrowedAmount: BigInt
Expand All @@ -85,30 +87,30 @@ type PoolSnapshot @entity {

type Tranche @entity {
id: ID! #poolId-trancheId
index: Int!
type: String! @index
pool: Pool! @index

trancheId: String!

isResidual: Boolean!
seniority: Int!
index: Int

isResidual: Boolean
seniority: Int
interestRatePerSec: BigInt
minRiskBuffer: BigInt

isActive: Boolean! @index
isActive: Boolean @index

tokenSupply: BigInt
tokenPrice: BigInt
sumDebt: BigInt

sumOutstandingInvestOrdersByPeriod: BigInt!
sumOutstandingRedeemOrdersByPeriod: BigInt!
sumOutstandingRedeemOrdersCurrencyByPeriod: BigInt!
sumOutstandingInvestOrdersByPeriod: BigInt
sumOutstandingRedeemOrdersByPeriod: BigInt
sumOutstandingRedeemOrdersCurrencyByPeriod: BigInt

sumFulfilledInvestOrdersByPeriod: BigInt!
sumFulfilledRedeemOrdersByPeriod: BigInt!
sumFulfilledRedeemOrdersCurrencyByPeriod: BigInt!
sumFulfilledInvestOrdersByPeriod: BigInt
sumFulfilledRedeemOrdersByPeriod: BigInt
sumFulfilledRedeemOrdersCurrencyByPeriod: BigInt

yield30DaysAnnualized: BigInt
yield90DaysAnnualized: BigInt
Expand Down Expand Up @@ -213,9 +215,9 @@ type InvestorTransaction @entity {
account: Account! @index #Account @index
pool: Pool! @index
tranche: Tranche! @index
epochNumber: Int!
epochNumber: Int
timestamp: Date!
epoch: Epoch! @index
epoch: Epoch @index

type: InvestorTransactionType!
tokenAmount: BigInt
Expand Down Expand Up @@ -256,6 +258,8 @@ type BorrowerTransaction @entity {

type Account @entity {
id: ID!
chain: Blockchain!
evmAddress: String @index

pureProxies: [PureProxy] @derivedFrom(field: "account")

Expand All @@ -267,12 +271,6 @@ type Account @entity {
currencyBalances: [CurrencyBalance] @derivedFrom(field: "account")
}

type EvmAccount @entity {
id: ID! #Address
account: Account! @index
chainId: Int!
}

type TrancheBalance @entity {
id: ID! # address - pool id - tranche id
account: Account! @index
Expand All @@ -288,14 +286,6 @@ type TrancheBalance @entity {
sumRedeemCollectedAmount: BigInt!
}

type CurrencyBalance @entity {
id: ID! # address - currencyId
account: Account! @index

currency: Currency!
amount: BigInt!
}

enum LoanStatus {
CREATED
ACTIVE
Expand Down Expand Up @@ -375,6 +365,24 @@ type Proxy @entity {
}

type Currency @entity {
id: ID!
id: ID! #TODO: chainId - currencyType - [currencySpec]
chain: Blockchain!
decimals: Int!

tokenAddress: String

pool: Pool
tranche: Tranche
}

type CurrencyBalance @entity {
id: ID! # address - currencyId
account: Account! @index

currency: Currency!
amount: BigInt!
}

type Blockchain @entity {
id: ID! #EVM chainId
}
6 changes: 4 additions & 2 deletions src/helpers/stateSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const poolId = '123456789',
blockNumber = 11234

describe('Given a populated pool,', () => {
const pool = PoolService.init(poolId, 'AUSD', BigInt(6000), 23, 12, timestamp, blockNumber)
const pool = PoolService.seed(poolId)
pool.init('AUSD', BigInt(6000), 23, 12, timestamp, blockNumber)

test('when a snapshot is taken, then the id is set correctly', async () => {
set.mockReset()
Expand Down Expand Up @@ -63,7 +64,8 @@ describe('Given a populated pool,', () => {
})

describe('Given a pool with non zero accumulators, ', () => {
const pool = PoolService.init(poolId, 'AUSD', BigInt(6000), 23, 12, timestamp, blockNumber)
const pool = PoolService.seed(poolId)
pool.init('AUSD', BigInt(6000), 23, 12, timestamp, blockNumber)
set.mockReset()
getByField.mockReset()
getByField.mockReturnValue([pool])
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ export interface TokensCurrencyId extends Enum {
isNative: boolean
asNative: null
isTranche: boolean
asTranche: ITuple<[u64, U8aFixed]> //poolId, trancheId
isKSM: boolean
asKSM: null
asTranche: ITuple<[poolId: u64, trancheId: U8aFixed]> //poolId, trancheId
isAUSD: boolean
asAUSD: null
isForeignAsset: boolean
asForeignAsset: u32
isStaking: boolean
asStaking: Enum
}

export interface TrancheSolution extends Struct {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from './mappings/handlers/loansHandlers'
export * from './mappings/handlers/proxyHandlers'
export * from './mappings/handlers/ormlTokensHandlers'
export * from './mappings/handlers/logHandlers'
export * from './mappings/handlers/ethHandlers'
export * from './mappings/handlers/evmHandlers'
4 changes: 2 additions & 2 deletions src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
const daysAgo90 = new Date(blockPeriodStart.valueOf() - 90 * 24 * 3600 * 1000)

// Update Pool States
const pools = await PoolService.getAll()
const pools = await PoolService.getActivePools()
for (const pool of pools) {
await pool.updateState()
await pool.updatePortfolioValuation()
Expand Down Expand Up @@ -61,7 +61,7 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
}

//Perform Snapshots and reset accumulators
await stateSnapshotter('Pool', 'PoolSnapshot', block, 'poolId')
await stateSnapshotter('Pool', 'PoolSnapshot', block, 'poolId', 'isActive', true)
await stateSnapshotter('Tranche', 'TrancheSnapshot', block, 'trancheId', 'isActive', true)
await stateSnapshotter('Loan', 'LoanSnapshot', block, 'loanId', 'isActive', true)

Expand Down
17 changes: 0 additions & 17 deletions src/mappings/handlers/ethHandlers.ts

This file was deleted.

Loading

0 comments on commit 91207d6

Please sign in to comment.