1

In the function swapExactETHForTokens of Uniswap is called like this:

 uniswap.swapExactETHForTokens{value: msg.value}(
            amountOut,
            path,
            msg.sender,
            deadline
        );

What does the part {value: msg.value} mean? Is this really correct syntasis at all? Meaning, the {...} itself in a function call.

And does the function not accept the value argument in the normal way, inside (...)?

1 Answer 1

0

That's how you can call a function and provide value to it.

The concept of value refers to Ethers sent along the call. The value is taken from the contract balance. You can't provide the value as a regular parameter inside () in Solidity if you want to send Ether, and not just some uint which happens to be called value.

Here's an example. If you call demo (remember to include value when calling the function, otherwise the contract doesn't have any value to forward), the some function call doesn't forward any value. But other call forwards 6 weis. Note also that functions need to be declared payable in order to be able to receive value.

contract A {   
    function demo() public payable {
        B inst = new B();
        inst.some(5);
        inst.other{value: 6}();
    }
}
contract B {
    uint private _value;
    function some(uint value) public {
        _value = value;
    }
    function other() payable public {
    }
}

You can see the syntax for example here: https://docs.soliditylang.org/en/latest/control-structures.html#external-function-calls

5
  • you really want to send Ether -> how is "really want" different from merely "want"? Does the intensity of my desire affect syntax?
    – Markus736
    Feb 26, 2022 at 14:32
  • and not just some uint which happens to be called value. --> why not?
    – Markus736
    Feb 26, 2022 at 14:33
  • very well, edited my answer to include an example Feb 26, 2022 at 14:56
  • You haven't explained "why"
    – Markus736
    Feb 26, 2022 at 18:03
  • Why you can't pass Ethers with just a function parameter value? Because, um, you can't. The language doesn't work like that. Feb 26, 2022 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.