Skip to content

Commit

Permalink
finish associating positions and accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Aug 23, 2023
1 parent 2963f0a commit 4cf0f09
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/mappings/base/base-pool.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, BigInt, Bytes, ethereum, log } from "@graphprotocol/graph-ts"
import { Account, AddQuoteToken, Bucket, BucketBankruptcy, Flashloan, Lend, LoanStamped, MoveQuoteToken, Pool, RemoveQuoteToken, ReserveAuctionKick, ReserveAuctionTake, Token, TransferLP, UpdateInterestRate } from "../../../generated/schema"
import { Account, AddQuoteToken, Bucket, BucketBankruptcy, Flashloan, Lend, LoanStamped, MoveQuoteToken, Pool, PositionLend, RemoveQuoteToken, ReserveAuctionKick, ReserveAuctionTake, Token, TransferLP, UpdateInterestRate } from "../../../generated/schema"
import {
AddQuoteToken as AddQuoteTokenERC20Event,
MoveQuoteToken as MoveQuoteTokenERC20Event,
Expand All @@ -22,6 +22,7 @@ import { addReserveAuctionToPool, getBurnInfo, getLenderInfo, isERC20Pool, updat
import { incrementTokenTxCount as incrementTokenTxCountERC20Pool } from "../../utils/token-erc20"
import { incrementTokenTxCount as incrementTokenTxCountERC721Pool } from "../../utils/token-erc721"
import { loadOrCreateReserveAuction, reserveAuctionKickerReward } from "../../utils/pool/reserve-auction"
import { saveOrRemovePositionLend } from "../../utils/position"


/*******************************/
Expand Down Expand Up @@ -638,6 +639,16 @@ export function _handleBucketBankruptcy(event: ethereum.Event, index: BigInt, lp
saveOrRemoveLend(lend)
}

// iterate through all bucket positionLends and set positionLend.lpb to zero
for (let i = 0; i < bucket.positionLends.length; i++) {
const positionLendId = bucket.positionLends[i]
const positionLend = PositionLend.load(positionLendId)!
positionLend.depositTime = bucketBankruptcy.blockTimestamp.plus(ONE_BI)
positionLend.lpb = ZERO_BD
positionLend.lpbValueInQuote = ZERO_BD
saveOrRemovePositionLend(positionLend)
}

// save entities to store
pool.save()
bucket.save()
Expand Down
12 changes: 11 additions & 1 deletion src/mappings/position-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { getBucketId } from "../utils/pool/bucket"
import { lpbValueInQuote, saveOrRemoveLend } from "../utils/pool/lend"
import { ONE_BI, ZERO_BD } from "../utils/constants"
import { addressToBytes, bigIntArrayToIntArray, wadToDecimal } from "../utils/convert"
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"
Expand Down Expand Up @@ -71,6 +71,10 @@ export function handleBurn(event: BurnEvent): void {
burn.blockTimestamp = event.block.timestamp
burn.transactionHash = event.transaction.hash

// remove tokenId from account's list of positions
const account = loadOrCreateAccount(burn.lender)
const index = account.positions.indexOf(bigIntToBytes(burn.tokenId))
if (index != -1) account.positions.splice(index, 1)
deletePosition(event.params.tokenId);

burn.save()
Expand Down Expand Up @@ -305,6 +309,12 @@ export function handleTransfer(event: TransferEvent): void {
position.owner = event.params.to
position.tokenURI = getTokenURI(event.address, transfer.tokenId)

// remove position from old account
const account = loadOrCreateAccount(transfer.from)
const index = account.positions.indexOf(bigIntToBytes(transfer.tokenId))
if (index != -1) account.positions.splice(index, 1)
updateAccountPositions(account, position)

token.save();
position.save()
transfer.save()
Expand Down
10 changes: 6 additions & 4 deletions src/utils/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@ export function deletePosition(tokenId: BigInt): void {

export function updatePositionLends(positionLend: PositionLend): void {
// add positionLend to bucket array if necessary
const bucket = Bucket.load(positionLend.bucket)!
const existingBucketIndex = bucket.positionLends.indexOf(positionLend.id)
if (existingBucketIndex != -1) {
bucket.positionLends = bucket.positionLends.concat([positionLend.id])
const bucket = Bucket.load(positionLend.bucket)
if (bucket != null) {
const existingBucketIndex = bucket.positionLends.indexOf(positionLend.id)
if (existingBucketIndex != -1) {
bucket.positionLends = bucket.positionLends.concat([positionLend.id])
}
}

// add positionLend to position array if necessary
Expand Down

0 comments on commit 4cf0f09

Please sign in to comment.