TypeError: "msg.value" and "callvalue()" can only be used in payable public functions

TypeError: "msg.value" and "callvalue()" can only be used in payable public functions

在我的合约中,我尝试使用 Uniswap 的 swapExactETHForTokens() 函数将 Eth 换成 Dai:

pragma solidity ^0.6.6;

interface IUniswap {
  function swapExactETHForTokens(
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline)
  external
  payable
  returns (uint[] memory amounts);
  function WETH() external pure returns (address);
}

contract MyContract {
  IUniswap uniswap;

  constructor(address _uniswap) public {
    uniswap = IUniswap(_uniswap);
  }

  function swapExactETHForTokens(uint amountOutMin, address token) external {
    address[] memory path = new address[](2);
    path[0] = uniswap.WETH();
    path[1] = token;
    uniswap.swapExactETHForTokens{value: msg.value}(
      amountOutMin, 
      path,
      msg.sender, 
      now
    );
  }
}

但是在 swapExactETHForTokens() 中的这一行:

uniswap.swapExactETHForTokens{value: msg.value}(

Remix IDE 显示以下错误:

contracts/MyContract.sol:26:42: TypeError: "msg.value" and "callvalue()" can only be used in payable public functions. Make the function "payable" or use an internal function to avoid this error.
    uniswap.swapExactETHForTokens{value: msg.value}(
                                         ^-------^

我很困惑,因为在顶部我已经在 IUniswap 界面中声明 swapExactETHForTokens() 可以支付。

此错误的搜索结果为空白,所以我认为最好在这里提问。我该如何解决这个问题?

我意识到我的错误:我需要将 payable 关键字添加到我自己的 swapExactETHForTokens() 实现中,如下所示:

  function swapExactETHForTokens(uint amountOutMin, address token) external payable { // payable keyword here
    address[] memory path = new address[](2);
    path[0] = uniswap.WETH();
    path[1] = token;
    uniswap.swapExactETHForTokens{value: msg.value}(
      amountOutMin, 
      path,
      msg.sender, 
      now
    );
  }