Skip to content

Commit

Permalink
wip allowances test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Sep 18, 2023
1 parent d2e7b76 commit 3d26124
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 2 deletions.
66 changes: 64 additions & 2 deletions tests/erc-721-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
} from "matchstick-as/assembly/index"
import { Address, BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts"
import { Account, Lend, Loan } from "../generated/schema"
import { handleAddCollateralNFT, handleAddQuoteToken, handleDrawDebtNFT, handleRepayDebt, handleMergeOrRemoveCollateralNFT, handleRemoveCollateral, handleKick, handleAuctionNFTSettle, handleSettle, handleTake, handleBucketTakeLPAwarded, handleBucketTake, handleTransferLP } from "../src/mappings/erc-721-pool"
import { createAddCollateralNFTEvent, createAddQuoteTokenEvent, createDrawDebtNFTEvent, createRepayDebtEvent, createMergeOrRemoveCollateralNFTEvent, createRemoveCollateralEvent, createKickEvent, createAuctionNFTSettleEvent, createSettleEvent, createTakeEvent, createBucketTakeEvent, createBucketTakeLPAwardedEvent, createTransferLPEvent } from "./utils/erc-721-pool-utils"
import { handleAddCollateralNFT, handleAddQuoteToken, handleDrawDebtNFT, handleRepayDebt, handleMergeOrRemoveCollateralNFT, handleRemoveCollateral, handleKick, handleAuctionNFTSettle, handleSettle, handleTake, handleBucketTakeLPAwarded, handleBucketTake, handleTransferLP, handleApproveLPTransferors, handleIncreaseLPAllowance, handleDecreaseLPAllowance } from "../src/mappings/erc-721-pool"
import { createAddCollateralNFTEvent, createAddQuoteTokenEvent, createDrawDebtNFTEvent, createRepayDebtEvent, createMergeOrRemoveCollateralNFTEvent, createRemoveCollateralEvent, createKickEvent, createAuctionNFTSettleEvent, createSettleEvent, createTakeEvent, createBucketTakeEvent, createBucketTakeLPAwardedEvent, createTransferLPEvent, createApproveLPTransferorsEvent, createIncreaseLPAllowanceEvent, createDecreaseLPAllowanceEvent } from "./utils/erc-721-pool-utils"

import { FIVE_PERCENT_BI, MAX_PRICE, MAX_PRICE_BI, MAX_PRICE_INDEX, ONE_BI, ONE_PERCENT_BI, ONE_WAD_BI, TWO_BI, ZERO_ADDRESS, ZERO_BD, ZERO_BI } from "../src/utils/constants"
import { assertBucketUpdate, assertLendUpdate, create721Pool, createAndHandleAddQuoteTokenEvent, createPool } from "./utils/common"
Expand Down Expand Up @@ -1510,6 +1510,68 @@ describe("Describe entity assertions", () => {
}
})

test("LPB Allowance and Transferors", () => {
const poolAddress = Address.fromString("0x0000000000000000000000000000000000000001")
const expectedPoolAddress = addressToBytes(poolAddress)
const lender = Address.fromString("0x0000000000000000000000000000000000000003")
const spender = Address.fromString("0x0000000000000000000000000000000000000005")
const transferors = [Address.fromString("0x0000000000000000000000000000000000000005")]
const indexes = [BigInt.fromI32(234), BigInt.fromI32(345), BigInt.fromI32(456), BigInt.fromI32(567), BigInt.fromI32(789)]
let amounts = [BigInt.fromI32(1000), BigInt.fromI32(1000), BigInt.fromI32(1000), BigInt.fromI32(1000), BigInt.fromI32(1000)]

// TODO: add quote token?

/******************************************/
/*** Approve Transferors and Allowances ***/
/******************************************/

// _handleApproveLPTransferors
const newApproveLPTransferorsEvent = createApproveLPTransferorsEvent(poolAddress, lender, transferors)
handleApproveLPTransferors(newApproveLPTransferorsEvent)

// _handleIncreaseLPAllowance
const newIncreaseLPAllowanceEvent = createIncreaseLPAllowanceEvent(poolAddress, lender, spender, indexes, amounts)
handleIncreaseLPAllowance(newIncreaseLPAllowanceEvent)

// _handleDecreaseLPAllowance
// cut the allowance in half for each bucket index
amounts = [BigInt.fromI32(500), BigInt.fromI32(500), BigInt.fromI32(500), BigInt.fromI32(500), BigInt.fromI32(500)]
const newDecreaseLPAllowanceEvent = createDecreaseLPAllowanceEvent(poolAddress, lender, spender, indexes, amounts)
handleDecreaseLPAllowance(newDecreaseLPAllowanceEvent)

/********************/
/*** Assert State ***/
/********************/

// check entities have been stored
assert.entityCount("LPTransferorList", 1)
assert.entityCount("LPAllowance", 5)
assert.entityCount("LPAllowanceList", 1)
assert.entityCount("ApproveLPTransferors", 1)
assert.entityCount("DecreaseLPAllowance", 1)
assert.entityCount("IncreaseLPAllowance", 1)
assert.entityCount("Pool", 2)

// check LPTransferorList state
assert.fieldEquals(
"LPTransferorList",
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
"pool",
`${expectedPoolAddress.toHexString()}`
)
assert.fieldEquals(
"LPTransferorList",
"0xa16081f360e3847006db660bae1c6d1b2e17ec2a01000000",
"lender",
`${lender.toHexString()}`
)

// _handleRevokeLPAllowance

// _handleRevokeLPTransferors

})

// TODO: finish implementing once a mergeOrRemoveCollateralNFT calldata becomes available
test("MergeOrRemoveCollateralNFT", () => {
// check entity is unavailable prior to storage
Expand Down
146 changes: 146 additions & 0 deletions tests/utils/erc-721-pool-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import { ethereum, Address, BigInt, Bytes } from "@graphprotocol/graph-ts"
import {
AddCollateralNFT,
AddQuoteToken,
ApproveLPTransferors,
AuctionNFTSettle,
BucketBankruptcy,
BucketTake,
BucketTakeLPAwarded,
DecreaseLPAllowance,
DrawDebtNFT,
Flashloan,
IncreaseLPAllowance,
Kick,
KickReserveAuction,
MergeOrRemoveCollateralNFT,
RemoveCollateral,
RepayDebt,
RevokeLPAllowance,
RevokeLPTransferors,
ReserveAuction,
Settle,
Take,
Expand Down Expand Up @@ -128,6 +133,147 @@ export function createAuctionNFTSettleEvent(
return auctionNftSettleEvent
}

export function createApproveLPTransferorsEvent(
pool: Address,
lender: Address,
transferors: Array<Address>
): ApproveLPTransferors {
let approveLPTransferorsEvent = changetype<ApproveLPTransferors>(newMockEvent())

approveLPTransferorsEvent.parameters = new Array()

approveLPTransferorsEvent.parameters.push(
new ethereum.EventParam("lender", ethereum.Value.fromAddress(lender))
)
approveLPTransferorsEvent.parameters.push(
new ethereum.EventParam(
"transferors",
ethereum.Value.fromAddressArray(transferors)
)
)

// update transaction target to the expected pool address
approveLPTransferorsEvent.address = pool

return approveLPTransferorsEvent
}

export function createDecreaseLPAllowanceEvent(
pool: Address,
lender: Address,
spender: Address,
indexes: Array<BigInt>,
amounts: Array<BigInt>
): DecreaseLPAllowance {
let decreaseLPAllowanceEvent = changetype<DecreaseLPAllowance>(newMockEvent())

decreaseLPAllowanceEvent.parameters = new Array()

decreaseLPAllowanceEvent.parameters.push(
new ethereum.EventParam("spender", ethereum.Value.fromAddress(spender))
)
decreaseLPAllowanceEvent.parameters.push(
new ethereum.EventParam(
"indexes",
ethereum.Value.fromUnsignedBigIntArray(indexes)
)
)
decreaseLPAllowanceEvent.parameters.push(
new ethereum.EventParam(
"amounts",
ethereum.Value.fromUnsignedBigIntArray(amounts)
)
)

// update transaction to expected addresses
decreaseLPAllowanceEvent.address = pool
decreaseLPAllowanceEvent.transaction.from = lender

return decreaseLPAllowanceEvent
}

export function createIncreaseLPAllowanceEvent(
pool: Address,
lender: Address,
spender: Address,
indexes: Array<BigInt>,
amounts: Array<BigInt>
): IncreaseLPAllowance {
let increaseLPAllowanceEvent = changetype<IncreaseLPAllowance>(newMockEvent())

increaseLPAllowanceEvent.parameters = new Array()

increaseLPAllowanceEvent.parameters.push(
new ethereum.EventParam("spender", ethereum.Value.fromAddress(spender))
)
increaseLPAllowanceEvent.parameters.push(
new ethereum.EventParam(
"indexes",
ethereum.Value.fromUnsignedBigIntArray(indexes)
)
)
increaseLPAllowanceEvent.parameters.push(
new ethereum.EventParam(
"amounts",
ethereum.Value.fromUnsignedBigIntArray(amounts)
)
)
// update transaction to expected addresses
increaseLPAllowanceEvent.address = pool
increaseLPAllowanceEvent.transaction.from = lender

return increaseLPAllowanceEvent
}

export function createRevokeLPAllowanceEvent(
pool: Address,
spender: Address,
indexes: Array<BigInt>
): RevokeLPAllowance {
let revokeLPAllowanceEvent = changetype<RevokeLPAllowance>(newMockEvent())

revokeLPAllowanceEvent.parameters = new Array()

revokeLPAllowanceEvent.parameters.push(
new ethereum.EventParam("spender", ethereum.Value.fromAddress(spender))
)
revokeLPAllowanceEvent.parameters.push(
new ethereum.EventParam(
"indexes",
ethereum.Value.fromUnsignedBigIntArray(indexes)
)
)
// update transaction target to the expected pool address
revokeLPAllowanceEvent.address = pool

return revokeLPAllowanceEvent
}

export function createRevokeLPTransferorsEvent(
pool: Address,
lender: Address,
transferors: Array<Address>
): RevokeLPTransferors {
let revokeLPTransferorsEvent = changetype<RevokeLPTransferors>(newMockEvent())

revokeLPTransferorsEvent.parameters = new Array()

revokeLPTransferorsEvent.parameters.push(
new ethereum.EventParam("lender", ethereum.Value.fromAddress(lender))
)
revokeLPTransferorsEvent.parameters.push(
new ethereum.EventParam(
"transferors",
ethereum.Value.fromAddressArray(transferors)
)
)

// update transaction target to the expected pool address
revokeLPTransferorsEvent.address = pool

return revokeLPTransferorsEvent
}

export function createBucketBankruptcyEvent(
pool: Address,
index: BigInt,
Expand Down

0 comments on commit 3d26124

Please sign in to comment.