maven: 直接上码: 无论是eth还是token都可以直接用了。直接上就行。 自己去试试就好了。 <dependency> <groupId>org.web3j</groupId> <artifactId>core</artifactId> <version>2.2.1</version> </dependency>
Map<String,String> result = new HashMap<>(); // BigInteger nonce = GetWeb3jConnection.getInstance().ethGetTransactionCount(mainAddress, DefaultBlockParameterName.PENDING) // .send().getTransactionCount(); ---这里获取最新的nonce String amountStr = qty.toString(); BigInteger amountWei = Convert.toWei(amountStr, Convert.Unit.ETHER).toBigInteger();//默认为18位小数 // amountWei = Convert.toWei(amountStr, Convert.Unit.MWEI).toBigInteger(); --看你币种的小数位为多少 //封装转账交易 Function function = new Function( "transfer", Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(toAddress), new org.web3j.abi.datatypes.generated.Uint256(amountWei)), Collections.<TypeReference<?>>emptyList()); //data内容 String data = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction(nonce,//nonce ERC20_GAS_PRICE, //GasPrice--自己设置自己喜欢的值, erc20GasLimit,//GasLimit--自己设置自己喜欢的值,必须大于21000 contractAddress,//合约地址 data);//inputdata,--如果是eth交易 直接设置为""就行 //手续费= (gasPrice * gasLimit ) / 10^18 ether --最大手续费 Credentials credentials = Credentials.create(privatekey);//加载凭证 byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, credentials);//对原始数据签名 String signMessageStr = Numeric.toHexString(signMessage);//封装签名 EthSendTransaction ethSendTransaction = GetWeb3jConnection. getInstance(). ethSendRawTransaction(signMessageStr).sendAsync().get();//发送交易 if(ethSendTransaction.hasError()){ log.error("transfer error:{},转入地址:{}", ethSendTransaction.getError().getMessage(),toAddress); throw new MyException("转账失败"); }else{ String txHashId = ethSendTransaction.getTransactionHash();//获取hash log.info("transfer Success:{}", txHashId); BigInteger gasPrice = ERC20_GAS_PRICE.divide(new BigInteger("10").pow(9));//拿到了Gwei价格 result.put("txHashId", txHashId); result.put("nonce", nonce +"");//这个交易的nonce result.put("gas", erc20GasLimit+"");//当前交易的gasLimit result.put("gasPrice", gasPrice+"");//当前交易的GasPrice return result; }
害怕的就先去infura注册一个链接,去Ropsten 领取测试币,以及发布一个erc20的代币就行(erc20Usdt的精准度为6, 其他基本都18)
喜欢点个赞 谢谢。
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算