|
|
@@ -17,6 +17,8 @@ import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
+import org.springframework.retry.annotation.Backoff;
|
|
|
+import org.springframework.retry.annotation.Retryable;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
@@ -31,9 +33,10 @@ public class AssetMintService {
|
|
|
private NFTService nftService;
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
- @Async
|
|
|
- public void mint(Long assetId, Long userId) {
|
|
|
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+ @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 1000))
|
|
|
+ public void mint(Long assetId) {
|
|
|
+ Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("asset不存在"));
|
|
|
+ User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
|
|
|
if (StringUtils.isEmpty(user.getPublicKey())) {
|
|
|
NFTAccount account = nftService.createAccount(user.getUsername() + "_");
|
|
|
user.setNftAccount(account.getAccountId());
|
|
|
@@ -42,7 +45,6 @@ public class AssetMintService {
|
|
|
userRepo.save(user);
|
|
|
}
|
|
|
try {
|
|
|
- Asset asset = assetRepo.findById(assetId).orElseThrow(new BusinessException("asset不存在"));
|
|
|
NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
|
|
|
if (nft != null) {
|
|
|
asset.setTokenId(nft.getTokenId());
|
|
|
@@ -62,33 +64,33 @@ public class AssetMintService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @Async
|
|
|
- public void mint(Asset asset) {
|
|
|
- User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
|
|
|
- if (StringUtils.isEmpty(user.getPublicKey())) {
|
|
|
- NFTAccount account = nftService.createAccount(user.getUsername() + "_");
|
|
|
- user.setNftAccount(account.getAccountId());
|
|
|
- user.setKmsId(account.getAccountKmsId());
|
|
|
- user.setPublicKey(account.getPublicKey());
|
|
|
- userRepo.save(user);
|
|
|
- }
|
|
|
- try {
|
|
|
- NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
|
|
|
- if (nft != null) {
|
|
|
- asset.setTokenId(nft.getTokenId());
|
|
|
- asset.setBlockNumber(nft.getBlockNumber());
|
|
|
- asset.setTxHash(nft.getTxHash());
|
|
|
- asset.setGasUsed(nft.getGasUsed());
|
|
|
- if (asset.getIpfsUrl() == null) {
|
|
|
- asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
|
|
|
- }
|
|
|
- assetRepo.save(asset);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("铸造失败", e);
|
|
|
- }
|
|
|
- applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
|
|
|
- }
|
|
|
+// @Async
|
|
|
+// public void mint(Asset asset) {
|
|
|
+// User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+// if (StringUtils.isEmpty(user.getPublicKey())) {
|
|
|
+// NFTAccount account = nftService.createAccount(user.getUsername() + "_");
|
|
|
+// user.setNftAccount(account.getAccountId());
|
|
|
+// user.setKmsId(account.getAccountKmsId());
|
|
|
+// user.setPublicKey(account.getPublicKey());
|
|
|
+// userRepo.save(user);
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// NFT nft = nftService.createToken(user.getNftAccount(), asset.getTokenId());
|
|
|
+// if (nft != null) {
|
|
|
+// asset.setTokenId(nft.getTokenId());
|
|
|
+// asset.setBlockNumber(nft.getBlockNumber());
|
|
|
+// asset.setTxHash(nft.getTxHash());
|
|
|
+// asset.setGasUsed(nft.getGasUsed());
|
|
|
+// if (asset.getIpfsUrl() == null) {
|
|
|
+// asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
|
|
|
+// }
|
|
|
+// assetRepo.save(asset);
|
|
|
+// }
|
|
|
+// } catch (Exception e) {
|
|
|
+// log.error("铸造失败", e);
|
|
|
+// }
|
|
|
+// applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
|
|
|
+// }
|
|
|
|
|
|
public String ipfsUpload(String url) {
|
|
|
try {
|