|
@@ -39,6 +39,8 @@ import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
+import org.springframework.scheduling.annotation.AsyncResult;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.ui.Model;
|
|
import org.springframework.ui.Model;
|
|
|
|
|
|
|
@@ -49,6 +51,7 @@ import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@@ -69,16 +72,18 @@ public class AssetService {
|
|
|
private WxPayProperties wxPayProperties;
|
|
private WxPayProperties wxPayProperties;
|
|
|
private WxPayService wxPayService;
|
|
private WxPayService wxPayService;
|
|
|
private Environment env;
|
|
private Environment env;
|
|
|
|
|
+ private AssetMintService assetMintService;
|
|
|
|
|
|
|
|
public Page<Asset> all(PageQuery pageQuery) {
|
|
public Page<Asset> all(PageQuery pageQuery) {
|
|
|
return assetRepo.findAll(JpaUtils.toSpecification(pageQuery, Asset.class), JpaUtils.toPageRequest(pageQuery));
|
|
return assetRepo.findAll(JpaUtils.toSpecification(pageQuery, Asset.class), JpaUtils.toPageRequest(pageQuery));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Asset createAsset(Collection collection, User user, Long orderId, BigDecimal price, String type) {
|
|
|
|
|
|
|
+ @Async
|
|
|
|
|
+ public Future<Asset> createAsset(Collection collection, User user, Long orderId, BigDecimal price, String type) {
|
|
|
Asset asset = Asset.create(collection, user);
|
|
Asset asset = Asset.create(collection, user);
|
|
|
asset.setOrderId(orderId);
|
|
asset.setOrderId(orderId);
|
|
|
asset.setPrice(price);
|
|
asset.setPrice(price);
|
|
|
- asset.setIpfsUrl(ipfsUpload(collection.getPic().get(0).getUrl()));
|
|
|
|
|
|
|
+ asset.setIpfsUrl(assetMintService.ipfsUpload(collection.getPic().get(0).getUrl()));
|
|
|
assetRepo.save(asset);
|
|
assetRepo.save(asset);
|
|
|
|
|
|
|
|
TokenHistory tokenHistory = tokenHistoryRepo.save(TokenHistory.builder()
|
|
TokenHistory tokenHistory = tokenHistoryRepo.save(TokenHistory.builder()
|
|
@@ -90,15 +95,16 @@ public class AssetService {
|
|
|
.operation(type)
|
|
.operation(type)
|
|
|
.price(price)
|
|
.price(price)
|
|
|
.build());
|
|
.build());
|
|
|
- mint(asset, tokenHistory.getId());
|
|
|
|
|
- return asset;
|
|
|
|
|
|
|
+ assetMintService.mint(asset, tokenHistory.getId());
|
|
|
|
|
+ return new AsyncResult<>(asset);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public Asset createAsset(BlindBoxItem winItem, User user, Long orderId, BigDecimal price, String type) {
|
|
|
|
|
|
|
+ @Async
|
|
|
|
|
+ public Future<Asset> createAsset(BlindBoxItem winItem, User user, Long orderId, BigDecimal price, String type) {
|
|
|
Asset asset = Asset.create(winItem, user);
|
|
Asset asset = Asset.create(winItem, user);
|
|
|
asset.setOrderId(orderId);
|
|
asset.setOrderId(orderId);
|
|
|
asset.setPrice(price);
|
|
asset.setPrice(price);
|
|
|
- asset.setIpfsUrl(ipfsUpload(winItem.getPic().get(0).getUrl()));
|
|
|
|
|
|
|
+ asset.setIpfsUrl(assetMintService.ipfsUpload(winItem.getPic().get(0).getUrl()));
|
|
|
assetRepo.save(asset);
|
|
assetRepo.save(asset);
|
|
|
TokenHistory tokenHistory = tokenHistoryRepo.save(TokenHistory.builder()
|
|
TokenHistory tokenHistory = tokenHistoryRepo.save(TokenHistory.builder()
|
|
|
.tokenId(asset.getTokenId())
|
|
.tokenId(asset.getTokenId())
|
|
@@ -109,55 +115,10 @@ public class AssetService {
|
|
|
.operation(type)
|
|
.operation(type)
|
|
|
.price(price)
|
|
.price(price)
|
|
|
.build());
|
|
.build());
|
|
|
- mint(asset, tokenHistory.getId());
|
|
|
|
|
- return asset;
|
|
|
|
|
|
|
+ assetMintService.mint(asset, tokenHistory.getId());
|
|
|
|
|
+ return new AsyncResult<>(asset);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void mint(Asset asset, Long historyId) {
|
|
|
|
|
- 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());
|
|
|
|
|
- if (nft != null) {
|
|
|
|
|
- asset.setTokenId(nft.getTokenId());
|
|
|
|
|
- asset.setBlockNumber(nft.getBlockNumber());
|
|
|
|
|
- asset.setTxHash(nft.getTxHash());
|
|
|
|
|
- asset.setGasUsed(nft.getGasUsed());
|
|
|
|
|
- asset.setIpfsUrl(ipfsUpload(asset.getPic().get(0).getUrl()));
|
|
|
|
|
- assetRepo.save(asset);
|
|
|
|
|
-
|
|
|
|
|
- tokenHistoryRepo.findById(historyId).ifPresent(tokenHistory -> {
|
|
|
|
|
- tokenHistory.setTokenId(nft.getTokenId());
|
|
|
|
|
- tokenHistoryRepo.save(tokenHistory);
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public String ipfsUpload(String url) {
|
|
|
|
|
- try {
|
|
|
|
|
- IPFS ipfs = new IPFS("112.74.34.84", 5001);
|
|
|
|
|
- HttpRequest request = HttpRequest.get(url);
|
|
|
|
|
- File file = File.createTempFile("ipfs", ".tmp");
|
|
|
|
|
- request.receive(file);
|
|
|
|
|
- NamedStreamable.FileWrapper file1 = new NamedStreamable.FileWrapper(file);
|
|
|
|
|
- MerkleNode put = ipfs.add(file1).get(0);
|
|
|
|
|
- Multihash multihash = ipfs.pin.add(put.hash).get(0);
|
|
|
|
|
- log.info("上传ipfs成功 {}", multihash.toBase58());
|
|
|
|
|
- return multihash.toBase58();
|
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
- }
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
public void publicShow(Long id) {
|
|
public void publicShow(Long id) {
|
|
|
Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
|
|
Asset asset = assetRepo.findById(id).orElseThrow(new BusinessException("无记录"));
|