微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

依旧是基于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();

转自:https://blog.csdn.net/u010123087/article/details/79637260

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐