From 6f2330700b0ee60c380a6894fee7fb029db2f5f1 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Sep 2023 13:17:52 -0400 Subject: [PATCH 1/4] add base methods for lpb management --- src/mappings/base/base-pool.ts | 61 ++++++++++++++++++++ src/mappings/erc-20-pool.ts | 100 +++++++++++++++++---------------- src/mappings/erc-721-pool.ts | 100 +++++++++++++++++---------------- 3 files changed, 165 insertions(+), 96 deletions(-) diff --git a/src/mappings/base/base-pool.ts b/src/mappings/base/base-pool.ts index 865f887..1dfba86 100644 --- a/src/mappings/base/base-pool.ts +++ b/src/mappings/base/base-pool.ts @@ -23,6 +23,8 @@ import { incrementTokenTxCount as incrementTokenTxCountERC20Pool } from "../../u import { incrementTokenTxCount as incrementTokenTxCountERC721Pool } from "../../utils/token-erc721" import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "../../utils/pool/reserve-auction" import { saveOrRemovePositionLend } from "../../utils/position" +import { decreaseAllowances, increaseAllowances, loadOrCreateAllowances, revokeAllowances } from "../../utils/pool/lp-allowances" +import { loadOrCreateTransferors, revokeTransferors } from "../../utils/pool/lp-transferors" /*******************************/ @@ -498,6 +500,65 @@ export function _handleTransferLP(erc20Event: TransferLPERC20Event | null, erc72 transferLP.save() } +/*************************************/ +/*** LPB Management Event Handlers ***/ +/*************************************/ + +export function _handleDecreaseLPAllowance(event: ethereum.Event, spender: Address, indexes: BigInt[], amounts: BigInt[]): void { + const poolId = addressToBytes(event.address) + const lender = event.transaction.from + const entity = loadOrCreateAllowances(poolId, lender, spender) + decreaseAllowances(entity, indexes, amounts) + + const pool = Pool.load(poolId)! + pool.txCount = pool.txCount.plus(ONE_BI) + + // save entities to the store + pool.save() + entity.save() +} + +export function _handleIncreaseLPAllowance(event: ethereum.Event, spender: Address, indexes: BigInt[], amounts: BigInt[]): void { + const poolId = addressToBytes(event.address) + const lender = event.transaction.from + const entity = loadOrCreateAllowances(poolId, lender, spender) + increaseAllowances(entity, indexes, amounts) + + const pool = Pool.load(poolId)! + pool.txCount = pool.txCount.plus(ONE_BI) + + // save entities to the store + pool.save() + entity.save() +} + +export function _handleRevokeLPAllowance(event: ethereum.Event, spender: Address, indexes: BigInt[]): void { + const poolId = addressToBytes(event.address) + const lender = event.transaction.from + const entity = loadOrCreateAllowances(poolId, lender, spender) + revokeAllowances(entity, indexes) + + const pool = Pool.load(poolId)! + pool.txCount = pool.txCount.plus(ONE_BI) + + // save entities to the store + pool.save() + entity.save() +} + +export function _handleRevokeLPTransferors(event: ethereum.Event, lender: Address, transferors: Address[]): void { + const poolId = addressToBytes(event.address) + const entity = loadOrCreateTransferors(poolId, lender) + revokeTransferors(entity, transferors) + + const pool = Pool.load(poolId)! + pool.txCount = pool.txCount.plus(ONE_BI) + + // save entities to the store + pool.save() + entity.save() +} + /*******************************/ /*** Reserves Event Handlers ***/ /*******************************/ diff --git a/src/mappings/erc-20-pool.ts b/src/mappings/erc-20-pool.ts index c4086da..d28d041 100644 --- a/src/mappings/erc-20-pool.ts +++ b/src/mappings/erc-20-pool.ts @@ -58,7 +58,7 @@ import { lpbValueInQuote } from "../utils/pool/lend" import { incrementTokenTxCount } from "../utils/token-erc20" import { approveTransferors, loadOrCreateTransferors, revokeTransferors } from "../utils/pool/lp-transferors" import { loadOrCreateAllowances, increaseAllowances, decreaseAllowances, revokeAllowances } from "../utils/pool/lp-allowances" -import { _handleAddQuoteToken, _handleBucketBankruptcy, _handleFlashLoan, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleTransferLP } from "./base/base-pool" +import { _handleAddQuoteToken, _handleBucketBankruptcy, _handleDecreaseLPAllowance, _handleFlashLoan, _handleIncreaseLPAllowance, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleRevokeLPAllowance, _handleRevokeLPTransferors, _handleTransferLP } from "./base/base-pool" export function handleAddCollateral(event: AddCollateralEvent): void { const addCollateral = new AddCollateral( @@ -332,18 +332,19 @@ export function handleBucketTakeLPAwarded( } export function handleDecreaseLPAllowance(event: DecreaseLPAllowanceEvent): void { - const poolId = addressToBytes(event.address) - const lender = event.transaction.from - const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - decreaseAllowances(entity, event.params.indexes, event.params.amounts) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const lender = event.transaction.from + // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) + // decreaseAllowances(entity, event.params.indexes, event.params.amounts) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleDecreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) } export function handleDrawDebt(event: DrawDebtEvent): void { @@ -402,18 +403,19 @@ export function handleFlashloan(event: FlashloanEvent): void { } export function handleIncreaseLPAllowance(event: IncreaseLPAllowanceEvent): void { - const poolId = addressToBytes(event.address) - const lender = event.transaction.from - const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - increaseAllowances(entity, event.params.indexes, event.params.amounts) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const lender = event.transaction.from + // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) + // increaseAllowances(entity, event.params.indexes, event.params.amounts) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleIncreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) } export function handleKick(event: KickEvent): void { @@ -630,34 +632,36 @@ export function handleResetInterestRate(event: ResetInterestRateEvent): void { } export function handleRevokeLPAllowance(event: RevokeLPAllowanceEvent): void { - const poolId = addressToBytes(event.address) - const lender = event.transaction.from - const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - revokeAllowances(entity, event.params.indexes) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const lender = event.transaction.from + // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) + // revokeAllowances(entity, event.params.indexes) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleRevokeLPAllowance(event, event.params.spender, event.params.indexes) } export function handleRevokeLPTransferors( event: RevokeLPTransferorsEvent ): void { - const poolId = addressToBytes(event.address) - const entity = loadOrCreateTransferors(poolId, event.params.lender) - revokeTransferors(entity, event.params.transferors) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const entity = loadOrCreateTransferors(poolId, event.params.lender) + // revokeTransferors(entity, event.params.transferors) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleRevokeLPTransferors(event, event.params.lender, event.params.transferors) } export function handleTake(event: TakeEvent): void { diff --git a/src/mappings/erc-721-pool.ts b/src/mappings/erc-721-pool.ts index 1a60006..cf6358a 100644 --- a/src/mappings/erc-721-pool.ts +++ b/src/mappings/erc-721-pool.ts @@ -56,7 +56,7 @@ import { getBorrowerInfoERC721Pool, getLoanId, loadOrCreateLoan, saveOrRemoveLoa import { getLiquidationAuctionId, loadOrCreateLiquidationAuction, updateLiquidationAuction, getAuctionStatus, loadOrCreateBucketTake, getAuctionInfoERC721Pool } from "../utils/pool/liquidation" import { updatePool, addLiquidationToPool, getLenderInfoERC721Pool } from "../utils/pool/pool" import { lpbValueInQuote } from "../utils/pool/lend" -import { _handleAddQuoteToken, _handleBucketBankruptcy, _handleFlashLoan, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleTransferLP } from "./base/base-pool" +import { _handleAddQuoteToken, _handleBucketBankruptcy, _handleDecreaseLPAllowance, _handleFlashLoan, _handleIncreaseLPAllowance, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleRevokeLPAllowance, _handleRevokeLPTransferors, _handleTransferLP } from "./base/base-pool" import { decreaseAllowances, increaseAllowances, loadOrCreateAllowances, revokeAllowances } from "../utils/pool/lp-allowances" import { loadOrCreateTransferors, revokeTransferors } from "../utils/pool/lp-transferors" @@ -844,67 +844,71 @@ export function handleTake(event: TakeEvent): void { // identical to ERC20Pool export function handleDecreaseLPAllowance(event: DecreaseLPAllowanceEvent): void { - const poolId = addressToBytes(event.address) - const lender = event.transaction.from - const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - decreaseAllowances(entity, event.params.indexes, event.params.amounts) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const lender = event.transaction.from + // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) + // decreaseAllowances(entity, event.params.indexes, event.params.amounts) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleDecreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) } // identical to ERC20Pool export function handleIncreaseLPAllowance(event: IncreaseLPAllowanceEvent): void { - const poolId = addressToBytes(event.address) - const lender = event.transaction.from - const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - increaseAllowances(entity, event.params.indexes, event.params.amounts) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const lender = event.transaction.from + // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) + // increaseAllowances(entity, event.params.indexes, event.params.amounts) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleIncreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) } // identical to ERC20Pool export function handleRevokeLPAllowance(event: RevokeLPAllowanceEvent): void { - const poolId = addressToBytes(event.address) - const lender = event.transaction.from - const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - revokeAllowances(entity, event.params.indexes) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const lender = event.transaction.from + // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) + // revokeAllowances(entity, event.params.indexes) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleRevokeLPAllowance(event, event.params.spender, event.params.indexes) } // identical to ERC20Pool export function handleRevokeLPTransferors( event: RevokeLPTransferorsEvent ): void { - const poolId = addressToBytes(event.address) - const entity = loadOrCreateTransferors(poolId, event.params.lender) - revokeTransferors(entity, event.params.transferors) - - const pool = Pool.load(poolId) - if (pool != null) { - pool.txCount = pool.txCount.plus(ONE_BI) - pool.save() - } - - entity.save() + // const poolId = addressToBytes(event.address) + // const entity = loadOrCreateTransferors(poolId, event.params.lender) + // revokeTransferors(entity, event.params.transferors) + + // const pool = Pool.load(poolId) + // if (pool != null) { + // pool.txCount = pool.txCount.plus(ONE_BI) + // pool.save() + // } + + // entity.save() + _handleRevokeLPTransferors(event, event.params.lender, event.params.transferors) } export function handleTransferLP(event: TransferLPEvent): void { From ea9d65de777ab0c0fef46b282410516c6fe873e1 Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 12 Sep 2023 13:33:20 -0400 Subject: [PATCH 2/4] reorg erc20 pool; abstract additional method --- src/mappings/base/base-pool.ts | 15 +++- src/mappings/erc-20-pool.ts | 138 ++++++++++++--------------------- src/mappings/erc-721-pool.ts | 85 +++++--------------- 3 files changed, 84 insertions(+), 154 deletions(-) diff --git a/src/mappings/base/base-pool.ts b/src/mappings/base/base-pool.ts index 1dfba86..94cd2c0 100644 --- a/src/mappings/base/base-pool.ts +++ b/src/mappings/base/base-pool.ts @@ -24,7 +24,7 @@ import { incrementTokenTxCount as incrementTokenTxCountERC721Pool } from "../../ import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "../../utils/pool/reserve-auction" import { saveOrRemovePositionLend } from "../../utils/position" import { decreaseAllowances, increaseAllowances, loadOrCreateAllowances, revokeAllowances } from "../../utils/pool/lp-allowances" -import { loadOrCreateTransferors, revokeTransferors } from "../../utils/pool/lp-transferors" +import { approveTransferors, loadOrCreateTransferors, revokeTransferors } from "../../utils/pool/lp-transferors" /*******************************/ @@ -504,6 +504,19 @@ export function _handleTransferLP(erc20Event: TransferLPERC20Event | null, erc72 /*** LPB Management Event Handlers ***/ /*************************************/ +export function _handleApproveLPTransferors(event: ethereum.Event, lender: Address, transferors: Address[]): void { + const poolId = addressToBytes(event.address) + const entity = loadOrCreateTransferors(poolId, lender) + approveTransferors(entity, transferors) + + const pool = Pool.load(poolId)! + pool.txCount = pool.txCount.plus(ONE_BI) + + // save entities to the store + pool.save() + entity.save() +} + export function _handleDecreaseLPAllowance(event: ethereum.Event, spender: Address, indexes: BigInt[], amounts: BigInt[]): void { const poolId = addressToBytes(event.address) const lender = event.transaction.from diff --git a/src/mappings/erc-20-pool.ts b/src/mappings/erc-20-pool.ts index d28d041..ee32ffd 100644 --- a/src/mappings/erc-20-pool.ts +++ b/src/mappings/erc-20-pool.ts @@ -57,8 +57,7 @@ import { updatePool, addLiquidationToPool } from "../utils/pool/pool" import { lpbValueInQuote } from "../utils/pool/lend" import { incrementTokenTxCount } from "../utils/token-erc20" import { approveTransferors, loadOrCreateTransferors, revokeTransferors } from "../utils/pool/lp-transferors" -import { loadOrCreateAllowances, increaseAllowances, decreaseAllowances, revokeAllowances } from "../utils/pool/lp-allowances" -import { _handleAddQuoteToken, _handleBucketBankruptcy, _handleDecreaseLPAllowance, _handleFlashLoan, _handleIncreaseLPAllowance, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleRevokeLPAllowance, _handleRevokeLPTransferors, _handleTransferLP } from "./base/base-pool" +import { _handleAddQuoteToken, _handleApproveLPTransferors, _handleBucketBankruptcy, _handleDecreaseLPAllowance, _handleFlashLoan, _handleIncreaseLPAllowance, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleRevokeLPAllowance, _handleRevokeLPTransferors, _handleTransferLP } from "./base/base-pool" export function handleAddCollateral(event: AddCollateralEvent): void { const addCollateral = new AddCollateral( @@ -128,16 +127,6 @@ export function handleAddQuoteToken(event: AddQuoteTokenEvent): void { _handleAddQuoteToken(event, null) } -export function handleApproveLPTransferors( - event: ApproveLPTransferorsEvent -): void { - const poolId = addressToBytes(event.address) - const entity = loadOrCreateTransferors(poolId, event.params.lender) - approveTransferors(entity, event.params.transferors) - - entity.save() -} - // ERC20Pool only // emitted in conjunction with Settle export function handleAuctionSettle(event: AuctionSettleEvent): void { @@ -331,22 +320,6 @@ export function handleBucketTakeLPAwarded( bucketTake.save() } -export function handleDecreaseLPAllowance(event: DecreaseLPAllowanceEvent): void { - // const poolId = addressToBytes(event.address) - // const lender = event.transaction.from - // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - // decreaseAllowances(entity, event.params.indexes, event.params.amounts) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() - _handleDecreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) -} - export function handleDrawDebt(event: DrawDebtEvent): void { const drawDebt = new DrawDebt( event.transaction.hash.concatI32(event.logIndex.toI32()) @@ -402,22 +375,6 @@ export function handleFlashloan(event: FlashloanEvent): void { _handleFlashLoan(event, event.params.token, event.params.receiver, event.params.amount) } -export function handleIncreaseLPAllowance(event: IncreaseLPAllowanceEvent): void { - // const poolId = addressToBytes(event.address) - // const lender = event.transaction.from - // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - // increaseAllowances(entity, event.params.indexes, event.params.amounts) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() - _handleIncreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) -} - export function handleKick(event: KickEvent): void { const kick = new Kick( event.transaction.hash.concatI32(event.logIndex.toI32()) @@ -619,51 +576,6 @@ export function handleRepayDebt(event: RepayDebtEvent): void { repayDebt.save() } -export function handleReserveAuctionKick(event: KickReserveAuctionEvent): void { - _handleReserveAuctionKick(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) -} - -export function handleReserveAuctionTake(event: ReserveAuctionEvent): void { - _handleReserveAuctionTake(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) -} - -export function handleResetInterestRate(event: ResetInterestRateEvent): void { - _handleInterestRateEvent(event.address, event, event.params.newRate); -} - -export function handleRevokeLPAllowance(event: RevokeLPAllowanceEvent): void { - // const poolId = addressToBytes(event.address) - // const lender = event.transaction.from - // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - // revokeAllowances(entity, event.params.indexes) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() - _handleRevokeLPAllowance(event, event.params.spender, event.params.indexes) -} - -export function handleRevokeLPTransferors( - event: RevokeLPTransferorsEvent -): void { - // const poolId = addressToBytes(event.address) - // const entity = loadOrCreateTransferors(poolId, event.params.lender) - // revokeTransferors(entity, event.params.transferors) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() - _handleRevokeLPTransferors(event, event.params.lender, event.params.transferors) -} - export function handleTake(event: TakeEvent): void { const take = new Take( event.transaction.hash.concatI32(event.logIndex.toI32()) @@ -783,11 +695,59 @@ export function handleSettle(event: SettleEvent): void { settle.save() } +/*************************************/ +/*** LPB Management Event Handlers ***/ +/*************************************/ + +export function handleApproveLPTransferors( + event: ApproveLPTransferorsEvent +): void { + _handleApproveLPTransferors(event, event.params.lender, event.params.transferors) +} + +export function handleDecreaseLPAllowance(event: DecreaseLPAllowanceEvent): void { + _handleDecreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) +} + +export function handleIncreaseLPAllowance(event: IncreaseLPAllowanceEvent): void { + _handleIncreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) +} + +export function handleRevokeLPAllowance(event: RevokeLPAllowanceEvent): void { + _handleRevokeLPAllowance(event, event.params.spender, event.params.indexes) +} + +export function handleRevokeLPTransferors( + event: RevokeLPTransferorsEvent +): void { + _handleRevokeLPTransferors(event, event.params.lender, event.params.transferors) +} + export function handleTransferLP(event: TransferLPEvent): void { event = changetype(event)! _handleTransferLP(event, null) } +/***************************/ +/*** Pool Event Handlers ***/ +/***************************/ + +export function handleResetInterestRate(event: ResetInterestRateEvent): void { + _handleInterestRateEvent(event.address, event, event.params.newRate); +} + export function handleUpdateInterestRate(event: UpdateInterestRateEvent): void { _handleInterestRateEvent(event.address, event, event.params.newRate); } + +/*******************************/ +/*** Reserves Event Handlers ***/ +/*******************************/ + +export function handleReserveAuctionKick(event: KickReserveAuctionEvent): void { + _handleReserveAuctionKick(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) +} + +export function handleReserveAuctionTake(event: ReserveAuctionEvent): void { + _handleReserveAuctionTake(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) +} diff --git a/src/mappings/erc-721-pool.ts b/src/mappings/erc-721-pool.ts index cf6358a..7a165aa 100644 --- a/src/mappings/erc-721-pool.ts +++ b/src/mappings/erc-721-pool.ts @@ -57,9 +57,6 @@ import { getLiquidationAuctionId, loadOrCreateLiquidationAuction, updateLiquidat import { updatePool, addLiquidationToPool, getLenderInfoERC721Pool } from "../utils/pool/pool" import { lpbValueInQuote } from "../utils/pool/lend" import { _handleAddQuoteToken, _handleBucketBankruptcy, _handleDecreaseLPAllowance, _handleFlashLoan, _handleIncreaseLPAllowance, _handleInterestRateEvent, _handleLoanStamped, _handleMoveQuoteToken, _handleRemoveQuoteToken, _handleReserveAuctionKick, _handleReserveAuctionTake, _handleRevokeLPAllowance, _handleRevokeLPTransferors, _handleTransferLP } from "./base/base-pool" -import { decreaseAllowances, increaseAllowances, loadOrCreateAllowances, revokeAllowances } from "../utils/pool/lp-allowances" -import { loadOrCreateTransferors, revokeTransferors } from "../utils/pool/lp-transferors" - /*******************************/ /*** Borrower Event Handlers ***/ @@ -842,54 +839,25 @@ export function handleTake(event: TakeEvent): void { /*** LPB Management Event Handlers ***/ /*************************************/ +// TODO: add to 721 schema +// export function handleApproveLPTransferors( +// event: ApproveLPTransferorsEvent +// ): void { +// _handleApproveLPTransferors(event, event.params.lender, event.params.transferors) +// } + // identical to ERC20Pool export function handleDecreaseLPAllowance(event: DecreaseLPAllowanceEvent): void { - // const poolId = addressToBytes(event.address) - // const lender = event.transaction.from - // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - // decreaseAllowances(entity, event.params.indexes, event.params.amounts) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() _handleDecreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) } // identical to ERC20Pool export function handleIncreaseLPAllowance(event: IncreaseLPAllowanceEvent): void { - // const poolId = addressToBytes(event.address) - // const lender = event.transaction.from - // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - // increaseAllowances(entity, event.params.indexes, event.params.amounts) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() _handleIncreaseLPAllowance(event, event.params.spender, event.params.indexes, event.params.amounts) } // identical to ERC20Pool export function handleRevokeLPAllowance(event: RevokeLPAllowanceEvent): void { - // const poolId = addressToBytes(event.address) - // const lender = event.transaction.from - // const entity = loadOrCreateAllowances(poolId, lender, event.params.spender) - // revokeAllowances(entity, event.params.indexes) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() _handleRevokeLPAllowance(event, event.params.spender, event.params.indexes) } @@ -897,17 +865,6 @@ export function handleRevokeLPAllowance(event: RevokeLPAllowanceEvent): void { export function handleRevokeLPTransferors( event: RevokeLPTransferorsEvent ): void { - // const poolId = addressToBytes(event.address) - // const entity = loadOrCreateTransferors(poolId, event.params.lender) - // revokeTransferors(entity, event.params.transferors) - - // const pool = Pool.load(poolId) - // if (pool != null) { - // pool.txCount = pool.txCount.plus(ONE_BI) - // pool.save() - // } - - // entity.save() _handleRevokeLPTransferors(event, event.params.lender, event.params.transferors) } @@ -916,20 +873,6 @@ export function handleTransferLP(event: TransferLPEvent): void { _handleTransferLP(null, event) } -/*******************************/ -/*** Reserves Event Handlers ***/ -/*******************************/ - -// identical to ERC20Pool -export function handleReserveAuctionKick(event: KickReserveAuctionEvent): void { - _handleReserveAuctionKick(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) -} - -// identical to ERC20Pool -export function handleReserveAuctionTake(event: ReserveAuctionEvent): void { - _handleReserveAuctionTake(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) -} - /***************************/ /*** Pool Event Handlers ***/ /***************************/ @@ -943,3 +886,17 @@ export function handleResetInterestRate(event: ResetInterestRateEvent): void { export function handleUpdateInterestRate(event: UpdateInterestRateEvent): void { _handleInterestRateEvent(event.address, event, event.params.newRate); } + +/*******************************/ +/*** Reserves Event Handlers ***/ +/*******************************/ + +// identical to ERC20Pool +export function handleReserveAuctionKick(event: KickReserveAuctionEvent): void { + _handleReserveAuctionKick(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) +} + +// identical to ERC20Pool +export function handleReserveAuctionTake(event: ReserveAuctionEvent): void { + _handleReserveAuctionTake(event, event.params.currentBurnEpoch, event.params.claimableReservesRemaining, event.params.auctionPrice) +} \ No newline at end of file From 0513c4ca037cafaf9d840ff46bc52cff59ef459e Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 15 Sep 2023 14:56:13 -0400 Subject: [PATCH 3/4] track token pools --- schema.graphql | 5 ++--- src/mappings/erc-20-pool-factory.ts | 8 +++++++- src/mappings/erc-721-pool-factory.ts | 8 +++++++- src/utils/pool/pool.ts | 9 +++++++++ src/utils/position.ts | 1 + 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/schema.graphql b/schema.graphql index 1613cf3..5cf6c8a 100644 --- a/schema.graphql +++ b/schema.graphql @@ -1,5 +1,3 @@ -# TODO: track approvals? -# TODO: track token liquidity across all Ajna pools # this supports both ERC20 and ERC721 tokens, as union types aren't currently supported type Token @entity { # token address @@ -14,11 +12,12 @@ type Token @entity { tokenType: String! # ERC20 or ERC721 # number of pools including this token poolCount: BigInt! + # list of pools including this token + pools: [Pool!]! # total supply of the token totalSupply: BigInt # transactions across all pools that include this token txCount: BigInt! - # TODO: track which tokenIds have been used in pools? } diff --git a/src/mappings/erc-20-pool-factory.ts b/src/mappings/erc-20-pool-factory.ts index 1b7e8f8..ca823be 100644 --- a/src/mappings/erc-20-pool-factory.ts +++ b/src/mappings/erc-20-pool-factory.ts @@ -6,7 +6,7 @@ import { ERC20Pool as ERC20PoolContract } from "../../generated/templates/ERC20P import { ONE_BI, ZERO_BI } from "../utils/constants" import { addressToBytes, wadToDecimal } from "../utils/convert" import { getTokenDecimals, getTokenName, getTokenSymbol, getTokenTotalSupply } from "../utils/token-erc20" -import { getRatesAndFees, loadOrCreatePool } from "../utils/pool/pool" +import { getRatesAndFees, loadOrCreatePool, updateTokenPools } from "../utils/pool/pool" import { loadOrCreateFactory } from "../utils/pool/pool-factory" import { Bytes } from "@graphprotocol/graph-ts" @@ -52,6 +52,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { collateralToken.txCount = ZERO_BI collateralToken.tokenType = "ERC20" collateralToken.poolCount = ONE_BI + collateralToken.pools = [] } else { collateralToken.poolCount = collateralToken.poolCount.plus(ONE_BI) } @@ -66,6 +67,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { quoteToken.txCount = ZERO_BI quoteToken.tokenType = "ERC20" quoteToken.poolCount = ONE_BI + quoteToken.pools = [] } else { quoteToken.poolCount = quoteToken.poolCount.plus(ONE_BI) } @@ -74,6 +76,10 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { const pool = loadOrCreatePool(event.params.pool_) ERC20Pool.create(event.params.pool_) // create data source template + // update list of pools including these tokens + updateTokenPools(collateralToken, pool) + updateTokenPools(quoteToken, pool) + // record pool metadata pool.createdAtTimestamp = event.block.timestamp pool.createdAtBlockNumber = event.block.number diff --git a/src/mappings/erc-721-pool-factory.ts b/src/mappings/erc-721-pool-factory.ts index 20e4dd8..05a7eb9 100644 --- a/src/mappings/erc-721-pool-factory.ts +++ b/src/mappings/erc-721-pool-factory.ts @@ -6,7 +6,7 @@ import { ERC721Pool as ERC721PoolContract } from "../../generated/templates/ERC7 import { ONE_BI, ZERO_BI } from "../utils/constants" import { addressToBytes, wadToDecimal } from "../utils/convert" import { loadOrCreateFactory } from "../utils/pool/pool-factory" -import { getPoolSubsetHash, getRatesAndFees, loadOrCreatePool } from "../utils/pool/pool" +import { getPoolSubsetHash, getRatesAndFees, loadOrCreatePool, updateTokenPools } from "../utils/pool/pool" import { getTokenName as getTokenNameERC721, getTokenSymbol as getTokenSymbolERC721} from "../utils/token-erc721" import { getTokenDecimals, getTokenName, getTokenSymbol, getTokenTotalSupply } from "../utils/token-erc20" import { BigInt, ByteArray, Bytes, ethereum } from "@graphprotocol/graph-ts" @@ -76,6 +76,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { collateralToken.txCount = ZERO_BI collateralToken.poolCount = ONE_BI collateralToken.tokenType = "ERC721" + collateralToken.pools = [] } else { collateralToken.poolCount = collateralToken.poolCount.plus(ONE_BI) } @@ -90,6 +91,7 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { quoteToken.txCount = ZERO_BI quoteToken.tokenType = "ERC20" quoteToken.poolCount = ONE_BI + quoteToken.pools = [] } else { quoteToken.poolCount = quoteToken.poolCount.plus(ONE_BI) } @@ -98,6 +100,10 @@ export function handlePoolCreated(event: PoolCreatedEvent): void { const pool = loadOrCreatePool(event.params.pool_) ERC721Pool.create(event.params.pool_) // create pool template + // update list of pools including these tokens + updateTokenPools(collateralToken, pool) + updateTokenPools(quoteToken, pool) + // record pool metadata pool.createdAtTimestamp = event.block.timestamp pool.createdAtBlockNumber = event.block.number diff --git a/src/utils/pool/pool.ts b/src/utils/pool/pool.ts index bab069e..3fa2c72 100644 --- a/src/utils/pool/pool.ts +++ b/src/utils/pool/pool.ts @@ -479,3 +479,12 @@ export function depositUpToIndex(poolAddress: Address, index: u32): BigInt { const poolContract = ERC20Pool.bind(poolAddress) return poolContract.depositUpToIndex(BigInt.fromU32(index)); } + +export function updateTokenPools(token: Token, pool: Pool): void { + const pools = token.pools + // get current index of pool in token's list of pools + const index = pools.indexOf(pool.id) + if (index == -1) { + token.pools = token.pools.concat([pool.id]) + } +} diff --git a/src/utils/position.ts b/src/utils/position.ts index c7eb567..a18facc 100644 --- a/src/utils/position.ts +++ b/src/utils/position.ts @@ -24,6 +24,7 @@ export function loadOrCreateLPToken(tokenAddress: Address): Token { token.tokenType = "ERC721" token.poolCount = ONE_BI token.totalSupply = ONE_BI + token.pools = [] } return token From 45b894dde6371989af2ff239b88337b902ec8429 Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 18 Sep 2023 10:44:16 -0400 Subject: [PATCH 4/4] cleanup updateTokenPools --- src/utils/pool/pool.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/pool/pool.ts b/src/utils/pool/pool.ts index 3fa2c72..92c75ab 100644 --- a/src/utils/pool/pool.ts +++ b/src/utils/pool/pool.ts @@ -481,9 +481,8 @@ export function depositUpToIndex(poolAddress: Address, index: u32): BigInt { } export function updateTokenPools(token: Token, pool: Pool): void { - const pools = token.pools // get current index of pool in token's list of pools - const index = pools.indexOf(pool.id) + const index = token.pools.indexOf(pool.id) if (index == -1) { token.pools = token.pools.concat([pool.id]) }