From 01b172562d2d72a3dce9e6f485414dc2abffb0b4 Mon Sep 17 00:00:00 2001 From: Tim Reznichenko Date: Wed, 8 Nov 2023 10:20:33 +0100 Subject: [PATCH 1/2] fix: don't update position in handleTransfer for burn address --- .gitignore | 1 + src/mappings/position-manager.ts | 39 +++++++++++++++++++------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 46092ae..e85605d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.DS_Store build/ generated/ data*/ diff --git a/src/mappings/position-manager.ts b/src/mappings/position-manager.ts index 0239b2a..298073f 100644 --- a/src/mappings/position-manager.ts +++ b/src/mappings/position-manager.ts @@ -29,6 +29,7 @@ import { deletePosition, getPoolForToken, getPositionInfo, getPositionLendId, lo import { getLenderInfo } from "../utils/pool/pool" import { getTokenURI } from "../utils/token-erc721" import { loadOrCreateAccount, updateAccountPositions } from "../utils/account" +import { ONE_BI, ZERO_ADDRESS, ZERO_BD } from "../utils/constants"; export function handleApproval(event: ApprovalEvent): void { const approval = new Approval( @@ -281,25 +282,31 @@ export function handleTransfer(event: TransferEvent): void { const token = loadOrCreateLPToken(event.address); transfer.token = token.id; token.txCount = token.txCount.plus(ONE_BI); - const position = loadOrCreatePosition(transfer.tokenId) - position.owner = event.params.to - position.tokenURI = getTokenURI(event.address, transfer.tokenId) - // remove position from old account - const oldOwnerAccount = loadOrCreateAccount(transfer.from) - const index = oldOwnerAccount.positions.indexOf(bigIntToBytes(transfer.tokenId)) - const accountPositions = oldOwnerAccount.positions - if (index != -1) accountPositions.splice(index, 1) - oldOwnerAccount.positions = accountPositions + if (event.params.to != ZERO_ADDRESS) { + const position = loadOrCreatePosition(transfer.tokenId); + position.owner = event.params.to; + position.tokenURI = getTokenURI(event.address, transfer.tokenId); - // add position to new account - const newOwnerAccount = loadOrCreateAccount(transfer.to) - updateAccountPositions(newOwnerAccount, position) + // add position to new account + const newOwnerAccount = loadOrCreateAccount(transfer.to); + updateAccountPositions(newOwnerAccount, position); + + position.save(); + newOwnerAccount.save(); + } + + // remove position from old account + const oldOwnerAccount = loadOrCreateAccount(transfer.from); + const index = oldOwnerAccount.positions.indexOf( + bigIntToBytes(transfer.tokenId) + ); + const accountPositions = oldOwnerAccount.positions; + if (index != -1) accountPositions.splice(index, 1); + oldOwnerAccount.positions = accountPositions; // save entities to store - oldOwnerAccount.save() - newOwnerAccount.save() + oldOwnerAccount.save(); token.save(); - position.save() - transfer.save() + transfer.save(); } From 042f40a77f8b6c26525a5c20addff11e234a5938 Mon Sep 17 00:00:00 2001 From: Tim Reznichenko Date: Thu, 9 Nov 2023 14:36:41 +0100 Subject: [PATCH 2/2] fix: correct logic for updatePositionLends helper (#80) * fix: correct logic for updatePositionLends helper * feat: include pool Entity in Position --- schema.graphql | 2 +- src/mappings/position-manager.ts | 14 +++----------- src/utils/position.ts | 8 +++++--- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/schema.graphql b/schema.graphql index eaec4aa..d45cec4 100644 --- a/schema.graphql +++ b/schema.graphql @@ -713,7 +713,7 @@ type Position @entity { tokenId: BigInt # tokenId indexes: [PositionLend!]! # list of PositionLends which constitute a position owner: Bytes! # address of the position owner - pool: Bytes! # address of the pool that the position is associated with + pool: Pool! # pool that the position is associated with token: Token! # pointer to LPToken entity tokenURI: String! # tokenURI of the positionNFT } diff --git a/src/mappings/position-manager.ts b/src/mappings/position-manager.ts index 298073f..d92c45c 100644 --- a/src/mappings/position-manager.ts +++ b/src/mappings/position-manager.ts @@ -1,4 +1,4 @@ -import { Address, BigInt, log } from "@graphprotocol/graph-ts" +import { BigInt, log } from "@graphprotocol/graph-ts" import { Approval as ApprovalEvent, ApprovalForAll as ApprovalForAllEvent, @@ -21,12 +21,9 @@ import { Transfer } from "../../generated/schema" import { getBucketId } from "../utils/pool/bucket" -import { lpbValueInQuote, saveOrRemoveLend } from "../utils/pool/lend" -import { ONE_BI, ZERO_BD } from "../utils/constants" +import { lpbValueInQuote } from "../utils/pool/lend" import { addressToBytes, bigIntArrayToIntArray, bigIntToBytes, wadToDecimal } from "../utils/convert" -import { getLendId, loadOrCreateLend } from "../utils/pool/lend" -import { deletePosition, getPoolForToken, getPositionInfo, getPositionLendId, loadOrCreateLPToken, loadOrCreatePosition, loadOrCreatePositionLend, saveOrRemovePositionLend, updatePositionLends } from "../utils/position" -import { getLenderInfo } from "../utils/pool/pool" +import { deletePosition, getPoolForToken, getPositionInfo, loadOrCreateLPToken, loadOrCreatePosition, loadOrCreatePositionLend, saveOrRemovePositionLend, updatePositionLends } from "../utils/position" import { getTokenURI } from "../utils/token-erc721" import { loadOrCreateAccount, updateAccountPositions } from "../utils/account" import { ONE_BI, ZERO_ADDRESS, ZERO_BD } from "../utils/constants"; @@ -100,9 +97,6 @@ export function handleMemorializePosition( memorialize.blockTimestamp = event.block.timestamp memorialize.transactionHash = event.transaction.hash - // update entities - const position = loadOrCreatePosition(memorialize.tokenId) - // get lend entities for each index with extant lpb const poolAddress = memorialize.pool const accountId = memorialize.lender @@ -125,12 +119,10 @@ export function handleMemorializePosition( // associate positionLend with Bucket and Position updatePositionLends(positionLend) - bucket.save() } // save entities to store memorialize.save() - position.save() } export function handleMint(event: MintEvent): void { diff --git a/src/utils/position.ts b/src/utils/position.ts index a18facc..fc46c08 100644 --- a/src/utils/position.ts +++ b/src/utils/position.ts @@ -73,16 +73,18 @@ export function updatePositionLends(positionLend: PositionLend): void { const bucket = Bucket.load(positionLend.bucket) if (bucket != null) { const existingBucketIndex = bucket.positionLends.indexOf(positionLend.id) - if (existingBucketIndex != -1) { + if (existingBucketIndex == -1) { bucket.positionLends = bucket.positionLends.concat([positionLend.id]) + bucket.save() } } // add positionLend to position array if necessary - const position = Position.load(bigIntToBytes(positionLend.tokenId))! + const position = loadOrCreatePosition(positionLend.tokenId) const existingPositionIndex = position.indexes.indexOf(positionLend.id) - if (existingPositionIndex != -1) { + if (existingPositionIndex == -1) { position.indexes = position.indexes.concat([positionLend.id]) + position.save() } }