Solidity 0.6 – How to Deposit WETH Using ‘address.transfer’

address.transferethersoliditysolidity-0.6.x

Solidity 0.6.0 introduced a breaking change in how ETH transfers are performed. The following doesn't work anymore:

weth.deposit.value(amount)();

What is the latest syntax for depositing ETH into the WETH contract?

Best Answer

Solidity 0.6 and 0.7

Here's a simple implementation that can you try out in Remix:

pragma solidity >=0.6.0;

interface WethLike {
    function deposit() external payable;

    function withdraw(uint256) external;
}

contract MyContract {
  WethLike weth;

  constructor(WethLike weth_) {
    weth = weth_;
  }

  function foo() external payable {
    weth.deposit{ value: msg.value }();
  }
}
Related Topic