Solidity

遠期合約

  • June 18, 2018

我是一個擁有可靠和智能合約的完整菜鳥。我一直在尋找解決以下問題的腳本。 使用者將乙太幣發送到智能合約,然後智能合約將乙太幣數量轉發到主地址。 所以我發現了這個

pragma solidity ^0.4.18;

/**
* Contract that will forward any incoming Ether to its creator
*/
contract Forwarder {
 // Address to which any funds sent to this contract will be forwarded
 address public destinationAddress;

 /**
  * Create the contract, and set the destination address to that of the creator
  */
 function Forwarder() public {
   destinationAddress = msg.sender;
 }

 /**
  * Default function; Gets called when Ether is deposited, and forwards it to the destination address
  */
 function() payable public {
       destinationAddress.transfer(msg.value);
 }

 /**
  * It is possible that funds were sent to this address before the contract was deployed.
  * We can flush those funds to the destination address.
  */
 function flush() public {
   destinationAddress.transfer(this.balance);
 }

}

第一個問題:這個腳本有效嗎?它符合我的描述嗎?

第二個問題:我的主要地址放在哪裡?我的地址是 0x7e0fE0Bd87F84906bc19438fb5F932e189Dd127e 插入後我的程式碼應該是什麼樣子?

我將發送 0.15 ETH 以獲得最佳答案。謝謝你。

它應該可以正常工作。我將其更新為 solc 0.4.21,使其更新,並刪除了,^因為這在某些情況下會導致不必要的歧義。還添加了事件發射器以觀察最佳實踐。刪除了評論以減少視覺噪音。

pragma solidity 0.4.21;

contract Forwarder {

 address public destinationAddress;
 event LogForwarded(address indexed sender, uint amount);
 event LogFlushed(address indexed sender, uint amount);

 function Forwarder() public {
   destinationAddress = msg.sender;
 }

 function() payable public {
   emit LogForwarded(msg.sender, msg.value);
   destinationAddress.transfer(msg.value);
 }

 function flush() public {
   emit LogFlushed(msg.sender, address(this).balance);
   destinationAddress.transfer(address(this).balance);
 }

}

希望能幫助到你。

契約有效。當您將部署合約時,您的主要地址將是您用於部署合約的帳戶的地址。這是在合約的建構子中完成的:

function Forwarder() public {
   destinationAddress = msg.sender;
 }

此功能僅在您部署合約時執行一次。它將部署 de 合約的人的地址值分配給destinationAddress

我希望這有幫助。

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