NFTService.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.antfinancial.mychain.baas.tool.restclient.RestClient;
  6. import com.antfinancial.mychain.baas.tool.restclient.RestClientProperties;
  7. import com.antfinancial.mychain.baas.tool.restclient.model.CallRestBizParam;
  8. import com.antfinancial.mychain.baas.tool.restclient.model.Method;
  9. import com.antfinancial.mychain.baas.tool.restclient.model.ReceiptDecoration;
  10. import com.antfinancial.mychain.baas.tool.restclient.response.BaseResp;
  11. import com.izouma.nineth.config.Constants;
  12. import com.izouma.nineth.config.GeneralProperties;
  13. import com.izouma.nineth.dto.NFT;
  14. import com.izouma.nineth.dto.NFTAccount;
  15. import com.izouma.nineth.exception.BusinessException;
  16. import com.izouma.nineth.utils.SnowflakeIdWorker;
  17. import lombok.AllArgsConstructor;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.codec.binary.Hex;
  20. import org.springframework.retry.annotation.Backoff;
  21. import org.springframework.retry.annotation.Retryable;
  22. import org.springframework.stereotype.Service;
  23. import java.math.BigInteger;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. @Service
  27. @Slf4j
  28. @AllArgsConstructor
  29. public class NFTService {
  30. private final RestClient restClient;
  31. private final RestClientProperties restClientProperties;
  32. private final GeneralProperties generalProperties;
  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 if ("400123".equals(baseResp.getCode())) {
  50. return account;
  51. } else {
  52. throw new RuntimeException(baseResp.getCode());
  53. }
  54. } catch (Exception e) {
  55. e.printStackTrace();
  56. log.error("创建账户失败", e);
  57. throw new BusinessException("创建账户失败");
  58. }
  59. }
  60. @Retryable(maxAttempts = 100, backoff = @Backoff(delay = 5000), value = BusinessException.class)
  61. public synchronized NFT createToken(String toAccount, String tokenId) throws Exception {
  62. JSONArray jsonArray = new JSONArray();
  63. jsonArray.add(Utils.getIdentityByName(toAccount));
  64. jsonArray.add(new BigInteger(tokenId, 16));
  65. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  66. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  67. .bizid(restClientProperties.getBizid())
  68. .account(restClientProperties.getAccount())
  69. .contractName(generalProperties.getContractName())
  70. .methodSignature("mint(identity,uint256)")
  71. .inputParamListStr(jsonArray.toJSONString())
  72. .outTypes("[]")//合约返回值类型
  73. .mykmsKeyId(restClientProperties.getKmsId())
  74. .method(Method.CALLCONTRACTBIZASYNC)
  75. .tenantid(restClientProperties.getTenantid())
  76. .gas(500000L)
  77. .build();
  78. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  79. if (!resp.isSuccess()) {
  80. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  81. }
  82. if ("200".equals(resp.getCode())) {
  83. log.info("EVM合约执行成功");
  84. // 合约调用交易回执内容
  85. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  86. BigInteger gasUsed = txReceipt.getGasUsed();
  87. long result = txReceipt.getResult();
  88. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  89. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  90. log.info("NFT生成成功 {}", nft);
  91. return nft;
  92. } else {
  93. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  94. String out = new String(matchAndReplaceNonEnglishChar(txReceipt.getOutput()));
  95. // 异步交易未成功需要根据状态码判断交易状态
  96. log.error("EVM合约执行未成功: {}, {}", resp.getCode(), out);
  97. if (out.endsWith("ERC721: token already minted")) {
  98. return new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  99. }
  100. }
  101. throw new BusinessException("创建nft失败");
  102. }
  103. public static byte[] matchAndReplaceNonEnglishChar(byte[] data) {
  104. List<Byte> list = new ArrayList<>();
  105. for (byte c : data) {
  106. if (c >= 32 && c <= 127) {
  107. list.add(c);
  108. }
  109. }
  110. byte[] bytes = new byte[list.size()];
  111. for (int i = 0; i < list.size(); i++) {
  112. bytes[i] = list.get(i);
  113. }
  114. return bytes;
  115. }
  116. public String ownerOf(String tokenId) throws Exception {
  117. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  118. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  119. .bizid(restClientProperties.getBizid())
  120. .account(restClientProperties.getAccount())
  121. .contractName(generalProperties.getContractName())
  122. .methodSignature("ownerOf(uint256)")
  123. .inputParamListStr("[" + new BigInteger(tokenId, 16) + "]")
  124. .outTypes("[identity]")//合约返回值类型
  125. .mykmsKeyId(restClientProperties.getKmsId())
  126. .method(Method.CALLCONTRACTBIZASYNC)
  127. .tenantid(restClientProperties.getTenantid())
  128. .gas(500000L)
  129. .build();
  130. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  131. if (!resp.isSuccess()) {
  132. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  133. }
  134. if ("200".equals(resp.getCode())) {
  135. log.info("EVM合约执行成功");
  136. // 合约调用交易回执内容
  137. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  138. BigInteger gasUsed = txReceipt.getGasUsed();
  139. long result = txReceipt.getResult();
  140. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  141. String identity = Hex.encodeHexString(txReceipt.getOutput());
  142. log.info("owner is: {}", identity);
  143. return identity;
  144. } else {
  145. // 异步交易未成功需要根据状态码判断交易状态
  146. log.error("EVM合约执行未成功: " + resp.getCode());
  147. throw new BusinessException("EVM合约执行未成功: " + resp.getCode());
  148. }
  149. }
  150. public NFT transferToken(String tokenId, String fromAccount, String toAccount) throws Exception {
  151. JSONArray jsonArray = new JSONArray();
  152. jsonArray.add(Utils.getIdentityByName("raex_official"));
  153. jsonArray.add(Utils.getIdentityByName("9th_HCWWflAZ_"));
  154. jsonArray.add("95057808871064671354760012409081314299");
  155. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  156. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  157. .bizid(restClientProperties.getBizid())
  158. .account(restClientProperties.getAccount())
  159. .contractName("raex12")
  160. .methodSignature("ownerTransfer(identity,identity,uint256)")
  161. .inputParamListStr(jsonArray.toJSONString())
  162. .outTypes("[]")//合约返回值类型
  163. .mykmsKeyId(restClientProperties.getKmsId())
  164. .method(Method.CALLCONTRACTBIZASYNC)
  165. .tenantid(restClientProperties.getTenantid())
  166. .gas(500000L)
  167. .build();
  168. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  169. if (!resp.isSuccess()) {
  170. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  171. }
  172. if ("200".equals(resp.getCode())) {
  173. log.info("EVM合约执行成功");
  174. // 合约调用交易回执内容
  175. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  176. BigInteger gasUsed = txReceipt.getGasUsed();
  177. long result = txReceipt.getResult();
  178. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  179. return new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  180. } else {
  181. // 异步交易未成功需要根据状态码判断交易状态
  182. log.error("EVM合约执行未成功: " + resp.getCode());
  183. }
  184. throw new BusinessException("转移token失败");
  185. }
  186. public NFT setApprovalForAll(String account) throws Exception {
  187. JSONArray jsonArray = new JSONArray();
  188. jsonArray.add(Utils.getIdentityByName(account));
  189. jsonArray.add(true);
  190. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  191. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  192. .bizid(restClientProperties.getBizid())
  193. .account(restClientProperties.getAccount())
  194. .contractName(generalProperties.getContractName())
  195. .methodSignature("setApprovalForAll(identity,bool)")
  196. .inputParamListStr(jsonArray.toJSONString())
  197. .outTypes("[]")//合约返回值类型
  198. .mykmsKeyId(restClientProperties.getKmsId())
  199. .method(Method.CALLCONTRACTBIZASYNC)
  200. .tenantid(restClientProperties.getTenantid())
  201. .gas(500000L)
  202. .build();
  203. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  204. if (!resp.isSuccess()) {
  205. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  206. }
  207. if ("200".equals(resp.getCode())) {
  208. log.info("EVM合约执行成功");
  209. // 合约调用交易回执内容
  210. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  211. BigInteger gasUsed = txReceipt.getGasUsed();
  212. long result = txReceipt.getResult();
  213. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  214. } else {
  215. // 异步交易未成功需要根据状态码判断交易状态
  216. log.error("EVM合约执行未成功: " + resp.getCode());
  217. }
  218. throw new BusinessException("创建nft失败");
  219. }
  220. public void deployContract(String name, String code) throws Exception {
  221. String orderId = "order_" + System.currentTimeMillis();
  222. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  223. .orderId(orderId)
  224. .bizid(restClientProperties.getBizid())
  225. .account(restClientProperties.getAccount())
  226. .contractName(name)
  227. .contractCode(code)
  228. .mykmsKeyId(restClientProperties.getKmsId())
  229. .method(Method.DEPLOYCONTRACTFORBIZ)
  230. .tenantid(restClientProperties.getTenantid())
  231. .gas(1000000L).build();
  232. BaseResp resp = restClient.chainCallForBiz(callRestBizParam);
  233. if (!resp.isSuccess()) {
  234. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  235. }
  236. log.info("EVM合约执行成功");
  237. // 合约调用交易回执内容
  238. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  239. BigInteger gasUsed = txReceipt.getGasUsed();
  240. long result = txReceipt.getResult();
  241. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  242. }
  243. }