主页 > imtoken授权管理系统 > web3j的传输方式有哪些

web3j的传输方式有哪些

imtoken授权管理系统 2023-11-24 05:14:24

今天,小编就给大家分享一些关于web3j传输方式的相关知识点。 内容详尽,逻辑清晰。 相信大部分人还是对这些知识了如指掌,所以特将这篇文章分享给大家参考。 我希望你读完它。 看完这篇,我们是有所收获的,一起来看看吧。

web3 传递函数

为了完成以太坊交易,必须有几个先决条件

1.对方的以太坊地址

2.确定转账金额

3、自己地址的转让权限

4. 对于大于转账金额的以太币,转账以太币其实就是提出交易,矿工会帮你计算,计算完成后交易才会成交,但是矿工需要支付一定的手续费(GAS) 进行计算。 如果付款太少,计算转账可能会很慢或不成功。

转移方式一:

代码如下

1 import org.web3j.crypto.Credentials;
 2 import org.web3j.crypto.RawTransaction;
 3 import org.web3j.crypto.TransactionEncoder;
 4 import org.web3j.protocol.Web3j;
 5 import org.web3j.protocol.core.DefaultBlockParameterName;
 6 import org.web3j.protocol.core.methods.response.EthGetTransactionCount;
 7 import org.web3j.protocol.core.methods.response.EthSendTransaction;
 8 import org.web3j.protocol.http.HttpService;
 9 import org.web3j.utils.Convert;
10 import org.web3j.utils.Numeric;
11 
12 import java.math.BigInteger;
13 
14 
15 public class TransactionTest {
16     public static void main(String[] args) throws Exception {
17         //设置需要的矿工费
18         BigInteger GAS_PRICE = BigInteger.valueOf(22_000_000_000L);
19         BigInteger GAS_LIMIT = BigInteger.valueOf(4_300_000);
20 
21         //调用的是kovan测试环境,这里使用的是infura这个客户端
22         Web3j web3j = Web3j.build(new HttpService("https://kovan.infura.io/"));
23         //转账人账户地址
24         String ownAddress = "0xD1c82c71cC567d63Fd53D5B91dcAC6156E5B96B3";
25         //被转人账户地址
26         String toAddress = "0x6e27727bbb9f0140024a62822f013385f4194999";
27         //转账人私钥
28         Credentials credentials = Credentials.create("xxxxxxxxxxxxx");
29         //        Credentials credentials = WalletUtils.loadCredentials(
30         //                "123",
31         //                "src/main/resources/UTC--2018-03-01T05-53-37.043Z--d1c82c71cc567d63fd53d5b91dcac6156e5b96b3");
32 
33         //getNonce(这里的Nonce我也不是很明白,大概是交易的笔数吧)
34         EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
35                 ownAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
36         BigInteger nonce = ethGetTransactionCount.getTransactionCount();
37 
38         //创建交易,这里是转0.5个以太币
39         BigInteger value = Convert.toWei("0.5", Convert.Unit.ETHER).toBigInteger();
40         RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
41                 nonce, GAS_PRICE, GAS_LIMIT, toAddress, value);
42 
43         //签名Transaction,这里要对交易做签名
44         byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
45         String hexValue = Numeric.toHexString(signedMessage);
46 
47         //发送交易
48         EthSendTransaction ethSendTransaction =
49                 web3j.ethSendRawTransaction(hexValue).sendAsync().get();
50         String transactionHash = ethSendTransaction.getTransactionHash();
51 
52         //获得到transactionHash后就可以到以太坊的网站上查询这笔交易的状态了
53         System.out.println(transactionHash);
54     }
55 }

注意:

上面的交易代码是离线交易,先组装交易,然后发送到链上,web3j提供在线交易web3以太坊转账,但是这个交易需要parity钱包web3以太坊转账,下载完整的区块链钱包,然后把账户绑定进去。

1、第27-31行,获取地址的信任证书有两种方式,一种是直接使用私钥,一种是使用keystore文件

2、

这个地址是您可以检查您的交易的地方

0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304是transactionHash

该地址为测试地址,如需查询主网,删除kovan

转移方式二:

import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;
import java.math.BigDecimal;
public class TransactionTest2 {
    public static void main(String[] args) throws Exception {
        Web3j web3j = Web3j.build(new HttpService("https://kovan.infura.io/"));
        String ownAddress = "0xD1c82c71cC567d63Fd53D5B91dcAC6156E5B96B3";
        String toAddress = "0x6e27727bbb9f0140024a62822f013385f4194999";
        Credentials credentials = Credentials.create("xxxxxxxx");
        TransactionReceipt transactionReceipt = Transfer.sendFunds(
                web3j, credentials, toAddress,
                BigDecimal.valueOf(0.2), Convert.Unit.ETHER).send();
        System.out.println(transactionReceipt.getTransactionHash());
    }
}

注意

这个也是离线的,但是代码量比较少。

评论:

如果你的kovan环境中没有ether,你可以去这里索取。 直接注册账号后,把你的账号地址发到群里,几分钟后钱就到账了。 主网和科文的账户地址是一样的,但是里面的币是不一样的。

以上就是《web3j的传输方式有哪些》一文的全部内容,感谢阅读! 相信大家看了这篇文章后,都收获颇丰。 小编每天都会为大家更新不一样的知识。 想要了解更多知识,请关注易速云行业资讯频道。