java使⽤web3J进⾏代币转账、余额查询
publicstaticvoidmain(String[]args){
/*(,args);
("启动成功");*/
//转出地址
Stringfrom="0x9175F9EcBbddC078e40C5e037AD31F4abf36628a";
//转⼊地址
Stringto="0xa7B049d3A19796B195B0e976ec43EB7a12f07Bf9";
//转⼊数量
Stringvalue="5";
//转出地址私钥
StringprivateKey="";
//合约地址
StringcontractAddress="0x57E0297510fA155eF165646c208C495E563B3342";
//位数,根据合约⾥⾯的来
intdecimal=4;
tokenDeal(from,to,value,privateKey,contractAddress,decimal);
}
publicstaticStringtokenDeal(Stringfrom,Stringto,Stringvalue,StringprivateKey,StringcontractAddress,intdecimal){
Web3jweb3j=(newHttpService("/v3/9aa3d95b3bc440fa88ea12eaa4456161"));
try{
//转账的凭证,需要传⼊私钥
Credentialscredentials=(privateKey);
//获取交易笔数
BigIntegernonce;
EthGetTransactionCountethGetTransactionCount=TransactionCount(from,G).nd();
if(ethGetTransactionCount==null){
returnnull;
}
nonce=nsactionCount();
//⼿续费
BigIntegergasPrice;
EthGasPriceethGasPrice=Price().ndAsync().get();
if(ethGasPrice==null){
returnnull;
returnnull;
}
gasPrice=Price();
//注意⼿续费的设置,这块很容易遇到问题
BigIntegergasLimit=f(60000L);
BigIntegerval=newBigDecimal(value).multiply(newBigDecimal("10").pow(decimal)).toBigInteger();//单位换算
Functionfunction=newFunction(
"transfer",
(newAddress(to),newUint256(val)),
tonList(newTypeReference
}));
//创建交易对象
StringencodedFunction=(function);
RawTransactionrawTransaction=Transaction(nonce,gasPrice,gasLimit,
contractAddress,encodedFunction);
//进⾏签名操作
byte[]signMessage=ssage(rawTransaction,credentials);
StringhexValue=tring(signMessage);
//发起交易
EthSendTransactionethSendTransaction=dRawTransaction(hexValue).ndAsync().get();
Stringhash=nsactionHash();
if(hash!=null){
//执⾏业务
("执⾏成功:"+hash);
returnhash;
}
}catch(Exceptionex){
//报错应进⾏错误处理
tackTrace();
}
returnnull;
}
查询余额
publicstaticvoidmain(String[]args){
//查询的钱包地址
Stringfrom="0x9175F9EcBbddC078e40C5e037AD31F4abf36628a";
//合约地址
StringcontractAddress="0x57E0297510fA155eF165646c208C495E563B3342";
//合约部署节点
Web3jweb3j=(newHttpService("/v3/9aa3d95b3bc440fa88ea12eaa4456161"));
try{
Stringcode=getERC20Balance(web3j,from,contractAddress);
("查询出来的余额:"+code);
}catch(ExecutionExceptione){
tackTrace();
}catch(InterruptedExceptione){
tackTrace();
}
}
privatestaticfinalBigDecimalWEI=newBigDecimal(10000);
/**
*获取ERC-20token指定地址余额
*
*@paramaddress查询地址
*@paramcontractAddress合约地址
*@return
*@throwsExecutionException
*@throwsInterruptedException
*/
publicstaticStringgetERC20Balance(Web3jweb3j,Stringaddress,StringcontractAddress)throwsExecutionException,InterruptedException{
StringmethodName="balanceOf";
List
List
AddressfromAddress=newAddress(address);
(fromAddress);
TypeReference
};
(typeReference);
Functionfunction=newFunction(methodName,inputParameters,outputParameters);
Stringdata=(function);
Transactiontransaction=EthCallTransaction(address,contractAddress,data);
EthCallethCall;
BigDecimalbalanceValue=;
try{
ethCall=l(transaction,).nd();
List
Integervalue=0;
if(results!=null&&()>0){
value=nt(f((0).getValue()));
}
balanceValue=newBigDecimal(value).divide(WEI,6,_DOWN);
}catch(IOExceptione){
tackTrace();
}
ng();
}
本文发布于:2023-03-04 00:38:29,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1677861509129206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:查询余额.doc
本文 PDF 下载地址:查询余额.pdf
留言与评论(共有 0 条评论) |