From fbfe005243cecf472de26157fe2403854af2756c Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 19 Dec 2023 16:48:02 -0500 Subject: [PATCH] add latest rc9 updates; fix tests --- abis/ERC20Pool.json | 25 ++++----- abis/ERC20PoolFactory.json | 6 ++ abis/ERC721Pool.json | 89 ++++++++++++++---------------- abis/ERC721PoolFactory.json | 6 ++ abis/PoolInfoUtils.json | 9 ++- schema.graphql | 6 +- src/mappings/erc-20-pool.ts | 8 +-- src/mappings/erc-721-pool.ts | 18 +++--- src/utils/pool/loan.ts | 26 +++------ subgraph.yaml | 4 +- tests/utils/mock-contract-calls.ts | 8 +-- 11 files changed, 102 insertions(+), 103 deletions(-) diff --git a/abis/ERC20Pool.json b/abis/ERC20Pool.json index 26d4424..bd0adbf 100644 --- a/abis/ERC20Pool.json +++ b/abis/ERC20Pool.json @@ -141,7 +141,7 @@ "internalType": "uint256" }, { - "name": "thresholdPrice_", + "name": "debtToCollateral_", "type": "uint256", "internalType": "uint256" }, @@ -1103,6 +1103,11 @@ "type": "uint256", "internalType": "uint256" }, + { + "name": "", + "type": "uint256", + "internalType": "uint256" + }, { "name": "", "type": "uint256", @@ -2126,17 +2131,17 @@ }, { "type": "error", - "name": "AllowanceAlreadySet", + "name": "AlreadyInitialized", "inputs": [] }, { "type": "error", - "name": "AlreadyInitialized", + "name": "AmountLTMinDebt", "inputs": [] }, { "type": "error", - "name": "AmountLTMinDebt", + "name": "AuctionActive", "inputs": [] }, { @@ -2254,11 +2259,6 @@ "name": "LUPBelowHTP", "inputs": [] }, - { - "type": "error", - "name": "LUPGreaterThanTP", - "inputs": [] - }, { "type": "error", "name": "LimitIndexExceeded", @@ -2424,11 +2424,6 @@ } ] }, - { - "type": "error", - "name": "PoolUnderCollateralized", - "inputs": [] - }, { "type": "error", "name": "PriceBelowLUP", @@ -2471,7 +2466,7 @@ }, { "type": "error", - "name": "ZeroThresholdPrice", + "name": "ZeroDebtToCollateral", "inputs": [] } ] diff --git a/abis/ERC20PoolFactory.json b/abis/ERC20PoolFactory.json index d46aca2..6958753 100644 --- a/abis/ERC20PoolFactory.json +++ b/abis/ERC20PoolFactory.json @@ -187,6 +187,12 @@ "type": "address", "indexed": false, "internalType": "address" + }, + { + "name": "subsetHash_", + "type": "bytes32", + "indexed": false, + "internalType": "bytes32" } ], "anonymous": false diff --git a/abis/ERC721Pool.json b/abis/ERC721Pool.json index 8180237..27f244e 100644 --- a/abis/ERC721Pool.json +++ b/abis/ERC721Pool.json @@ -141,7 +141,7 @@ "internalType": "uint256" }, { - "name": "thresholdPrice_", + "name": "debtToCollateral_", "type": "uint256", "internalType": "uint256" }, @@ -619,6 +619,38 @@ ], "stateMutability": "nonpayable" }, + { + "type": "function", + "name": "getBorrowerTokenIds", + "inputs": [ + { + "name": "borrower_", + "type": "address", + "internalType": "address" + } + ], + "outputs": [ + { + "name": "", + "type": "uint256[]", + "internalType": "uint256[]" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "getBucketTokenIds", + "inputs": [], + "outputs": [ + { + "name": "", + "type": "uint256[]", + "internalType": "uint256[]" + } + ], + "stateMutability": "view" + }, { "type": "function", "name": "increaseLPAllowance", @@ -1166,6 +1198,11 @@ "type": "uint256", "internalType": "uint256" }, + { + "name": "", + "type": "uint256", + "internalType": "uint256" + }, { "name": "", "type": "uint256", @@ -1326,38 +1363,6 @@ ], "stateMutability": "view" }, - { - "type": "function", - "name": "totalBorrowerTokens", - "inputs": [ - { - "name": "borrower_", - "type": "address", - "internalType": "address" - } - ], - "outputs": [ - { - "name": "", - "type": "uint256", - "internalType": "uint256" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "totalBucketTokens", - "inputs": [], - "outputs": [ - { - "name": "", - "type": "uint256", - "internalType": "uint256" - } - ], - "stateMutability": "view" - }, { "type": "function", "name": "totalT0Debt", @@ -2265,17 +2270,17 @@ }, { "type": "error", - "name": "AllowanceAlreadySet", + "name": "AlreadyInitialized", "inputs": [] }, { "type": "error", - "name": "AlreadyInitialized", + "name": "AmountLTMinDebt", "inputs": [] }, { "type": "error", - "name": "AmountLTMinDebt", + "name": "AuctionActive", "inputs": [] }, { @@ -2393,11 +2398,6 @@ "name": "LUPBelowHTP", "inputs": [] }, - { - "type": "error", - "name": "LUPGreaterThanTP", - "inputs": [] - }, { "type": "error", "name": "LimitIndexExceeded", @@ -2514,11 +2514,6 @@ } ] }, - { - "type": "error", - "name": "PoolUnderCollateralized", - "inputs": [] - }, { "type": "error", "name": "PriceBelowLUP", @@ -2561,7 +2556,7 @@ }, { "type": "error", - "name": "ZeroThresholdPrice", + "name": "ZeroDebtToCollateral", "inputs": [] } ] diff --git a/abis/ERC721PoolFactory.json b/abis/ERC721PoolFactory.json index ef866eb..553b41b 100644 --- a/abis/ERC721PoolFactory.json +++ b/abis/ERC721PoolFactory.json @@ -240,6 +240,12 @@ "type": "address", "indexed": false, "internalType": "address" + }, + { + "name": "subsetHash_", + "type": "bytes32", + "indexed": false, + "internalType": "bytes32" } ], "anonymous": false diff --git a/abis/PoolInfoUtils.json b/abis/PoolInfoUtils.json index 4068ab6..f3c9baf 100644 --- a/abis/PoolInfoUtils.json +++ b/abis/PoolInfoUtils.json @@ -46,7 +46,7 @@ "internalType": "uint256" }, { - "name": "thresholdPrice_", + "name": "debtToCollateral_", "type": "uint256", "internalType": "uint256" }, @@ -120,7 +120,7 @@ "internalType": "uint256" }, { - "name": "thresholdPrice_", + "name": "debtToCollateral_", "type": "uint256", "internalType": "uint256" }, @@ -200,6 +200,11 @@ "name": "t0Np_", "type": "uint256", "internalType": "uint256" + }, + { + "name": "thresholdPrice_", + "type": "uint256", + "internalType": "uint256" } ], "stateMutability": "view" diff --git a/schema.graphql b/schema.graphql index 0375be9..c0cc99f 100644 --- a/schema.graphql +++ b/schema.graphql @@ -193,12 +193,14 @@ type Loan @entity { liquidationAuction: LiquidationAuction # collateral tokens deposited in a pool by the borrower collateralPledged: BigDecimal! - # borrower's threshold price in t0 terms - t0ThresholdPrice: BigDecimal! + # borrower's threshold price + thresholdPrice: BigDecimal! # list of tokenIds pledged by the borrower tokenIdsPledged: [BigInt!]! # debt in T0 terms, useful when the caller knows the pending inflator t0debt: BigDecimal! + # borrowers t0 neutral price + t0Np: BigDecimal! } type Account @entity { diff --git a/src/mappings/erc-20-pool.ts b/src/mappings/erc-20-pool.ts index f13e7ed..12eacf5 100644 --- a/src/mappings/erc-20-pool.ts +++ b/src/mappings/erc-20-pool.ts @@ -98,7 +98,7 @@ export function handleDrawDebt(event: DrawDebtEvent): void { loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // update account's list of pools and loans if necessary updateAccountPools(account, pool) @@ -152,7 +152,7 @@ export function handleRepayDebt(event: RepayDebtEvent): void { loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // update account loans if necessary updateAccountLoans(account, loan) @@ -442,7 +442,7 @@ export function handleBucketTake(event: BucketTakeEvent): void { loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // retrieve auction information on the take's auction const auctionInfo = getAuctionInfoERC20Pool(bucketTake.borrower, pool) @@ -557,7 +557,7 @@ export function handleTake(event: TakeEvent): void { loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // update liquidation auction state const auctionId = loan.liquidationAuction! diff --git a/src/mappings/erc-721-pool.ts b/src/mappings/erc-721-pool.ts index 392f3c9..7ed6261 100644 --- a/src/mappings/erc-721-pool.ts +++ b/src/mappings/erc-721-pool.ts @@ -52,7 +52,7 @@ import { getBucketId, getBucketInfo, loadOrCreateBucket, updateBucketLends } fro import { addressToBytes, decimalToWad, wadToDecimal } from "../utils/convert" import { ZERO_BD, ONE_BI, ONE_WAD_BI } from "../utils/constants" import { getLendId, loadOrCreateLend, saveOrRemoveLend } from "../utils/pool/lend" -import { getBorrowerInfoERC721Pool, getLoanId, loadOrCreateLoan, saveOrRemoveLoan } from "../utils/pool/loan" +import { getBorrowerInfo, getLoanId, loadOrCreateLoan, saveOrRemoveLoan } from "../utils/pool/loan" 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" @@ -92,11 +92,11 @@ export function handleDrawDebtNFT(event: DrawDebtNFTEvent): void { // update loan state const loanId = getLoanId(pool.id, accountId) const loan = loadOrCreateLoan(loanId, pool.id, drawDebtNFT.borrower) - const borrowerInfo = getBorrowerInfoERC721Pool(addressToBytes(event.params.borrower), pool.id) + const borrowerInfo = getBorrowerInfo(addressToBytes(event.params.borrower), pool.id) loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) loan.tokenIdsPledged = loan.tokenIdsPledged.concat(event.params.tokenIdsPledged) // update account's list of pools and loans if necessary @@ -141,11 +141,11 @@ export function handleRepayDebt(event: RepayDebtEvent): void { const loanId = getLoanId(pool.id, accountId) const loan = loadOrCreateLoan(loanId, pool.id, repayDebt.borrower) - const borrowerInfo = getBorrowerInfoERC721Pool(accountId, pool.id) + const borrowerInfo = getBorrowerInfo(accountId, pool.id) loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // retrieve the tokenIdsPledged that were pulled in this event const numberOfTokensPulled = event.params.collateralPulled.div(ONE_WAD_BI).toI32() @@ -626,11 +626,11 @@ export function handleBucketTake(event: BucketTakeEvent): void { // update loan state const loanId = getLoanId(pool.id, addressToBytes(event.params.borrower)) const loan = loadOrCreateLoan(loanId, pool.id, addressToBytes(event.params.borrower)) - const borrowerInfo = getBorrowerInfoERC721Pool(addressToBytes(event.params.borrower), pool.id) + const borrowerInfo = getBorrowerInfo(addressToBytes(event.params.borrower), pool.id) loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // unique to ERC721Pools // remove collateral taken from loan and pool @@ -754,11 +754,11 @@ export function handleTake(event: TakeEvent): void { // update loan state const loanId = getLoanId(pool.id, addressToBytes(event.params.borrower)) const loan = Loan.load(loanId)! - const borrowerInfo = getBorrowerInfoERC721Pool(addressToBytes(event.params.borrower), pool.id) + const borrowerInfo = getBorrowerInfo(addressToBytes(event.params.borrower), pool.id) loan.collateralPledged = wadToDecimal(borrowerInfo.collateral) loan.t0debt = wadToDecimal(borrowerInfo.t0debt) loan.t0Np = wadToDecimal(borrowerInfo.t0Np) - loan.t0ThresholdPrice = wadToDecimal(borrowerInfo.t0ThresholdPrice) + loan.thresholdPrice = wadToDecimal(borrowerInfo.thresholdPrice) // remove tokenIdsTaken from loan and pool tokenIdsPledged const numberOfTokensToTake = getWadCollateralFloorTokens(event.params.collateral).toI32() diff --git a/src/utils/pool/loan.ts b/src/utils/pool/loan.ts index e5f0e64..5792d89 100644 --- a/src/utils/pool/loan.ts +++ b/src/utils/pool/loan.ts @@ -21,7 +21,8 @@ export function loadOrCreateLoan(loanId: Bytes, poolId: Bytes, borrower: Bytes): loan.poolAddress = poolId.toHexString() loan.collateralPledged = ZERO_BD loan.t0debt = ZERO_BD - loan.t0ThresholdPrice = ZERO_BD + loan.t0Np = ZERO_BD + loan.thresholdPrice = ZERO_BD loan.inLiquidation = false loan.liquidationAuction = null loan.tokenIdsPledged = [] @@ -47,29 +48,18 @@ export class BorrowerInfo { t0debt: BigInt collateral: BigInt t0Np: BigInt - t0ThresholdPrice: BigInt - constructor(t0debt: BigInt, collateral: BigInt, t0Np: BigInt, t0ThresholdPrice: BigInt) { + thresholdPrice: BigInt + constructor(t0debt: BigInt, collateral: BigInt, t0Np: BigInt, thresholdPrice: BigInt) { this.t0debt = t0debt this.collateral = collateral this.t0Np = t0Np - this.t0ThresholdPrice = t0ThresholdPrice + this.thresholdPrice = thresholdPrice } } export function getBorrowerInfo(borrower: Bytes, poolId: Bytes): BorrowerInfo { - const poolContract = ERC20Pool.bind(Address.fromBytes(poolId)) - const borrowerInfoResult = poolContract.borrowerInfo(Address.fromBytes(borrower)) - - return new BorrowerInfo( - borrowerInfoResult.value0, - borrowerInfoResult.value1, - borrowerInfoResult.value2, - borrowerInfoResult.value3 - ) -} - -export function getBorrowerInfoERC721Pool(borrower: Bytes, poolId: Bytes): BorrowerInfo { - const poolContract = ERC721Pool.bind(Address.fromBytes(poolId)) - const borrowerInfoResult = poolContract.borrowerInfo(Address.fromBytes(borrower)) + const poolInfoUtilsAddress = poolInfoUtilsAddressTable.get(dataSource.network())! + const poolInfoUtilsContract = PoolInfoUtils.bind(poolInfoUtilsAddress) + const borrowerInfoResult = poolInfoUtilsContract.borrowerInfo(Address.fromBytes(poolId), Address.fromBytes(borrower)) return new BorrowerInfo( borrowerInfoResult.value0, diff --git a/subgraph.yaml b/subgraph.yaml index e6bc1ce..16b0fc3 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -69,7 +69,7 @@ dataSources: - name: PoolInfoUtils file: ./abis/PoolInfoUtils.json eventHandlers: - - event: PoolCreated(address) + - event: PoolCreated(address,bytes32) handler: handlePoolCreated file: ./src/mappings/erc-20-pool-factory.ts network: goerli @@ -97,7 +97,7 @@ dataSources: - name: PoolInfoUtils file: ./abis/PoolInfoUtils.json eventHandlers: - - event: PoolCreated(address) + - event: PoolCreated(address,bytes32) handler: handlePoolCreated file: ./src/mappings/erc-721-pool-factory.ts network: goerli diff --git a/tests/utils/mock-contract-calls.ts b/tests/utils/mock-contract-calls.ts index ab97f4d..8d2dcb2 100644 --- a/tests/utils/mock-contract-calls.ts +++ b/tests/utils/mock-contract-calls.ts @@ -5,7 +5,7 @@ import { BucketInfo } from "../../src/utils/pool/bucket" import { positionManagerAddressTable, poolInfoUtilsAddressTable, ZERO_BI, ONE_BI, poolInfoUtilsMulticallAddressTable } from '../../src/utils/constants'; import { BurnInfo, DebtInfo, LoansInfo, PoolPricesInfo, PoolUtilizationInfo, ReservesInfo, PoolDetails, RatesAndFees, PoolBalanceDetails, depositUpToIndex } from '../../src/utils/pool/pool'; import { AuctionInfo, AuctionStatus } from "../../src/utils/pool/liquidation" -import { BorrowerInfo, thresholdPrice } from '../../src/utils/pool/loan'; +import { BorrowerInfo } from '../../src/utils/pool/loan'; import { wdiv, wmin, wmul } from "../../src/utils/math" import { addressToBytes, decimalToWad } from "../../src/utils/convert" import { Pool } from "../../generated/schema" @@ -137,13 +137,13 @@ export function mockGetPositionInfo(tokenId: BigInt, bucketIndex: BigInt, expect /***************************/ export function mockGetBorrowerInfo(pool: Address, borrower: Address, expectedInfo: BorrowerInfo): void { - createMockedFunction(pool, 'borrowerInfo', 'borrowerInfo(address):(uint256,uint256,uint256,uint256)') - .withArgs([ethereum.Value.fromAddress(borrower)]) + createMockedFunction(poolInfoUtilsAddressTable.get(dataSource.network())!, 'borrowerInfo', 'borrowerInfo(address,address):(uint256,uint256,uint256,uint256)') + .withArgs([ethereum.Value.fromAddress(pool), ethereum.Value.fromAddress(borrower)]) .returns([ ethereum.Value.fromUnsignedBigInt(expectedInfo.t0debt), ethereum.Value.fromUnsignedBigInt(expectedInfo.collateral), ethereum.Value.fromUnsignedBigInt(expectedInfo.t0Np), - ethereum.Value.fromUnsignedBigInt(expectedInfo.t0ThresholdPrice) + ethereum.Value.fromUnsignedBigInt(expectedInfo.thresholdPrice) ]) }