NFTService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.common.VMTypeEnum;
  6. import com.alipay.mychain.sdk.domain.transaction.LogEntry;
  7. import com.alipay.mychain.sdk.utils.ByteUtils;
  8. import com.antfinancial.mychain.baas.tool.restclient.RestClient;
  9. import com.antfinancial.mychain.baas.tool.restclient.RestClientProperties;
  10. import com.antfinancial.mychain.baas.tool.restclient.model.CallRestBizParam;
  11. import com.antfinancial.mychain.baas.tool.restclient.model.ClientParam;
  12. import com.antfinancial.mychain.baas.tool.restclient.model.Method;
  13. import com.antfinancial.mychain.baas.tool.restclient.model.ReceiptDecoration;
  14. import com.antfinancial.mychain.baas.tool.restclient.response.BaseResp;
  15. import com.izouma.nineth.config.Constants;
  16. import com.izouma.nineth.dto.NFT;
  17. import com.izouma.nineth.dto.NFTAccount;
  18. import com.izouma.nineth.exception.BusinessException;
  19. import com.izouma.nineth.utils.HashUtils;
  20. import com.izouma.nineth.utils.SnowflakeIdWorker;
  21. import lombok.AllArgsConstructor;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.retry.annotation.Backoff;
  24. import org.springframework.retry.annotation.Retryable;
  25. import org.springframework.stereotype.Service;
  26. import java.math.BigInteger;
  27. @Service
  28. @Slf4j
  29. @AllArgsConstructor
  30. public class NFTService {
  31. private final RestClient restClient;
  32. private final RestClientProperties restClientProperties;
  33. public NFTAccount createAccount(String username) {
  34. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  35. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  36. .bizid(Constants.bizId)
  37. .account(restClientProperties.getAccount())
  38. .mykmsKeyId(restClientProperties.getKmsId())
  39. .newAccountId(username)
  40. .newAccountKmsId(Constants.kmsKey)
  41. .method(Method.TENANTCREATEACCUNT)
  42. .gas(100000L).build();
  43. try {
  44. BaseResp baseResp = restClient.chainCallForBiz(callRestBizParam);
  45. NFTAccount account = new NFTAccount(username, Constants.kmsKey, baseResp.getData());
  46. if (baseResp.isSuccess()) {
  47. log.info("创建账户成功 {}", account);
  48. return account;
  49. } else {
  50. throw new RuntimeException(baseResp.getCode());
  51. }
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. log.error("创建账户失败", e);
  55. throw new BusinessException("创建账户失败");
  56. }
  57. }
  58. @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 5000), value = BusinessException.class)
  59. public NFT createToken(String toAccount) throws Exception {
  60. JSONArray jsonArray = new JSONArray();
  61. jsonArray.add(Utils.getIdentityByName(toAccount));
  62. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  63. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  64. .bizid(restClientProperties.getBizid())
  65. .account(restClientProperties.getAccount())
  66. .contractName(Constants.CONTRACT_NAME)
  67. .methodSignature("mint(identity)")
  68. .inputParamListStr(jsonArray.toJSONString())
  69. .outTypes("[]")//合约返回值类型
  70. .mykmsKeyId(restClientProperties.getKmsId())
  71. .method(Method.CALLCONTRACTBIZASYNC)
  72. .tenantid(restClientProperties.getTenantid())
  73. .gas(500000L)
  74. .build();
  75. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  76. if (!resp.isSuccess()) {
  77. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  78. }
  79. if ("200".equals(resp.getCode())) {
  80. log.info("EVM合约执行成功");
  81. // 合约调用交易回执内容
  82. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  83. BigInteger gasUsed = txReceipt.getGasUsed();
  84. long result = txReceipt.getResult();
  85. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  86. for (LogEntry logEntry : txReceipt.getLogs()) {
  87. if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
  88. String tokenId = logEntry.getTopics().get(3);
  89. txReceipt.getBlockNumber();
  90. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  91. log.info("NFT生成成功 {}", nft);
  92. return nft;
  93. }
  94. }
  95. } else {
  96. // 异步交易未成功需要根据状态码判断交易状态
  97. log.error("EVM合约执行未成功: " + resp.getCode());
  98. }
  99. throw new BusinessException("创建nft失败");
  100. }
  101. public NFT transferToken(String tokenId, String fromAccount, String toAccount) throws Exception {
  102. JSONArray jsonArray = new JSONArray();
  103. jsonArray.add(Utils.getIdentityByName(fromAccount));
  104. jsonArray.add(Utils.getIdentityByName(toAccount));
  105. jsonArray.add(Long.parseLong(tokenId, 16));
  106. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  107. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  108. .bizid(restClientProperties.getBizid())
  109. .account(restClientProperties.getAccount())
  110. .contractName(Constants.CONTRACT_NAME)
  111. .methodSignature("transferFrom(identity,identity,uint256)")
  112. .inputParamListStr(jsonArray.toJSONString())
  113. .outTypes("[]")//合约返回值类型
  114. .mykmsKeyId(restClientProperties.getKmsId())
  115. .method(Method.CALLCONTRACTBIZASYNC)
  116. .tenantid(restClientProperties.getTenantid())
  117. .gas(500000L)
  118. .build();
  119. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  120. if (!resp.isSuccess()) {
  121. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  122. }
  123. if ("200".equals(resp.getCode())) {
  124. log.info("EVM合约执行成功");
  125. // 合约调用交易回执内容
  126. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  127. BigInteger gasUsed = txReceipt.getGasUsed();
  128. long result = txReceipt.getResult();
  129. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  130. for (LogEntry logEntry : txReceipt.getLogs()) {
  131. if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
  132. String transferTokenId = logEntry.getTopics().get(3);
  133. txReceipt.getBlockNumber();
  134. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  135. log.info("NFT转移成功 {}", nft);
  136. return nft;
  137. }
  138. }
  139. } else {
  140. // 异步交易未成功需要根据状态码判断交易状态
  141. log.error("EVM合约执行未成功: " + resp.getCode());
  142. }
  143. throw new BusinessException("创建nft失败");
  144. }
  145. public NFT setApprovalForAll(String account) throws Exception {
  146. JSONArray jsonArray = new JSONArray();
  147. jsonArray.add(Utils.getIdentityByName(account));
  148. jsonArray.add(true);
  149. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  150. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  151. .bizid(restClientProperties.getBizid())
  152. .account(restClientProperties.getAccount())
  153. .contractName(Constants.CONTRACT_NAME)
  154. .methodSignature("setApprovalForAll(identity,bool)")
  155. .inputParamListStr(jsonArray.toJSONString())
  156. .outTypes("[]")//合约返回值类型
  157. .mykmsKeyId(restClientProperties.getKmsId())
  158. .method(Method.CALLCONTRACTBIZASYNC)
  159. .tenantid(restClientProperties.getTenantid())
  160. .gas(500000L)
  161. .build();
  162. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  163. if (!resp.isSuccess()) {
  164. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  165. }
  166. if ("200".equals(resp.getCode())) {
  167. log.info("EVM合约执行成功");
  168. // 合约调用交易回执内容
  169. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  170. BigInteger gasUsed = txReceipt.getGasUsed();
  171. long result = txReceipt.getResult();
  172. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  173. } else {
  174. // 异步交易未成功需要根据状态码判断交易状态
  175. log.error("EVM合约执行未成功: " + resp.getCode());
  176. }
  177. throw new BusinessException("创建nft失败");
  178. }
  179. public void deployContract() throws Exception {
  180. ClientParam clientParam = restClient.createDeployContractTransaction(
  181. restClientProperties.getDefaultAccount(),
  182. "rest_sol_test_2ghd2g2fr",
  183. ByteUtils.hexStringToBytes("6080604052606460005534801561001557600080fd5b50610128806100256000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631ab06ee514605857806360fe47b114608c5780636d4ce63c1460b6575b600080fd5b348015606357600080fd5b50608a600480360381019080803590602001909291908035906020019092919050505060de565b005b348015609757600080fd5b5060b46004803603810190808035906020019092919050505060e9565b005b34801560c157600080fd5b5060c860f3565b6040518082815260200191505060405180910390f35b816000819055505050565b8060008190555050565b600080549050905600a165627a7a723058205bcd66c88d325808b2a6429cba1e79b49c8a6cfa4190c9158d4b63a9030ea1d70029"),
  184. VMTypeEnum.EVM,
  185. 50000L);
  186. BaseResp resp = restClient.chainCall(
  187. clientParam.getHash(),
  188. clientParam.getSignData(),
  189. Method.DEPLOYCONTRACT);
  190. }
  191. }