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

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

posted @   人艰不拆_zmc  阅读(1705)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-06-14 JavaScript基础总结三部曲之一
2014-06-14 poj1753Flip Game(dfs)
点击右上角即可分享
微信分享提示