以太坊 钱包 转账 查询指定代币余额

前一篇讲了下怎么生成钱包  现在再来说一下如何进行转账 查指定代币余额 

依旧是基于web3j的   好了直接上代码 

//建立连接

Admin web3j = AdminFactory.build(new HttpService("你自己站点的地址"));

//获取指定钱包的比特币余额

BigInteger integer=web3j.ethGetBalance(“钱包地址”,DefaultBlockParameterName.LATEST).send().getBalance();

//获取指定钱包的指定币种余额

value=web3j.ethCall(Transaction.createEthCallTransaction(“钱包地址”,”代币地址”, “交易串”),DefaultBlockParameterName.PENDING).send().getValue();

//交易串的获取应该是这么做的 这么做的话需要去阅读以太坊源码 并找到方法的名称

//所以我直接写死了"0x70a08231000000000000000000000000cb1bf954b73031918a58f001c3c3e7fb66daaf7c"

//前边几位是固定写死的 后边的cb1bf954b73031918a58f001c3c3e7fb66daaf7c 是要查询余额的钱包地址   只需要替换下

//地址就可以了

Function function = new Function(
        "查询余额方法名称",
        Arrays.asList(new Address(“钱包地址”)),
        Arrays.asList(new TypeReference<Address>(){})
);
交易串= FunctionEncoder.encode(function);

//获取NONCE
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
        fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get();
//交易的发起者在之前进行过的交易数量
BigInteger nonce = ethGetTransactionCount.getTransactionCount();

//创建交易  注意金额 保留小数点后8位 要转化为整数 比如0.00000001 转化为1
Function function = new Function(
        "transfer",//交易的方法名称  
        Arrays.asList(new Address("收款钱包地址"),new Uint256("金额")),
        Arrays.asList(new TypeReference<Address>(){},new TypeReference<Uint256>(){})
);
String encodedFunction = FunctionEncoder.encode(function);
//智能合约事物
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Constants.GAS_PRICE, Constants.GAS_LIMIT,"代币地址",encodedFunction);
//通过私钥获取凭证  当然也可以根据其他的获取 其他方式详情请看web3j
Credentials credentials = Credentials.create("私钥");

byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
//发送事务
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
//事物的HASH
String transactionHash = ethSendTransaction.getTransactionHash();

到此 钱包的转账就完成了   好了 就这些  代码写的比较渣 所以没有传github 请见谅

如果转载请标明出处 谢谢合作






猜你喜欢

转载自blog.csdn.net/u010123087/article/details/79637260
今日推荐