Commit 2d9bbf38 authored by tangtuo's avatar tangtuo

修改nft发行逻辑

parent 84fd3bfc
......@@ -14,6 +14,8 @@ public enum ResultCode implements IErrorCode {
TOKEN_VALID_ERROR(413, "token校验失败"),
CODE_ERROR(414, "验证码发送失败"),
PUBLISH_ERROR(415, "nft发行失败"),
RECEIVE_ERROR(416, "nft领取失败"),
TRANSFER_ERROR(416, "nft转让失败"),
;
......
......@@ -4,6 +4,7 @@ import cn.fzm.chain.simplesdk.client.ParaChainClient;
import cn.fzm.chain.simplesdk.constant.TxStatusEnum;
import cn.fzm.chain.simplesdk.model.TxResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.common.constant.RedisConstant;
......@@ -78,21 +79,23 @@ public class CommemorateNftServiceImpl extends ServiceImpl<CommemorateNftMapper,
long tokenId = Long.parseLong(split[1]);
// 构建上链信息
HashMap<String, String> map = new HashMap<>();
map.put("author", user.getNickname());
map.put("publishAddress", user.getWallet());
map.put("name", commemorateNft.getName());
map.put("author", user.getNickname());
map.put("hash", commemorateNft.getFileHash());
String tokenInfo = JsonUtil.toJson(map);
String tokenInfo = JSONUtil.toJsonStr(map);
log.info("加密上链数据: {}", tokenInfo);
String tradeHash = paraChainClient.evmSetTokenInfo(contractName, tokenId, tokenInfo, wallet, privkey, true);
if (StringUtils.isBlank(tradeHash)) {
throw GlobalException.newException(ResultCode.PUBLISH_ERROR, "nft发行失败");
}
String realHash = paraChainClient.getRealTxHashFromGrp(hash);
// 确认交易结果
TxResult txResult = paraChainClient.cycleConfirmTxWithHash(hash, true, 1000);
TxResult txResult = paraChainClient.cycleConfirmTxWithHash(realHash, false, 1000);
if (!TxStatusEnum.SUCCESS.equals(txResult.getStatus())) {
throw GlobalException.newException(ResultCode.PUBLISH_ERROR, txResult.getErrMsg().getValue());
}
commemorateNft.setNftHash(hash).setTokenId(tokenId).setPublishTime(new Date());
commemorateNft.setNftHash(realHash).setTokenId(tokenId).setPublishTime(new Date());
save(commemorateNft);
// 使用nft发行量作为Redisson信号量限制领取次数
RSemaphore semaphore = redisson.getSemaphore(RedisConstant.COMMEMORATE_NFT_PREFIX + commemorateNft.getId());
......@@ -131,8 +134,9 @@ public class CommemorateNftServiceImpl extends ServiceImpl<CommemorateNftMapper,
if (!TxStatusEnum.SUCCESS.equals(txResult.getStatus())) {
// 领取失败要释放信号量
semaphore.release();
throw GlobalException.newException(ResultCode.PUBLISH_ERROR, txResult.getErrMsg().getValue());
throw GlobalException.newException(ResultCode.RECEIVE_ERROR, txResult.getErrMsg().getValue());
}
String realHash = paraChainClient.getRealTxHashFromGrp(hash);
Nft nft = new Nft();
BeanUtil.copyProperties(commemorateNft, nft, true);
// 获取当前用户领取nft的领取顺序编号
......@@ -140,7 +144,7 @@ public class CommemorateNftServiceImpl extends ServiceImpl<CommemorateNftMapper,
// nft编号为BJIFF11+5为顺序编号 : BJIFF1100123
String code = "00000" + availablePermits;
nft.setNftId("BJIFF11" + code.substring(code.length() - 5));
nft.setTransferHash(hash);
nft.setTransferHash(realHash);
nft.setUserId(user.getId());
nft.setCreateDate(new Date());
nft.setUpdateDate(new Date());
......
......@@ -7,6 +7,7 @@ import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.common.constant.RedisConstant;
......@@ -103,31 +104,33 @@ public class NftServiceImpl extends ServiceImpl<NftMapper, Nft> implements NftSe
// 获取用户的私钥
String privkey = paraChainClient.walletDumpPrivkey(wallet);
// 发行nft
String txHash = paraChainClient.evmPublishNFT1155(contractName, wallet, privkey, 3, true);
String txHash = paraChainClient.evmPublishNFT1155(contractName, wallet, privkey, 1, true);
if (StringUtils.isBlank(txHash) || !txHash.contains("-")) {
throw GlobalException.newException(ResultCode.FAILED, "nft发行失败");
}
String[] split = txHash.split("-");
String hash = split[0];
long tokenId = Long.parseLong(split[1]);
String realHash = paraChainClient.getRealTxHashFromGrp(hash);
Nft nft = getById(nftDto.getId());
// 构建上链信息
HashMap<String, String> map = new HashMap<>();
map.put("author", nft.getAuthor());
map.put("name", nft.getName());
map.put("hash", nft.getFileHash());
String tokenInfo = JsonUtil.toJson(map);
map.put("publishAddress", nft.getPublishAddress());
String tokenInfo = JSONUtil.toJsonStr(map);
log.info("加密上链数据: {}", tokenInfo);
String tradeHash = paraChainClient.evmSetTokenInfo(contractName, tokenId, tokenInfo, wallet, privkey, true);
if (StringUtils.isBlank(tradeHash)) {
throw GlobalException.newException(ResultCode.PUBLISH_ERROR, "nft发行失败");
}
// 确认交易结果
TxResult txResult = paraChainClient.cycleConfirmTxWithHash(hash, true, 1000);
TxResult txResult = paraChainClient.cycleConfirmTxWithHash(realHash, false, 1000);
if (!TxStatusEnum.SUCCESS.equals(txResult.getStatus())) {
throw GlobalException.newException(ResultCode.PUBLISH_ERROR, txResult.getErrMsg().getValue());
}
nft.setNftHash(hash);
nft.setNftHash(realHash);
nft.setTokenId(tokenId);
nft.setPublishTime(new Date());
nft.setNftId(nftDto.getNftId());
......@@ -151,43 +154,47 @@ public class NftServiceImpl extends ServiceImpl<NftMapper, Nft> implements NftSe
// 转出人的个人信息
User user = userService.getUserByToken();
if (!nft.getUserId().equals(user.getId())) {
throw GlobalException.newException(ResultCode.FORBIDDEN, "您无权转让他人的nft");
throw GlobalException.newException(ResultCode.TRANSFER_ERROR, "您无权转让他人的nft");
}
// 校验短信验证码
// todo 现在用的是登录验证码模板,后续需要修改短信模板
if (!smsUtil.validateCode(smsProperties.getMessageLoginCodetype(), user.getTelephone(), param.getCode(), param.getCodeType())) {
throw GlobalException.newException(ResultCode.FAILED, "短信验证码校验失败");
throw GlobalException.newException(ResultCode.TRANSFER_ERROR, "短信验证码校验失败");
}
// 校验接收人的个人信息
User receiveUser = userService.getUserByWallet(param.getReceiveWallet());
if (receiveUser == null) {
throw GlobalException.newException(ResultCode.FAILED, "接手人非本系统注册用户,暂不支持转让");
throw GlobalException.newException(ResultCode.TRANSFER_ERROR, "接手人非本系统注册用户,暂不支持转让");
}
// 不能自己向自己转让nft
if (user.getWallet().equals(receiveUser.getWallet())) {
throw GlobalException.newException(ResultCode.FAILED, "不能向自己转让nft");
throw GlobalException.newException(ResultCode.TRANSFER_ERROR, "不能向自己转让nft");
}
// 转让nft
String hash = paraChainClient.evmTransferNFT1155(contractName, user.getWallet(), null, param.getReceiveWallet(), nft.getTokenId(), param.getCount(), true);
if (StringUtils.isBlank(hash)) {
throw GlobalException.newException(ResultCode.FAILED, "nft领取失败");
throw GlobalException.newException(ResultCode.TRANSFER_ERROR, "nft领取失败");
}
// 确认转让结果
TxResult txResult = paraChainClient.cycleConfirmTxWithHash(hash, true, 1000);
if (!TxStatusEnum.SUCCESS.equals(txResult.getStatus())) {
throw GlobalException.newException(ResultCode.PUBLISH_ERROR, txResult.getErrMsg().getValue());
throw GlobalException.newException(ResultCode.TRANSFER_ERROR, txResult.getErrMsg().getValue());
}
String realHash = paraChainClient.getRealTxHashFromGrp(hash);
if (StringUtils.isBlank(realHash)) {
throw GlobalException.newException(ResultCode.TRANSFER_ERROR);
}
// 修改nft的拥有者用户id
Nft transferNft = new Nft().
setUserId(receiveUser.getId()).
setTransferHash(hash);
setTransferHash(realHash);
transferNft.setId(nft.getId());
updateById(transferNft);
// 保存nft的转让记录
NftTransferRecord record = new NftTransferRecord();
record.setNftId(nft.getId()).
setNftHash(nft.getNftHash()).
setTransferHash(hash).
setTransferHash(realHash).
setFromAddress(user.getWallet()).
setToAddress(receiveUser.getWallet());
nftTransferRecordService.save(record);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment