Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:ajna-finance/subgraph into info-…
Browse files Browse the repository at this point in the history
…multicall-v2
  • Loading branch information
Mike committed Dec 7, 2023
2 parents 2739272 + 042f40a commit aceb109
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
build/
generated/
data*/
Expand Down
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
53 changes: 26 additions & 27 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,14 +21,12 @@ 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";

export function handleApproval(event: ApprovalEvent): void {
const approval = new Approval(
Expand Down Expand Up @@ -99,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 @@ -124,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 Expand Up @@ -281,25 +274,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();
}
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 aceb109

Please sign in to comment.