Abi
如何使用 java 使用 ABI 解碼輸入數據?
npm ethereum-input-data-decoder上有一個工具。是的,我可以將輸入數據讀取為十六進制
byte[] bytes = Hex.decodeHex(transactionReceipt.getInput().substring(2).toCharArray()); System.out.println(new String(bytes, "UTF-8"));
但我需要結構化輸出。以json為例。我想用java來做。不是 npm
行!我解決了。
首先,我創建了自己的契約,它是從org.web3j.tx擴展****契約。重寫了兩個方法(我需要從合約接收多個值):executeCallMultipleValueReturnAsync,executeCallMultipleValueReturn但與 Contract 中的內容相同,並創建了自己的 executeCall 方法,這與來自 web3j Contract 的私有executeCall相同,但不是將DefaultBlockParameterName.LATEST傳遞給 web3j。 ethCall 我正在傳遞新的 DefaultBlockParameterNumber(blockNumber),所需的 blockNumber 為 BigInteger。所以這裡是:
public class MyEthereumContract extends Contract { private String contractAddress; private BigInteger blockNumber; protected MyEthereumContract(String contractBinary, String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { super(contractBinary, contractAddress, web3j, transactionManager, gasPrice, gasLimit); } protected MyEthereumContract(String contractBinary, String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { super(contractBinary, contractAddress, web3j, credentials, gasPrice, gasLimit); } protected MyEthereumContract(String contractBinary, String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger blockNumber) { super(contractBinary, contractAddress, web3j, credentials, gasPrice, gasLimit); this.contractAddress = contractAddress; this.blockNumber = blockNumber; } @Override protected CompletableFuture<List<Type>> executeCallMultipleValueReturnAsync(Function function) { return Async.run(() -> executeCallMultipleValueReturn(function)); } @Override protected List<Type> executeCallMultipleValueReturn( Function function) throws InterruptedException, ExecutionException { return executeCall(function); } private List<Type> executeCall( Function function) throws InterruptedException, ExecutionException { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall ethCall = web3j.ethCall( Transaction.createEthCallTransaction( transactionManager.getFromAddress(), contractAddress, encodedFunction), new DefaultBlockParameterNumber(blockNumber)) .sendAsync().get(); String value = ethCall.getValue(); return FunctionReturnDecoder.decode(value, function.getOutputParameters()); } }
比我添加新的建構子到類,從契約生成,我從載入方法呼叫