web3j转账

 web3 转账功能

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

  1、对方的以太坊地址

  2、确定要转账的金额

  3、自己地址的转账权限

  4、大于转账金额的以太币,以太币转账其实就是提出一个交易,矿工会帮你计算,计算完成即达成交易,但是矿工计算需要支付一定的费用(GAS),支付过少,计算转账就有可能很慢或者不成功。

 

  转账方式1:

  代码如下

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/<your-token>"));
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提供在线交易,但是这种交易需要parity钱包,将完整的区块链钱包下载下来,然后绑定账户进去。

 

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

2、https://kovan.etherscan.io/tx/0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304

这个地址是可以查到你的这笔交易的

0x88cb6e625b57cadd6d7f71872433c2e638014fca30e47c649f2831db79b54304是transactionHash

这个地址是测试地址,如果需要查询主网上的,删除kovan

 

转账方式2:

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/<your-token>"));
        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环境中没有以太币的话,可以到https://gitter.im/kovan-testnet/faucet这里去要,直接注册账号之后,把你的账号地址发到群里就行了,过几分钟就会给你转钱的,主网的账号地址和kovan是一样的,但是里面的币是不一样的。

 

如果想系统的学习java开发以太坊的话推荐一个很受欢迎的在线互动教程:
java以太坊,主要是介绍使用java围绕web3j库进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。

转载于:https://my.oschina.net/u/3837977/blog/1859230

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在uniapp中,可以通过调用web3.js库来实现拉起web3转账的功能。下面是一个简单的示例代码: 1. 首先,需要在uniapp项目中引入web3.js库。可以通过npm安装web3.js,然后在需要使用的页面中引入: ```javascript import Web3 from 'web3'; ``` 2. 在需要拉起web3转账的地方,可以创建一个web3实例,并连接到以太坊网络: ```javascript const web3 = new Web3(window.ethereum); ``` 3. 接下来,需要获取用户的授权来访问其以太坊账户。可以使用`ethereum.enable()`方法来请求用户授权: ```javascript await window.ethereum.enable(); ``` 4. 然后,可以使用web3实例来发送转账交易。以下是一个简单的示例: ```javascript const account = web3.eth.accounts[0]; // 获取当前用户的以太坊账户地址 const transaction = { from: account, to: '0x1234567890abcdef', // 目标地址 value: web3.utils.toWei('1', 'ether'), // 转账金额(以太为单位) }; web3.eth.sendTransaction(transaction) .on('transactionHash', function(hash){ console.log('Transaction hash:', hash); }) .on('receipt', function(receipt){ console.log('Transaction receipt:', receipt); }) .on('error', function(error){ console.error('Transaction error:', error); }); ``` 以上代码中,`from`字段表示转账发起者的以太坊账户地址,`to`字段表示目标地址,`value`字段表示转账金额。 需要注意的是,以上代码只是一个简单示例,实际使用时还需要进行错误处理、用户授权状态检查等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值