Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to compute Gas + Uniswap Fees needed for a trade? #42

Open
thegitparticle opened this issue Mar 15, 2022 · 2 comments
Open

How to compute Gas + Uniswap Fees needed for a trade? #42

thegitparticle opened this issue Mar 15, 2022 · 2 comments

Comments

@thegitparticle
Copy link

After constructing a trade, how do I go about calculating gas fees + uniswap fees needed for a trade?

@niZmosis
Copy link

niZmosis commented Mar 16, 2022

Once you call the trade (which I like to call the quote function, as it doesn't make a transaction) function of the UniswapPairFactory, the gas information will be in the object returned. You would have needed to provide the gas price in the UniswapPairSettings constructor. If you don't have enough balance for the input token, it will not be able to calculate it for you and will be undefined.

It'll look similar to this. I made my own gas function since BSC wants to use the old gasGasPrice function

// EIP-1559 supported chains use getFeeData
export async function getGasPriceForProvider(provider) { return [bscChainId].includes(provider._network.chainId) ? await provider?.getGasPrice() : (await provider?.getFeeData()).maxFeePerGas }

let settings = new UniswapPairSettings({ slippage, deadlineMinutes, disableMultihops: false, uniswapVersions, gasSettings: { getGasPrice: async () => formatUnits(await getGasPriceForProvider(provider), 'gwei') } })

Everything should be taken care of for you for tokens as far as gas ect go. If you are using Ethers, you don't need to provide the gasprice, limit, nonce ect, it's all automatic, just call wallet.sendTransaction(trade.transaction)

For the native coin for the chain you are using you will need to cacluate it yourself it looks like. From the docs for the input token balance

// if the balance of the users has enough to perform this trade, does not consider gas prices
// right now if your doing ETH > ERC20

@thegitparticle
Copy link
Author

Yes, this makes sense for gas. But how about the uniswap swap fee?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants