Created
July 21, 2021 00:51
-
-
Save sc0Vu/7fb557020564bb2e54e6ce68d0b56e2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const BigNumber = require('bignumber.js'); | |
import { JsonRpcProvider } from '@ethersproject/providers'; | |
import { | |
SOR as SOR2, | |
SwapTypes, | |
DisabledOptions, | |
SwapOptions, | |
PoolFilter, | |
fetchSubgraphPools | |
} from '@balancer-labs/sor2'; | |
const provider = new JsonRpcProvider( | |
`https://mainnet.infura.io/v3/${process.env.INFURA}` | |
); | |
const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F'; | |
const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; | |
const amountIn = new BigNumber('1'); | |
const tokenIn = USDC; | |
const tokenOut = DAI; | |
const gasPrice = new BigNumber('30000000000'); // You can set gas price to whatever the current price is. | |
const swapCost = new BigNumber('100000'); // A pool swap costs approx 100000 gas | |
// URL for pools data | |
const poolsUrl = `https://storageapi.fleek.co/balancer-bucket/balancer-exchange/pools` | |
async function swapExactIn() { | |
const network = await provider.getNetwork() | |
const chainId = network.chainId | |
const maxPools = 100 | |
const disableOptions: DisabledOptions = { | |
isOverRide: false, | |
disabledTokens: [] | |
} | |
const poolsSourceV2 = `https://storageapi.fleek.co/johngrantuk-team-bucket/poolsV2.json` | |
const subgraphPoolsV2 = `https://api.thegraph.com/subgraphs/name/balancer-labs/balancer-v2` | |
const sor2 = new SOR2( | |
provider, | |
gasPrice, | |
maxPools, | |
chainId, | |
poolsSourceV2, | |
swapCost, | |
disableOptions | |
) | |
const subgraphPools = await fetchSubgraphPools(subgraphPoolsV2) | |
console.log('Fetch pool result: ', await sor2.fetchPools(true, subgraphPools)) | |
const swapOptions: SwapOptions = { | |
poolTypeFilter: PoolFilter.All, | |
timestamp: Math.floor(Date.now() / 1000) | |
} | |
await sor2.setCostOutputToken( | |
tokenOut, | |
6, | |
swapCost | |
) | |
const swapInfo2 = await sor2.getSwaps( | |
tokenIn, | |
tokenOut, | |
SwapTypes.SwapExactIn, | |
amountIn, | |
swapOptions | |
) | |
console.log(swapInfo2) | |
} | |
swapExactIn(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment