Web3j

如何在交易中讀取代幣的符號和名稱?

  • July 26, 2021
  1. 如何在交易中讀取代幣的符號和名稱?我試圖從交易中解碼輸入數據,但沒有成功。最新的 Web3j 在 java 中沒有 toAscii 方法。
  2. 我應該為此使用奇偶校驗跟踪器嗎?追踪有什麼好處?內部交易?

謝謝

根據 Mikko Ohtamaa 的回答,我提出以下 python 程式碼:

if __name__ == "__main__":

   EIP20_ABI = json.loads(
       '[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]')  # noqa: 501

   address = w3.toChecksumAddress("0x384776cb3E453402eCF070B48D79e9874763CB50")

   token_contract = contract = w3.eth.contract(address, abi=EIP20_ABI, ContractFactoryClass=ConciseContract)

   print(token_contract.symbol())
   print(token_contract.name())

主要區別在於:

  1. 使用工廠(無需呼叫函式方法)
  2. 將雜湊合約地址轉換為校驗和。
  3. 為python程式碼中的數據解碼定義json模式。

在 Java 中你也應該這樣做。

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