Skip to content

Commit 24e985a

Browse files
authored
feat: store pending rewards (#68)
* feat: account for accrued to treasury * fix: explicitly add Pool abi * fix: store in reserveHistory * feat: update mapping on v3 to account for unclaimed * fix: move poolconfig tot he actual pool * fix: add config to mapping * fix: split flashloan fee * fix: add portal fee to schema * fix: track backing fee * track unbacked fee split as well
1 parent a53f09f commit 24e985a

9 files changed

Lines changed: 125 additions & 21 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/v3.schema.graphql

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ type Protocol @entity {
22
# just 1 for now
33
id: ID!
44
pools: [Pool!]! @derivedFrom(field: "protocol")
5-
bridgeProtocolFee: BigInt
6-
flashloanPremiumTotal: BigInt
7-
flashloanPremiumToProtocol: BigInt
85
}
96

107
# service entity, when we receiving an event we should wknow which pool is it
@@ -26,6 +23,10 @@ type Pool @entity {
2623
proxyPriceProvider: Bytes
2724
lastUpdateTimestamp: Int!
2825

26+
bridgeProtocolFee: BigInt
27+
flashloanPremiumTotal: BigInt
28+
flashloanPremiumToProtocol: BigInt
29+
2930
reserves: [Reserve!]! @derivedFrom(field: "pool")
3031
supplyHistory: [Supply!]! @derivedFrom(field: "pool")
3132
mintUnbackedHistory: [MintUnbacked!]! @derivedFrom(field: "pool")
@@ -266,6 +267,8 @@ type FlashLoan @entity {
266267
target: Bytes!
267268
amount: BigInt!
268269
totalFee: BigInt!
270+
lpFee: BigInt!
271+
protocolFee: BigInt!
269272
#protocolFee: BigInt!
270273
initiator: User!
271274
timestamp: Int!
@@ -324,6 +327,8 @@ type BackUnbacked @entity {
324327
backer: User!
325328
amount: BigInt!
326329
fee: BigInt!
330+
lpFee: BigInt!
331+
protocolFee: BigInt!
327332
timestamp: Int!
328333
}
329334

@@ -380,6 +385,7 @@ type ReserveParamsHistoryItem @entity {
380385
priceInEth: BigInt!
381386
priceInUsd: BigDecimal!
382387
timestamp: Int!
388+
accruedToTreasury: BigInt!
383389
totalScaledVariableDebt: BigInt!
384390
totalCurrentVariableDebt: BigInt!
385391
totalPrincipalStableDebt: BigInt!
@@ -392,8 +398,12 @@ type ReserveParamsHistoryItem @entity {
392398
lifetimeBorrows: BigInt! # default: 0
393399
lifetimeLiquidated: BigInt! # default: 0
394400
lifetimeFlashLoans: BigInt!
395-
lifetimeFlashLoanPremium: BigInt!
401+
lifetimeFlashLoanPremium: BigInt! #deprecated
402+
lifetimeFlashLoanLPPremium: BigInt!
403+
lifetimeFlashLoanProtocolPremium: BigInt!
396404
lifetimeReserveFactorAccrued: BigInt!
405+
lifetimePortalLPFee: BigInt!
406+
lifetimePortalProtocolFee: BigInt!
397407
lifetimeSuppliersInterestEarned: BigInt!
398408
# lifetimeStableDebFeeCollected: BigInt!
399409
# lifetimeVariableDebtFeeCollected: BigInt!
@@ -531,6 +541,7 @@ type Reserve @entity {
531541
totalCurrentVariableDebt: BigInt! # default: 0
532542
totalSupplies: BigInt! # default: 0
533543
liquidityRate: BigInt! # suppliers interest
544+
accruedToTreasury: BigInt!
534545
averageStableRate: BigInt!
535546
variableBorrowRate: BigInt!
536547
stableBorrowRate: BigInt!
@@ -571,7 +582,11 @@ type Reserve @entity {
571582
lifetimeBorrows: BigInt! # default: 0
572583
lifetimeLiquidated: BigInt! # default: 0
573584
lifetimeFlashLoans: BigInt! # default: 0
574-
lifetimeFlashLoanPremium: BigInt! # default: 0
585+
lifetimeFlashLoanPremium: BigInt! # deprecated
586+
lifetimeFlashLoanLPPremium: BigInt! # default: 0
587+
lifetimeFlashLoanProtocolPremium: BigInt!
588+
lifetimePortalLPFee: BigInt!
589+
lifetimePortalProtocolFee: BigInt!
575590
lifetimeSuppliersInterestEarned: BigInt!
576591
lifetimeReserveFactorAccrued: BigInt!
577592
# lifetimeStableDebFeeCollected: BigInt!

src/helpers/initializers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ export function getOrInitReserve(underlyingAsset: Bytes, event: ethereum.Event):
228228
reserve.lifetimeLiquidated = zeroBI();
229229
reserve.lifetimeFlashLoans = zeroBI();
230230
reserve.lifetimeFlashLoanPremium = zeroBI();
231+
reserve.lifetimeFlashLoanLPPremium = zeroBI();
232+
reserve.lifetimeFlashLoanProtocolPremium = zeroBI();
233+
234+
reserve.lifetimePortalLPFee = zeroBI();
235+
reserve.lifetimePortalProtocolFee = zeroBI();
231236

232237
reserve.stableDebtLastUpdateTimestamp = 0;
233238
reserve.lastUpdateTimestamp = 0;
@@ -340,6 +345,10 @@ export function getOrInitReserveParamsHistoryItem(
340345
reserveParamsHistoryItem.lifetimeLiquidated = zeroBI();
341346
reserveParamsHistoryItem.lifetimeFlashLoans = zeroBI();
342347
reserveParamsHistoryItem.lifetimeFlashLoanPremium = zeroBI();
348+
reserveParamsHistoryItem.lifetimeFlashLoanLPPremium = zeroBI();
349+
reserveParamsHistoryItem.lifetimeFlashLoanProtocolPremium = zeroBI();
350+
reserveParamsHistoryItem.lifetimePortalLPFee = zeroBI();
351+
reserveParamsHistoryItem.lifetimePortalProtocolFee = zeroBI();
343352
reserveParamsHistoryItem.lifetimeReserveFactorAccrued = zeroBI();
344353
reserveParamsHistoryItem.lifetimeDepositorsInterestEarned = zeroBI();
345354
// reserveParamsHistoryItem.lifetimeStableDebFeeCollected = zeroBI();

src/helpers/v3/initializers.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ChainlinkAggregator,
1313
ContractToPoolMapping,
1414
Protocol,
15+
Pool,
1516
} from '../../../generated/schema';
1617
import {
1718
PRICE_ORACLE_ASSET_PLATFORM_SIMPLE,
@@ -182,6 +183,7 @@ export function getOrInitReserve(underlyingAsset: Bytes, event: ethereum.Event):
182183
reserve.totalCurrentVariableDebt = zeroBI();
183184
reserve.totalPrincipalStableDebt = zeroBI();
184185
reserve.totalSupplies = zeroBI();
186+
reserve.accruedToTreasury = zeroBI();
185187

186188
reserve.isPaused = false;
187189
reserve.isDropped = false;
@@ -198,6 +200,8 @@ export function getOrInitReserve(underlyingAsset: Bytes, event: ethereum.Event):
198200
reserve.lifetimeLiquidated = zeroBI();
199201
reserve.lifetimeFlashLoans = zeroBI();
200202
reserve.lifetimeFlashLoanPremium = zeroBI();
203+
reserve.lifetimeFlashLoanLPPremium = zeroBI();
204+
reserve.lifetimeFlashLoanProtocolPremium = zeroBI();
201205

202206
reserve.stableDebtLastUpdateTimestamp = 0;
203207
reserve.lastUpdateTimestamp = 0;
@@ -263,6 +267,7 @@ export function getOrInitReserveParamsHistoryItem(
263267
reserveParamsHistoryItem.liquidityIndex = zeroBI();
264268
reserveParamsHistoryItem.liquidityRate = zeroBI();
265269
reserveParamsHistoryItem.totalLiquidity = zeroBI();
270+
reserveParamsHistoryItem.accruedToTreasury = zeroBI();
266271
reserveParamsHistoryItem.totalATokenSupply = zeroBI();
267272
reserveParamsHistoryItem.availableLiquidity = zeroBI();
268273
reserveParamsHistoryItem.totalLiquidityAsCollateral = zeroBI();
@@ -282,6 +287,8 @@ export function getOrInitReserveParamsHistoryItem(
282287
reserveParamsHistoryItem.lifetimeLiquidated = zeroBI();
283288
reserveParamsHistoryItem.lifetimeFlashLoans = zeroBI();
284289
reserveParamsHistoryItem.lifetimeFlashLoanPremium = zeroBI();
290+
reserveParamsHistoryItem.lifetimeFlashLoanLPPremium = zeroBI();
291+
reserveParamsHistoryItem.lifetimeFlashLoanProtocolPremium = zeroBI();
285292
reserveParamsHistoryItem.lifetimeReserveFactorAccrued = zeroBI();
286293
reserveParamsHistoryItem.lifetimeSuppliersInterestEarned = zeroBI();
287294
// reserveParamsHistoryItem.lifetimeStableDebFeeCollected = zeroBI();

src/mapping/lending-pool-address-provider/v3.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
PoolConfigurator as PoolConfiguratorContract,
1414
} from '../../../generated/templates';
1515
import { createMapContractToPool, getOrInitPriceOracle } from '../../helpers/v3/initializers';
16-
import { Pool } from '../../../generated/schema';
16+
import { ContractToPoolMapping, Pool } from '../../../generated/schema';
1717

1818
let POOL_COMPONENTS = [
1919
'poolDataProvider',
@@ -47,6 +47,14 @@ function genericAddressProviderUpdate(
4747
}
4848
pool.lastUpdateTimestamp = event.block.timestamp.toI32();
4949
pool.save();
50+
if (component == 'poolConfigurator') {
51+
const configuratorMapping = ContractToPoolMapping.load(newAddress.toHexString());
52+
if (!configuratorMapping) {
53+
const mapping = new ContractToPoolMapping(newAddress.toHexString());
54+
mapping.pool = poolAddress;
55+
mapping.save();
56+
}
57+
}
5058
}
5159

5260
export function handleProxyCreated(event: ProxyCreated): void {

src/mapping/lending-pool-configurator/v3.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
getOrInitSubToken,
1212
getOrInitReserve,
1313
getOrInitReserveConfigurationHistoryItem,
14-
getProtocol,
14+
getPoolByContract,
1515
} from '../../helpers/v3/initializers';
1616
import { Bytes, Address, ethereum, log, BigInt } from '@graphprotocol/graph-ts';
1717
import {
@@ -43,7 +43,7 @@ import {
4343
} from '../../../generated/templates/PoolConfigurator/PoolConfigurator';
4444
import { DefaultReserveInterestRateStrategy } from '../../../generated/templates/PoolConfigurator/DefaultReserveInterestRateStrategy';
4545

46-
import { EModeCategory, Reserve } from '../../../generated/schema';
46+
import { EModeCategory, Pool, Reserve } from '../../../generated/schema';
4747
import { zeroAddress, zeroBI } from '../../utils/converters';
4848

4949
export function saveReserve(reserve: Reserve, event: ethereum.Event): void {
@@ -231,23 +231,26 @@ export function handleDebtCeilingChanged(event: DebtCeilingChanged): void {
231231
}
232232

233233
export function handleBridgeProtocolFeeUpdated(event: BridgeProtocolFeeUpdated): void {
234-
let protocol = getProtocol();
235-
protocol.bridgeProtocolFee = event.params.newBridgeProtocolFee;
236-
protocol.save();
234+
let poolId = getPoolByContract(event);
235+
let pool = Pool.load(poolId) as Pool;
236+
pool.bridgeProtocolFee = event.params.newBridgeProtocolFee;
237+
pool.save();
237238
}
238239

239240
export function handleFlashloanPremiumTotalUpdated(event: FlashloanPremiumTotalUpdated): void {
240-
let protocol = getProtocol();
241-
protocol.flashloanPremiumTotal = event.params.newFlashloanPremiumTotal;
242-
protocol.save();
241+
let poolId = getPoolByContract(event);
242+
let pool = Pool.load(poolId) as Pool;
243+
pool.flashloanPremiumTotal = event.params.newFlashloanPremiumTotal;
244+
pool.save();
243245
}
244246

245247
export function handleFlashloanPremiumToProtocolUpdated(
246248
event: FlashloanPremiumToProtocolUpdated
247249
): void {
248-
let protocol = getProtocol();
249-
protocol.flashloanPremiumToProtocol = event.params.newFlashloanPremiumToProtocol;
250-
protocol.save();
250+
let poolId = getPoolByContract(event);
251+
let pool = Pool.load(poolId) as Pool;
252+
pool.flashloanPremiumToProtocol = event.params.newFlashloanPremiumToProtocol;
253+
pool.save();
251254
}
252255

253256
export function handleBorrowableInIsolationChanged(event: BorrowableInIsolationChanged): void {

src/mapping/lending-pool/v3.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BigInt } from '@graphprotocol/graph-ts';
1+
import { BigInt, log } from '@graphprotocol/graph-ts';
22
import {
33
Borrow,
44
Supply,
@@ -22,6 +22,7 @@ import {
2222
getOrInitReserve,
2323
getOrInitUser,
2424
getOrInitUserReserve,
25+
getPoolByContract,
2526
} from '../../helpers/v3/initializers';
2627
import {
2728
Borrow as BorrowAction,
@@ -38,6 +39,7 @@ import {
3839
UserEModeSet as UserEModeSetAction,
3940
MintedToTreasury as MintedToTreasuryAction,
4041
IsolationModeTotalDebtUpdated as IsolationModeTotalDebtUpdatedAction,
42+
Pool,
4143
} from '../../../generated/schema';
4244
import { getHistoryEntityId } from '../../utils/id-generation';
4345
import { calculateGrowth } from '../../helpers/math';
@@ -212,13 +214,28 @@ export function handleLiquidationCall(event: LiquidationCall): void {
212214
export function handleFlashLoan(event: FlashLoan): void {
213215
let initiator = getOrInitUser(event.params.initiator);
214216
let poolReserve = getOrInitReserve(event.params.asset, event);
217+
let poolId = getPoolByContract(event);
218+
let pool = Pool.load(poolId) as Pool;
215219

216220
let premium = event.params.premium;
217-
221+
let premiumToProtocol = premium
222+
.times(pool.flashloanPremiumToProtocol as BigInt)
223+
.plus(BigInt.fromI32(5000))
224+
.div(BigInt.fromI32(10000));
225+
let premiumToLP = premium.minus(premiumToProtocol);
226+
log.error('premium {},{},{}', [
227+
premium.toString(),
228+
premiumToProtocol.toString(),
229+
premiumToLP.toString(),
230+
]);
218231
poolReserve.availableLiquidity = poolReserve.availableLiquidity.plus(premium);
219232

220233
poolReserve.lifetimeFlashLoans = poolReserve.lifetimeFlashLoans.plus(event.params.amount);
221234
poolReserve.lifetimeFlashLoanPremium = poolReserve.lifetimeFlashLoanPremium.plus(premium);
235+
poolReserve.lifetimeFlashLoanLPPremium = poolReserve.lifetimeFlashLoanLPPremium.plus(premiumToLP);
236+
poolReserve.lifetimeFlashLoanProtocolPremium = poolReserve.lifetimeFlashLoanProtocolPremium.plus(
237+
premiumToProtocol
238+
);
222239
poolReserve.totalATokenSupply = poolReserve.totalATokenSupply.plus(premium);
223240

224241
poolReserve.save();
@@ -229,6 +246,8 @@ export function handleFlashLoan(event: FlashLoan): void {
229246
flashLoan.target = event.params.target;
230247
flashLoan.initiator = initiator.id;
231248
flashLoan.totalFee = premium;
249+
flashLoan.lpFee = premiumToLP;
250+
flashLoan.protocolFee = premiumToProtocol;
232251
flashLoan.amount = event.params.amount;
233252
flashLoan.timestamp = event.block.timestamp.toI32();
234253
flashLoan.save();
@@ -326,6 +345,21 @@ export function handleBackUnbacked(event: BackUnbacked): void {
326345
let userReserve = getOrInitUserReserve(backer, event.params.reserve, event);
327346
let amount = event.params.amount;
328347

348+
let poolId = getPoolByContract(event);
349+
let pool = Pool.load(poolId) as Pool;
350+
351+
let premium = event.params.fee;
352+
let premiumToProtocol = premium
353+
.times(pool.bridgeProtocolFee as BigInt)
354+
.plus(BigInt.fromI32(5000))
355+
.div(BigInt.fromI32(10000));
356+
let premiumToLP = premium.minus(premiumToProtocol);
357+
poolReserve.lifetimePortalLPFee = poolReserve.lifetimePortalLPFee.plus(premiumToLP);
358+
poolReserve.lifetimePortalProtocolFee = poolReserve.lifetimePortalProtocolFee.plus(
359+
premiumToProtocol
360+
);
361+
poolReserve.save();
362+
329363
let backUnbacked = new BackUnbackedAction(getHistoryEntityId(event));
330364
backUnbacked.pool = poolReserve.pool;
331365
backUnbacked.backer = userReserve.user;
@@ -334,6 +368,8 @@ export function handleBackUnbacked(event: BackUnbacked): void {
334368
backUnbacked.amount = amount;
335369
backUnbacked.timestamp = event.block.timestamp.toI32();
336370
backUnbacked.fee = event.params.fee;
371+
backUnbacked.lpFee = premiumToLP;
372+
backUnbacked.protocolFee = premiumToProtocol;
337373

338374
backUnbacked.save();
339375
}

src/mapping/tokenization/tokenization-v3.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Pool } from '../../../generated/templates/Pool/Pool';
12
import {
23
BalanceTransfer,
34
Mint as ATokenMint,
@@ -19,6 +20,7 @@ import {
1920
STokenBalanceHistoryItem,
2021
UserReserve,
2122
Reserve,
23+
Pool as PoolSchema,
2224
StableTokenDelegatedAllowance,
2325
VariableTokenDelegatedAllowance,
2426
} from '../../../generated/schema';
@@ -29,10 +31,11 @@ import {
2931
getOrInitUser,
3032
getPriceOracleAsset,
3133
getOrInitReserveParamsHistoryItem,
34+
getPoolByContract,
3235
} from '../../helpers/v3/initializers';
3336
import { zeroBI } from '../../utils/converters';
3437
import { calculateUtilizationRate } from '../../helpers/reserve-logic';
35-
import { Address, BigInt, ethereum, log } from '@graphprotocol/graph-ts';
38+
import { Address, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts';
3639
import { rayDiv, rayMul } from '../../helpers/math';
3740
import { getHistoryEntityId } from '../../utils/id-generation';
3841

@@ -105,6 +108,9 @@ function saveReserve(reserve: Reserve, event: ethereum.Event): void {
105108
reserveParamsHistoryItem.lifetimeWithdrawals = reserve.lifetimeWithdrawals;
106109
reserveParamsHistoryItem.lifetimeLiquidated = reserve.lifetimeLiquidated;
107110
reserveParamsHistoryItem.lifetimeFlashLoanPremium = reserve.lifetimeFlashLoanPremium;
111+
reserveParamsHistoryItem.lifetimeFlashLoanLPPremium = reserve.lifetimeFlashLoanLPPremium;
112+
reserveParamsHistoryItem.lifetimeFlashLoanProtocolPremium =
113+
reserve.lifetimeFlashLoanProtocolPremium;
108114
reserveParamsHistoryItem.lifetimeFlashLoans = reserve.lifetimeFlashLoans;
109115
// reserveParamsHistoryItem.lifetimeStableDebFeeCollected = reserve.lifetimeStableDebFeeCollected;
110116
// reserveParamsHistoryItem.lifetimeVariableDebtFeeCollected = reserve.lifetimeVariableDebtFeeCollected;
@@ -122,6 +128,7 @@ function saveReserve(reserve: Reserve, event: ethereum.Event): void {
122128
reserveParamsHistoryItem.liquidityRate = reserve.liquidityRate;
123129
reserveParamsHistoryItem.totalATokenSupply = reserve.totalATokenSupply;
124130
reserveParamsHistoryItem.averageStableBorrowRate = reserve.averageStableRate;
131+
reserveParamsHistoryItem.accruedToTreasury = reserve.accruedToTreasury;
125132
let priceOracleAsset = getPriceOracleAsset(reserve.price);
126133
reserveParamsHistoryItem.priceInEth = priceOracleAsset.priceInEth;
127134

@@ -169,6 +176,23 @@ function tokenMint(event: ethereum.Event, onBehalf: Address, value: BigInt, inde
169176
let aToken = getOrInitSubToken(event.address);
170177
let poolReserve = getOrInitReserve(aToken.underlyingAssetAddress, event);
171178
poolReserve.totalATokenSupply = poolReserve.totalATokenSupply.plus(value);
179+
let poolId = getPoolByContract(event);
180+
let pool = PoolSchema.load(poolId);
181+
if (pool && pool.pool) {
182+
let poolContract = Pool.bind(Address.fromString((pool.pool as Bytes).toHexString()));
183+
const reserveData = poolContract.try_getReserveData(
184+
Address.fromString(aToken.underlyingAssetAddress.toHexString())
185+
);
186+
if (!reserveData.reverted) {
187+
poolReserve.accruedToTreasury = reserveData.value.accruedToTreasury;
188+
} else {
189+
log.error('error reading reserveData. Pool: {}, Underlying: {}', [
190+
(pool.pool as Bytes).toHexString(),
191+
aToken.underlyingAssetAddress.toHexString(),
192+
]);
193+
}
194+
}
195+
172196
// Check if we are minting to treasury for mainnet and polygon
173197
if (
174198
onBehalf.toHexString() != '0xB2289E329D2F85F1eD31Adbb30eA345278F21bcf'.toLowerCase() &&

0 commit comments

Comments
 (0)