Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ethereum 如何使用web3j java签署erc20令牌事务_Ethereum_Smartcontracts_Erc20_Web3 Java - Fatal编程技术网

Ethereum 如何使用web3j java签署erc20令牌事务

Ethereum 如何使用web3j java签署erc20令牌事务,ethereum,smartcontracts,erc20,web3-java,Ethereum,Smartcontracts,Erc20,Web3 Java,我想把erc20代币从一个账户转到另一个账户。我知道我可以使用智能合约包装类的transfer或transferFrom函数。但在我的例子中,erc20令牌事务需要在客户端签名。在智能合约包装器函数中,无法传递signedTransaction。那么,如何在java中签署erc20令牌事务并使用web3j执行该事务呢 我发现了类似的问题。但是,它的代码不是用java编写的。我不知道如何在ABI中编码传递函数。 提前谢谢。有点晚了,但可能会有人: 您可以尝试以下方法: import org.we

我想把erc20代币从一个账户转到另一个账户。我知道我可以使用智能合约包装类的transfer或transferFrom函数。但在我的例子中,erc20令牌事务需要在客户端签名。在智能合约包装器函数中,无法传递signedTransaction。那么,如何在java中签署erc20令牌事务并使用web3j执行该事务呢

我发现了类似的问题。但是,它的代码不是用java编写的。我不知道如何在ABI中编码传递函数。


提前谢谢。

有点晚了,但可能会有人:

您可以尝试以下方法:

import org.web3j.abi.TypeReference;
导入org.web3j.abi.datatypes.Address;
导入org.web3j.abi.datatypes.Bool;
导入org.web3j.abi.datatypes.generated.Uint256;
导入org.web3j.crypto.Credentials;
导入org.web3j.crypto.RawTransaction;
导入org.web3j.protocol.core.methods.response.EthGetTransactionCount;
导入org.web3j.utils.Convert;
导入org.web3j.utils.Numeric;
导入java.math.biginger;
导入java.util.array;
导入java.util.Collections;
导入java.util.function.function;
//创建凭据
字符串privateKeyHexValue=Numeric.toHexString(privateKey.data());
凭据=凭据。创建(privateKeyHexValue);
//获取下一个可用的nonce
EthGetTransactionCount EthGetTransactionCount=web3.EthGetTransactionCount(credentials.getAddress(),DefaultBlockParameterName.LATEST).send();
BigInteger nonce=ethGetTransactionCount.getTransactionCount();
//要传输的值(ERC20令牌)
biginger balance=Convert.toWei(“1”,Convert.Unit.ETHER).toBigInteger();
//创建传递函数
功能=传输(地址、余额);
字符串encodedFunction=FunctionEncoder.encode(函数);
//气体参数
BigInteger gasLimit=BigInteger.valueOf(71000);//您应该从api获取此信息
BigInteger gasPrice=新的BigInteger(“d693a400”,16);//十进制3600000000
//创建事务
RawTransaction RawTransaction=
RawTransaction.createTransaction(
nonce,gasPrice,gasLimit,,编码功能);
//签署交易
字节[]signedMessage=TransactionEncoder.signMessage(原始事务,凭证);
字符串hexValue=Numeric.toHexString(signedMessage);
//发送事务
EthSendTransaction EthSendTransaction=web3.ethSendRawTransaction(hexValue.sendAsync().get();
String transactionHash=ethSendTransaction.getTransactionHash();
传递函数可以是:

私有函数传输(字符串到,BigInteger值){
返回新函数(
“转让”,
Arrays.asList(新地址(to)、新Uint256(值)),
Collections.singletonList(新的TypeReference(){
}));
}