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

fix: add liquidity example #373

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
13 changes: 8 additions & 5 deletions examples/addLiquidity/addLiquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ async function runAgainstFork() {
const userAccount = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045';
// 80BAL-20WETH
const pool = {
id: '0xB2456A6f51530053bC41b0EE700fe6A2C37282e8',
address: '0xB2456A6f51530053bC41b0EE700fe6A2C37282e8' as Address,
id: '0x03Bf996C7BD45B3386cb41875761d45e27EaB284',
address: '0x03Bf996C7BD45B3386cb41875761d45e27EaB284' as Address,
};
const amountsIn = [
{
rawAmount: parseEther('0.01'),
rawAmount: parseEther('0.001'),
decimals: 18,
address: '0x7b79995e5f793a07bc00c21412e50ecae098e7f9' as Address,
},
{
rawAmount: parseEther('0.01'),
rawAmount: parseEther('0.001'),
decimals: 18,
address: '0xb19382073c7a0addbb56ac6af1808fa49e377b75' as Address,
},
Expand Down Expand Up @@ -77,7 +77,10 @@ const addLiquidity = async ({
slippage,
}) => {
// API is used to fetch relevant pool data
const balancerApi = new BalancerApi('https://api-v3.balancer.fi/', chainId);
const balancerApi = new BalancerApi(
'https://test-api-v3.balancer.fi/',
chainId,
);
const poolState = await balancerApi.pools.fetchPoolState(poolId);

// Construct the AddLiquidityInput, in this case an AddLiquidityUnbalanced
Expand Down
12 changes: 8 additions & 4 deletions src/data/providers/balancer-api/modules/pool-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { BalancerApiClient } from '../../client';
import { PoolState, PoolStateWithBalances } from '../../../../../entities';
import { mapPoolType } from '../../../../../utils/poolTypeMapper';

import { API_CHAIN_NAMES } from '../../../../../utils/constants';

export class Pools {
readonly poolStateQuery = `
query GetPool($id: String!) {
poolGetPool(id:$id) {
query poolGetPool($id: String!, $chain: GqlChain!) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realise the API had changed to require the chain field. Seems nicely handled 👍

poolGetPool(id: $id, chain:$chain) {
id
address
type
Expand All @@ -19,8 +21,8 @@ export class Pools {
}`;

readonly poolStateWithRawTokensQuery = `
query GetPool($id: String!) {
poolGetPool(id:$id) {
query GetPool($id: String!, $chain: GqlChain!) {
poolGetPool(id:$id, chain:$chain) {
id
address
type
Expand All @@ -44,6 +46,8 @@ export class Pools {
query: this.poolStateQuery,
variables: {
id: id.toLowerCase(),
// the API requires chain names to be sent as uppercase strings
chain: API_CHAIN_NAMES[this.balancerApiClient.chainId],
},
});
const poolGetPool: PoolState = {
Expand Down
16 changes: 16 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ export enum ChainId {
SEPOLIA = 11155111,
}

// The Balancer API requires the chain to be passed as a specific string
export const API_CHAIN_NAMES: Record<number, string> = {
[ChainId.MAINNET]: 'MAINNET',
[ChainId.OPTIMISM]: 'OPTIMISM',
[ChainId.GNOSIS_CHAIN]: 'GNOSIS',
[ChainId.POLYGON]: 'POLYGON',
[ChainId.FANTOM]: 'FANTOM',
[ChainId.FRAXTAL]: 'FRAXTAL',
[ChainId.ZKSYNC]: 'ZKEVM',
[ChainId.BASE]: 'BASE',
[ChainId.MODE]: 'MODE',
[ChainId.ARBITRUM_ONE]: 'ARBITRUM',
[ChainId.AVALANCHE]: 'AVALANCHE',
[ChainId.SEPOLIA]: 'SEPOLIA',
};

export const CHAINS: Record<number, Chain> = {
[ChainId.MAINNET]: mainnet,
[ChainId.GOERLI]: goerli,
Expand Down
Loading