Skip to content

Instantly share code, notes, and snippets.

@raddy
Created September 8, 2022 19:48
Show Gist options
  • Save raddy/99f317d22a651826d7e53cc7f4b10597 to your computer and use it in GitHub Desktop.
Save raddy/99f317d22a651826d7e53cc7f4b10597 to your computer and use it in GitHub Desktop.
Example fetching multicall
const { ethers } = require("ethers");
const { program } = require("commander");
const { formatUnits } = require("@ethersproject/units");
const quoterABI = require("./quoter.json");
const erc20Abi = require("./erc20.json");
const config = require('config');
const {Multicall, ContractCallContext} = require("ethereum-multicall");
const quoterAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6'
function get_context(from_token, to_token, pool_fee, qty, ref)
{
const input_decimals = 18;
const in_qty = ethers.BigNumber.from("10").pow(input_decimals).mul(qty);
const params = [
from_token,
to_token,
pool_fee,
qty,
0
];
const context = {
reference: ref,
contractAddress: quoterAddress,
abi: quoterABI,
calls: [{ reference: 'quote', methodName: 'quoteExactInputSingle', methodParameters: params }]
};
return context;
}
const fetch_multi = async (
from_token,
to_token,
qty,
rpcUrl
) => {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const multicall = new Multicall({ ethersProvider: provider, tryAggregate: true });
const contexts = [
get_context(from_token, to_token, 500, qty, `quote_${qty}`),
get_context(from_token, to_token, 500, qty * 2, `quote_${qty * 2}`),
get_context(from_token, to_token, 500, qty * 3, `quote_${qty * 3}`),
get_context(from_token, to_token, 500, qty * 5, `quote_${qty * 5}`),
get_context(from_token, to_token, 500, qty * 10, `quote_${qty* 10}`)
];
const results = await multicall.call(contexts,{
});
console.log(results);
}
const get_quote = async (
from_token,
to_token,
qty,
rpcUrl,
block
) => {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const quoter = new ethers.Contract(quoterAddress, quoterABI, provider);
const dest_token = new ethers.Contract(to_token, erc20Abi, provider);
const dest_decimals = 18;// await dest_token.decimals();
const over = block ? {blockTag : parseInt(block)} : {};
const quotedAmountOut = await quoter.callStatic.quoteExactInputSingle(
from_token,
to_token,
500,
qty,
0, over
)
const formattedAmountOut =formatUnits(quotedAmountOut, dest_decimals);
console.log(formattedAmountOut);
}
const main = async () => {
program
.argument("<from_token>", "Source Token Address")
.argument("<to_token>", "Destination Token Address")
.argument("<quantity>", "Source Token Quantity")
.argument("<chain>", "Chain Name")
.option("--block <blocknum>", "Block Number")
.action(async (from_token, to_token, quantity, chain, options) => {
const rpcUrl = config.get(`rpc.${chain}`);
/*await get_quote(
from_token,
to_token,
quantity,
rpcUrl,
options.block
);*/
await fetch_multi(
from_token,
to_token,
parseInt(quantity),
rpcUrl
);
});
program.parseAsync();
};
main().catch((e) => console.error(e.toString()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment