Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve pool snapshots #600

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions networks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,15 @@ polygon:
network: matic
graft:
# FXPoolDeployerTracker start block
block: 54559455
# block: 54559455
# this will be the base of the unpruned deployment, so make sure it has not been pruned
# it should be possible to run time travel queries on it going back to the vault's startBlock
base: QmUqS6BAVQgvstEsVrxuwsu1DwQdfAdj3Q6gz2j3DbUYQ9
# base: QmUqS6BAVQgvstEsVrxuwsu1DwQdfAdj3Q6gz2j3DbUYQ9
graft_pruned:
# FXPoolDeployerTracker start block
block: 54559455
# block: 54559455
# this will be the base of the pruned deployment, so it is ok if it is a pruned subgraph
base: QmUqS6BAVQgvstEsVrxuwsu1DwQdfAdj3Q6gz2j3DbUYQ9
# base: QmUqS6BAVQgvstEsVrxuwsu1DwQdfAdj3Q6gz2j3DbUYQ9
EventEmitter:
address: "0xcdcECFa416EE3022030E707dC3EA13a8997D74c8"
startBlock: 38152461
Expand Down
22 changes: 22 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ type PoolToken @entity {
oldPriceRate: BigDecimal # TODO: make mandatory at next full sync
priceRate: BigDecimal!
balance: BigDecimal!
volume: BigDecimal!
swapFees: BigDecimal!
paidProtocolFees: BigDecimal # TODO: make mandatory at next full sync
cashBalance: BigDecimal!
managedBalance: BigDecimal!
Expand Down Expand Up @@ -333,6 +335,26 @@ type PoolSnapshot @entity {
totalShares: BigDecimal!
swapVolume: BigDecimal!
protocolFee: BigDecimal # TODO: make mandatory at next full sync
totalSwapFees: [BigDecimal!]!
totalSwapVolumes: [BigDecimal!]!
totalProtocolFees: [BigDecimal!]!
swapFees: BigDecimal!
liquidity: BigDecimal!
swapsCount: BigInt!
holdersCount: BigInt!
timestamp: Int!
}

type PoolSnapshotHourly @entity {
id: ID!
pool: Pool!
amounts: [BigDecimal!]!
totalShares: BigDecimal!
swapVolume: BigDecimal!
protocolFee: BigDecimal # TODO: make mandatory at next full sync
totalSwapFees: [BigDecimal!]!
totalSwapVolumes: [BigDecimal!]!
totalProtocolFees: [BigDecimal!]!
swapFees: BigDecimal!
liquidity: BigDecimal!
swapsCount: BigInt!
Expand Down
3 changes: 3 additions & 0 deletions src/mappings/helpers/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export const assets: Assets = {
Address.fromString('0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0'), // MATIC
Address.fromString('0xA13a9247ea42D743238089903570127DdA72fE44'), // bb-a-USD
Address.fromString('0x60D604890feaa0b5460B28A424407c24fe89374a'), // bb-a-WETH-V3
Address.fromString('0x865377367054516e17014CcdED1e7d814EDC9ce4'), // DOLA
Address.fromString('0xae78736Cd615f374D3085123A210448E74Fc6393'), // rETH
Address.fromString('0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee'), // weETH
],
fxAssetAggregators: [
[
Expand Down
67 changes: 67 additions & 0 deletions src/mappings/helpers/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BalancerSnapshot,
Balancer,
FXOracle,
PoolSnapshotHourly,
} from '../../types/schema';
import { ERC20 } from '../../types/Vault/ERC20';
import { WeightedPool } from '../../types/Vault/WeightedPool';
Expand Down Expand Up @@ -248,13 +249,24 @@ export function createPoolSnapshot(pool: Pool, timestamp: i32): void {

let tokens = pool.tokensList;
let amounts = new Array<BigDecimal>(tokens.length);
let protocolFees = new Array<BigDecimal>(tokens.length);
let volumes = new Array<BigDecimal>(tokens.length);
let swapFees = new Array<BigDecimal>(tokens.length);
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
let tokenAddress = Address.fromString(token.toHexString());
let poolToken = loadPoolToken(poolId, tokenAddress);
if (poolToken == null) continue;

volumes[i] = poolToken.volume;
amounts[i] = poolToken.balance;
swapFees[i] = poolToken.swapFees;

if (poolToken.address == pool.address.toHex()) {
protocolFees[i] = pool.totalProtocolFeePaidInBPT ? pool.totalProtocolFeePaidInBPT : ZERO_BD;
} else {
protocolFees[i] = poolToken.paidProtocolFees ? poolToken.paidProtocolFees : ZERO_BD;
}
}

snapshot.pool = poolId;
Expand All @@ -264,12 +276,67 @@ export function createPoolSnapshot(pool: Pool, timestamp: i32): void {
snapshot.swapFees = pool.totalSwapFee;
snapshot.liquidity = pool.totalLiquidity;
snapshot.protocolFee = pool.totalProtocolFee;
snapshot.totalProtocolFees = protocolFees;
snapshot.totalSwapVolumes = volumes;
snapshot.totalSwapFees = swapFees;
snapshot.swapsCount = pool.swapsCount;
snapshot.holdersCount = pool.holdersCount;
snapshot.timestamp = dayTimestamp;
snapshot.save();
}

export function createPoolSnapshotHourly(pool: Pool, timestamp: i32): void {
const hourIndex = timestamp / 3600; // get unique hour within unix history
const hourStartUnix = hourIndex * 3600; // want the rounded effect

let poolId = pool.id;
if (pool == null || !pool.tokensList) return;

let snapshotId = poolId + '-' + hourIndex.toString();
let snapshot = PoolSnapshotHourly.load(snapshotId);

if (!snapshot) {
snapshot = new PoolSnapshotHourly(snapshotId);
}

let tokens = pool.tokensList;
let amounts = new Array<BigDecimal>(tokens.length);
let protocolFees = new Array<BigDecimal>(tokens.length);
let volumes = new Array<BigDecimal>(tokens.length);
let swapFees = new Array<BigDecimal>(tokens.length);
for (let i = 0; i < tokens.length; i++) {
let token = tokens[i];
let tokenAddress = Address.fromString(token.toHexString());
let poolToken = loadPoolToken(poolId, tokenAddress);
if (poolToken == null) continue;

volumes[i] = poolToken.volume;
amounts[i] = poolToken.balance;
swapFees[i] = poolToken.swapFees;

if (poolToken.address == pool.address.toHex()) {
protocolFees[i] = pool.totalProtocolFeePaidInBPT ? pool.totalProtocolFeePaidInBPT : ZERO_BD;
} else {
protocolFees[i] = poolToken.paidProtocolFees ? poolToken.paidProtocolFees : ZERO_BD;
}
}

snapshot.pool = poolId;
snapshot.amounts = amounts;
snapshot.totalShares = pool.totalShares;
snapshot.swapVolume = pool.totalSwapVolume;
snapshot.swapFees = pool.totalSwapFee;
snapshot.liquidity = pool.totalLiquidity;
snapshot.protocolFee = pool.totalProtocolFee;
snapshot.totalProtocolFees = protocolFees;
snapshot.totalSwapVolumes = volumes;
snapshot.totalSwapFees = swapFees;
snapshot.swapsCount = pool.swapsCount;
snapshot.holdersCount = pool.holdersCount;
snapshot.timestamp = hourStartUnix;
snapshot.save();
}

export function createUserEntity(address: Address): void {
let addressHex = address.toHex();
if (User.load(addressHex) == null) {
Expand Down
2 changes: 2 additions & 0 deletions src/mappings/poolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
createPoolSnapshot,
hexToBigInt,
getBalancerSnapshot,
createPoolSnapshotHourly,
} from './helpers/misc';
import { ONE_BD, ProtocolFeeType, VAULT_ADDRESS, ZERO_ADDRESS, ZERO_BD } from './helpers/constants';
import { updateAmpFactor } from './helpers/stable';
Expand Down Expand Up @@ -657,6 +658,7 @@ export function handleTransfer(event: Transfer): void {

// create or update pool's snapshot
createPoolSnapshot(pool, event.block.timestamp.toI32());
createPoolSnapshotHourly(pool, event.block.timestamp.toI32());

let vault = Balancer.load('2') as Balancer;
let vaultProtocolFee = vault.totalProtocolFee ? vault.totalProtocolFee : ZERO_BD;
Expand Down
4 changes: 3 additions & 1 deletion src/mappings/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { hasVirtualSupply, isComposableStablePool, isLinearPool, isFXPool, PoolT
import {
bytesToAddress,
createPoolSnapshot,
createPoolSnapshotHourly,
getBalancerSnapshot,
getToken,
getTokenPriceId,
Expand Down Expand Up @@ -179,8 +180,9 @@ export function updatePoolLiquidity(poolId: string, block_number: BigInt, timest
updateBptPrice(pool);
}

// Create or update pool daily snapshot
// Create or update pool snapshots
createPoolSnapshot(pool, timestamp.toI32());
createPoolSnapshotHourly(pool, timestamp.toI32());

// Update global stats
let vault = Balancer.load('2') as Balancer;
Expand Down
4 changes: 4 additions & 0 deletions src/mappings/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,10 +558,14 @@ export function handleSwapEvent(event: SwapEvent): void {

let newInAmount = poolTokenIn.balance.plus(tokenAmountIn);
poolTokenIn.balance = newInAmount;
poolTokenIn.volume = poolTokenIn.volume.plus(tokenAmountIn);
poolTokenIn.swapFees = poolTokenIn.swapFees.plus(tokenAmountIn.times(pool.swapFee));
poolTokenIn.save();

let newOutAmount = poolTokenOut.balance.minus(tokenAmountOut);
poolTokenOut.balance = newOutAmount;
poolTokenOut.volume = poolTokenOut.volume.plus(tokenAmountOut);
poolTokenOut.swapFees = poolTokenOut.swapFees.plus(tokenAmountOut.times(pool.swapFee));
poolTokenOut.save();

let swapId = transactionHash.toHexString().concat(logIndex.toString());
Expand Down
Loading