NFTServiceTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.Method;
  10. import com.antfinancial.mychain.baas.tool.restclient.model.ReceiptDecoration;
  11. import com.antfinancial.mychain.baas.tool.restclient.response.BaseResp;
  12. import com.izouma.nineth.ApplicationTests;
  13. import com.izouma.nineth.config.GeneralProperties;
  14. import com.izouma.nineth.dto.NFT;
  15. import com.izouma.nineth.utils.HashUtils;
  16. import com.izouma.nineth.utils.SnowflakeIdWorker;
  17. import com.izouma.nineth.utils.TokenUtils;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.codec.binary.Hex;
  20. import org.apache.commons.lang3.RandomStringUtils;
  21. import org.junit.Test;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import java.io.File;
  24. import java.math.BigInteger;
  25. import java.util.Scanner;
  26. @Slf4j
  27. public class NFTServiceTest extends ApplicationTests {
  28. @Autowired
  29. private NFTService nftService;
  30. @Autowired
  31. private RestClient restClient;
  32. @Autowired
  33. private RestClientProperties restClientProperties;
  34. @Autowired
  35. private GeneralProperties generalProperties;
  36. @Test
  37. public void createAccount() {
  38. nftService.createAccount(RandomStringUtils.randomAlphabetic(8));
  39. }
  40. @Test
  41. public void createToken() throws Exception {
  42. JSONArray jsonArray = new JSONArray();
  43. jsonArray.add(Utils.getIdentityByName("raex_official"));
  44. String s = TokenUtils.genTokenId();
  45. jsonArray.add(new BigInteger(s, 16).toString());
  46. log.info("data {}", jsonArray.toJSONString());
  47. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  48. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  49. .bizid(restClientProperties.getBizid())
  50. .account(restClientProperties.getAccount())
  51. .contractName(generalProperties.getContractName())
  52. .methodSignature("mint(identity,uint256)")
  53. .inputParamListStr(jsonArray.toJSONString())
  54. .outTypes("[]")//合约返回值类型
  55. .mykmsKeyId(restClientProperties.getKmsId())
  56. .method(Method.CALLCONTRACTBIZASYNC)
  57. .tenantid(restClientProperties.getTenantid())
  58. .gas(500000L)
  59. .build();
  60. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  61. if (!resp.isSuccess()) {
  62. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  63. }
  64. if ("200".equals(resp.getCode())) {
  65. log.info("EVM合约执行成功");
  66. // 合约调用交易回执内容
  67. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  68. BigInteger gasUsed = txReceipt.getGasUsed();
  69. long result = txReceipt.getResult();
  70. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  71. for (LogEntry logEntry : txReceipt.getLogs()) {
  72. if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
  73. String tokenId = logEntry.getTopics().get(3);
  74. txReceipt.getBlockNumber();
  75. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  76. log.info("NFT生成成功 {}", nft);
  77. }
  78. }
  79. } else {
  80. // 异步交易未成功需要根据状态码判断交易状态
  81. log.error("EVM合约执行未成功: " + resp.getCode());
  82. }
  83. }
  84. @Test
  85. public void testCreateToken() {
  86. }
  87. @Test
  88. public void transferToken() throws Exception {
  89. nftService.transferToken("000000000000000000000000000000000000000000000000000000000000009d",
  90. "9th_BHlKkGWw", "9th_test");
  91. }
  92. @Test
  93. public void setApprovalForAll() throws Exception {
  94. nftService.setApprovalForAll("9th_test");
  95. }
  96. @Test
  97. public void deployContract() throws Exception {
  98. ProcessBuilder builder = new ProcessBuilder("/Users/drew/Projects/Java/9th/src/main/contract/mychain_solc", "--bin", "9th.sol");
  99. builder.directory(new File("/Users/drew/Projects/Java/9th/src/main/contract").getAbsoluteFile());
  100. Process process = builder.start();
  101. Scanner s = new Scanner(process.getInputStream());
  102. StringBuilder text = new StringBuilder();
  103. while (s.hasNextLine()) {
  104. text.append(s.nextLine());
  105. text.append("\n");
  106. }
  107. s.close();
  108. int result = process.waitFor();
  109. String[] lines = text.toString().split("\n");
  110. int idx = 0;
  111. for (int i = 0; i < lines.length; i++) {
  112. if ("======= 9th.sol:ERC721PresetMinterPauserAutoId =======".equals(lines[i])) {
  113. idx = i;
  114. break;
  115. }
  116. }
  117. System.out.println(lines[idx]);
  118. System.out.println(lines[idx + 2]);
  119. nftService.deployContract("nine_space_test" + System.currentTimeMillis(), lines[idx + 2]);
  120. }
  121. @Test
  122. public void ownerof() throws Exception {
  123. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  124. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  125. .bizid(restClientProperties.getBizid())
  126. .account(restClientProperties.getAccount())
  127. .contractName("raex12")
  128. .methodSignature("ownerOf(uint256)")
  129. .inputParamListStr("[\"95057808871064671354760012409081314299\"]")
  130. .outTypes("[identity]")//合约返回值类型
  131. .mykmsKeyId(restClientProperties.getKmsId())
  132. .method(Method.CALLCONTRACTBIZASYNC)
  133. .tenantid(restClientProperties.getTenantid())
  134. .gas(500000L)
  135. .build();
  136. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  137. if (!resp.isSuccess()) {
  138. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  139. }
  140. if ("200".equals(resp.getCode())) {
  141. log.info("EVM合约执行成功");
  142. // 合约调用交易回执内容
  143. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  144. BigInteger gasUsed = txReceipt.getGasUsed();
  145. long result = txReceipt.getResult();
  146. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  147. log.info("owner:{}", Hex.encodeHexString(txReceipt.getOutput()));
  148. for (LogEntry logEntry : txReceipt.getLogs()) {
  149. if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
  150. String tokenId = logEntry.getTopics().get(3);
  151. txReceipt.getBlockNumber();
  152. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  153. log.info("NFT生成成功 {}", nft);
  154. }
  155. }
  156. } else {
  157. // 异步交易未成功需要根据状态码判断交易状态
  158. log.error("EVM合约执行未成功: " + resp.getCode());
  159. }
  160. }
  161. @Test
  162. public void transfer() throws Exception {
  163. JSONArray jsonArray = new JSONArray();
  164. jsonArray.add(Utils.getIdentityByName("raex_official"));
  165. jsonArray.add(Utils.getIdentityByName("9th_HCWWflAZ_"));
  166. jsonArray.add("95057808871064671354760012409081314299");
  167. CallRestBizParam callRestBizParam = CallRestBizParam.builder()
  168. .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
  169. .bizid(restClientProperties.getBizid())
  170. .account(restClientProperties.getAccount())
  171. .contractName("raex12")
  172. .methodSignature("ownerTransfer(identity,identity,uint256)")
  173. .inputParamListStr(jsonArray.toJSONString())
  174. .outTypes("[]")//合约返回值类型
  175. .mykmsKeyId(restClientProperties.getKmsId())
  176. .method(Method.CALLCONTRACTBIZASYNC)
  177. .tenantid(restClientProperties.getTenantid())
  178. .gas(500000L)
  179. .build();
  180. BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
  181. if (!resp.isSuccess()) {
  182. log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
  183. }
  184. if ("200".equals(resp.getCode())) {
  185. log.info("EVM合约执行成功");
  186. // 合约调用交易回执内容
  187. ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
  188. BigInteger gasUsed = txReceipt.getGasUsed();
  189. long result = txReceipt.getResult();
  190. log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
  191. log.info("owner:{}", Hex.encodeHexString(txReceipt.getOutput()));
  192. for (LogEntry logEntry : txReceipt.getLogs()) {
  193. if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
  194. String tokenId = logEntry.getTopics().get(3);
  195. txReceipt.getBlockNumber();
  196. NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
  197. log.info("NFT生成成功 {}", nft);
  198. }
  199. }
  200. } else {
  201. // 异步交易未成功需要根据状态码判断交易状态
  202. log.error("EVM合约执行未成功: " + resp.getCode());
  203. }
  204. }
  205. }