如何在智能合约中接收 ETH

时间:2021-05-21 01:07:05

标签: blockchain ethereum solidity truffle cryptocurrency

我正在开发一个 ico,我得到了这个代码,众筹是使用 dai 代币进行的,但我想使用以太币, 我该怎么做?

IERC20 public dai = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F);
function buy(uint etherAmount)
    external
    icoActive() {
    require(
      etherAmount >= minPurchase && etherAmount <= maxPurchase, 
      'have to buy between minPurchase and maxPurchase'
    );
    uint tokenAmount = etherAmount.div(price);
    
    require(
      tokenAmount <= availableTokens, 
      'Not enough tokens left for sale'
    );
    dai.transferFrom(msg.sender, address(this), etherAmount);
    token.mint(address(this), tokenAmount);
    sales[msg.sender] = Sale(
        msg.sender,
        tokenAmount,
        false
    );
}

1 个答案:

答案 0 :(得分:0)

您可以使用 payable 函数修饰符。见documentation how to receive Ether in smart contracts