xiongzhu 4 лет назад
Родитель
Сommit
39cc30827c

+ 1 - 0
src/main/java/com/izouma/nineth/repo/OrderRepo.java

@@ -34,4 +34,5 @@ public interface OrderRepo extends JpaRepository<Order, Long>, JpaSpecificationE
             "where c.minterId = ?1 and c.source = 'OFFICIAL' and o.status <> 'NOT_PAID' and o.status <> 'CANCELLED'")
     long countSales(Long userId);
 
+    Optional<Order> findByTransactionId(String txId);
 }

+ 4 - 1
src/main/java/com/izouma/nineth/service/AssetMintService.java

@@ -18,9 +18,11 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.context.ApplicationContext;
 import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.AsyncResult;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
+import java.util.concurrent.Future;
 
 @Service
 @Slf4j
@@ -32,7 +34,7 @@ public class AssetMintService {
     private ApplicationContext applicationContext;
 
     @Async
-    public void mint(Asset asset) {
+    public Future<Asset> mint(Asset asset) {
         User user = userRepo.findById(asset.getUserId()).orElseThrow(new BusinessException("用户不存在"));
         if (StringUtils.isEmpty(user.getPublicKey())) {
             NFTAccount account = nftService.createAccount(user.getUsername() + "_");
@@ -57,6 +59,7 @@ public class AssetMintService {
             log.error("铸造失败", e);
         }
         applicationContext.publishEvent(new CreateAssetEvent(this, true, asset));
+        return new AsyncResult<>(asset);
     }
 
     public String ipfsUpload(String url) {

+ 24 - 1
src/main/java/com/izouma/nineth/service/NFTService.java

@@ -23,6 +23,8 @@ import org.springframework.retry.annotation.Retryable;
 import org.springframework.stereotype.Service;
 
 import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
 
 @Service
 @Slf4j
@@ -48,6 +50,8 @@ public class NFTService {
             if (baseResp.isSuccess()) {
                 log.info("创建账户成功 {}", account);
                 return account;
+            } else if ("400123".equals(baseResp.getCode())) {
+                return account;
             } else {
                 throw new RuntimeException(baseResp.getCode());
             }
@@ -91,12 +95,31 @@ public class NFTService {
             log.info("NFT生成成功 {}", nft);
             return nft;
         } else {
+            ReceiptDecoration txReceipt = JSON.parseObject(resp.getData(), ReceiptDecoration.class);
+            String out = new String(matchAndReplaceNonEnglishChar(txReceipt.getOutput()));
             // 异步交易未成功需要根据状态码判断交易状态
-            log.error("EVM合约执行未成功: " + resp.getCode());
+            log.error("EVM合约执行未成功: {}, {}", resp.getCode(), out);
+            if (out.endsWith("ERC721: token already minted")) {
+                return new NFT(txReceipt.getHash(), tokenId, txReceipt.getBlockNumber(), txReceipt.getGasUsed());
+            }
         }
         throw new BusinessException("创建nft失败");
     }
 
+    public static byte[] matchAndReplaceNonEnglishChar(byte[] data) {
+        List<Byte> list = new ArrayList<>();
+        for (byte c : data) {
+            if (c >= 32 && c <= 127) {
+                list.add(c);
+            }
+        }
+        byte[] bytes = new byte[list.size()];
+        for (int i = 0; i < list.size(); i++) {
+            bytes[i] = list.get(i);
+        }
+        return bytes;
+    }
+
     public String ownerOf(String tokenId) throws Exception {
         CallRestBizParam callRestBizParam = CallRestBizParam.builder()
                 .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))

+ 5 - 1
src/test/java/com/izouma/nineth/CommonTest.java

@@ -5,6 +5,7 @@ import com.izouma.nineth.config.Constants;
 import com.izouma.nineth.domain.BaseEntity;
 import com.izouma.nineth.domain.BlindBoxItem;
 import com.izouma.nineth.domain.User;
+import com.izouma.nineth.utils.TokenUtils;
 import com.izouma.nineth.web.BaseController;
 import io.ipfs.api.IPFS;
 import io.ipfs.api.MerkleNode;
@@ -51,6 +52,7 @@ import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.math.RoundingMode;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
@@ -375,6 +377,8 @@ public class CommonTest {
 
     @Test
     public void random256() throws NoSuchAlgorithmException {
-        System.out.println(new File("/Users/drew/Downloads/images").list((dir, name) -> Pattern.matches(".*\\.png", name)));
+        String tokenId = TokenUtils.genTokenId();
+        System.out.println(tokenId);
+        System.out.println(new BigInteger(tokenId, 16));
     }
 }

+ 92 - 0
src/test/java/com/izouma/nineth/service/AdapayServiceTest.java

@@ -11,6 +11,7 @@ import com.huifu.adapay.model.Payment;
 import com.huifu.adapay.model.Refund;
 import com.izouma.nineth.ApplicationTests;
 import com.izouma.nineth.config.GeneralProperties;
+import com.izouma.nineth.repo.OrderRepo;
 import com.izouma.nineth.utils.SnowflakeIdWorker;
 import com.izouma.nineth.utils.excel.BigIntegerConverter;
 import com.izouma.nineth.utils.excel.LocalDateConverter;
@@ -26,12 +27,15 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 public class AdapayServiceTest extends ApplicationTests {
     @Autowired
     private AdapayService     adapayService;
     @Autowired
     private GeneralProperties generalProperties;
+    @Autowired
+    private OrderRepo         orderRepo;
 
     @Test
     public void testPay() throws BaseAdaPayException {
@@ -189,4 +193,92 @@ public class AdapayServiceTest extends ApplicationTests {
         refundParams.put("refund_order_no", new SnowflakeIdWorker(0, 0).nextId() + "");
         Map<String, Object> response = Refund.create("002112021120816060510314566462621900800", refundParams);
     }
+
+    @Test
+    public void refund1() throws BaseAdaPayException {
+        List<RefundOrder> orders = EasyExcel.read("/Users/drew/Desktop/refund2.xlsx")
+                .head(RefundOrder.class)
+                .registerConverter(new LocalDateConverter())
+                .registerConverter(new LocalDateTimeConverter())
+                .registerConverter(new BigIntegerConverter())
+                .sheet().doReadSync();
+        System.out.println(orders.size());
+
+        orders = orders.stream().parallel().filter(o ->
+                        o.amount.equals("60.90") && !orderRepo.findByTransactionId(o.getId()).isPresent())
+                .collect(Collectors.toList());
+        System.out.println(orders);
+//        orders.stream().parallel().forEach(o -> {
+//            orderRepo.findByTransactionId(o.getId()).ifPresent(order -> {
+//
+//            });
+//        });
+        for (RefundOrder order : orders) {
+            String refundId = new SnowflakeIdWorker(0, 0).nextId() + "";
+            Map<String, Object> refundParams = new HashMap<>();
+            refundParams.put("refund_amt", order.getAmount());
+            refundParams.put("refund_order_no", new SnowflakeIdWorker(0, 0).nextId() + "");
+            Map<String, Object> response = Refund.create(order.getId(), refundParams);
+            order.setRefundId(refundId);
+        }
+        EasyExcel.write("/Users/drew/Desktop/refund3.xlsx", RefundOrder.class).sheet("sheet")
+                .registerConverter(new LocalDateConverter())
+                .registerConverter(new LocalDateTimeConverter())
+                .registerConverter(new BigIntegerConverter())
+                .doWrite(orders);
+    }
+
+    @Test
+    public void queryrefund1() throws BaseAdaPayException {
+        List<RefundOrder> orders = EasyExcel.read("/Users/drew/Desktop/refund的副本.xlsx")
+                .head(RefundOrder.class)
+                .registerConverter(new LocalDateConverter())
+                .registerConverter(new LocalDateTimeConverter())
+                .registerConverter(new BigIntegerConverter())
+                .sheet().doReadSync();
+        System.out.println(orders.size());
+        List<String> success = new ArrayList<>();
+        List<String> fail = new ArrayList<>();
+        for (RefundOrder order : orders) {
+            Map<String, Object> refundParams = new HashMap<>();
+            refundParams.put("refund_order_no", order.getRefundId());
+            Map<String, Object> refund = Refund.query(refundParams);
+            System.out.println(refund.get("refunds"));
+            try {
+                if (((JSONArray) refund.get("refunds")).getJSONObject(0).getString("trans_status").equals("S")) {
+                    success.add(order.getId());
+                } else {
+                    fail.add(order.getId());
+                }
+            } catch (Exception e) {
+                fail.add(order.getId());
+            }
+        }
+        System.out.println("success:" + success.size());
+        System.out.println("fail:" + fail.size());
+        System.out.println(StringUtils.join(fail, ","));
+    }
+
+    @Test
+    public void queryOrder() throws BaseAdaPayException {
+        Map<String, Object> paymentParams = new HashMap<>();
+        paymentParams.put("app_id", "app_0e8d3acb-3d95-4ebb-8445-e470c378a787");
+        paymentParams.put("page_index", "1");
+        paymentParams.put("page_size", "20");
+        paymentParams.put("payment_id", "002112021122415003310320348180126400512");
+        Map<String, Object> paymentList = Payment.queryList(paymentParams);
+        System.out.println(JSON.toJSONString(paymentList, SerializerFeature.PrettyFormat));
+    }
+
+    @Test
+    public void refunds() throws BaseAdaPayException {
+        Map<String, Object> refundParams = new HashMap<>();
+        refundParams.put("refund_order_no", "923969330914263040");
+        Map<String, Object> refund = Refund.query(refundParams);
+
+        Map<String, Object> refundParams1 = new HashMap<>();
+        refundParams1.put("refund_amt", "0.01");
+        refundParams1.put("refund_order_no", "923969330914263040");
+        Map<String, Object> response = Refund.create("002112021122415001010320348080327135232", refundParams1);
+    }
 }

+ 22 - 0
src/test/java/com/izouma/nineth/service/AssetServiceTest.java

@@ -9,6 +9,7 @@ import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.List;
+import java.util.concurrent.Future;
 
 class AssetServiceTest extends ApplicationTests {
     @Autowired
@@ -109,4 +110,25 @@ class AssetServiceTest extends ApplicationTests {
         }
         System.out.println(assets);
     }
+
+    @Test
+    public void fixMint() {
+        assetRepo.findAll().stream()
+                .filter(a -> StringUtils.isEmpty(a.getTxHash()) && StringUtils.isNotEmpty(a.getTokenId()))
+                .forEach(a -> {
+                    Future<Asset> f = assetMintService.mint(a);
+                    while (!f.isDone()) {
+                        try {
+                            Thread.sleep(300);
+                        } catch (InterruptedException e) {
+                            e.printStackTrace();
+                        }
+                    }
+                    try {
+                        Thread.sleep(300);
+                    } catch (InterruptedException e) {
+                        e.printStackTrace();
+                    }
+                });
+    }
 }

+ 6 - 11
src/test/java/com/izouma/nineth/service/NFTServiceTest.java

@@ -15,6 +15,7 @@ import com.izouma.nineth.config.GeneralProperties;
 import com.izouma.nineth.dto.NFT;
 import com.izouma.nineth.utils.HashUtils;
 import com.izouma.nineth.utils.SnowflakeIdWorker;
+import com.izouma.nineth.utils.TokenUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.binary.Hex;
 import org.apache.commons.lang3.RandomStringUtils;
@@ -23,10 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 
 import java.io.File;
 import java.math.BigInteger;
-import java.security.MessageDigest;
-import java.util.Random;
 import java.util.Scanner;
-import java.util.concurrent.ThreadLocalRandom;
 
 @Slf4j
 public class NFTServiceTest extends ApplicationTests {
@@ -48,19 +46,16 @@ public class NFTServiceTest extends ApplicationTests {
     public void createToken() throws Exception {
         JSONArray jsonArray = new JSONArray();
         jsonArray.add(Utils.getIdentityByName("raex_official"));
-        Random random = ThreadLocalRandom.current();
-        byte[] r = new byte[32]; //Means 2048 bit
-        random.nextBytes(r);
-        MessageDigest m = MessageDigest.getInstance("MD5");
-        m.update(r, 0, r.length);
-        BigInteger bi = new BigInteger(1, m.digest());
-        jsonArray.add(bi.toString());
+
+        String s = TokenUtils.genTokenId();
+
+        jsonArray.add(new BigInteger(s, 16).toString());
         log.info("data {}", jsonArray.toJSONString());
         CallRestBizParam callRestBizParam = CallRestBizParam.builder()
                 .orderId(String.valueOf(new SnowflakeIdWorker(0, 0).nextId()))
                 .bizid(restClientProperties.getBizid())
                 .account(restClientProperties.getAccount())
-                .contractName("raex12")
+                .contractName(generalProperties.getContractName())
                 .methodSignature("mint(identity,uint256)")
                 .inputParamListStr(jsonArray.toJSONString())
                 .outTypes("[]")//合约返回值类型