I am trying to implement a method for calculating token buy/sell tax. The one that devs implement it in the transfer()
function.
I've done some research and there are 2 options.
eth_call
- simulate a swap, calculate the difference- deploy the
ERC20
smart contract on a local hardhat/ganache, execute swap and see the difference
The eth_call
seems to be better for me but I'm facing some issues.
// Encode function data
const encodedData = router.interface.encodeFunctionData('swapExactETHForTokens', [
0,
path,
to,
deadline,
]);
// Get amountsOut using eth_call, performs a SIMULATION
const callResult = await this.provider.call({
data: encodedData,
value: parseUnits('2', 18),
to: constants.UNISWAP_ROUTER_ADDRESS,
});
console.log('callResult', callResult);
// Decode result
const decodedResult = router.interface.decodeFunctionResult(
'swapExactETHForTokens',
callResult
);
it does not return the actual amount including taxes, the response is just an array of the amounts out first is amount of ETH and second is the token amount. if I set the amountIn 0, then I get 0% tax, if I increase it then the amountOut of token decreases.