Skip to content

Commit

Permalink
call getPositionInfo; add depositTime to PositionLend
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Aug 23, 2023
1 parent 0333536 commit 3b2f0aa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
4 changes: 2 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -716,12 +716,12 @@ type Position @entity {
tokenURI: String! # tokenURI of the positionNFT
}

# TODO: add depositTime?
# created to handle the seperate lpb positions for each bucket index
type PositionLend @entity {
id: Bytes! # $positionId + '|' + $bucketIndex
bucket: Bucket! # pointer to associated Bucket entity
bucketIndex: BigInt! # index of the bucket with lpb
bucketIndex: Int! # index of the bucket with lpb
depositTime: BigInt! # time at which the position was deposited
lpb: BigDecimal! # amount of LPB position has in the bucket
lpbValueInQuote: BigDecimal! # quote equivalent value of LPB in the bucket
}
Expand Down
14 changes: 6 additions & 8 deletions src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { getDepositTime, lpbValueInQuote } from "../utils/pool/lend"
import { ONE_BI, ZERO_BD } from "../utils/constants"
import { addressToBytes, bigIntArrayToIntArray, wadToDecimal } from "../utils/convert"
import { getLendId, loadOrCreateLend } from "../utils/pool/lend"
import { deletePosition, getPoolForToken, getPositionLendId, loadOrCreateLPToken, loadOrCreatePosition, loadOrCreatePositionLend } from "../utils/position"
import { deletePosition, getPoolForToken, getPositionInfo, getPositionLendId, loadOrCreateLPToken, loadOrCreatePosition, loadOrCreatePositionLend } from "../utils/position"
import { getLenderInfo } from "../utils/pool/pool"

export function handleApproval(event: ApprovalEvent): void {
Expand Down Expand Up @@ -76,6 +76,7 @@ export function handleBurn(event: BurnEvent): void {
burn.save()
}

// Lends are updated in the associated `TransferLP` event
export function handleMemorializePosition(
event: MemorializePositionEvent
): void {
Expand Down Expand Up @@ -108,15 +109,12 @@ export function handleMemorializePosition(
// create PositionLend entity to track each lpb associated with the position
const positionLendId = getPositionLendId(memorialize.tokenId, BigInt.fromI32(index))
const positionLend = loadOrCreatePositionLend(positionLendId, bucketId, index)

// TODO: access the TransferLP event at the correct log index?
// TODO: track the lpb associated with each lend that was memorialized via RPC call
// positionLend.lpb =
const positionInfo = getPositionInfo(memorialize.tokenId, BigInt.fromI32(index))
//track the lpb and depositTime associated with each lend that was memorialized via RPC call
positionLend.depositTime = positionInfo.depositTime
positionLend.lpb = wadToDecimal(positionInfo.lpb)
positionLend.lpbValueInQuote = lpbValueInQuote(memorialize.pool, index, positionLend.lpb)
positionLend.save()

// Lends are updated in the associated `TransferLP` event

// add PositionLend to position
positionIndexes.push(positionLendId)
}
Expand Down
33 changes: 32 additions & 1 deletion src/utils/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { getTokenName, getTokenSymbol, getTokenURI } from "./token-erc721"
import { PositionManager } from "../../generated/PositionManager/PositionManager"
import { bigIntToBytes } from "../utils/convert"

/*****************************/
/*** Constructor Functions ***/
/*****************************/

export function loadOrCreateLPToken(tokenAddress: Address): Token {
const id = addressToBytes(tokenAddress)
let token = Token.load(id)
Expand Down Expand Up @@ -62,8 +66,35 @@ export function deletePosition(tokenId: BigInt): void {
store.remove('Position', bigIntToBytes(tokenId).toHexString())
}

export function deletePositionLend(positionLendId: Bytes): void {
store.remove('PositionLend', positionLendId.toHexString())
}

/*******************************/
/*** Contract Call Functions ***/
/*******************************/

export function getPoolForToken(tokenId: BigInt): Address {
const positionManagerAddress = positionManagerAddressTable.get(dataSource.network())!
const positionManagerContract = PositionManager.bind(positionManagerAddress);
return positionManagerContract.poolKey(tokenId)
}
}

export class PositionInfo {
lpb: BigInt
depositTime: BigInt
constructor(lpb: BigInt, depositTime: BigInt) {
this.lpb = lpb
this.depositTime = depositTime
}
}
export function getPositionInfo(tokenId: BigInt, bucketIndex: BigInt): PositionInfo {
const positionManagerAddress = positionManagerAddressTable.get(dataSource.network())!
const positionManagerContract = PositionManager.bind(positionManagerAddress);
const positionInfoResult = positionManagerContract.getPositionInfo(tokenId, bucketIndex)

return new PositionInfo(
positionInfoResult.value0,
positionInfoResult.value1
)
}

0 comments on commit 3b2f0aa

Please sign in to comment.