链上数据交易模板

   请一定要注意下面三点:

//1.必要情况下可以提高gasPrice 5%-30%左右(做到可以随时动态调整)
    //2.nonce需要自己内部维护一份(可以采用redis去维护,每个转账账号维护自己对应的nonce,每次交易成功自增+1,多服情况下记得要加锁处理)
    //3.转账账号不要只设置一个,需要要多个账号去实现交易转账(防止链上大量交易排队的情况)

下面是简单例子(未去实现上面注意的三点,自己手动业务处理下即可)

/**NFT批量转账
     * @param wallet
     * @param headIds
     * @param privateKey
     * @param contractAddress
     * @param coinRpcUrl
     * @param chainId
     * @return
     * @throws Exception
     */
    public static String safeBatchTransferFromForNft(String from,String to,JSONArray headIds, String privateKey,String contractAddress, String coinRpcUrl,long chainId) throws Exception {
        Web3j web3j = null;
        OkHttpClient okHttpClient = null;
        String  str = null;
        try {
            okHttpClient = OKHttpUtil.createOkHttpClient();
            web3j = Web3j.build(new HttpService(coinRpcUrl, okHttpClient));
            //Uint256[] list=new Uint256[headIds.size()];
            List<Uint256> li=new java.util.ArrayList<Uint256>();
            for(int i=0;i<headIds.size();i++){
            //    list[i]=new Uint256(new BigInteger(headIds.get(i)+""));
                li.add(new Uint256(new BigInteger(headIds.get(i)+"")));
            }
            BigInteger nonce;
            EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(from,
                    DefaultBlockParameterName.PENDING).send();
            if (ethGetTransactionCount == null) {
                return null;
            }
            nonce = ethGetTransactionCount.getTransactionCount();
            BigInteger gasPrice;
            EthGasPrice ethGasPrice = web3j.ethGasPrice().sendAsync().get();
            if (ethGasPrice == null) {
                return null;
            }
            // 加载转账所需的凭证,用私钥
            Credentials credentials = Credentials.create(privateKey);
            
            gasPrice = ethGasPrice.getGasPrice();

            DynamicArray<Uint256> array = new DynamicArray<Uint256>(Uint256.class , li);

            Function function = new Function(BtTradeConstant.safeBatchTransferFromForNft, Arrays.asList(new Address(from),new Address(to),array,DynamicBytes.DEFAULT), Arrays.asList(new TypeReference<Type>() {
            }));
            String encodedFunction = FunctionEncoder.encode(function);
            
            //获取gas
            Transaction transaction = Transaction.createEthCallTransaction(from, contractAddress, encodedFunction);
            EthEstimateGas ethEstimateGas=web3j.ethEstimateGas(transaction).sendAsync().get();
            BigInteger  gas=BigInteger.valueOf(0L);
            if(ethEstimateGas.getError()==null){
                 gas=ethEstimateGas.getAmountUsed();
            }else{
                throw new Exception(ethEstimateGas.getError().getMessage());
            }
         
          // BigInteger gasLimit =BigInteger.valueOf(4000000L);
            BigInteger gasLimit=gas.multiply(BigInteger.valueOf(2L));
            
            RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit,contractAddress, encodedFunction);
            // 签名Transaction
            byte[] signMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
            String hexValue = Numeric.toHexString(signMessage);
            // 发送交易
            EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
            logger.warn("================safeBatchTransferFromForNft结果==" + JSON.toJSONString(ethSendTransaction));
            if(ethSendTransaction.getError()==null){
                str = ethSendTransaction.getTransactionHash();
            }else{
                throw new Exception(ethSendTransaction.getError().getMessage());
            }
        }  catch(Exception e){
            logger.error("================safeBatchTransferFromForNft异常==",e);
            throw new Exception(e.getMessage());
        } finally {
            if (web3j != null) {
                web3j.shutdown();
            }
        }
        return str;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值