Skip to content

Commit

Permalink
update to final v3 vault
Browse files Browse the repository at this point in the history
  • Loading branch information
mendesfabio committed Dec 5, 2024
1 parent 88b0065 commit c14f0cd
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 58 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/.*/
Expand Down
2 changes: 1 addition & 1 deletion subgraphs/v3-vault/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
9 changes: 7 additions & 2 deletions subgraphs/v3-vault/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions subgraphs/v3-vault/src/helpers/misc.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
94 changes: 49 additions & 45 deletions subgraphs/v3-vault/src/mappings/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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<BigInt>(
(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;
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Address>(buffer.wrappedToken));
let underlyingToken = getToken(changetype<Address>(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<Address>(buffer.wrappedToken));
let underlyingToken = getToken(changetype<Address>(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();
}
66 changes: 66 additions & 0 deletions subgraphs/v3-vault/subgraph.gnosis.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
Loading

0 comments on commit c14f0cd

Please sign in to comment.