Skip to content

Instantly share code, notes, and snippets.

@johngrantuk
Created June 26, 2025 11:55
Show Gist options
  • Select an option

  • Save johngrantuk/818ea7b16ca4483573599b8171fcb367 to your computer and use it in GitHub Desktop.

Select an option

Save johngrantuk/818ea7b16ca4483573599b8171fcb367 to your computer and use it in GitHub Desktop.
Shows how to fetch list of pool addresses from API, filter out specific address then query API for SOR paths.
/**
*
* Run with:
* pnpm example ./examples/swaps/zeKrakenExample.ts
*/
import { Address } from 'viem';
import {
BalancerApi,
API_ENDPOINT,
ChainId,
SwapKind,
Token,
TokenAmount,
} from '../../src';
// GraphQL query to fetch pool addresses
const POOL_ADDRESSES_QUERY = `
query MyQuery {
poolGetPools(
orderBy: totalLiquidity
where: {chainIn: MAINNET, protocolVersionIn: 3}
first: 10000
) {
address
}
}
`;
// Function to fetch pool addresses using API
export const fetchPoolAddresses = async (): Promise<string[]> => {
try {
const response = await fetch('https://test-api-v3.balancer.fi/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: POOL_ADDRESSES_QUERY,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.errors) {
throw new Error(`GraphQL errors: ${JSON.stringify(data.errors)}`);
}
const poolAddresses = data.data.poolGetPools.map(
(pool: { address: string }) => pool.address,
);
return poolAddresses;
} catch (error) {
console.error('Error fetching pool addresses:', error);
throw error;
}
};
export const zeKrakenExample = async () => {
const chainId = ChainId.MAINNET;
const swapKind = SwapKind.GivenIn;
const tokenIn = new Token(
chainId,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
18,
'WETH',
);
const tokenOut = new Token(
chainId,
'0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0',
18,
'wstETH',
);
const swapAmount = TokenAmount.fromHumanAmount(tokenIn, '100000');
const poolAddresses = await fetchPoolAddresses();
const poolAddressesToExclude = [
'0x6b31a94029fd7840d780191b6d63fa0d269bd883',
];
const filteredPoolAddresses = poolAddresses.filter(
(address) => !poolAddressesToExclude.includes(address),
);
// API is used to fetch best path from available liquidity
const balancerApi = new BalancerApi(API_ENDPOINT, chainId);
const sorPathsAllPools = await balancerApi.sorSwapPaths.fetchSorSwapPaths({
chainId,
tokenIn: tokenIn.address,
tokenOut: tokenOut.address,
swapKind,
swapAmount,
useProtocolVersion: 3,
});
console.log('SOR Paths With All Pools:');
console.log(sorPathsAllPools);
const sorPathsExcluding = await balancerApi.sorSwapPaths.fetchSorSwapPaths({
chainId,
tokenIn: tokenIn.address,
tokenOut: tokenOut.address,
swapKind,
swapAmount,
poolIds: filteredPoolAddresses as Address[],
useProtocolVersion: 3,
});
console.log('\nSOR Paths Excluding:');
console.log(sorPathsExcluding);
};
export default zeKrakenExample;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment