Skip to content

Commit

Permalink
fix: correct logic for updatePositionLends helper (#80)
Browse files Browse the repository at this point in the history
* fix: correct logic for updatePositionLends helper

* feat: include pool Entity in Position
  • Loading branch information
imbaniac authored Nov 9, 2023
1 parent 43ca0dc commit 042f40a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
2 changes: 1 addition & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 3 additions & 11 deletions src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions src/utils/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down

0 comments on commit 042f40a

Please sign in to comment.