NFTService.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.izouma.nineth.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alipay.mychain.sdk.api.utils.Utils;
  5. import com.alipay.mychain.sdk.domain.transaction.LogEntry;
  6. import com.antfinancial.mychain.baas.tool.restclient.RestClient;
  7. import com.antfinancial.mychain.baas.tool.restclient.RestClientProperties;
  8. import com.antfinancial.mychain.baas.tool.restclient.model.CallRestBizParam;
  9. import com.antfinancial.mychain.baas.tool.restclient.model.ClientParam;
  10. import com.antfinancial.mychain.baas.tool.restclient.model.Method;
  11. import com.antfinancial.mychain.baas.tool.restclient.model.ReceiptDecoration;
  12. import com.antfinancial.mychain.baas.tool.restclient.response.BaseResp;
  13. import com.izouma.nineth.config.Constants;
  14. import com.izouma.nineth.dto.NFT;
  15. import com.izouma.nineth.dto.NFTAccount;
  16. import com.izouma.nineth.exception.BusinessException;
  17. import com.izouma.nineth.utils.HashUtils;
  18. import com.izouma.nineth.utils.SnowflakeIdWorker;
  19. import lombok.AllArgsConstructor;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.stereotype.Service;
  22. import java.math.BigInteger;
  23. @Service
  24. @Slf4j
  25. @AllArgsConstructor
  26. public class NFTService {
  27. private final RestClient restClient;
  28. private final RestClientProperties restClientProperties;
  29. public NFTAccount createAccount(String username) {
  30. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  31. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  32. .bizid(Constants.bizId)
  33. .account(restClientProperties.getAccount())
  34. .mykmsKeyId(restClientProperties.getKmsId())
  35. .newAccountId(username)
  36. .newAccountKmsId(Constants.kmsKey)
  37. .method(Method.TENANTCREATEACCUNT)
  38. .gas(100000L).build();
  39. try {
  40. BaseResp baseResp = restClient.chainCallForBiz(callRestBizParam);
  41. NFTAccount account = new NFTAccount(username, Constants.kmsKey, baseResp.getData());
  42. if (baseResp.isSuccess()) {
  43. log.info("创建账户成功 {}", account);
  44. return account;
  45. } else {
  46. throw new RuntimeException(baseResp.getCode());
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. log.error("创建账户失败", e);
  51. throw new BusinessException("创建账户失败");
  52. }
  53. }
  54. public NFT createToken(String toAccount) throws Exception {
  55. JSONArray jsonArray = new JSONArray();
  56. jsonArray.add(Utils.getIdentityByName(toAccount));
  57. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  58. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  59. .bizid(restClientProperties.getBizid())
  60. .account(restClientProperties.getAccount())
  61. .contractName(Constants.CONTRACT_NAME)
  62. .methodSignature("mint(identity)")
  63. .inputParamListStr(jsonArray.toJSONString())
  64. .outTypes("[]")//合约返回值类型
  65. .mykmsKeyId(restClientProperties.getKmsId())
  66. .method(Method.CALLCONTRACTBIZASYNC)
  67. .tenantid(restClientProperties.getTenantid())
  68. .gas(500000L)
  69. .build();
  70. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  71. if (!resp.isSuccess()) {
  72. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  73. }
  74. if ("200".equals(resp.getCode())) {
  75. log.info("EVM合约执行成功");
  76. // 合约调用交易回执内容
  77. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  78. BigInteger gasUsed = txReceipt.getGasUsed();
  79. long result = txReceipt.getResult();
  80. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  81. for (LogEntry logEntry : txReceipt.getLogs()) {
  82. if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
  83. String tokenId = logEntry.getTopics().get(3);
  84. txReceipt.getBlockNumber();
  85. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  86. log.info("NFT生成成功 {}", nft);
  87. return nft;
  88. }
  89. }
  90. } else {
  91. // 异步交易未成功需要根据状态码判断交易状态
  92. log.error("EVM合约执行未成功: " + resp.getCode());
  93. }
  94. return null;
  95. }
  96. }