xiaoyong931011
2022-02-25 89b7fb1d316cfce7eb98a27c8d668da493933f7f
src/main/java/com/xcong/excoin/modules/blackchain/service/Trc20Service.java
@@ -2,9 +2,19 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunlight.tronsdk.TrxService;
import com.sunlight.tronsdk.transaction.TransactionResult;
import com.xcong.excoin.modules.blackchain.model.Trc20TransactionsData;
import com.xcong.excoin.modules.blackchain.model.Trc20TransactionsResult;
import org.apache.commons.codec.binary.Hex;
import org.tron.common.crypto.SignInterface;
import org.tron.common.crypto.SignUtils;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Utils;
import org.tron.walletserver.WalletApi;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -16,31 +26,83 @@
    private final static String FULL_NODE_URL = "https://api.trongrid.io";
    public static Map<String,String> createAddress(){
        String url  ="https://api.trongrid.io/wallet/generateaddress";
        // {privateKey=ed0bae6e49fa4dd8a622fe77baa0d6e4077155b28ed6637870745c6da3cf372e, address=THeRNk25ps69fzqhN6tZ4Ecxur3RvDNDtY, hexAddress=4154336ea2a3a26be8a722053ee26c61e3f7f3e0ec}
        String s = HttpUtil.get(url);
        return JSONObject.parseObject(s,Map.class);
    public final static String TRX_PRIVATE_KEY = "9c651a69a07f282774d16b1557a8c98e0c6aba54bc9b0b15799695f706c4ae41";
    public final static String TRX_ADDRESS = "TAmEULfd7MCiuuYcUSHrL6dWdKTeLzguyR";
    public final static String POOL_ADDRESS = "TFZb78r6SMXSvP84GjiwtWng7Z4X1QDEoB";
    public final static String API_KEY="a7b0c96a-cfcd-474d-88c5-75c6277fedbf";
    /**
     * 创建用户钱包地址
     **/
    public static  Map<String,String> createAddress() {
//        String url = http + "/wallet/generateaddress";
        SignInterface sign = SignUtils.getGeneratedRandomSign(Utils.getRandom(), true);
        byte[] priKey = sign.getPrivateKey();
        byte[] address = sign.getAddress();
        String priKeyStr = Hex.encodeHexString(priKey);
        String base58check = WalletApi.encode58Check(address);
        String hexString = ByteArray.toHexString(address);
        Map<String,String> jsonAddress = new HashMap<>();
        jsonAddress.put("address", base58check);
        jsonAddress.put("hexAddress", hexString);
        jsonAddress.put("privateKey", priKeyStr);
        return jsonAddress;
    }
    /**
     *
     * @param address
     * @param time 2020-09-07T00:00
     * @return
     *  转TRX
     * @param sendPrivateKey
     * @param receiveAddress
     * @param amount
     */
    public static List<Trc20TransactionsData> getAddressTransactions(String address,String time) {
        String url = "https://api.trongrid.io/v1/accounts/"+address+"/transactions/trc20?only_confirmed=true&only_to=true&min_timestamp="+time+"&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
        //String url = "https://api.trongrid.io/v1/accounts/"+address+"/transactions/trc20?limit=100&contract_address=TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t&only_confirmed=true";
        String s = HttpUtil.get(url);
        Trc20TransactionsResult trc20TransactionsResult = JSONObject.parseObject(s, Trc20TransactionsResult.class);
        if (trc20TransactionsResult.isSuccess()) {
            return trc20TransactionsResult.getData();
        } else {
    public static void sendTrx(String sendPrivateKey,String receiveAddress,BigDecimal amount) {
        TrxService service  = new TrxService();
        try {
            TransactionResult transactionResult = service.testSendTrxTransaction(sendPrivateKey, receiveAddress, amount,API_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     *  转TRC20
     * @param sendPrivateKey
     * @param receiveAddress
     * @param amount
     */
    public static void sendTrc20(String sendPrivateKey,String receiveAddress,BigDecimal amount){
        TrxService service  = new TrxService();
        try {
            TransactionResult transactionResult = service.sendTrc20TransactionTest(sendPrivateKey, receiveAddress, amount,API_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static BigDecimal getTrxBalance(String address){
        TrxService service  = new TrxService();
        try {
            BigDecimal trxBalanceTest = service.getTrxBalanceTest(address,API_KEY);
            return trxBalanceTest;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static BigDecimal getTrc20Balance(String address){
        TrxService service  = new TrxService();
        try {
            BigDecimal trxBalanceTest = service.trc20BalanceOfTest(address,API_KEY);
            return trxBalanceTest;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void main(String[] args) {
        System.out.println(createAddress());
    }