I want to use the Uniswap router contract to buy a token. However, I got confused by the amountoutmin(unit256) in the swapExactETHForTokensSupportingFeeOnTransferTokens of uniswap. I read the unswap documentation and it says "The minimum amount of output tokens that must be received for the transaction not to revert." Please can someone suggest what I am expected to write there and what it means. Thank you.
[Ethereum] Uniswap’s AmountoutMin(uint256)
contract-developmentsolidityuniswap
Related Solutions
What is the meaning of stipend if transactions are always have gas limit set by sender?
Not really. When EOA calls contract A, the gas limit is set in the transaction, this is true. However, when contract A calls contract B, contract A may set limit on how much gas the contract B is allowed to spend. This limit may be lower than the remaining gas contract A has by itself at the moment. In such case, even if call to contract B will run out of gas and thus fail, contract A will still have some gas to handle the situation.
When, in what cases, does this damn 'stipend' apply?
There are several ways how one contract may call another contract, and these ways behave differently:
- One may just call other contract's method as a function. In this case called contract will be allowed to spend all the gas remaining in the transaction.
- One may use functions
call
,delegatecall
, orstaticall
that allow explicitly specifying how much gas to called contract will be allowed to spend (though by default these functions allow spending all the gas remaining). - One may use functions
transfer
ofsend
that allow called contract to spend at most 2300 gas, and this hardcoded value cannot be changed.
Could someone please provide a piece of code or describe a scenario when 2300 stipend is not enough?
Sure. Lets assume the following implementation of contract B:
contractB {
uint private etherReceived = 0;
function () external payable {
etherReceived += msg.value;
}
}
The contract tries to count how much Ether it ever received via fallback function. This implementation has a problem, as it updates storage variable inside fallback function and such update costs 5000 gas or more. Thus, transfer
to such contract from another contract will fail.
Could someone please provide a piece of code or describe a scenario when ... we set gasLimit which is enough to execute transfer function?
Function transfer
has hardcoded gas limit of 2300 which cannot be changed. However here is an example of a contract whose fallback function fits into this harsh limit and yet does something useful:
contractB {
event Deposit (address from, uint value);
function () external payable {
emit Deposit (msg.sender, msg.value);
}
}
Best Answer
The Uniswap Router is a periphery contract which means that it's not strictly necessary. But still you should use it due to the fact that it protects you against various kinds of attacks on your trades.
One of the attack vectors is frontrunning your trades. That means you enter your trade, some bot notices it in the Ethereum mempool (before it's executed), creates their own trade which gets executed before your trade and their trade makes your trade less profitable for you.
To prevent this kinds of attacks the router provides various mechanisms; one of them is that
amountoutmin
. You could just send X amount of Eth to Uniswap and say "give me the maximum amount of tokens for this amount of Eth I give you" but that would be suspectible to frontrunning attacks. So you also need to specify how many tokens you want at minimum withamountoutmin
. If the trading price has shifted too much between when you send the transaction and when it gets executed your trade gets reverted.So you have to know in advance how many tokens you'd like to get, at minimum.