范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

以太坊web3j开发常用代码片段

  获取账户的Noncepublic static BigInteger getNonce(Web3j web3j, String addr) {  try {      EthGetTransactionCount getNonce = web3j.ethGetTransactionCount(addr,DefaultBlockParameterName.PENDING).send();       if (getNonce == null){                 throw new RuntimeException("net error");             }             return getNonce.getTransactionCount();         } catch (IOException e) {             throw new RuntimeException("net error");         }     }
  获取ETH余额public static BigDecimal getBalance(Web3j web3j, String address) {         try {             EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();             return Convert.fromWei(new BigDecimal(ethGetBalance.getBalance()),Convert.Unit.ETHER);         } catch (IOException e) {             e.printStackTrace();             return null;         }     }
  获取代币余额 方法一public static BigInteger getTokenBalance(Web3j web3j, String fromAddress, String contractAddress) {         String methodName = "balanceOf";         List inputParameters = new ArrayList<>();         List> outputParameters = new ArrayList<>();         Address address = new Address(fromAddress);         inputParameters.add(address);          TypeReference typeReference = new TypeReference() {         };         outputParameters.add(typeReference);         Function function = new Function(methodName, inputParameters, outputParameters);         String data = FunctionEncoder.encode(function);         Transaction transaction = Transaction.createEthCallTransaction(fromAddress, contractAddress, data);          EthCall ethCall;         BigInteger balanceValue = BigInteger.ZERO;         try {             ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();             List results = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());             balanceValue = (BigInteger) results.get(0).getValue();         } catch (IOException e) {             e.printStackTrace();         }         return balanceValue;     }
  获取代币余额 方法二 (仅支持主链上的代币)String tokenBanceUrl =  "https://api.etherscan.io/api?module=account&action=tokenbalance"      + "&contractaddress=0x5aA8D6dE8CBf23DAC734E6f904B93bD056B15b81"//Token合约地址      + "&address=0xd4279e30e27f52ca60fac3cc9670c7b9b1eeefdc"//要查询额的账户余地址      + "&tag=latest&apikey=YourApiKeyToken";    String result = HttpRequestUtil.sendGet(tokenBanceUrl, "");   TokenBalanceResult tokenBalanceResult = JSON.parseObject(result, TokenBalanceResult.class);   System.out.println(tokenBalanceResult.toString());   if (tokenBalanceResult.getStatus() == 1) {    BigDecimal tokenCount = new BigDecimal(tokenBalanceResult.getResult())      .pide(new BigDecimal(10).pow(FinalValue.TOKEN_DECIMALS));    return tokenCount.floatValue();   }构造交易// 构造eth交易 Transaction transaction = Transaction.createEtherTransaction(fromAddr, nonce, gasPrice, null, toAddr, value); // 构造合约调用交易 Transaction transaction = Transaction.createFunctionCallTransaction(fromAddr, nonce, gasPrice, null, contractAddr, funcABI);估算gasLimitpublic static BigInteger getTransactionGasLimit(Web3j web3j, Transaction transaction) {         try {             EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send();             if (ethEstimateGas.hasError()){                 throw new RuntimeException(ethEstimateGas.getError().getMessage());             }             return ethEstimateGas.getAmountUsed();         } catch (IOException e) {             throw new RuntimeException("net error");         }     }转账ETHpublic static String transferETH(Web3j web3j, String fromAddr, String privateKey, String toAddr, BigDecimal amount, String data){         // 获得nonce         BigInteger nonce = getNonce(web3j, fromAddr);         // value 转换         BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();          // 构建交易         Transaction transaction = Transaction.createEtherTransaction(fromAddr, nonce, gasPrice, null, toAddr, value);         // 计算gasLimit         BigInteger gasLimit = getTransactionGasLimit(web3j, transaction);          // 查询调用者余额,检测余额是否充足         BigDecimal ethBalance = getBalance(web3j, fromAddr);         BigDecimal balance = Convert.toWei(ethBalance, Convert.Unit.ETHER);         // balance < amount + gasLimit ??         if (balance.compareTo(amount.add(new BigDecimal(gasLimit.toString()))) < 0) {             throw new RuntimeException("余额不足,请核实");         }          return signAndSend(web3j, nonce, gasPrice, gasLimit, toAddr, value, data, chainId, privateKey);     }转账代币public static String transferToken(Web3j web3j, String fromAddr, String privateKey, String toAddr, String contractAddr, long amount) {          BigInteger nonce = getNonce(web3j, fromAddr);         // 构建方法调用信息         String method = "transfer";          // 构建输入参数         List inputArgs = new ArrayList<>();         inputArgs.add(new Address(toAddr));         inputArgs.add(new Uint256(BigDecimal.valueOf(amount).multiply(BigDecimal.TEN.pow(18)).toBigInteger()));          // 合约返回值容器         List> outputArgs = new ArrayList<>();          String funcABI = FunctionEncoder.encode(new Function(method, inputArgs, outputArgs));          Transaction transaction = Transaction.createFunctionCallTransaction(fromAddr, nonce, gasPrice, null, contractAddr, funcABI);         RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, null, contractAddr, null, funcABI);          BigInteger gasLimit = getTransactionGasLimit(web3j, transaction);          // 获得余额         BigDecimal ethBalance = getBalance(web3j, fromAddr);         BigInteger tokenBalance = getTokenBalance(web3j, fromAddr, contractAddr);         BigInteger balance = Convert.toWei(ethBalance, Convert.Unit.ETHER).toBigInteger();          if (balance.compareTo(gasLimit) < 0) {             throw new RuntimeException("手续费不足,请核实");         }         if (tokenBalance.compareTo(BigDecimal.valueOf(amount).toBigInteger()) < 0) {             throw new RuntimeException("代币不足,请核实");         }          return signAndSend(web3j, nonce, gasPrice, gasLimit, contractAddr, BigInteger.ZERO, funcABI, chainId, privateKey);     }转账代币方法二public static String transferToken2(String fromAddr,String toAddr,String amount) {   String contractAddress = "0xa22c2217e785f7796c9e8826c6be55c2e481f9f5";   Web3j web3j =MyWalletUtils.getWeb3j();      Credentials credentials = MyWalletUtils.getCredentials();      System.out.println("我的钱包地址:"+credentials.getAddress());   try {    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(      fromAddr, DefaultBlockParameterName.LATEST).sendAsync().get();      BigInteger nonce = ethGetTransactionCount.getTransactionCount();      Function function = new Function(              "transfer",              Arrays.asList(new Address(toAddr), new Uint256(new BigInteger(amount))),               Arrays.asList(new TypeReference() {              }));      String encodedFunction = FunctionEncoder.encode(function);      RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei("18", Convert.Unit.GWEI).toBigInteger(),              Convert.toWei("100000", Convert.Unit.WEI).toBigInteger(), contractAddress, encodedFunction);      byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);      String hexValue = Numeric.toHexString(signedMessage);      System.out.println("transfer hexValue:" + hexValue);      EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();      if (ethSendTransaction.hasError()) {       System.out.println("transfer error:"+ ethSendTransaction.getError().getMessage());       return ethSendTransaction.getError().getMessage();      } else {          String transactionHash = ethSendTransaction.getTransactionHash();         return "";      }   } catch (Exception e) {    e.printStackTrace();    return e.getMessage();   }  }对交易签名,并发送交易public static String signAndSend(Web3j web3j, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String to, BigInteger value, String data, byte chainId, String privateKey) {         String txHash = "";         RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, to, value, data);         if (privateKey.startsWith("0x")){             privateKey = privateKey.substring(2);         }          ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));         Credentials credentials = Credentials.create(ecKeyPair);          byte[] signMessage;         // 主网是1 responst测试网是3  具体查看ChainId         if (chainId > ChainId.NONE){             signMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);         } else {             signMessage = TransactionEncoder.signMessage(rawTransaction, credentials);         }          String signData = Numeric.toHexString(signMessage);         if (!"".equals(signData)) {             try {                 EthSendTransaction send = web3j.ethSendRawTransaction(signData).send();                 txHash = send.getTransactionHash();                 System.out.println(JSON.toJSONString(send));             } catch (IOException e) {                 throw new RuntimeException("交易异常");             }         }         return txHash;     }获取token代理额度public static BigInteger getAllowanceBalance(Web3j web3j, String fromAddr, String toAddr, String contractAddress) {         String methodName = "allowance";         List inputParameters = new ArrayList<>();         inputParameters.add(new Address(fromAddr));         inputParameters.add(new Address(toAddr));          List> outputs = new ArrayList<>();         TypeReference typeReference = new TypeReference() {         };         outputs.add(typeReference);          Function function = new Function(methodName, inputParameters, outputs);         String data = FunctionEncoder.encode(function);         Transaction transaction = Transaction.createEthCallTransaction(fromAddr, contractAddress, data);         EthCall ethCall;         BigInteger balanceValue = BigInteger.ZERO;         try {             ethCall = web3j.ethCall(transaction, DefaultBlockParameterName.LATEST).send();             List result = FunctionReturnDecoder.decode(ethCall.getValue(), function.getOutputParameters());             balanceValue = (BigInteger) result.get(0).getValue();         } catch (IOException e) {             e.printStackTrace();         }         return balanceValue;     }

使用QThreadPool多线程PyQt6应用程序在不影响UI的情况下并发运行后台任务在构建PythonGUI应用程序时,一个常见的问题是在试图执行长时间运行的后台任务时锁定接口。在本教程中,我将介绍在PyQt6中实现并发执行的最你知道图结构数据有哪些?5分钟讲明白我们知道,只要是能够被序列数据表示的物品,都可以通过Item2vec方法训练出Embedding。但是,互联网的数据可不仅仅是序列数据那么简单,越来越多的数据被我们以图的形式展现出贵州推动煤炭产业结构战略性调整中国经济导报中国发展网记者吴承坤2022年12月13日,贵州省推动煤炭产业结构战略性调整实施方案(以下简称)正式印发。实施方案主要是锚定2025年2030年两个关键时间节点目标,通中国两轮电动车群雄纷争三巨头格局形成,龙头豪夺销量五连冠电动车有四个轮子的,也有三轮的,但更多是两轮的。市场调研报告给出的数据显示,全国纯电动汽车保有量在1000万辆左右,而两轮电动车保有量多达3。5亿辆,这也说明绝大多数人出行靠的是小春节成都周边怎么玩,看看达人们的崇州路线(四)到崇州的古镇中去!今日推介达人尔玛蔷薇小红书旅行博主生活及探店博主多平台旅行达人作为一枚私人订制导游,成都周边两日游我真的要好好推荐一下崇州啦既要清净人少又好玩儿有趣既要美食美景又路途适中真建议姐妹时尚摄影师CharlotteNavio作品欣赏夏洛特纳维奥1991年出生于马赛。她在法国南部度过了她的青年时代,在普罗旺斯地区艾克斯学习造型艺术,并于2012年加入巴黎的Gobelins形象学校。她十几岁时就开始在南方拍摄她的卡贴机解锁注意事项千万不要申请和你手机运营商不一致eSIM卡!开始之前不要被一些网络上面的手机贩子带节奏,什么年后会出黑解,ICCID解锁马上会来到等等,还给你苹果手机的销量,销量下滑苹果都会开黑解,数码盖饭多年的卡贴机从业经验和直觉黑解大概高端真成了!小米13销量口碑双丰收,跃升国产手机第一位近年来基本所有国产手机厂商,目标都是冲击高端市场。不过提到高端旗舰手机时,以前很多人首先想到的都是苹果华为。然而现在不一样了,小米13凭借着全能的配置与超高的颜值手感,一举成为了高索尼PS5不能长期竖向放置?维修时才可能出现问题IT之家1月16日消息,根据官方的说法,索尼PlayStation5和XboxSeriesXS都可以垂直或水平放置,但最近一则报道引起了玩家的恐慌,那就是有维修店主建议玩家不要长期想五年不换手机,可考虑这4款12256G手机,堪称行业最佳很多人使用手机,不到三年就顶不住了,不是变卡了就是没内存了,很难用到五年以上,所以我建议大家现在换手机可以选择性能强内存大的手机。下面给大家分享的这4款12256G手机就很合适,配OPPOFindX6系列再曝标准版或配1。5K屏幕目前,OPPOFindN2系列手机已经正式发布,其推出了OPPOFindN2OPPOFindN2Flip两款不同折叠方案的设备。而随着这两款折叠新机的正式亮相,不少用户也将关注集中
狂飙带火了强盛集团,法人竟是孙红雷?回应纯属巧合近日,热播剧狂飙中的强盛集团引起网友关注,有网友发现,现实中的强盛集团法人为孙红雷。对此,山东强盛集团工作人员回应称,网友的发现属于巧合,我方法人孙红雷与演员孙红雷不是同一人。企查公安部春节假期全国道路交通安全形势平稳本报北京1月27日电(记者张天培)记者从公安部获悉春节期间(截至1月27日18时),全国道路交通安全形势总体平稳,一次死亡3人以上较大道路交通事故起数较去年春节假期同比下降33,未300亿演员吴京,能否再创辉煌,突破1000亿大关?头条创作挑战赛随着流浪地球2热播之后,吴京迎来了他职业巅峰的突破,主演和导演的电影票房突破300亿直逼400亿,远超第二名沈腾和第三名黄渤,成为中国电影史上的一个里程碑。能够取得如新春走基层丨回乡见闻高速时代来临,豫西四县交界古镇期待新蝶变点蓝字关注,不迷路编者按新春佳节来临,证券时报记者奔赴祖国四面八方,以系列报道的方式,追寻新春的气息,记录春天的故事,带您一起感受烟火升腾里中国经济的强劲活力。在连续错过三个春节后痛心坚守!卓尔老总10年前就感叹足协太黑,被李铁挖坑很无奈世界上任何事情都是环环相扣的,当你觉察一丝猫腻时,狠心反转才是最佳选择,否则再长期的坚守都会带来霉运。武汉长江足球俱乐部的解散看似是部苦情戏,实则早已是迹象环生,若不是卓尔高层的坚太大意了!中国女乒小将决赛被判无奈输球,颁奖环节直接黑脸乒乓球卡塔尔站支线赛已经落下了帷幕,最终国乒大获全胜,包揽了男单女单混双女双和男双5项冠军,值得称赞。不过,小将蒯曼在颁奖环节却黑脸了,欲知详情,且听第六人分解女单决赛,蒯曼和何卓在江西38万彩礼算不算高?本人江西赣州人89年生,在2022年年中的时候经同事介绍了一个女生,87年离异未带娃。我加了她微信聊天,一开始聊的好好的后来聊到彩礼她说要20万我就没聊了。本人家境不怎么好付不起这大年初七,开工大吉!今天,大年初七,传说是人类的诞辰日,即人的生日,民间把这天叫做人日节或人胜节,反映了人们祈福纳吉的愿望和对人本身的尊重。人丁兴旺人寿年丰人才辈出人杰地灵,古往今来,人们对于自身的绵外媒中国消费复苏势头强劲据香港经济日报网站1月27日报道,27日是中国内地春节连假最后一天,旅游平台企业携程称,在疫情以来首个无需就地过年的春节,该平台的国内外旅行订单皆迎来三年巅峰,整体比去年增长逾4倍新春走基层美丽乡村村民参与建,文化古村焕新颜白墙,灰瓦,石板路,丛丛绿竹绕宅生。走进五莲县于里镇小窑村,古色古香的气息扑面而来,一幅宜居宜业和美乡村的画卷展现在眼前。原来村庄道路坑洼不平,抬眼不远处就能看到断壁残垣,也没有像慧小媛下厨给他们做了这个!前几天,主播李戈陈祺宋琨已经轮番上阵,秀厨艺,送祝福。这回,主播慧媛化身厨娘,架势十足。慧小媛变身小厨娘,为他们送上多福小丸子1hr慧媛变身小厨娘多福小丸子上桌主播慧媛这段时间,有