Go-Ethereum
Web3j - 發送 erc20 令牌
我想問一下如何正確地將 ERC20 代幣從一個錢包發送到另一個錢包,以下是我的程式碼:
public static void main(String[] args) throws Exception { Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/ Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send(); String clientVersion = web3ClientVersion.getWeb3ClientVersion(); Credentials creds = WalletUtils.loadCredentials("password", "path/to/wallet"); EthGetBalance ethGetBalance = web3.ethGetBalance(creds.getAddress(), DefaultBlockParameterName.LATEST) .sendAsync().get(); BigInteger wei = ethGetBalance.getBalance(); FixedSupplyToken_sol_FixedSupplyToken contract = FixedSupplyToken_sol_FixedSupplyToken .load("0x<CONTRACT_ADDRESS>", web3, creds, gasPrice, gasLimit); BigInteger tokenAmount = contract.balanceOf(creds.getAddress()).send(); contract.transfer("0x<TO_ADDRESS>", new BigInteger("10000")).send(); }
當我嘗試執行此程式碼時,它的工作,交易被送出到我的私人網路,但它返回這些錯誤。
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. Exception in thread "main" org.web3j.protocol.exceptions.TransactionException: Error processing request: unknown transaction at org.web3j.tx.response.TransactionReceiptProcessor.sendTransactionReceiptRequest(TransactionReceiptProcessor.java:32) at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:37) at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:29) at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:72) at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51) at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:70) at org.web3j.tx.Contract.executeTransaction(Contract.java:223) at org.web3j.tx.Contract.executeTransaction(Contract.java:207) at org.web3j.tx.Contract.executeTransaction(Contract.java:201) at org.web3j.tx.Contract.lambda$executeRemoteCallTransaction$3(Contract.java:240) at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30) at com.gio.myapp.App.main(App.java:50)
我在這裡錯過了什麼嗎?
您可以使用以下功能手動編碼數據:
public static String encodeTransferData(String toAddress, BigInteger sum) { Function function = new Function( "transfer", // function we're calling Arrays.asList(new Address(toAddress), new Uint256(sum)), // Parameters to pass as Solidity Types Arrays.asList(new org.web3j.abi.TypeReference<Bool>() {})); return FunctionEncoder.encode(function); }
並以這種方式呼叫契約:
public static void main(String[] args) throws Exception { Web3j web3 = Web3j.build(new HttpService()); // defaults to http://localhost:8545/ Credentials creds = WalletUtils.loadCredentials("password", "path/to/wallet"); RawTransactionManager manager = new RawTransactionManager(web3, creds); String toAddress = "0x..."; // destination address String contractAddress = "0x..."; BigInteger sum = BigInteger.valueOf(10000); // amount you want to send String data = encodeTransferData(toAddress, sum); BigInteger gasPrice = web3.ethGasPrice().send().getGasPrice(); BigInteger gasLimit = BigInteger.valueOf(120000); // set gas limit here EthSendTransaction transaction = manager.sendTransaction(gasPrice, gasLimit, contractAddress, data, null); }
這與在區塊鏈中找不到交易雜湊有關。
例如,嘗試在事務之後將事務雜湊輸出為字元串。
也嘗試更換線路
contract.transfer("0x<TO_ADDRESS>", new BigInteger("10000")).send();
和
contract.transfer("0x<TO_ADDRESS>", new BigInteger("10000")).sendAsync().get();
可以幫助您更明確地定位問題,甚至解決問題。