簡體   English   中英

如何在智能合約中接收 ETH

[英]How to receive ETH in a smart contract

我正在做一個 ico,我得到了這個代碼,眾籌是用 dai 代幣,但我想用 ether,我該怎么做?

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
    );
}

您可以使用payable的 function 修飾符。 請參閱文檔如何在智能合約中接收以太幣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM