Solidity

讓合約創建者地址成為唯一可以在合約中執行功能的地址

  • May 15, 2020

我有三個相互互動的契約。

合約“接收乙太幣”可以從兩個“發送乙太幣”合約之一接收乙太幣 合約

“先發送乙太幣”只能由部署合約的人訪問。

任何人都應該可以訪問“Send Ether Second”合約。

當我在 Remix 中執行“Send Ether First”時,我得到了

"transact to SendEtherFirst.sendViaCall errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.  Debug the transaction to get more information. " 

是不是說“Receive Ether”的地址是唯一可以“Send Ether First”的地址?

我可以通過“Send Ether Second”發送乙太沒有問題。

如何使“Send Ether First”的發送者與部署合約的人相同?我似乎無法解決它。

pragma solidity ^0.5.11;

contract ReceiveEther {
uint256 public count;
address public owner= address(this);

function () external payable {
   require(count < 2);
   count++;
}

function getBalance() public view returns (uint) {
   return address(this).balance;
   }
}

contract SendEtherFirst {
   address owner;
   function sendViaCall (address payable _to) public payable {
       (bool sent, bytes memory data) = _to.call.value(msg.value)("");
      require(msg.sender == owner);
       require (sent, "failed to send ether");
   }
}

 contract SendEtherSecond {
   function sendViaCall (address payable _to) public payable {
       (bool sent, bytes memory data) = _to.call.value(msg.value)("");
       require (sent, "failed to send ether");
   }
}

感謝您的任何幫助

將地址移至“SendEtherFirst”合約是必不可少的。了解地址和 msg.sender 以及它們如何相互作用也是如此。

pragma solidity ^0.5.11;

contract ReceiveEther {
   uint256 public count;
       function () external payable {
           require(count < 2);
               count++;
}

       function getBalance() public view returns (uint) {
           return address(this).balance;
   }
}

contract SendEtherFirst {
   address public creator= msg.sender;
       function sendViaCall (address payable _to) public payable {
           require(msg.sender == creator);
           (bool sent, bytes memory data) = _to.call.value(msg.value)("");
           require (sent, "failed to send ether");
   }
}

contract SendEtherSecond {
       function sendViaCall (address payable _to) public payable {
           (bool sent, bytes memory data) = _to.call.value(msg.value)("");
           require (sent, "failed to send ether");
   }
}

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