Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug awarding LP to wrong actor handling bucketTake #58

Merged
merged 4 commits into from
Aug 21, 2023
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
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ type Lend @entity {
id: Bytes!
# bucket to which a lend was made
bucket: Bucket!
# index of the bucket, for querying
bucketIndex: Int!
# pool address
poolAddress: String!
# address of the lender
Expand Down
10 changes: 5 additions & 5 deletions src/erc-20-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function handleAddCollateral(event: AddCollateralEvent): void {

// update lend state
const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, addCollateral.actor)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, addCollateral.actor)
lend.depositTime = addCollateral.blockTimestamp
lend.lpb = lend.lpb.plus(addCollateral.lpAwarded)
lend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, lend.lpb)
Expand Down Expand Up @@ -319,9 +319,9 @@ export function handleBucketTake(event: BucketTakeEvent): void {
const lpAwardedId = event.transaction.hash.concatI32(event.logIndex.toI32() - 1);
const bucketTakeLpAwarded = BucketTakeLPAwarded.load(lpAwardedId)!
const kickerLendId = getLendId(bucketId, bucketTakeLpAwarded.kicker)
const kickerLend = loadOrCreateLend(bucketId, kickerLendId, pool.id, bucketTakeLpAwarded.kicker)
const kickerLend = loadOrCreateLend(bucketId, kickerLendId, pool.id, bucket.bucketIndex, bucketTakeLpAwarded.kicker)
kickerLend.depositTime = bucketTake.blockTimestamp
kickerLend.lpb = kickerLend.lpb.plus(bucketTakeLpAwarded.lpAwardedTaker)
kickerLend.lpb = kickerLend.lpb.plus(bucketTakeLpAwarded.lpAwardedKicker)
kickerLend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, kickerLend.lpb)
updateBucketLends(bucket, kickerLendId)

Expand All @@ -332,7 +332,7 @@ export function handleBucketTake(event: BucketTakeEvent): void {

// update lend state for taker
const takerLendId = getLendId(bucketId, bucketTakeLpAwarded.taker)
const takerLend = loadOrCreateLend(bucketId, takerLendId, pool.id, bucketTakeLpAwarded.taker)
const takerLend = loadOrCreateLend(bucketId, takerLendId, pool.id, bucket.bucketIndex, bucketTakeLpAwarded.taker)
takerLend.depositTime = bucketTake.blockTimestamp
takerLend.lpb = takerLend.lpb.plus(bucketTakeLpAwarded.lpAwardedTaker)
takerLend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, takerLend.lpb)
Expand Down Expand Up @@ -607,7 +607,7 @@ export function handleRemoveCollateral(event: RemoveCollateralEvent): void {

// update lend state
const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, removeCollateral.claimer)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, removeCollateral.claimer)
if (removeCollateral.lpRedeemed.le(lend.lpb)) {
lend.lpb = lend.lpb.minus(removeCollateral.lpRedeemed)
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/erc-721-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export function handleAddCollateralNFT(event: AddCollateralNFTEvent): void {

// update lend state
const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, addCollateralNFT.actor)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, addCollateralNFT.actor)
lend.depositTime = addCollateralNFT.blockTimestamp
lend.lpb = lend.lpb.plus(addCollateralNFT.lpAwarded)
lend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, lend.lpb)
Expand Down Expand Up @@ -370,7 +370,7 @@ export function handleRemoveCollateral(event: RemoveCollateralEvent): void {
account.txCount = account.txCount.plus(ONE_BI)

const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, removeCollateral.claimer)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, removeCollateral.claimer)
if (removeCollateral.lpRedeemed.le(lend.lpb)){
lend.lpb = lend.lpb.minus(removeCollateral.lpRedeemed)
} else {
Expand Down Expand Up @@ -463,7 +463,7 @@ export function handleMergeOrRemoveCollateralNFT(

// update lend state
const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, event.params.actor)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, event.params.actor)
lend.depositTime = mergeOrRemove.blockTimestamp
lend.lpb = wadToDecimal(getLenderInfoERC721Pool(pool.id, index, event.params.actor).lpBalance)
lend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, lend.lpb)
Expand Down Expand Up @@ -769,9 +769,9 @@ export function handleBucketTake(event: BucketTakeEvent): void {
const lpAwardedId = event.transaction.hash.concatI32(event.logIndex.toI32() - 1);
const bucketTakeLpAwarded = BucketTakeLPAwarded.load(lpAwardedId)!
const kickerLendId = getLendId(bucketId, bucketTakeLpAwarded.kicker)
const kickerLend = loadOrCreateLend(bucketId, kickerLendId, pool.id, bucketTakeLpAwarded.kicker)
const kickerLend = loadOrCreateLend(bucketId, kickerLendId, pool.id, bucket.bucketIndex, bucketTakeLpAwarded.kicker)
kickerLend.depositTime = bucketTake.blockTimestamp
kickerLend.lpb = kickerLend.lpb.plus(bucketTakeLpAwarded.lpAwardedTaker)
kickerLend.lpb = kickerLend.lpb.plus(bucketTakeLpAwarded.lpAwardedKicker)
kickerLend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, kickerLend.lpb)
updateBucketLends(bucket, kickerLendId)

Expand All @@ -782,7 +782,7 @@ export function handleBucketTake(event: BucketTakeEvent): void {

// update lend state for taker
const takerLendId = getLendId(bucketId, bucketTakeLpAwarded.taker)
const takerLend = loadOrCreateLend(bucketId, takerLendId, pool.id, bucketTakeLpAwarded.taker)
const takerLend = loadOrCreateLend(bucketId, takerLendId, pool.id, bucket.bucketIndex, bucketTakeLpAwarded.taker)
takerLend.depositTime = bucketTake.blockTimestamp
takerLend.lpb = takerLend.lpb.plus(bucketTakeLpAwarded.lpAwardedTaker)
takerLend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, takerLend.lpb)
Expand Down
12 changes: 6 additions & 6 deletions src/mappings/base/base-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function _handleAddQuoteToken(erc20Event: AddQuoteTokenERC20Event | null,

// update lend state
const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, addQuoteToken.lender)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, addQuoteToken.lender)
lend.depositTime = blockTimestamp
lend.lpb = lend.lpb.plus(addQuoteToken.lpAwarded)
lend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, lend.lpb)
Expand Down Expand Up @@ -211,7 +211,7 @@ export function _handleMoveQuoteToken(erc20Event: MoveQuoteTokenERC20Event | nul

// update from bucket lend state
const fromBucketLendId = getLendId(fromBucketId, lender)
const fromBucketLend = loadOrCreateLend(fromBucketId, fromBucketLendId, pool.id, moveQuoteToken.lender)
const fromBucketLend = loadOrCreateLend(fromBucketId, fromBucketLendId, pool.id, fromBucket.bucketIndex, moveQuoteToken.lender)
if (moveQuoteToken.lpRedeemedFrom.le(fromBucketLend.lpb)) {
fromBucketLend.lpb = fromBucketLend.lpb.minus(moveQuoteToken.lpRedeemedFrom)
} else {
Expand All @@ -223,7 +223,7 @@ export function _handleMoveQuoteToken(erc20Event: MoveQuoteTokenERC20Event | nul

// update to bucket lend state
const toBucketLendId = getLendId(toBucketId, lender)
const toBucketLend = loadOrCreateLend(toBucketId, toBucketLendId, pool.id, moveQuoteToken.lender)
const toBucketLend = loadOrCreateLend(toBucketId, toBucketLendId, pool.id, toBucket.bucketIndex, moveQuoteToken.lender)
toBucketLend.depositTime = getDepositTime(fromBucketLend.depositTime, toBucketLend)
toBucketLend.lpb = toBucketLend.lpb.plus(wadToDecimal(lpAwardedTo))
toBucketLend.lpbValueInQuote = lpbValueInQuote(pool.id, toBucket.bucketIndex, toBucketLend.lpb)
Expand Down Expand Up @@ -327,7 +327,7 @@ export function _handleRemoveQuoteToken(erc20Event: RemoveQuoteTokenERC20Event |

// update lend state
const lendId = getLendId(bucketId, accountId)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, removeQuote.lender)
const lend = loadOrCreateLend(bucketId, lendId, pool.id, bucket.bucketIndex, removeQuote.lender)
if (removeQuote.lpRedeemed.le(lend.lpb)) {
lend.lpb = lend.lpb.minus(removeQuote.lpRedeemed)
} else {
Expand Down Expand Up @@ -425,15 +425,15 @@ export function _handleTransferLP(erc20Event: TransferLPERC20Event | null, erc72

// event does not reveal LP amounts transferred for each bucket, so query the pool and update
// remove old lend
const oldLend = loadOrCreateLend(bucketId, oldLendId, pool.id, transferLP.owner)
const oldLend = loadOrCreateLend(bucketId, oldLendId, pool.id, bucketIndex.toU32(), transferLP.owner)
oldLend.lpb = wadToDecimal(getLenderInfo(pool.id, bucketIndex, owner).lpBalance)
oldLend.lpbValueInQuote = lpbValueInQuote(pool.id, bucket.bucketIndex, oldLend.lpb)
oldLend.save()
updateAccountLends(oldOwnerAccount, oldLend)
updateBucketLends(bucket, oldLendId)

// add new lend
const newLend = loadOrCreateLend(bucketId, newLendId, pool.id, transferLP.newOwner)
const newLend = loadOrCreateLend(bucketId, newLendId, pool.id, bucketIndex.toU32(), transferLP.newOwner)
const newLendInfo = getLenderInfo(pool.id, bucketIndex, newOwner)
newLend.depositTime = newLendInfo.depositTime
newLend.lpb = wadToDecimal(newLendInfo.lpBalance)
Expand Down
4 changes: 2 additions & 2 deletions src/position-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ export function handleMoveLiquidity(event: MoveLiquidityEvent): void {

const bucketIdFrom = getBucketId(moveLiquidity.pool, moveLiquidity.fromIndex)
const lendIdFrom = getLendId(bucketIdFrom, moveLiquidity.lender)
const lendFrom = loadOrCreateLend(bucketIdFrom, lendIdFrom, moveLiquidity.pool, moveLiquidity.lender)
const lendFrom = loadOrCreateLend(bucketIdFrom, lendIdFrom, moveLiquidity.pool, moveLiquidity.fromIndex, moveLiquidity.lender)
const bucketIdTo = getBucketId(moveLiquidity.pool, moveLiquidity.toIndex)
const lendIdTo = getLendId(bucketIdTo, moveLiquidity.lender)
const lendTo = loadOrCreateLend(bucketIdTo, lendIdTo, moveLiquidity.pool, moveLiquidity.lender)
const lendTo = loadOrCreateLend(bucketIdTo, lendIdTo, moveLiquidity.pool, moveLiquidity.toIndex, moveLiquidity.lender)

const lendToInfo = getLenderInfo(moveLiquidity.pool, event.params.toIndex, Address.fromBytes(lendTo.lender))
lendTo.depositTime = lendToInfo.depositTime
Expand Down
5 changes: 3 additions & 2 deletions src/utils/pool/lend.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, BigDecimal, BigInt, Bytes, dataSource } from "@graphprotocol/graph-ts"
import { Lend } from "../../../generated/schema"
import { Bucket, Lend } from "../../../generated/schema"
import { PoolInfoUtils } from "../../../generated/templates/ERC20Pool/PoolInfoUtils"

import { poolInfoUtilsAddressTable, ZERO_BD, ZERO_BI } from "../constants"
Expand All @@ -14,13 +14,14 @@ export function getLendId(bucketId: Bytes, accountId: Bytes): Bytes {
return bucketId.concat(Bytes.fromUTF8('|').concat(accountId))
}

export function loadOrCreateLend(bucketId: Bytes, lendId: Bytes, poolId: Bytes, lender: Bytes): Lend {
export function loadOrCreateLend(bucketId: Bytes, lendId: Bytes, poolId: Bytes, bucketIndex: u32, lender: Bytes): Lend {
let lend = Lend.load(lendId)
if (lend == null) {
// create new lend if one already been stored
lend = new Lend(lendId) as Lend

lend.bucket = bucketId
lend.bucketIndex = bucketIndex
lend.depositTime = ZERO_BI
lend.lender = lender
lend.pool = poolId
Expand Down
3 changes: 2 additions & 1 deletion tests/position-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { assertPosition, createApprovalEvent, createBurnEvent, createMemorialize
import { bigIntToBytes, wadToDecimal } from "../src/utils/convert"
import { create721Pool, mockGetLPBValueInQuote, mockGetLenderInfo, mockGetPoolKey, mockGetTokenName, mockGetTokenSymbol } from "./utils/common"
import { Lend } from "../generated/schema"
import { getLendId, loadOrCreateLend } from "../src/utils/pool/lend"
import { getLendId } from "../src/utils/pool/lend"
import { getBucketId } from "../src/utils/pool/bucket"
import { FIVE_PERCENT_BI, ZERO_BI } from "../src/utils/constants"

Expand Down Expand Up @@ -277,6 +277,7 @@ describe("Describe entity assertions", () => {
const bucketId = getBucketId(pool, fromIndex.toU32())
const lend = new Lend(getLendId(bucketId, lender))
lend.bucket = bucketId
lend.bucketIndex = fromIndex.toU32()
lend.depositTime = BigInt.fromI32(1000)
lend.lender = lender
lend.pool = pool
Expand Down