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

Feat/precompute nliq #125

Merged
merged 17 commits into from
Feb 12, 2024
309 changes: 309 additions & 0 deletions modules/pool/abi/BalancerQueries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
[
{
"inputs": [
{
"internalType": "contract IVault",
"name": "_vault",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "enum IVault.SwapKind",
"name": "kind",
"type": "uint8"
},
{
"components": [
{
"internalType": "bytes32",
"name": "poolId",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "assetInIndex",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "assetOutIndex",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "userData",
"type": "bytes"
}
],
"internalType": "struct IVault.BatchSwapStep[]",
"name": "swaps",
"type": "tuple[]"
},
{
"internalType": "contract IAsset[]",
"name": "assets",
"type": "address[]"
},
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "bool",
"name": "fromInternalBalance",
"type": "bool"
},
{
"internalType": "address payable",
"name": "recipient",
"type": "address"
},
{
"internalType": "bool",
"name": "toInternalBalance",
"type": "bool"
}
],
"internalType": "struct IVault.FundManagement",
"name": "funds",
"type": "tuple"
}
],
"name": "queryBatchSwap",
"outputs": [
{
"internalType": "int256[]",
"name": "assetDeltas",
"type": "int256[]"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "poolId",
"type": "bytes32"
},
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"components": [
{
"internalType": "contract IAsset[]",
"name": "assets",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "minAmountsOut",
"type": "uint256[]"
},
{
"internalType": "bytes",
"name": "userData",
"type": "bytes"
},
{
"internalType": "bool",
"name": "toInternalBalance",
"type": "bool"
}
],
"internalType": "struct IVault.ExitPoolRequest",
"name": "request",
"type": "tuple"
}
],
"name": "queryExit",
"outputs": [
{
"internalType": "uint256",
"name": "bptIn",
"type": "uint256"
},
{
"internalType": "uint256[]",
"name": "amountsOut",
"type": "uint256[]"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "bytes32",
"name": "poolId",
"type": "bytes32"
},
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"components": [
{
"internalType": "contract IAsset[]",
"name": "assets",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "maxAmountsIn",
"type": "uint256[]"
},
{
"internalType": "bytes",
"name": "userData",
"type": "bytes"
},
{
"internalType": "bool",
"name": "fromInternalBalance",
"type": "bool"
}
],
"internalType": "struct IVault.JoinPoolRequest",
"name": "request",
"type": "tuple"
}
],
"name": "queryJoin",
"outputs": [
{
"internalType": "uint256",
"name": "bptOut",
"type": "uint256"
},
{
"internalType": "uint256[]",
"name": "amountsIn",
"type": "uint256[]"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"components": [
{
"internalType": "bytes32",
"name": "poolId",
"type": "bytes32"
},
{
"internalType": "enum IVault.SwapKind",
"name": "kind",
"type": "uint8"
},
{
"internalType": "contract IAsset",
"name": "assetIn",
"type": "address"
},
{
"internalType": "contract IAsset",
"name": "assetOut",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "userData",
"type": "bytes"
}
],
"internalType": "struct IVault.SingleSwap",
"name": "singleSwap",
"type": "tuple"
},
{
"components": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "bool",
"name": "fromInternalBalance",
"type": "bool"
},
{
"internalType": "address payable",
"name": "recipient",
"type": "address"
},
{
"internalType": "bool",
"name": "toInternalBalance",
"type": "bool"
}
],
"internalType": "struct IVault.FundManagement",
"name": "funds",
"type": "tuple"
}
],
"name": "querySwap",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "vault",
"outputs": [
{
"internalType": "contract IVault",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]
3 changes: 1 addition & 2 deletions modules/pool/lib/pool-gql-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ import { networkContext } from '../../network/network-context.service';
import { fixedNumber } from '../../view-helpers/fixed-number';
import { parseUnits } from 'ethers/lib/utils';
import { formatFixed } from '@ethersproject/bignumber';
import { BalancerChainIds, BeethovenChainIds, chainIdToChain, chainToIdMap } from '../../network/network-config';
import { BeethovenChainIds, chainToIdMap } from '../../network/network-config';
import { GithubContentService } from '../../content/github-content.service';
import { SanityContentService } from '../../content/sanity-content.service';
import { FeaturedPool } from '../../content/content-types';
import { ElementData, FxData, GyroData, LinearData, StableData } from '../subgraph-mapper';

export class PoolGqlLoaderService {
Expand Down
22 changes: 21 additions & 1 deletion modules/pool/lib/pool-on-chain-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { formatFixed } from '@ethersproject/bignumber';
import { PrismaPoolType } from '@prisma/client';
import { Prisma, PrismaPoolType } from '@prisma/client';
import { isSameAddress } from '@balancer-labs/sdk';
import { prisma } from '../../../prisma/prisma-client';
import { isStablePool } from './pool-utils';
Expand All @@ -10,6 +10,7 @@ import { fetchOnChainPoolData } from './pool-onchain-data';
import { fetchOnChainGyroFees } from './pool-onchain-gyro-fee';
import { networkContext } from '../../network/network-context.service';
import { LinearData, StableData } from '../subgraph-mapper';
import { fetchTokenPairData } from './pool-on-chain-tokenpair-data';

const SUPPORTED_POOL_TYPES: PrismaPoolType[] = [
'WEIGHTED',
Expand All @@ -33,6 +34,7 @@ export class PoolOnChainDataService {
return {
chain: networkContext.chain,
vaultAddress: networkContext.data.balancer.v2.vaultAddress,
balancerQueriesAddress: networkContext.data.balancer.v2.balancerQueriesAddress,
yieldProtocolFeePercentage: networkContext.data.balancer.v2.defaultSwapFeePercentage,
swapProtocolFeePercentage: networkContext.data.balancer.v2.defaultSwapFeePercentage,
gyroConfig: networkContext.data.gyro?.config,
Expand Down Expand Up @@ -101,13 +103,19 @@ export class PoolOnChainDataService {
this.options.vaultAddress,
networkContext.chain === 'ZKEVM' ? 190 : 1024,
);
const tokenPairData = await fetchTokenPairData(
filteredPools,
this.options.balancerQueriesAddress,
networkContext.chain === 'ZKEVM' ? 190 : 1024,
);
const gyroFees = await (this.options.gyroConfig
? fetchOnChainGyroFees(gyroPools, this.options.gyroConfig, networkContext.chain === 'ZKEVM' ? 190 : 1024)
: Promise.resolve({} as { [address: string]: string }));

const operations = [];
for (const pool of filteredPools) {
const onchainData = onchainResults[pool.id];
const { tokenPairs } = tokenPairData[pool.id];
const { amp, poolTokens } = onchainData;

try {
Expand Down Expand Up @@ -201,6 +209,18 @@ export class PoolOnChainDataService {
);
}

// always update tokenPair data
if (pool.dynamicData) {
operations.push(
prisma.prismaPoolDynamicData.update({
where: { id_chain: { id: pool.id, chain: this.options.chain } },
data: {
tokenPairsData: tokenPairs,
},
}),
);
}

for (let i = 0; i < poolTokens.tokens.length; i++) {
const tokenAddress = poolTokens.tokens[i];
const poolToken = pool.tokens.find((token) => isSameAddress(token.address, tokenAddress));
Expand Down
Loading
Loading