-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add user position calculation
- Loading branch information
Showing
10 changed files
with
114 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
subgraphs/user-position-v3/template/mappings/position-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
/* eslint-disable prefer-const */ | ||
import { Address } from "@graphprotocol/graph-ts"; | ||
import { | ||
DecreaseLiquidity, | ||
IncreaseLiquidity, | ||
Transfer, | ||
} from "../generated/NonfungiblePositionManager/NonfungiblePositionManager"; | ||
import { loadTransaction } from "../utils"; | ||
import { loadTransaction } from "../utils/schema"; | ||
import { ADDRESS_ZERO } from "../utils/constants"; | ||
import { UserPosition } from "../generated/schema"; | ||
|
||
export function handleIncreaseLiquidity(event: IncreaseLiquidity): void { | ||
let transaction = loadTransaction(event); | ||
transaction.tokenId = event.params.tokenId; | ||
transaction.save(); | ||
} | ||
|
||
export function handleDecreaseLiquidity(event: DecreaseLiquidity): void { | ||
let transaction = loadTransaction(event); | ||
transaction.tokenId = event.params.tokenId; | ||
transaction.save(); | ||
} | ||
|
||
export function handleTransfer(event: Transfer): void { | ||
let transaction = loadTransaction(event); | ||
if (event.params.from.equals(Address.fromString(ADDRESS_ZERO))) { | ||
transaction.positionOwner = event.params.to; | ||
} else { | ||
let userPosition = UserPosition.load(event.params.tokenId.toString()); | ||
userPosition.owner = event.params.to; | ||
userPosition.save(); | ||
} | ||
transaction.save(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
subgraphs/user-position-v3/template/utils/constants.template.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* eslint-disable prefer-const */ | ||
import { BigInt, BigDecimal } from "@graphprotocol/graph-ts"; | ||
|
||
export const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000"; | ||
|
||
export let ZERO_BI = BigInt.fromI32(0); | ||
export let ONE_BI = BigInt.fromI32(1); | ||
export let ZERO_BD = BigDecimal.fromString("0"); | ||
export let ONE_BD = BigDecimal.fromString("1"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"; | ||
import { Transaction, UserPosition } from "../generated/schema"; | ||
import { ZERO_BI } from "./constants"; | ||
|
||
export function loadTransaction(event: ethereum.Event): Transaction { | ||
let transaction = Transaction.load(event.transaction.hash.toHexString()); | ||
if (transaction === null) { | ||
transaction = new Transaction(event.transaction.hash.toHexString()); | ||
} | ||
transaction.blockNumber = event.block.number; | ||
transaction.timestamp = event.block.timestamp; | ||
transaction.gasUsed = event.transaction.gasUsed; | ||
transaction.gasPrice = event.transaction.gasPrice; | ||
transaction.isPositionUpdated = false; | ||
transaction.save(); | ||
return transaction as Transaction; | ||
} | ||
|
||
export function updateUserPosition(tx: Transaction, poolId: string): void { | ||
if ( | ||
tx.isPositionUpdated === false && | ||
tx.tickLower !== null && | ||
tx.tickUpper !== null && | ||
tx.tokenId !== null && | ||
tx.positionOwner !== null && | ||
(tx.increaseLiquidityAmount !== null || tx.decreaseLiquidityAmount !== null) | ||
) { | ||
let userPosition = UserPosition.load(tx.tokenId.toString()); | ||
if (userPosition === null) { | ||
userPosition = new UserPosition(tx.tokenId.toString()); | ||
userPosition.liquidity = ZERO_BI; | ||
userPosition.pool = poolId; | ||
} | ||
userPosition.tickLower = tx.tickLower as BigInt; | ||
userPosition.tickUpper = tx.tickUpper as BigInt; | ||
userPosition.owner = tx.positionOwner as Bytes; | ||
if (tx.increaseLiquidityAmount !== null) { | ||
userPosition.liquidity = userPosition.liquidity.plus(tx.increaseLiquidityAmount as BigInt); | ||
} | ||
if (tx.decreaseLiquidityAmount !== null) { | ||
userPosition.liquidity = userPosition.liquidity.minus(tx.decreaseLiquidityAmount as BigInt); | ||
} | ||
|
||
tx.isPositionUpdated = true; | ||
tx.save(); | ||
userPosition.save(); | ||
} | ||
} |
Oops, something went wrong.