Solidity
自動將所有代幣和 Eth 發送到一個地址
我有幾個孩子錢包和一個父母錢包。每當這些子錢包收到傳入的 ERC20 代幣或 ETH 時。我想將它們自動發送到父錢包。
我知道我可以在下面發送整個 eth 餘額。
beneficiary.transfer(this.balance);
我怎樣才能為 erc20 代幣做到這一點。
提前致謝。
您正在尋求有效地**“轉發”**您的餘額。一種方法是使用與此處突出顯示的契約類似的契約。
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); } }
使用目前的 ERC20 代幣標準自動執行此操作實際上是不可能的 - 這被視為該早期標準的失敗和不一致。
這就是為什麼正在開發新的“高級”代幣標準(例如ERC223、ERC777和ERC827)來處理將代幣轉移到合約可以觸發行動的情況。