Solidity

如何將交易的輸入轉換為可讀值?

  • March 18, 2022

我有以下交易的輸入:

{
   name: 'trade',
   types: [
       'address',
       'uint256',
       'address',
       'uint256',
       'uint256',
       'uint256',
       'address',
       'uint8',
       'bytes32',
       'bytes32',
       'uint256'
   ],
   inputs: ['ac709fcb44a43c35f0da4e3163b117a17f3770f5',
       <BN: 878678326eac900000>,
       '0000000000000000000000000000000000000000', 
       <BN: bf2aa18455018000>, 
       <BN: 302bdf>,
       <BN: dc51da3a>,
       'd8eeda4ee2657bb267bc35c1d60babdc5aedd269', 
       <BN: 1c>,
       <Buffer b4 bf 0 d cd 37 ca 73 bd 5 a bc e9 53 2 b 03 3 d cc 64 27 be 16 06 83 de 11 3e fc a4 13 20 c2 7 f 3e>, 
       <Buffer 78 79 9 d 84 04 0e 5 c 24 58 28 b1 b5 31 ef 97 e1 af 12 49 46 f4 dc 1 d b5 a2 0 c f2 8 a 70 73 b2 69>, 
       <BN: 275dcc081d0c2351e0>
   ]
}

該數據來自原始交易輸入,即

0x0a19b14a000000000000000000000000ac709fcb44a43c35f0da4e3163b117a17f3770f50000000000000000000000000000000000000000000000878678326eac9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bf2aa184550180000000000000000000000000000000000000000000000000000000000000302bdf00000000000000000000000000000000000000000000000000000000dc51da3a000000000000000000000000d8eeda4ee2657bb267bc35c1d60babdc5aedd269000000000000000000000000000000000000000000000000000000000000001cb4bf0dcd37ca73bd5abce9532b033dcc6427be160683de113efca41320c27f3e78799d84040e5c245828b1b531ef97e1af124946f4dc1db5a20cf28a7073b2690000000000000000000000000000000000000000000000275dcc081d0c2351e0

如何將輸入轉換為可讀的字元串值?

PS:readable string values我的意思是將值從BNBuffer 類型轉換為 utf8 字元串。

用於解碼編碼輸入數據的工具ethereum-input-data-decoderuint將類型返回為Big Numbers並將byte32類型返回為Buffers

要轉換為可讀格式,只需呼叫該toString方法。

例子

// big number to decimal string
myBN.toString(10) 

// buffer to hex string
myBuffer.toString(16) 

這是執行此操作的眾多方法之一。對於這種方法,您需要契約的原始碼和地址。

  1. 使用元遮罩或在線上 ide上輸入您的自定義 web3 提供程序
  2. 編譯你的原始碼
  3. 輸入已部署合約的地址以載入到 ide
  4. 啟用收聽網路單選按鈕
  5. 您將能夠讀取已送出交易的輸入

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