以太币从一方交易到另一方

优质
小牛编辑
117浏览
2023-12-01

以太币从一方交易到另一方

在双方之间发送以太币Ether需要交易对象的最少量的信息:

  • to :目的地钱包地址
  • value:价值,希望发送到目的地的以太币数量
BigInteger value = Convert.toWei("1.0", Convert.Unit.ETHER).toBigInteger();
RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(
             <nonce>, <gas price>, <gas limit>, <toAddress>, value);
// send...

但是,建议你使用TransferClass来发送以太币Ether,它负责对nonce管理和通过不断的轮询为你提供响应:

Web3j web3 = Web3j.build(new HttpService());  // defaults to http://localhost:8545/
Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile");
TransactionReceipt transactionReceipt = Transfer.sendFunds(
        web3, credentials, "0x<address>|<ensName>",
        BigDecimal.valueOf(1.0), Convert.Unit.ETHER).send();