Solidity

智能合約是接收資金還是地址?

  • January 16, 2022
//SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

contract ExceptionExample {

   mapping(address => uint) public balanceReceived;

   function receiveMoney() public payable {
       balanceReceived[msg.sender] += msg.value;
   }

   function withdrawMoney(address payable _to, uint _amount) public {
       if(_amount <= balanceReceived[msg.sender]) {
           balanceReceived[msg.sender] -= _amount;
           _to.transfer(_amount);
       }
   }
}

假設我部署了一個地址為 a 的智能合約。現在,當我與函式 receiveMoney() 互動並發送 20 eth 時。智能合約正在獲取乙太幣嗎?而且,在該 receiveMoney 函式中,它只是增加了我發送到該地址的金額,對嗎?

你說的對。當您呼叫receiveMoney時,假設 1 ETH 作為價值,智能合約的餘額會增加 1 ETH。

引用自:https://ethereum.stackexchange.com/questions/119100