import cn.hutool.json.JSONUtil;
import com.table.transfer.module.enpty.TransactionInfoDTO;
import com.table.transfer.module.enpty.TransferRequest;
import com.table.transfer.module.service.EthereumService;
import com.table.transfer.util.ether.EtherNonceManager;

import java.math.BigDecimal;
import java.math.BigInteger;

public class BlockchainServiceTest {
    private static final EthereumService ethereumService= new EthereumService();

    //   SENDADDRESSMAP.put("ETH","0x6F18844e79BeefF5f2D1eE30508aB695A829C12e");
//        SENDADDRESSMAP.put("tron","TDFtN7nyfweR8WeUraZM7WuCrtUTruD4Er");
//        SENDADDRESSMAP.put("BSC","0xcBea8F7907078B07B0f491D1B65Ea92905E57827");


    public static void main(String[] args) throws Exception {
//        Web3jManager.init();
        TransferRequest bscTransferRequest = new TransferRequest();
        bscTransferRequest.setToAddress("0x4472E7346a38AfB457562895c4Aa974C2A516a84");
        bscTransferRequest.setFromAddress("0xe78d1f011c52f15012a7f964072750e7b8d7b80a");
        bscTransferRequest.setPrivateKey("7f79238fc14b539fc6d7d3697db7fc4c2578f03307547b8625ff30f6d48da433");
        bscTransferRequest.setAmount("0.0000097311403");
        bscTransferRequest.setContractAddress("0x55d398326f99059ff775485246999027b3197955");
        bscTransferRequest.setChain("BSC");
        bscTransferRequest.setCoin("BNB");
        //獲取nonce
        BigInteger bscNonce = EtherNonceManager.getNonce(bscTransferRequest.getChain(), bscTransferRequest.getFromAddress());
        System.out.println("bsc链获取nonce" + bscNonce);
        //获取gasLimit和SafeGasPrice
        BigInteger bscSafeGasPrice = ethereumService.getSafeGasPrice(bscTransferRequest);
        System.out.println("bsc链获取GasPrice" + bscSafeGasPrice);
        BigInteger bscGasLimit = ethereumService.estimateGasSafely(bscTransferRequest);
        System.out.println("bsc链获取bscGasLimit" + bscGasLimit);
        //获取代币和主币余额
        BigDecimal tokenBalance=ethereumService.getTokenBalance(bscTransferRequest.getAssetCode(),bscTransferRequest.getContractAddress(),bscTransferRequest.getFromAddress());
        BigDecimal balance = ethereumService.getBalance(bscTransferRequest.getChain(), bscTransferRequest.getFromAddress());
        System.out.println("代币余额:"+tokenBalance);
        System.out.println("主币余额:"+balance);

        Long lastBlock = ethereumService.getLastBlock(bscTransferRequest.getChain());
        System.out.println("获取最"+bscTransferRequest.getChain()+"新区块:"+lastBlock);


        TransactionInfoDTO transactionInfoByHash = ethereumService.getTransactionInfoByHash(bscTransferRequest.getAssetCode(), "0xda1fbf2fe5f5afbe2c7fde92bce9eb2d1baa6e6b3a5ca430845b78ce879422a3");
        System.out.println(bscTransferRequest.getChain()+"链根据hash查询交易结果："+JSONUtil.toJsonStr(transactionInfoByHash));


        ethereumService.transfer(bscTransferRequest);
        BigInteger ethNonce = EtherNonceManager.getNonce("ETH", "0x6F18844e79BeefF5f2D1eE30508aB695A829C12e");
        System.out.println("eth链获取nonce" + ethNonce);

    }

}
