Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test #44

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
.gitignore
./src/main/resources/

HappyRedPacket.java

# Package Files #
*.jar
*.war
Expand Down Expand Up @@ -39,7 +41,7 @@ gradle.properties

/target
/out
/src/test
#/src/test
.env
/dist
wallet/%
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dl/officialsite/aave/AaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 。立即发送邮件告警;

Expand Down
96 changes: 96 additions & 0 deletions src/test/java/com/dl/officialsite/AaveTest.java
Original file line number Diff line number Diff line change
@@ -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<BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger> 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

}
}
Original file line number Diff line number Diff line change
@@ -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("[email protected]", "Test Subject1", "Testing body11");

//Send a pre-configured mail
//emailService.sendPreConfiguredMail("Exception occurred everywhere.. where are you ????");
}



}
112 changes: 112 additions & 0 deletions src/test/java/com/dl/officialsite/UtilTests.java
Original file line number Diff line number Diff line change
@@ -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 + "*******");


}



}