From 5bbd0e06e219f38a3f66c3dff7a4702fa2a5ca50 Mon Sep 17 00:00:00 2001 From: liberhe <411497616@qq.com> Date: Fri, 3 Nov 2023 12:06:50 +0800 Subject: [PATCH] add test --- .gitignore | 4 +- .../com/dl/officialsite/aave/AaveService.java | 4 +- .../java/com/dl/officialsite/AaveTest.java | 96 +++++++++++++++ .../OfficialSiteApplicationTests.java | 38 ++++++ .../java/com/dl/officialsite/UtilTests.java | 112 ++++++++++++++++++ 5 files changed, 251 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/dl/officialsite/AaveTest.java create mode 100644 src/test/java/com/dl/officialsite/OfficialSiteApplicationTests.java create mode 100644 src/test/java/com/dl/officialsite/UtilTests.java diff --git a/.gitignore b/.gitignore index bc00a73e..b3f03a7c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ .gitignore ./src/main/resources/ +HappyRedPacket.java + # Package Files # *.jar *.war @@ -39,7 +41,7 @@ gradle.properties /target /out -/src/test +#/src/test .env /dist wallet/% diff --git a/src/main/java/com/dl/officialsite/aave/AaveService.java b/src/main/java/com/dl/officialsite/aave/AaveService.java index d12adb4d..113b866f 100644 --- a/src/main/java/com/dl/officialsite/aave/AaveService.java +++ b/src/main/java/com/dl/officialsite/aave/AaveService.java @@ -38,8 +38,8 @@ public class AaveService { // 1 完成自己地址的健康系数,总借款,总存款, LTV数据获取 -// 2 完成USDC、DAI、USDT 借贷利率的获取 -// 3 完成ETH 、BTC借贷利率获取 +// 2 完成USDC、DAI、USDT,LUSD 借贷利率的获取 +// 3 完成ETH 、BTC借贷利率 以及价格获取。 // 4 用DL的官网邮箱 发送team0 成员,这些数据。 每天发送一次。 // 5 半小时调一次接口 ,如果HF< 1.2 。立即发送邮件告警; diff --git a/src/test/java/com/dl/officialsite/AaveTest.java b/src/test/java/com/dl/officialsite/AaveTest.java new file mode 100644 index 00000000..1e5499e3 --- /dev/null +++ b/src/test/java/com/dl/officialsite/AaveTest.java @@ -0,0 +1,96 @@ +package com.dl.officialsite; + +import com.dl.officialsite.aave.AaveService; +import com.dl.officialsite.contract.ilendingpool.ILendingPool; +import com.dl.officialsite.contract.ilendingpooladdressesprovider.ILendingPoolAddressesProvider; +import com.dl.officialsite.contract.ipool.IPool; +import com.dl.officialsite.contract.ipooladdressesprovider.IPoolAddressesProvider; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.web3j.crypto.Credentials; +import org.web3j.protocol.Web3j; +import org.web3j.protocol.http.HttpService; +import org.web3j.tuples.Tuple; +import org.web3j.tuples.generated.Tuple6; +import org.web3j.tx.gas.ContractGasProvider; +import org.web3j.tx.gas.StaticGasProvider; + +import java.math.BigInteger; + +import static org.web3j.tx.gas.DefaultGasProvider.GAS_LIMIT; +import static org.web3j.tx.gas.DefaultGasProvider.GAS_PRICE; + +public class AaveTest { + + public static ContractGasProvider GAS_PROVIDER = new StaticGasProvider(GAS_PRICE, GAS_LIMIT); + public static final Logger logger = LoggerFactory.getLogger(AaveTest.class); + + BigInteger e16 = new BigInteger("10000000000000000"); + BigInteger e8 = new BigInteger("100000000"); + BigInteger e23 = new BigInteger("100000000000000000000000"); + + Web3j web3j = Web3j.build(new HttpService("https://polygon-rpc.com")); + String whale = "0xEf53a1797Df32c238F46f1037C8a30Ac884E178C"; + + public Credentials getCredentials() { + return Credentials.create("1"); + } + + + + + @Test + public void getHFV3() throws Exception { + + // polygonv3 + String lendingPoolAddressProviderV3 = "0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb"; + IPoolAddressesProvider addrProV3 = IPoolAddressesProvider.load(lendingPoolAddressProviderV3, web3j, getCredentials(), GAS_PROVIDER); + String s = addrProV3.getPool().send(); + logger.info("v3 pool: " + s); + + IPool lendingPoolv3 = IPool.load(s, web3j, getCredentials(), GAS_PROVIDER); + Tuple6 accountData = lendingPoolv3.getUserAccountData(whale).send(); + logger.info("Restult:" + accountData.toString()); + BigInteger supply = accountData.getValue1().divide(e8); + BigInteger borrow = accountData.getValue2().divide(e8); + + + BigInteger LTV = accountData.getValue5().divide(BigInteger.valueOf(100)); + + BigInteger hf1 = accountData.getValue6(); + logger.info("*******hf: " + hf1.divide(e16).floatValue() / 100); + logger.info("*******borrow: " + borrow); + logger.info("*******supply: " + supply); + logger.info("LTV:" + LTV + "%" ); + + + // get Interest + String usdc= "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174"; + String dai= "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063"; + + IPool.ReserveData reserveData = lendingPoolv3.getReserveData(usdc).send(); + + logger.info( "USDC variable deposit interest: " + reserveData.currentLiquidityRate.divide(e23).floatValue()/100) ; + logger.info( "USDC variable borrow interest: " + reserveData.currentVariableBorrowRate.divide(e23).floatValue()/100) ; + + reserveData = lendingPoolv3.getReserveData(dai).send(); + logger.info( "DAI variable deposit interest: " + reserveData.currentLiquidityRate.divide(e23).floatValue()/100) ; + logger.info( "DAI variable borrow interest: " + reserveData.currentVariableBorrowRate.divide(e23).floatValue()/100) ; + } + + + @Test + public void getHFV2() throws Exception { + //polygon V2 + String lendingPoolAddressProviderV2 = "0xd05e3E715d945B59290df0ae8eF85c1BdB684744"; + ILendingPoolAddressesProvider addrPro = ILendingPoolAddressesProvider.load(lendingPoolAddressProviderV2, web3j, getCredentials(), GAS_PROVIDER); + logger.info("addrPro:" + addrPro.getContractAddress()); + String lendingPoolAddress = addrPro.getLendingPool().send(); + ILendingPool lendingPool = ILendingPool.load(lendingPoolAddress, web3j, getCredentials(), GAS_PROVIDER); + BigInteger hf = lendingPool.getUserAccountData(whale).send().getValue6(); + logger.info("*******hf: " + hf.divide(e16).floatValue() / 100); + //todo the same logic + + } +} diff --git a/src/test/java/com/dl/officialsite/OfficialSiteApplicationTests.java b/src/test/java/com/dl/officialsite/OfficialSiteApplicationTests.java new file mode 100644 index 00000000..84b802d4 --- /dev/null +++ b/src/test/java/com/dl/officialsite/OfficialSiteApplicationTests.java @@ -0,0 +1,38 @@ +package com.dl.officialsite; + +import com.dl.officialsite.mail.EmailService; +import com.dl.officialsite.member.MemberController; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class OfficialSiteApplicationTests { + public static final Logger logger = LoggerFactory.getLogger(MemberController.class); + + @Autowired + EmailService emailService; + @Test + void contextLoads() { + } + + + + @Test + public void mailTest() { + + //Get the mailer instance + + + //Send a composed mail + emailService.sendSimpleMessage("411497616@qq.com", "Test Subject1", "Testing body11"); + + //Send a pre-configured mail + //emailService.sendPreConfiguredMail("Exception occurred everywhere.. where are you ????"); + } + + + +} diff --git a/src/test/java/com/dl/officialsite/UtilTests.java b/src/test/java/com/dl/officialsite/UtilTests.java new file mode 100644 index 00000000..9f837948 --- /dev/null +++ b/src/test/java/com/dl/officialsite/UtilTests.java @@ -0,0 +1,112 @@ +package com.dl.officialsite; + +import com.dl.officialsite.common.base.BaseResponse; +import com.dl.officialsite.member.MemberController; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.web3j.crypto.Keys; +import org.web3j.crypto.Sign; + +import java.math.BigInteger; +import java.security.SignatureException; + +import static org.web3j.crypto.Sign.getEthereumMessageHash; +import static org.web3j.utils.Numeric.hexStringToByteArray; + + +class UtilTests { + public static final Logger logger = LoggerFactory.getLogger(MemberController.class); + + + @Test + public void checkSignature() throws SignatureException { +// SignInfo sign = new SignInfo("0x2fE023204958fc4c44f639CE72D3bdC0f025Adfe",new BigInteger("1c",16) +// , new BigInteger("0ea7d9596f0c90d2cf417a079c6c56ef10dda520cf392e89ae5a7bdc1d09c8db",16) +// ,new BigInteger("037f66fde24749ba842ef5de24902451f0fe9baf5cd1b0b9dca42c71baaf98b0",16),"hello"); +// byte[] v = new byte[] {(byte) sign.getV()}; +// byte[] r = Numeric.toBytesPadded(sign.getR(), 32); +// byte[] s = Numeric.toBytesPadded(sign.getS(), 32); +// Sign.SignatureData signatureData = new Sign.SignatureData(v,r ,s ); +// BigInteger publicKey = Sign.signedMessageHashToKey(getEthereumMessageHash(sign.getMessage().getBytes()), signatureData); +// String address = Keys.getAddress(publicKey); +// logger.info("*******"+address); + + } + + + @Test + public void checkSignature1() throws SignatureException { + String message = "localhost:3000 wants you to sign in with your Ethereum account:\n" + + "0x2fE023204958fc4c44f639CE72D3bdC0f025Adfe\n" + + "\n" + + "Sign in with Etherem to Dapp-Learning\n" + + "\n" + + "URI: http://localhost:3000\n" + + "Version: 1\n" + + "Chain ID: 1\n" + + "Nonce: ABA490FDFFF5742158889682D9308F1AF\n" + + "Issued At: 2023-10-15T15:35:00.404Z"; + + + + byte[] v1 = hexStringToByteArray("0x1b"); + byte[] r1 = hexStringToByteArray("0x64d251ade78cf78ad3f1b1115421f6ac97eff32c8c2b8404db6484bc3d6fe67f"); + byte[] s1 = hexStringToByteArray("0x2afeb45431847f3fa513921542a18727ec72413b05a8c6c3a39323d4e4095f16"); + Sign.SignatureData signatureData1 = new Sign.SignatureData(v1,r1 ,s1); + BigInteger publicKey1 = Sign.signedMessageHashToKey(getEthereumMessageHash(message.getBytes()), signatureData1); + String address1 = Keys.getAddress(publicKey1); + + logger.info("*******" + address1 + "*******"); + + + + } + + + @Test + public void checkNonce() throws SignatureException { + String message = "localhost:3000 wants you to sign in with your Ethereum account:\n" + + "0x2fE023204958fc4c44f639CE72D3bdC0f025Adfe\n" + + "\n" + + "Sign in with Etherem to Dapp-Learning\n" + + "\n" + + "URI: http://localhost:3000\n" + + "Version: 1\n" + + "Chain ID: 1\n" + + "Nonce: ABA490FDFFF5742158889682D9308F1AF\n" + + "Issued At: 2023-10-15T15:35:00.404Z"; + + + //JSONObject obj = new JSONObject(message); + int index = message.indexOf("Nonce:"); + String nonce = message.substring(index+7, index+39); + + + + logger.info("*******" + nonce + "*******"); + + } + + + + @Test + public void objectToJson() throws JsonProcessingException { + + + BaseResponse baseResponse = BaseResponse.failWithReason("2001", "please login in"); + + ObjectMapper objectMapper = new ObjectMapper(); + + String s = objectMapper.writeValueAsString( baseResponse ); + + logger.info("*******" + s + "*******"); + + + } + + + +}