Solidity

使用 erc223 為內部交易數據編碼原始交易數據以進行傳輸

  • January 18, 2019

將erc223代幣轉移到ERC223Receiver合約的內部交易呼叫後,如何對數據進行編碼?

例子:

Contract ERC223Receiver {
   function foo(uint256 a) {
       // some work
   }
}


Contract Other {
   function bar(address token, address receiver, uint256 amount, uint256 a) {
       bytes4 sig = bytes4(sha3("foo(uint256)"));

       bytes data = bytes(sig + a) // ????

       token.transfer(receiver, amount, data);
   }
}

我設法使用彙編解決了這個問題:

Contract Other {
   function bar(address token, address receiver, uint256 amount, uint256 a) {
       bytes4 sig = bytes4(sha3("foo(uint256)"));
       uint256 argsSize = 1 * 32;
       // sig + arguments size
       uint256 dataSize = 4 + argsSize;

       bytes memory m_data = new bytes(dataSize);

       assembly {
           // Add the signature first to memory
           mstore(add(m_data, 0x20), sig)
           // Add the parameters
           mstore(add(m_data, 0x24), a)
       }

       token.transfer(receiver, amount, m_data);
   }
}

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