| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- 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.common.VMTypeEnum;
- import com.alipay.mychain.sdk.domain.transaction.LogEntry;
- import com.alipay.mychain.sdk.utils.ByteUtils;
- 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.ClientParam;
- 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.Constants;
- 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.springframework.retry.annotation.Backoff;
- import org.springframework.retry.annotation.Retryable;
- import org.springframework.stereotype.Service;
- import java.math.BigInteger;
- @Service
- @Slf4j
- @AllArgsConstructor
- public class NFTService {
- private final RestClient restClient;
- private final RestClientProperties restClientProperties;
- public NFTAccount createAccount(String username) {
- CallRestBizParam callRestBizParam = CallRestBizParam.builder()
- .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
- .bizid(Constants.bizId)
- .account(restClientProperties.getAccount())
- .mykmsKeyId(restClientProperties.getKmsId())
- .newAccountId(username)
- .newAccountKmsId(Constants.kmsKey)
- .method(Method.TENANTCREATEACCUNT)
- .gas(100000L).build();
- try {
- BaseResp baseResp = restClient.chainCallForBiz(callRestBizParam);
- NFTAccount account = new NFTAccount(username, Constants.kmsKey, baseResp.getData());
- if (baseResp.isSuccess()) {
- log.info("创建账户成功 {}", account);
- return account;
- } else {
- throw new RuntimeException(baseResp.getCode());
- }
- } catch (Exception e) {
- e.printStackTrace();
- log.error("创建账户失败", e);
- throw new BusinessException("创建账户失败");
- }
- }
- @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 5000), value = BusinessException.class)
- public NFT createToken(String toAccount) throws Exception {
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(Utils.getIdentityByName(toAccount));
- CallRestBizParam callRestBizParam = CallRestBizParam.builder()
- .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
- .bizid(restClientProperties.getBizid())
- .account(restClientProperties.getAccount())
- .contractName(Constants.CONTRACT_NAME)
- .methodSignature("mint(identity)")
- .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);
- return nft;
- }
- }
- } else {
- // 异步交易未成功需要根据状态码判断交易状态
- log.error("EVM合约执行未成功: " + resp.getCode());
- }
- throw new BusinessException("创建nft失败");
- }
- 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));
- CallRestBizParam callRestBizParam = CallRestBizParam.builder()
- .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
- .bizid(restClientProperties.getBizid())
- .account(restClientProperties.getAccount())
- .contractName(Constants.CONTRACT_NAME)
- .methodSignature("transferFrom(identity,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 transferTokenId = logEntry.getTopics().get(3);
- txReceipt.getBlockNumber();
- NFT nft = new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
- log.info("NFT转移成功 {}", nft);
- return nft;
- }
- }
- } else {
- // 异步交易未成功需要根据状态码判断交易状态
- log.error("EVM合约执行未成功: " + resp.getCode());
- }
- throw new BusinessException("创建nft失败");
- }
- public NFT setApprovalForAll(String account) throws Exception {
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(Utils.getIdentityByName(account));
- jsonArray.add(true);
- CallRestBizParam callRestBizParam = CallRestBizParam.builder()
- .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
- .bizid(restClientProperties.getBizid())
- .account(restClientProperties.getAccount())
- .contractName(Constants.CONTRACT_NAME)
- .methodSignature("setApprovalForAll(identity,bool)")
- .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);
- } else {
- // 异步交易未成功需要根据状态码判断交易状态
- log.error("EVM合约执行未成功: " + resp.getCode());
- }
- throw new BusinessException("创建nft失败");
- }
- public void deployContract() throws Exception {
- ClientParam clientParam = restClient.createDeployContractTransaction(
- restClientProperties.getDefaultAccount(),
- "rest_sol_test_2ghd2g2fr",
- ByteUtils.hexStringToBytes("6080604052606460005534801561001557600080fd5b50610128806100256000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631ab06ee514605857806360fe47b114608c5780636d4ce63c1460b6575b600080fd5b348015606357600080fd5b50608a600480360381019080803590602001909291908035906020019092919050505060de565b005b348015609757600080fd5b5060b46004803603810190808035906020019092919050505060e9565b005b34801560c157600080fd5b5060c860f3565b6040518082815260200191505060405180910390f35b816000819055505050565b8060008190555050565b600080549050905600a165627a7a723058205bcd66c88d325808b2a6429cba1e79b49c8a6cfa4190c9158d4b63a9030ea1d70029"),
- VMTypeEnum.EVM,
- 50000L);
- BaseResp resp = restClient.chainCall(
- clientParam.getHash(),
- clientParam.getSignData(),
- Method.DEPLOYCONTRACT);
- }
- }
|