通过web3j发起转账、代币转账

2021-08-27  本文已影响0人  侧耳倾听y

工作中需要在链上发起一笔交易,在此记录一下。

1. 引入依赖

        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>geth</artifactId>
            <version>4.3.0</version>
        </dependency>

2. 发起转账

        Web3j web3j = Web3j.build(new HttpService("节点"));
        // 发送地址
        String fromAddress = "";
        // 接收地址
        String toAddress = "";
        Credentials credentials = Credentials.create("私钥");

        EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();

        BigInteger nonce = ethGetTransactionCount.getTransactionCount();
        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
                nonce, 
                // gas price
                Convert.toWei("0.1", Convert.Unit.GWEI).toBigInteger(),
                // gas limit
                BigInteger.valueOf(300000L), 
                toAddress, 
                // amount
                Convert.toWei("0.1", Convert.Unit.GWEI).toBigInteger());
        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        String hexValue = Numeric.toHexString(signedMessage);
        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
        // 交易hash
        System.out.println(ethSendTransaction.getTransactionHash());

3. 发起代币转账

        Web3j web3j = Web3j.build(new HttpService("节点"));
        Credentials credentials = Credentials.create("密钥");

        String fromAddress = credentials.getAddress();
        String toAddress = "";

        EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
        BigInteger nonce = ethGetTransactionCount.getTransactionCount();


        Function function = new Function(
                "transfer",
                Arrays.asList(new Address(toAddress), new Uint256(1L)),
                Arrays.asList(new TypeReference<Type>() {
                }));

        String encodedFunction = FunctionEncoder.encode(function);

        RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei("0.1", Convert.Unit.GWEI).toBigInteger(),
                BigInteger.valueOf(300000L), "合约地址", encodedFunction);

        byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
        String hexValue = Numeric.toHexString(signedMessage);
        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();

        String transactionHash = ethSendTransaction.getTransactionHash();
        System.out.println(transactionHash);

4. 发起转账 EIP155签名

        Web3j web3j = Web3j.build(new HttpService(""));
        String netVersion = web3j.netVersion().send().getNetVersion();
        // 发送地址
        String fromAddress = "";
        // 接收地址
        String toAddress = "";
        Credentials credentials = Credentials.create("");

        EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();

        BigInteger nonce = ethGetTransactionCount.getTransactionCount();

        RawTransactionManager transactionManager = new RawTransactionManager(
                web3j, credentials, Long.parseLong(netVersion));
        RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
                nonce,
                // gas price
                Convert.toWei("0.1", Convert.Unit.GWEI).toBigInteger(),
                // gas limit
                BigInteger.valueOf(300000L),
                toAddress,
                // amount
                Convert.toWei("0.1", Convert.Unit.GWEI).toBigInteger());
        String sign = transactionManager.sign(rawTransaction);
        EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(sign).sendAsync().get();
        // 交易hash
        System.out.println(ethSendTransaction.getTransactionHash());

例外附上生成地址的代码:

        // 钱包存放路径
        String walletFilePath = "";
        // 钱包密码
        String password = "";
        //生成钱包,对应目录下会创建对应的私钥文件。
        String walletFileName = WalletUtils.generateNewWalletFile(password, new File(walletFilePath), false);

        // 加载指定位置的钱包
        Credentials credentials = WalletUtils.loadCredentials(password, walletFilePath + "/" + walletFileName);
        String address = credentials.getAddress();
        System.out.println("address:" + address);
        System.out.println("PrivateKey:" + credentials.getEcKeyPair().getPrivateKey().toString(16));
        System.out.println("PublicKey:" + credentials.getEcKeyPair().getPublicKey().toString(16));
上一篇下一篇

猜你喜欢

热点阅读