当前位置:   article > 正文

【web3j】java调用链上合约里的方法_web3j 结构体decode解码

web3j 结构体decode解码

提前准备infura的链接链上合约地址,合约里的方法名钱包私钥(小狐狸插件里点账户地址右边的三个点,再点账户详情就看到了)。注:下面的示例调用了两个get方法,传参Uint

一、用到的包

  1. <dependency>
  2. <groupId>com.squareup.okhttp3</groupId>
  3. <artifactId>okhttp</artifactId>
  4. <version>4.3.1</version>
  5. </dependency>
  6. <!-- web3j -->
  7. <dependency>
  8. <groupId>org.web3j</groupId>
  9. <artifactId>core</artifactId>
  10. <version>4.8.7</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.web3j</groupId>
  14. <artifactId>codegen</artifactId>
  15. <version>5.0.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>commons-io</groupId>
  19. <artifactId>commons-io</artifactId>
  20. <version>2.4</version>
  21. </dependency>
  22. import org.web3j.abi.FunctionEncoder;
  23. import org.web3j.abi.FunctionReturnDecoder;
  24. import org.web3j.abi.TypeReference;
  25. import org.web3j.abi.datatypes.*;
  26. import org.web3j.protocol.Web3j;
  27. import org.web3j.protocol.core.DefaultBlockParameterName;
  28. import org.web3j.protocol.core.methods.request.Transaction;
  29. import org.web3j.protocol.core.methods.response.EthCall;
  30. import org.web3j.protocol.http.HttpService;
  31. import java.math.BigInteger;
  32. import java.util.Collections;
  33. import java.util.List;

二、代码

  1. /**
  2. * @author maomo
  3. * @version 1.0.0
  4. * @date 2022-10-31
  5. **/
  6. public class Web3Utils {
  7. static Web3j web3j = Web3j.build(new HttpService("https://goerli.infura.io/v3/***************"));
  8. /** 链上合约地址 */
  9. public static final String CONTRACT_ADDRESS = "***************";
  10. /** 没有拥有者时返回的地址 */
  11. public static String DEFAULT_OWNER = "0x0000000000000000000000000000000000000000";
  12. /** 方法名,通过tokenId获取ownerAddress(这是erc721自带的方法) */
  13. public static String OWNER_OF = "ownerOf";
  14. /** 方法名,通过nftId获取ownerAddress(这是合约里自定义的方法) */
  15. public static String GET_OWNER_BY_NFT_ID = "getOwnerByNFTId";
  16. public static void main(String[] args) {
  17. System.out.println("ownerOf:"+new Web3Utils().getOwnerByTokenId(1));
  18. System.out.println("getOwnerByNFTId:"+new Web3Utils().getOwnerByNftId(1));
  19. }
  20. /**
  21. * 调用合约的只读方法,无需gas
  22. * @param functionName 合约方法名
  23. * @param param 参数
  24. * @throws Exception 异常
  25. */
  26. public String getValue(String functionName,String param) {
  27. try {
  28. Function function = new Function(
  29. functionName,
  30. Collections.singletonList(new Uint(new BigInteger(param))),
  31. Collections.singletonList(new TypeReference<Address>() {
  32. }));
  33. String encodedFunction = FunctionEncoder.encode(function);
  34. EthCall response = web3j.ethCall(
  35. Transaction.createEthCallTransaction(null, contractAddress, encodedFunction),
  36. DefaultBlockParameterName.LATEST)
  37. .sendAsync().get();
  38. List<Type> results = FunctionReturnDecoder.decode(response.getValue(), function.getOutputParameters());
  39. Type type = results.get(0);
  40. String value = (String) type.getValue();
  41. if(ObjectUtils.isEmpty(value) || DEFAULT_OWNER.equals(value)){
  42. return null;
  43. }
  44. return (String)type.getValue();
  45. }catch (Exception ignore){
  46. return null;
  47. }
  48. }
  49. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/415697
推荐阅读
相关标签
  

闽ICP备14008679号