|
|
@@ -1,16 +1,42 @@
|
|
|
package com.izouma.nineth.service;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alipay.mychain.sdk.api.utils.Utils;
|
|
|
+import com.alipay.mychain.sdk.domain.transaction.LogEntry;
|
|
|
+import com.antfinancial.mychain.baas.tool.restclient.RestClient;
|
|
|
+import com.antfinancial.mychain.baas.tool.restclient.RestClientProperties;
|
|
|
+import com.antfinancial.mychain.baas.tool.restclient.model.CallRestBizParam;
|
|
|
+import com.antfinancial.mychain.baas.tool.restclient.model.Method;
|
|
|
+import com.antfinancial.mychain.baas.tool.restclient.model.ReceiptDecoration;
|
|
|
+import com.antfinancial.mychain.baas.tool.restclient.response.BaseResp;
|
|
|
import com.izouma.nineth.ApplicationTests;
|
|
|
+import com.izouma.nineth.config.GeneralProperties;
|
|
|
+import com.izouma.nineth.dto.NFT;
|
|
|
+import com.izouma.nineth.utils.HashUtils;
|
|
|
+import com.izouma.nineth.utils.SnowflakeIdWorker;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.RandomStringUtils;
|
|
|
import org.junit.Test;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.math.BigInteger;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.util.Random;
|
|
|
import java.util.Scanner;
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
+@Slf4j
|
|
|
public class NFTServiceTest extends ApplicationTests {
|
|
|
@Autowired
|
|
|
- private NFTService nftService;
|
|
|
+ private NFTService nftService;
|
|
|
+ @Autowired
|
|
|
+ private RestClient restClient;
|
|
|
+ @Autowired
|
|
|
+ private RestClientProperties restClientProperties;
|
|
|
+ @Autowired
|
|
|
+ private GeneralProperties generalProperties;
|
|
|
|
|
|
@Test
|
|
|
public void createAccount() {
|
|
|
@@ -19,8 +45,51 @@ public class NFTServiceTest extends ApplicationTests {
|
|
|
|
|
|
@Test
|
|
|
public void createToken() throws Exception {
|
|
|
- for (int i = 0; i < 10; i++) {
|
|
|
- nftService.createToken("raex_official");
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ jsonArray.add(Utils.getIdentityByName("raex_official"));
|
|
|
+ Random random = ThreadLocalRandom.current();
|
|
|
+ byte[] r = new byte[32]; //Means 2048 bit
|
|
|
+ random.nextBytes(r);
|
|
|
+ MessageDigest m = MessageDigest.getInstance("MD5");
|
|
|
+ m.update(r, 0, r.length);
|
|
|
+ BigInteger bi = new BigInteger(1, m.digest());
|
|
|
+ jsonArray.add(bi.toString());
|
|
|
+ log.info("data {}", jsonArray.toJSONString());
|
|
|
+ CallRestBizParam callRestBizParam = CallRestBizParam.builder()
|
|
|
+ .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
|
|
|
+ .bizid(restClientProperties.getBizid())
|
|
|
+ .account(restClientProperties.getAccount())
|
|
|
+ .contractName("raex111")
|
|
|
+ .methodSignature("mint(identity,uint256)")
|
|
|
+ .inputParamListStr(jsonArray.toJSONString())
|
|
|
+ .outTypes("[]")//合约返回值类型
|
|
|
+ .mykmsKeyId(restClientProperties.getKmsId())
|
|
|
+ .method(Method.CALLCONTRACTBIZASYNC)
|
|
|
+ .tenantid(restClientProperties.getTenantid())
|
|
|
+ .gas(500000L)
|
|
|
+ .build();
|
|
|
+ BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
|
|
|
+ if (!resp.isSuccess()) {
|
|
|
+ log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
|
|
|
+ }
|
|
|
+ if ("200".equals(resp.getCode())) {
|
|
|
+ log.info("EVM合约执行成功");
|
|
|
+ // 合约调用交易回执内容
|
|
|
+ ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
|
|
|
+ BigInteger gasUsed = txReceipt.getGasUsed();
|
|
|
+ long result = txReceipt.getResult();
|
|
|
+ log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
|
|
|
+ for (LogEntry logEntry : txReceipt.getLogs()) {
|
|
|
+ if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
|
|
|
+ String tokenId = logEntry.getTopics().get(3);
|
|
|
+ txReceipt.getBlockNumber();
|
|
|
+ NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
|
|
|
+ log.info("NFT生成成功 {}", nft);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 异步交易未成功需要根据状态码判断交易状态
|
|
|
+ log.error("EVM合约执行未成功: " + resp.getCode());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -70,4 +139,44 @@ public class NFTServiceTest extends ApplicationTests {
|
|
|
|
|
|
nftService.deployContract("nine_space_test" + System.currentTimeMillis(), lines[idx + 2]);
|
|
|
}
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void ownerof() throws Exception {
|
|
|
+ CallRestBizParam callRestBizParam = CallRestBizParam.builder()
|
|
|
+ .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
|
|
|
+ .bizid(restClientProperties.getBizid())
|
|
|
+ .account(restClientProperties.getAccount())
|
|
|
+ .contractName("raex111")
|
|
|
+ .methodSignature("ownerOf(uint256)")
|
|
|
+ .inputParamListStr("[\"305596637601946315409962168553415519496\"]")
|
|
|
+ .outTypes("[identity]")//合约返回值类型
|
|
|
+ .mykmsKeyId(restClientProperties.getKmsId())
|
|
|
+ .method(Method.CALLCONTRACTBIZASYNC)
|
|
|
+ .tenantid(restClientProperties.getTenantid())
|
|
|
+ .gas(500000L)
|
|
|
+ .build();
|
|
|
+ BaseResp resp = restClient.bizChainCallWithReceipt(callRestBizParam);
|
|
|
+ if (!resp.isSuccess()) {
|
|
|
+ log.info("EVM合约执行失败: " + resp.getCode() + ", " + resp.getData());
|
|
|
+ }
|
|
|
+ if ("200".equals(resp.getCode())) {
|
|
|
+ log.info("EVM合约执行成功");
|
|
|
+ // 合约调用交易回执内容
|
|
|
+ ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
|
|
|
+ BigInteger gasUsed = txReceipt.getGasUsed();
|
|
|
+ long result = txReceipt.getResult();
|
|
|
+ log.info("EVM合约交易内容: 哈希 " + txReceipt.getHash() + ", 消耗燃料 " + gasUsed + ", 结果 " + result);
|
|
|
+ for (LogEntry logEntry : txReceipt.getLogs()) {
|
|
|
+ if (logEntry.getTopics().get(0).equals(HashUtils.Keccak256("Transfer(identity,identity,uint256)"))) {
|
|
|
+ String tokenId = logEntry.getTopics().get(3);
|
|
|
+ txReceipt.getBlockNumber();
|
|
|
+ NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
|
|
|
+ log.info("NFT生成成功 {}", nft);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 异步交易未成功需要根据状态码判断交易状态
|
|
|
+ log.error("EVM合约执行未成功: " + resp.getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|