Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

132 implement naming changes [BREAKING] #134

Merged
merged 2 commits into from
Dec 8, 2022
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
148 changes: 74 additions & 74 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,35 @@ type Pool @entity {
metadata: String!

minEpochTime: Int!
maxNavAge: Int!
maxPortfolioValuationAge: Int!

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

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

# Aggregated transaction data over the last period
totalBorrowed_: BigInt
totalRepaid_: BigInt
totalInvested_: BigInt
totalRedeemed_: BigInt
totalNumberOfLoans_: BigInt
sumBorrowedAmount_R: BigInt
sumRepaidAmount_R: BigInt
sumInvestedAmount_R: BigInt
sumRedeemedAmount_R: BigInt
sumNumberOfLoans_R: BigInt

totalNumberOfActiveLoans: BigInt!
totalDebtOverdue: BigInt!
totalWrittenOff_: BigInt!
sumNumberOfActiveLoans: BigInt!
sumDebtOverdue: BigInt!
sumDebtWrittenOff_R: BigInt!

# Cumulated transaction data since pool creation
totalEverBorrowed: BigInt
totalEverNumberOfLoans: BigInt
sumBorrowedAmount: BigInt
sumNumberOfLoans: BigInt

tranches: [Tranche] @derivedFrom(field: "pool")
}
Expand All @@ -60,25 +60,25 @@ type PoolSnapshot @entity {
periodStart: Date! @index

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

# Aggregated transaction data over the last period
totalBorrowed_: BigInt
totalRepaid_: BigInt
totalInvested_: BigInt
totalRedeemed_: BigInt
totalNumberOfLoans_: BigInt
sumBorrowedAmount_R: BigInt
sumRepaidAmount_R: BigInt
sumInvestedAmount_R: BigInt
sumRedeemedAmount_R: BigInt
sumNumberOfLoans_R: BigInt

totalNumberOfActiveLoans: BigInt!
sumNumberOfActiveLoans: BigInt!

# Cumulated transaction data since pool creation
totalEverBorrowed: BigInt
totalEverNumberOfLoans: BigInt
sumBorrowedAmount: BigInt
sumNumberOfLoans: BigInt
}

type Tranche @entity {
Expand All @@ -94,19 +94,19 @@ type Tranche @entity {
interestRatePerSec: BigInt
minRiskBuffer: BigInt

active: Boolean! @index
isActive: Boolean! @index

supply: BigInt
price: BigInt
debt: BigInt
tokenSupply: BigInt
tokenPrice: BigInt
sumDebt: BigInt

outstandingInvestOrders_: BigInt!
outstandingRedeemOrders_: BigInt!
outstandingRedeemOrdersCurrency_: BigInt!
sumOutstandingInvestOrders_R: BigInt!
sumOutstandingRedeemOrders_R: BigInt!
sumOutstandingRedeemOrdersCurrency_R: BigInt!

fulfilledInvestOrders_: BigInt!
fulfilledRedeemOrders_: BigInt!
fulfilledRedeemOrdersCurrency_: BigInt!
sumFulfilledInvestOrders_R: BigInt!
sumFulfilledRedeemOrders_R: BigInt!
sumFulfilledRedeemOrdersCurrency_R: BigInt!

yield30DaysAnnualized: BigInt
yield90DaysAnnualized: BigInt
Expand All @@ -122,17 +122,17 @@ type TrancheSnapshot @entity {
blockNumber: Int!
periodStart: Date! @index

supply: BigInt
price: BigInt
debt: BigInt
tokenSupply: BigInt
tokenPrice: BigInt
sumDebt: BigInt

outstandingInvestOrders_: BigInt!
outstandingRedeemOrders_: BigInt!
outstandingRedeemOrdersCurrency_: BigInt!
sumOutstandingInvestOrders_R: BigInt!
sumOutstandingRedeemOrders_R: BigInt!
sumOutstandingRedeemOrdersCurrency_R: BigInt!

fulfilledInvestOrders_: BigInt!
fulfilledRedeemOrders_: BigInt!
fulfilledRedeemOrdersCurrency_: BigInt!
sumFulfilledInvestOrders_R: BigInt!
sumFulfilledRedeemOrders_R: BigInt!
sumFulfilledRedeemOrdersCurrency_R: BigInt!

yield30DaysAnnualized: BigInt
yield90DaysAnnualized: BigInt
Expand All @@ -151,10 +151,10 @@ type Epoch @entity {
executedAt: Date

# Aggregated data during this epoch
totalBorrowed: BigInt!
totalRepaid: BigInt!
totalInvested: BigInt!
totalRedeemed: BigInt!
sumBorrowedAmount: BigInt!
sumRepaidAmount: BigInt!
sumInvestedAmount: BigInt!
sumRedeemedAmount: BigInt!

epochStates: [EpochState] @derivedFrom(field: "epoch")

Expand All @@ -166,17 +166,17 @@ type EpochState @entity {
epoch: Epoch! @index
trancheId: String!

price: BigInt
tokenPrice: BigInt

outstandingInvestOrders: BigInt!
outstandingRedeemOrders: BigInt!
outstandingRedeemOrdersCurrency: BigInt!
fulfilledInvestOrders: BigInt
fulfilledRedeemOrders: BigInt
fulfilledRedeemOrdersCurrency: BigInt
sumOutstandingInvestOrders: BigInt!
sumOutstandingRedeemOrders: BigInt!
sumOutstandingRedeemOrdersCurrency: BigInt!
sumFulfilledInvestOrders: BigInt
sumFulfilledRedeemOrders: BigInt
sumFulfilledRedeemOrdersCurrency: BigInt

investFulfillment: BigInt
redeemFulfillment: BigInt
investFulfillmentPercentage: BigInt
redeemFulfillmentPercentage: BigInt
}

enum InvestorTransactionType {
Expand All @@ -201,8 +201,8 @@ type OutstandingOrder @entity {
epochNumber: Int!
timestamp: Date!

invest: BigInt!
redeem: BigInt!
investAmount: BigInt!
redeemAmount: BigInt!
}

type InvestorTransaction @entity {
Expand Down Expand Up @@ -265,13 +265,13 @@ type TrancheBalance @entity {
pool: Pool! @index
tranche: Tranche! @index

investOrdered: BigInt!
investUncollected: BigInt!
investCollected: BigInt!
sumInvestOrderedAmount: BigInt!
sumInvestUncollectedAmount: BigInt!
sumInvestCollectedAmount: BigInt!

redeemOrdered: BigInt!
redeemUncollected: BigInt!
redeemCollected: BigInt!
sumRedeemOrderedAmount: BigInt!
sumRedeemUncollectedAmount: BigInt!
sumRedeemCollectedAmount: BigInt!
}

type CurrencyBalance @entity {
Expand Down Expand Up @@ -315,21 +315,21 @@ type Loan @entity {

interestRatePerSec: BigInt

adminWrittenOff: Boolean
isAdminWrittenOff: Boolean

pool: Pool!

active: Boolean! @index
isActive: Boolean! @index
status: LoanStatus!

outstandingDebt: BigInt

totalBorrowed_: BigInt
totalRepaid_: BigInt
borrowedAmount_R: BigInt
repaidAmount_R: BigInt

writeOffIndex: Int
writtenOffPercentage_: BigInt
writtenOffAmount_: BigInt
writtenOffPercentage_R: BigInt
writtenOffAmount_R: BigInt
penaltyInterestRatePerSec: BigInt
}

Expand All @@ -343,12 +343,12 @@ type LoanSnapshot @entity {

outstandingDebt: BigInt

totalBorrowed_: BigInt
totalRepaid_: BigInt
borrowedAmount_R: BigInt
repaidAmount_R: BigInt

writeOffIndex: Int
writtenOffPercentage_: BigInt
writtenOffAmount_: BigInt
writtenOffPercentage_R: BigInt
writtenOffAmount_R: BigInt
penaltyInterestRatePerSec: BigInt
}

Expand All @@ -366,7 +366,7 @@ type Proxy @entity {
delegator: String!
delegatee: String!
proxyType: String
removed: Boolean
isRemoved: Boolean
delay: Int
}

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/stateSnapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('Given a pool with non zero accumulators, ', () => {
getByField.mockReturnValue([pool])

test('when a entity is snapshotted, then the accumulators are reset to 0', async () => {
const accumulatorProps = Object.getOwnPropertyNames(pool).filter((prop) => prop.endsWith('_'))
const accumulatorProps = Object.getOwnPropertyNames(pool).filter((prop) => prop.endsWith('_R'))
const zeroedProps = accumulatorProps.reduce((acc, currentProp) => ({ ...acc, [currentProp]: BigInt(0) }), {})
const accumulatedProps = accumulatorProps.reduce(
(acc, currentProp) => ({ ...acc, [currentProp]: BigInt('999999999999999999') }),
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/stateSnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getPeriodStart } from './timekeeperService'
/**
* Creates a snapshot of a generic stateModel to a snapshotModel.
* A snapshotModel has the same fields as the originating stateModel, however a timestamp and a blockNumber are added.
* Fields ending with an _ underscore are reset to 0 at the end of a period. All such resettable fields must be of type
* Fields ending with an _R underscore are reset to 0 at the end of a period. All such resettable fields must be of type
* BigInt.
* @param stateModel - the data model to be snapshotted
* @param snapshotModel - the data model where the snapshot is saved. (must have additional timestamp and
Expand Down Expand Up @@ -38,7 +38,7 @@ async function _stateSnapshotter(
if (fkReferenceName) snapshotEntity[fkReferenceName] = stateEntity.id

const propNames = Object.getOwnPropertyNames(stateEntity)
const propNamesToReset = propNames.filter((propName) => propName.endsWith('_'))
const propNamesToReset = propNames.filter((propName) => propName.endsWith('_R'))
for (const propName of propNamesToReset) {
stateEntity[propName] = BigInt(0)
}
Expand Down
12 changes: 6 additions & 6 deletions src/mappings/handlers/blockHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
const pools = await PoolService.getAll()
for (const pool of pools) {
await pool.updateState()
await pool.updateNav()
await pool.updatePortfolioValuation()
await pool.computePoolValue()
await pool.resetTotalDebtOverdue()
await pool.resetDebtOverdue()

// Update tranche states
const tranches = await TrancheService.getActives(pool.id)
Expand All @@ -52,17 +52,17 @@ async function _handleBlock(block: SubstrateBlock): Promise<void> {
await loan.updateOutstandingDebt(normalizedDebt, interestRate)
await loan.save()

if (loan.maturityDate < block.timestamp) await pool.increaseTotalDebtOverdue(loan.outstandingDebt)
if (loan.maturityDate < block.timestamp) await pool.increaseDebtOverdue(loan.outstandingDebt)
}

await pool.updateTotalNumberOfActiveLoans(BigInt(Object.keys(activeLoanData).length))
await pool.updateNumberOfActiveLoans(BigInt(Object.keys(activeLoanData).length))
await pool.save()
}

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

//Update tracking of period and continue
await (await timekeeper).update(blockPeriodStart)
Expand Down
14 changes: 7 additions & 7 deletions src/mappings/handlers/investmentsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent
hash: event.extrinsic.extrinsic.hash.toString(),
amount: newAmount.toBigInt(),
digits: ((await CurrencyService.get(pool.currencyId)) as CurrencyService).decimals,
price: tranche.price,
price: tranche.tokenPrice,
fee: BigInt(0),
timestamp: event.block.timestamp,
}
Expand All @@ -53,7 +53,7 @@ async function _handleInvestOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent

// Initialise or update outstanding transaction
const oo = await OutstandingOrderService.getOrInit(orderData)
const oldAmount = oo.invest
const oldAmount = oo.investAmount
await oo.updateInvest(orderData)
await oo.save()

Expand Down Expand Up @@ -98,7 +98,7 @@ async function _handleRedeemOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent
hash: event.extrinsic.extrinsic.hash.toString(),
amount: amount.toBigInt(),
digits: digits,
price: tranche.price,
price: tranche.tokenPrice,
fee: BigInt(0),
timestamp: event.block.timestamp,
}
Expand All @@ -115,7 +115,7 @@ async function _handleRedeemOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent

// Initialise outstanding transaction
const oo = await OutstandingOrderService.getOrInit(orderData)
const oldAmount = oo.redeem
const oldAmount = oo.redeemAmount
await oo.updateRedeem(orderData)
await oo.save()

Expand All @@ -125,7 +125,7 @@ async function _handleRedeemOrderUpdated(event: SubstrateEvent<OrderUpdatedEvent

// Update epochState outstanding total
const epoch = await EpochService.getById(poolId.toString(), pool.currentEpoch)
await epoch.updateOutstandingRedeemOrders(trancheId.toHex(), orderData.amount, oldAmount, tranche.price, digits)
await epoch.updateOutstandingRedeemOrders(trancheId.toHex(), orderData.amount, oldAmount, tranche.tokenPrice, digits)
await epoch.saveWithStates()

// Update trancheBalance
Expand Down Expand Up @@ -165,7 +165,7 @@ async function _handleInvestOrdersCollected(event: SubstrateEvent<InvestOrdersCo
hash: event.extrinsic.extrinsic.hash.toString(),
timestamp: event.block.timestamp,
digits: ((await CurrencyService.get(pool.currencyId)) as CurrencyService).decimals,
price: tranche.price,
price: tranche.tokenPrice,
amount: payoutInvestmentInvest.toBigInt(),
}

Expand Down Expand Up @@ -211,7 +211,7 @@ async function _handleRedeemOrdersCollected(event: SubstrateEvent<RedeemOrdersCo
hash: event.extrinsic.extrinsic.hash.toString(),
timestamp: event.block.timestamp,
digits: ((await CurrencyService.get(pool.currencyId)) as CurrencyService).decimals,
price: tranche.price,
price: tranche.tokenPrice,
amount: payoutInvestmentRedeem.toBigInt(),
}

Expand Down
Loading