From c14f0cdce170da017d01f5b7b078a4eff373d0a0 Mon Sep 17 00:00:00 2001 From: mendesfabio Date: Thu, 5 Dec 2024 07:46:53 -0300 Subject: [PATCH] update to final v3 vault --- .gitignore | 4 +- subgraphs/v3-vault/package.json | 2 +- subgraphs/v3-vault/schema.graphql | 9 +- subgraphs/v3-vault/src/helpers/misc.ts | 22 +++++ subgraphs/v3-vault/src/mappings/vault.ts | 94 ++++++++++--------- subgraphs/v3-vault/subgraph.gnosis.yaml | 66 +++++++++++++ .../{template.yaml => subgraph.sepolia.yaml} | 22 +++-- subgraphs/v3-vault/subgraph.yaml | 66 +++++++++++++ 8 files changed, 227 insertions(+), 58 deletions(-) create mode 100644 subgraphs/v3-vault/subgraph.gnosis.yaml rename subgraphs/v3-vault/{template.yaml => subgraph.sepolia.yaml} (72%) create mode 100644 subgraphs/v3-vault/subgraph.yaml diff --git a/.gitignore b/.gitignore index c165d66..ab8a162 100644 --- a/.gitignore +++ b/.gitignore @@ -4,10 +4,10 @@ dist node_modules package-lock.json pnpm-lock.yaml -subgraph*.yaml +# subgraph.yaml +# subgraph*.yaml subgraphs/**/src/types subgraphs/*/node_modules -subgraph.yaml .DS_STORE .pnp.* **/tests/.*/ diff --git a/subgraphs/v3-vault/package.json b/subgraphs/v3-vault/package.json index 290e351..a00ffd0 100644 --- a/subgraphs/v3-vault/package.json +++ b/subgraphs/v3-vault/package.json @@ -8,7 +8,7 @@ "test": "graph test" }, "dependencies": { - "@graphprotocol/graph-cli": "0.66.0", + "@graphprotocol/graph-cli": "^0.66.0", "@graphprotocol/graph-ts": "0.32.0" }, "devDependencies": { diff --git a/subgraphs/v3-vault/schema.graphql b/subgraphs/v3-vault/schema.graphql index 0cfd2b3..ec23320 100644 --- a/subgraphs/v3-vault/schema.graphql +++ b/subgraphs/v3-vault/schema.graphql @@ -38,8 +38,6 @@ type Pool @entity { swapFee: BigDecimal! "Total shares of the Pool" totalShares: BigDecimal! - "Address of the pause manager for this Pool" - pauseManager: Bytes! "Timestamp when the pause window ends" pauseWindowEndTime: BigInt! "Block number when the Pool was created" @@ -51,6 +49,13 @@ type Pool @entity { "Indicates whether the Pool has been initialized" isInitialized: Boolean! + "Account empowered to pause/unpause the pool" + pauseManager: Bytes! + "Account empowered to set static swap fees for a pool" + swapFeeManager: Bytes! + "Account empowered to set the pool creator fee percentage" + poolCreator: Bytes! + "Protocol swap fee percentage" protocolSwapFee: BigDecimal! "Protocol yield fee percentage" diff --git a/subgraphs/v3-vault/src/helpers/misc.ts b/subgraphs/v3-vault/src/helpers/misc.ts index fd06a71..9e5216a 100644 --- a/subgraphs/v3-vault/src/helpers/misc.ts +++ b/subgraphs/v3-vault/src/helpers/misc.ts @@ -1,5 +1,27 @@ import { BigDecimal, BigInt } from "@graphprotocol/graph-ts"; +export function hexToBigInt(hex: string): BigInt { + let hexUpper = hex.toUpperCase(); + let bigInt = BigInt.fromI32(0); + let power = BigInt.fromI32(1); + + for (let i = hex.length - 1; i >= 0; i--) { + let char = hexUpper.charCodeAt(i); + let value = 0; + + if (char >= 48 && char <= 57) { + value = char - 48; + } else if (char >= 65 && char <= 70) { + value = char - 55; + } + + bigInt = bigInt.plus(BigInt.fromI32(value).times(power)); + power = power.times(BigInt.fromI32(16)); + } + + return bigInt; +} + export function tokenToDecimal(amount: BigInt, decimals: i32): BigDecimal { let scale = BigInt.fromI32(10) .pow(decimals as u8) diff --git a/subgraphs/v3-vault/src/mappings/vault.ts b/subgraphs/v3-vault/src/mappings/vault.ts index 05e2d69..6ad39b3 100644 --- a/subgraphs/v3-vault/src/mappings/vault.ts +++ b/subgraphs/v3-vault/src/mappings/vault.ts @@ -2,9 +2,10 @@ import { Address, BigDecimal, BigInt, log } from "@graphprotocol/graph-ts"; import { BufferSharesBurned, BufferSharesMinted, + LiquidityAdded, LiquidityAddedToBuffer, + LiquidityRemoved, LiquidityRemovedFromBuffer, - PoolBalanceChanged, PoolRegistered, Swap as SwapEvent, Unwrap, @@ -31,7 +32,7 @@ import { updateProtocolFeeAmounts, } from "../helpers/entities"; import { ZERO_ADDRESS, ZERO_BD, ZERO_BI } from "../helpers/constants"; -import { scaleDown } from "../helpers/misc"; +import { hexToBigInt, scaleDown } from "../helpers/misc"; import { BPT } from "../types/templates"; import { ERC20 } from "../types/Vault/ERC20"; import { VaultExtension } from "../types/Vault/VaultExtension"; @@ -50,7 +51,6 @@ export function handlePoolRegistered(event: PoolRegistered): void { pool.address = poolAddress; pool.factory = event.params.factory; pool.pauseWindowEndTime = event.params.pauseWindowEndTime; - pool.pauseManager = event.params.roleAccounts.pauseManager; pool.totalShares = ZERO_BD; pool.isInitialized = false; pool.swapsCount = ZERO_BI; @@ -60,6 +60,10 @@ export function handlePoolRegistered(event: PoolRegistered): void { pool.poolCreatorSwapFee = ZERO_BD; pool.poolCreatorYieldFee = ZERO_BD; + pool.swapFeeManager = event.params.roleAccounts.swapFeeManager; + pool.pauseManager = event.params.roleAccounts.pauseManager; + pool.poolCreator = event.params.roleAccounts.poolCreator; + let poolContract = ERC20.bind(poolAddress); let symbolCall = poolContract.try_symbol(); let nameCall = poolContract.try_name(); @@ -139,32 +143,12 @@ export function handlePoolRegistered(event: PoolRegistered): void { } /************************************ - ****** DEPOSITS & WITHDRAWALS ****** + ********** ADDS & REMOVES ********** ************************************/ -export function handlePoolBalanceChanged(event: PoolBalanceChanged): void { - let amounts: BigInt[] = event.params.deltas; - - if (amounts.length === 0) { - return; - } - - createUser(event.params.liquidityProvider); - - let total: BigInt = amounts.reduce( - (sum, amount) => sum.plus(amount), - ZERO_BI - ); - if (total.gt(ZERO_BI)) { - handlePoolJoined(event); - } else { - handlePoolExited(event); - } -} - -function handlePoolJoined(event: PoolBalanceChanged): void { +export function handleLiquidityAdded(event: LiquidityAdded): void { let poolAddress = event.params.pool; - let amounts: BigInt[] = event.params.deltas; + let amounts: BigInt[] = event.params.amountsAddedRaw; let transactionHash = event.transaction.hash; let logIndex = event.logIndex; @@ -189,7 +173,10 @@ function handlePoolJoined(event: PoolBalanceChanged): void { for (let i: i32 = 0; i < poolTokens.length; i++) { let poolToken = poolTokens[i]; - let joinAmount = scaleDown(event.params.deltas[i], poolToken.decimals); + let joinAmount = scaleDown( + event.params.amountsAddedRaw[i], + poolToken.decimals + ); joinAmounts[i] = joinAmount; poolToken.balance = poolToken.balance.plus(joinAmount); poolToken.save(); @@ -209,9 +196,9 @@ function handlePoolJoined(event: PoolBalanceChanged): void { createPoolSnapshot(pool, event.block.timestamp.toI32()); } -function handlePoolExited(event: PoolBalanceChanged): void { +export function handleLiquidityRemoved(event: LiquidityRemoved): void { let poolAddress = event.params.pool; - let amounts: BigInt[] = event.params.deltas; + let amounts: BigInt[] = event.params.amountsRemovedRaw; let transactionHash = event.transaction.hash; let logIndex = event.logIndex; @@ -234,7 +221,7 @@ function handlePoolExited(event: PoolBalanceChanged): void { for (let i: i32 = 0; i < poolTokens.length; i++) { let poolToken = poolTokens[i]; let exitAmount = scaleDown( - event.params.deltas[i].neg(), + event.params.amountsRemovedRaw[i], poolToken.decimals ); exitAmounts[i] = exitAmount; @@ -460,43 +447,60 @@ export function handleBufferSharesBurned(event: BufferSharesBurned): void { bufferShare.save(); } +// For Wrap/Unwrap events, bufferBalances is encoded into bytes as follows: +// [ 16 bytes | 16 bytes ] +// [ wrappedBalance | underlyingBalance ] +// [MSB LSB] export function handleUnwrap(event: Unwrap): void { let buffer = getBuffer(event.params.wrappedToken); let wrappedToken = getToken(changetype
(buffer.wrappedToken)); let underlyingToken = getToken(changetype
(buffer.underlyingToken)); - let burnedShares = scaleDown( - event.params.burnedShares, + // Convert to hex and remove the 0x prefix + const bufferBalances = event.params.bufferBalances.toHex().slice(2); + + // Each byte represents 2 hex digits + // Thus each balance is represented by 32 chars + let wrappedBalance = bufferBalances.slice(0, 32); + let underlyingBalance = bufferBalances.slice(32, 64); + + buffer.underlyingBalance = scaleDown( + hexToBigInt(underlyingBalance), wrappedToken.decimals ); - let withdrawnUnderlying = scaleDown( - event.params.withdrawnUnderlying, + buffer.wrappedBalance = scaleDown( + hexToBigInt(wrappedBalance), underlyingToken.decimals ); - - buffer.underlyingBalance = - buffer.underlyingBalance.minus(withdrawnUnderlying); - buffer.wrappedBalance = buffer.wrappedBalance.plus(burnedShares); buffer.save(); } +// For Wrap/Unwrap events, bufferBalances is encoded into bytes as follows: +// [ 16 bytes | 16 bytes ] +// [ wrappedBalance | underlyingBalance ] +// [MSB LSB] export function handleWrap(event: Wrap): void { let buffer = getBuffer(event.params.wrappedToken); let wrappedToken = getToken(changetype
(buffer.wrappedToken)); let underlyingToken = getToken(changetype
(buffer.underlyingToken)); - let mintedShares = scaleDown( - event.params.mintedShares, + // Convert to hex and remove the 0x prefix + const bufferBalances = event.params.bufferBalances.toHex().slice(2); + + // Each byte represents 2 hex digits + // Thus each balance is represented by 32 chars + let wrappedBalance = bufferBalances.slice(0, 32); + let underlyingBalance = bufferBalances.slice(32, 64); + + buffer.underlyingBalance = scaleDown( + hexToBigInt(underlyingBalance), wrappedToken.decimals ); - let depositedUnderlying = scaleDown( - event.params.depositedUnderlying, + buffer.wrappedBalance = scaleDown( + hexToBigInt(wrappedBalance), underlyingToken.decimals ); - - buffer.underlyingBalance = buffer.underlyingBalance.plus(depositedUnderlying); - buffer.wrappedBalance = buffer.wrappedBalance.minus(mintedShares); buffer.save(); } diff --git a/subgraphs/v3-vault/subgraph.gnosis.yaml b/subgraphs/v3-vault/subgraph.gnosis.yaml new file mode 100644 index 0000000..721a271 --- /dev/null +++ b/subgraphs/v3-vault/subgraph.gnosis.yaml @@ -0,0 +1,66 @@ +specVersion: 1.0.0 +schema: + file: ./schema.graphql +dataSources: + - kind: ethereum + name: Vault + network: sepolia + source: + abi: Vault + address: "0xbA1333333333a1BA1108E8412f11850A5C319bA9" + startBlock: 37360338 + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - Vault + - Pool + abis: + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC4626 + file: ./abis/ERC4626.json + - name: Vault + file: ./abis/Vault.json + - name: VaultExtension + file: ./abis/VaultExtension.json + - name: ProtocolFeeController + file: ./abis/ProtocolFeeController.json + eventHandlers: + - event: PoolRegistered(indexed address,indexed address,(address,uint8,address,bool)[],uint256,uint32,(address,address,address),(bool,bool,bool,bool,bool,bool,bool,bool,bool,bool,address),(bool,bool,bool,bool)) + handler: handlePoolRegistered + - event: LiquidityAdded(indexed address,indexed address,indexed uint8,uint256,uint256[],uint256[]) + handler: handleLiquidityAdded + - event: LiquidityRemoved(indexed address,indexed address,indexed uint8,uint256,uint256[],uint256[]) + handler: handleLiquidityRemoved + - event: Swap(indexed address,indexed address,indexed address,uint256,uint256,uint256,uint256) + handler: handleSwap + - event: LiquidityAddedToBuffer(indexed address,uint256,uint256,bytes32) + handler: handleLiquidityAddedToBuffer + - event: LiquidityRemovedFromBuffer(indexed address,uint256,uint256,bytes32) + handler: handleLiquidityRemovedFromBuffer + - event: Wrap(indexed address,uint256,uint256,bytes32) + handler: handleWrap + - event: Unwrap(indexed address,uint256,uint256,bytes32) + handler: handleUnwrap + file: ./src/mappings/vault.ts +templates: + - kind: ethereum/contract + name: BPT + network: sepolia + source: + abi: BPT + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - PoolShare + abis: + - name: BPT + file: ./abis/ERC20.json + eventHandlers: + - event: Transfer(indexed address,indexed address,uint256) + handler: handleTransfer + file: ./src/mappings/bpt.ts diff --git a/subgraphs/v3-vault/template.yaml b/subgraphs/v3-vault/subgraph.sepolia.yaml similarity index 72% rename from subgraphs/v3-vault/template.yaml rename to subgraphs/v3-vault/subgraph.sepolia.yaml index e51b0c2..4545380 100644 --- a/subgraphs/v3-vault/template.yaml +++ b/subgraphs/v3-vault/subgraph.sepolia.yaml @@ -4,11 +4,11 @@ schema: dataSources: - kind: ethereum name: Vault - network: {{ network }} + network: sepolia source: abi: Vault - address: "{{ Vault.address }}" - startBlock: {{ Vault.startBlock }} + address: "0xbA1333333333a1BA1108E8412f11850A5C319bA9" + startBlock: 7212247 mapping: kind: ethereum/events apiVersion: 0.0.7 @@ -30,19 +30,25 @@ dataSources: eventHandlers: - event: PoolRegistered(indexed address,indexed address,(address,uint8,address,bool)[],uint256,uint32,(address,address,address),(bool,bool,bool,bool,bool,bool,bool,bool,bool,bool,address),(bool,bool,bool,bool)) handler: handlePoolRegistered - - event: PoolBalanceChanged(indexed address,indexed address,uint256,int256[],uint256[]) - handler: handlePoolBalanceChanged + - event: LiquidityAdded(indexed address,indexed address,indexed uint8,uint256,uint256[],uint256[]) + handler: handleLiquidityAdded + - event: LiquidityRemoved(indexed address,indexed address,indexed uint8,uint256,uint256[],uint256[]) + handler: handleLiquidityRemoved - event: Swap(indexed address,indexed address,indexed address,uint256,uint256,uint256,uint256) handler: handleSwap - - event: LiquidityAddedToBuffer(indexed address,uint256,uint256) + - event: LiquidityAddedToBuffer(indexed address,uint256,uint256,bytes32) handler: handleLiquidityAddedToBuffer - - event: LiquidityRemovedFromBuffer(indexed address,uint256,uint256) + - event: LiquidityRemovedFromBuffer(indexed address,uint256,uint256,bytes32) handler: handleLiquidityRemovedFromBuffer + - event: Wrap(indexed address,uint256,uint256,bytes32) + handler: handleWrap + - event: Unwrap(indexed address,uint256,uint256,bytes32) + handler: handleUnwrap file: ./src/mappings/vault.ts templates: - kind: ethereum/contract name: BPT - network: {{ network }} + network: sepolia source: abi: BPT mapping: diff --git a/subgraphs/v3-vault/subgraph.yaml b/subgraphs/v3-vault/subgraph.yaml new file mode 100644 index 0000000..b488fc3 --- /dev/null +++ b/subgraphs/v3-vault/subgraph.yaml @@ -0,0 +1,66 @@ +specVersion: 1.0.0 +schema: + file: ./schema.graphql +dataSources: + - kind: ethereum + name: Vault + network: sepolia + source: + abi: Vault + address: "0xbA1333333333a1BA1108E8412f11850A5C319bA9" + startBlock: 21332121 + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - Vault + - Pool + abis: + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC4626 + file: ./abis/ERC4626.json + - name: Vault + file: ./abis/Vault.json + - name: VaultExtension + file: ./abis/VaultExtension.json + - name: ProtocolFeeController + file: ./abis/ProtocolFeeController.json + eventHandlers: + - event: PoolRegistered(indexed address,indexed address,(address,uint8,address,bool)[],uint256,uint32,(address,address,address),(bool,bool,bool,bool,bool,bool,bool,bool,bool,bool,address),(bool,bool,bool,bool)) + handler: handlePoolRegistered + - event: LiquidityAdded(indexed address,indexed address,indexed uint8,uint256,uint256[],uint256[]) + handler: handleLiquidityAdded + - event: LiquidityRemoved(indexed address,indexed address,indexed uint8,uint256,uint256[],uint256[]) + handler: handleLiquidityRemoved + - event: Swap(indexed address,indexed address,indexed address,uint256,uint256,uint256,uint256) + handler: handleSwap + - event: LiquidityAddedToBuffer(indexed address,uint256,uint256,bytes32) + handler: handleLiquidityAddedToBuffer + - event: LiquidityRemovedFromBuffer(indexed address,uint256,uint256,bytes32) + handler: handleLiquidityRemovedFromBuffer + - event: Wrap(indexed address,uint256,uint256,bytes32) + handler: handleWrap + - event: Unwrap(indexed address,uint256,uint256,bytes32) + handler: handleUnwrap + file: ./src/mappings/vault.ts +templates: + - kind: ethereum/contract + name: BPT + network: sepolia + source: + abi: BPT + mapping: + kind: ethereum/events + apiVersion: 0.0.7 + language: wasm/assemblyscript + entities: + - PoolShare + abis: + - name: BPT + file: ./abis/ERC20.json + eventHandlers: + - event: Transfer(indexed address,indexed address,uint256) + handler: handleTransfer + file: ./src/mappings/bpt.ts