Swap Quote
Get a token swap quote based on an input or output value.
Below are the two methods to get a quote for a swap from a given route using JSON-RPC or REST-API:
Contract ID: SaucerSwapV1RouterV3
When providing HBAR in the path array, use the wrapped HBAR token ID (WHBAR).
Get Output Quote from Input Amount
⛽ No gas cost
Get the output amounts from a given input amount and a swap route.
Function name: getAmountsOut
Solidity Interface & Function Body
//IUniswapV2Router01.sol
function getAmountsOut(
uint amountIn,
address[] calldata path
) external view returns (uint[] memory amounts);
//UniswapV2Router02.sol
function getAmountsOut(uint amountIn, address[] memory path)
public
view
virtual
override
returns (uint[] memory amounts)
{
return UniswapV2Library.getAmountsOut(factory, amountIn, path);
}
Code Overview
Resources:
//Typescript
import * as ethers from 'ethers'; //V6
//Set one of Hedera's JSON RPC Relay as the provider
const provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', {
batchMaxCount: 1, //workaround for V6
});
//ABI data for the getAmountsOut
const abi = ['function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'];
//Load the ABI
const abiInterfaces = new ethers.Interface(abi);
//Example route
const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
const route = [tokenIn, tokenOut];
const routerContract = new ethers.Contract(routerEvmAddress, abiInterfaces.fragments, provider);
const result = await routerContract.getAmountsOut(inputAmountInSmallestUnit, route);
const amounts = result; //uint[] amounts
const finalOutputAmount = amounts[amounts.length - 1]; //in token's smallest unit
Resources:
//Typescript
import * as ethers from 'ethers'; //V6
//ABI data for the getAmountsOut
const abi = ['function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'];
//Load the ABI
const abiInterfaces = new ethers.Interface(abi);
//Example route (WHBAR > SAUCE)
const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
const route = [tokenIn, tokenOut];
const routerContract = ContractId.fromString(config.router);
const params = [inputAmountInSmallestUnit, route];
const encodedData = abiInterfaces.encodeFunctionData(abiInterfaces.getFunction('getAmountsOut')!, params);
const url = `${mirrorNodeBaseUrl}/api/v1/contracts/call`;
const data = {
'block': 'latest',
'data': encodedData,
'to': routerContract.toSolidityAddress(),
};
const response = await axios.post(url, data, { headers: {'content-type': 'application/json'} });
const amounts = abiInterfaces.decodeFunctionResult('getAmountsOut', response.data.result)[0]; //uint[] amounts
const finalOutputAmount = amounts[amounts.length - 1]; //in token's smallest unit
Get Input Quote from Output Amount
⛽ No gas cost
Get the input amounts from a given output amount and a swap route.
Function name: getAmountsIn
Solidity Interface & Function Body
//IUniswapV2Router01.sol
function getAmountsIn(
uint amountOut,
address[] calldata path
) external view returns (uint[] memory amounts);
//UniswapV2Router02.sol
function getAmountsIn(uint amountOut, address[] memory path)
public
view
virtual
override
returns (uint[] memory amounts)
{
return UniswapV2Library.getAmountsIn(factory, amountOut, path);
}
Code Overview
Resources:
//Typescript
import * as ethers from 'ethers'; //V6
const networkId = 'testnet';
const provider = new ethers.JsonRpcProvider(`https://${networkId}.hashio.io/api`, '', {
batchMaxCount: 1,
});
//ABI data for the getAmountsIn
const abi = ['function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts)'];
//Load the ABI
const abiInterfaces = new ethers.Interface(abi);
//Example route (WHBAR > SAUCE)
const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
const route = [tokenIn, tokenOut];
const routerContract = new ethers.Contract(routerEvmAddress, abiInterfaces.fragments, provider);
const result = await routerContract.getAmountsIn(outputAmountInSmallestUnit, route);
const amounts = results; //uint[] amounts
const finalInputAmount = amounts[0]; //in token's smallest unit
Resources:
//Typescript
import * as ethers from 'ethers'; //V6
//ABI data for the getAmountsIn
const abi = ['function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts)'];
//Load the ABI
const abiInterfaces = new ethers.Interface(abi);
//Example route (WHBAR > SAUCE)
const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
const route = [tokenIn, tokenOut];
const routerContract = ContractId.fromString(config.router);
const params = [outputAmountInSmallestUnit, route];
const encodedData = abiInterfaces.encodeFunctionData(abiInterfaces.getFunction('getAmountsIn')!, params);
const url = `${mirrorNodeBaseUrl}/api/v1/contracts/call`;
const data = {
'block': 'latest',
'data': encodedData,
'to': routerContract.toSolidityAddress(),
};
const response = await axios.post(url, data, { headers: {'content-type': 'application/json'} });
const amounts = abiInterfaces.decodeFunctionResult('getAmountsIn', response.data.result)[0]; //uint[] amounts
const finalInputAmount = amounts[0]; //in token's smallest unit
Last updated