|
|
@@ -3,22 +3,21 @@ 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.config.GeneralProperties;
|
|
|
import com.izouma.nineth.config.Constants;
|
|
|
+import com.izouma.nineth.config.GeneralProperties;
|
|
|
import com.izouma.nineth.dto.NFT;
|
|
|
import com.izouma.nineth.dto.NFTAccount;
|
|
|
import com.izouma.nineth.exception.BusinessException;
|
|
|
-import com.izouma.nineth.utils.HashUtils;
|
|
|
import com.izouma.nineth.utils.SnowflakeIdWorker;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.codec.binary.Hex;
|
|
|
import org.springframework.retry.annotation.Backoff;
|
|
|
import org.springframework.retry.annotation.Retryable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -60,15 +59,16 @@ public class NFTService {
|
|
|
}
|
|
|
|
|
|
@Retryable(maxAttempts = 100, backoff = @Backoff(delay = 5000), value = BusinessException.class)
|
|
|
- public synchronized NFT createToken(String toAccount) throws Exception {
|
|
|
+ public synchronized NFT createToken(String toAccount, String tokenId) throws Exception {
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
jsonArray.add(Utils.getIdentityByName(toAccount));
|
|
|
+ jsonArray.add(new BigInteger(tokenId, 16));
|
|
|
CallRestBizParam callRestBizParam = CallRestBizParam.builder()
|
|
|
.orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
|
|
|
.bizid(restClientProperties.getBizid())
|
|
|
.account(restClientProperties.getAccount())
|
|
|
.contractName(generalProperties.getContractName())
|
|
|
- .methodSignature("mint(identity)")
|
|
|
+ .methodSignature("mint(identity,uint256)")
|
|
|
.inputParamListStr(jsonArray.toJSONString())
|
|
|
.outTypes("[]")//合约返回值类型
|
|
|
.mykmsKeyId(restClientProperties.getKmsId())
|
|
|
@@ -87,15 +87,9 @@ public class NFTService {
|
|
|
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);
|
|
|
- return nft;
|
|
|
- }
|
|
|
- }
|
|
|
+ NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
|
|
|
+ log.info("NFT生成成功 {}", nft);
|
|
|
+ return nft;
|
|
|
} else {
|
|
|
// 异步交易未成功需要根据状态码判断交易状态
|
|
|
log.error("EVM合约执行未成功: " + resp.getCode());
|
|
|
@@ -103,19 +97,52 @@ public class NFTService {
|
|
|
throw new BusinessException("创建nft失败");
|
|
|
}
|
|
|
|
|
|
+ public String ownerOf(String tokenId) throws Exception {
|
|
|
+ CallRestBizParam callRestBizParam = CallRestBizParam.builder()
|
|
|
+ .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
|
|
|
+ .bizid(restClientProperties.getBizid())
|
|
|
+ .account(restClientProperties.getAccount())
|
|
|
+ .contractName(generalProperties.getContractName())
|
|
|
+ .methodSignature("ownerOf(uint256)")
|
|
|
+ .inputParamListStr("[" + new BigInteger(tokenId, 16) + "]")
|
|
|
+ .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);
|
|
|
+ String identity = Hex.encodeHexString(txReceipt.getOutput());
|
|
|
+ log.info("owner is: {}", identity);
|
|
|
+ return identity;
|
|
|
+ } else {
|
|
|
+ // 异步交易未成功需要根据状态码判断交易状态
|
|
|
+ log.error("EVM合约执行未成功: " + resp.getCode());
|
|
|
+ throw new BusinessException("EVM合约执行未成功: " + resp.getCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
public NFT transferToken(String tokenId, String fromAccount, String toAccount) throws Exception {
|
|
|
JSONArray jsonArray = new JSONArray();
|
|
|
- jsonArray.add(Utils.getIdentityByName(fromAccount));
|
|
|
- jsonArray.add(Utils.getIdentityByName(toAccount));
|
|
|
- jsonArray.add(Long.parseLong(tokenId, 16));
|
|
|
-
|
|
|
+ jsonArray.add(Utils.getIdentityByName("raex_official"));
|
|
|
+ jsonArray.add(Utils.getIdentityByName("9th_HCWWflAZ_"));
|
|
|
+ jsonArray.add("95057808871064671354760012409081314299");
|
|
|
CallRestBizParam callRestBizParam = CallRestBizParam.builder()
|
|
|
.orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
|
|
|
.bizid(restClientProperties.getBizid())
|
|
|
.account(restClientProperties.getAccount())
|
|
|
- .contractName(generalProperties.getContractName())
|
|
|
- .methodSignature("transferFrom(identity,identity,uint256)")
|
|
|
+ .contractName("raex12")
|
|
|
+ .methodSignature("ownerTransfer(identity,identity,uint256)")
|
|
|
.inputParamListStr(jsonArray.toJSONString())
|
|
|
.outTypes("[]")//合约返回值类型
|
|
|
.mykmsKeyId(restClientProperties.getKmsId())
|
|
|
@@ -134,20 +161,12 @@ public class NFTService {
|
|
|
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 transferTokenId = logEntry.getTopics().get(3);
|
|
|
- txReceipt.getBlockNumber();
|
|
|
- NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
|
|
|
- log.info("NFT转移成功 {}", nft);
|
|
|
- return nft;
|
|
|
- }
|
|
|
- }
|
|
|
+ return new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
|
|
|
} else {
|
|
|
// 异步交易未成功需要根据状态码判断交易状态
|
|
|
log.error("EVM合约执行未成功: " + resp.getCode());
|
|
|
}
|
|
|
- throw new BusinessException("创建nft失败");
|
|
|
+ throw new BusinessException("转移token失败");
|
|
|
}
|
|
|
|
|
|
public NFT setApprovalForAll(String account) throws Exception {
|