All Downloads are FREE. Search and download functionalities are using the official Maven repository.

vite.ViteRpc Maven / Gradle / Ivy

The newest version!
package vite;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import vite.api.service.RpcViteService;
import vite.api.utils.Generator;
import vite.api.vo.*;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

import static vite.ViteWalletHelper.digest;
import static vite.utils.BytesUtils.byteMerger;


public class ViteRpc {


    private RpcViteService rpcViteService;

    static final DateTimeFormatter dateTimeFormatOfGoInput = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'+08:00'");

    public ViteRpc(String baseUrl) {
        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        dateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        rpcViteService = Generator.createService(RpcViteService.class, baseUrl);
    }


    public AccountBlock ledgerGetLatestBlock(String accountAddress) {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getLatestBlock");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(accountAddress);
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));

        if (jsonObject.getJSONObject("result") != null) {
            return jsonObject.getJSONObject("result").toJavaObject(AccountBlock.class);
        }
        return null;
    }


    public AccountBlock getBlockByHeight(String address, String height) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getBlockByHeight");
        List parems = new ArrayList<>();
        parems.add(address);
        parems.add(height);
        requestJson.setParams(parems);
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (jsonObject.getJSONObject("result") != null) {
            return jsonObject.getJSONObject("result").toJavaObject(AccountBlock.class);
        }
        return null;
    }


    public List getBlocksByHash(String address, String hash, int n) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getBlocksByHash");
        List parems = new ArrayList<>();
        parems.add(address);
        parems.add(hash);
        parems.add(n);
        requestJson.setParams(parems);
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (jsonObject.getJSONObject("result") != null) {
            return jsonObject.getJSONArray("result").toJavaList(AccountBlock.class);
        }
        return null;
    }


    public AccountBlock getBlockByHash(String blockHash) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getBlockByHash");
        List parems = new ArrayList<>();
        parems.add(blockHash);
        requestJson.setParams(parems);
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (jsonObject.getJSONObject("result") != null) {
            return jsonObject.getJSONObject("result").toJavaObject(AccountBlock.class);
        }
        return null;
    }

    public List getOnroadBlocksByAddress(String accountAddress) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("onroad_getOnroadBlocksByAddress");
        List params = new ArrayList<>();
        requestJson.setParams(params);
        params.add(accountAddress);
        params.add(0);
        params.add(10);
        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (jsonObject.getJSONArray("result") != null) {
            return jsonObject.getJSONArray("result").toJavaList(AccountBlock.class);
        }
        return null;
    }

    public AccountDetail getAccountByAccAddr(String accountAddress) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getAccountByAccAddr");
        List params = new ArrayList<>();
        requestJson.setParams(params);
        params.add(accountAddress);

        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));
        System.out.println(jsonObject.getJSONObject("result"));
        if (jsonObject.getJSONObject("result") != null) {
            return jsonObject.getJSONObject("result").toJavaObject(AccountDetail.class);
        }
        return null;
    }


    public Map> getConfirmedBalances(String hash, List addresses, List tokenIds) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getConfirmedBalances");
        List params = new ArrayList<>();
        requestJson.setParams(params);
        params.add(hash);
        params.add(addresses);
        params.add(tokenIds);

        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));

        if (jsonObject.getJSONObject("result") != null) {
            Map> map = jsonObject.getJSONObject("result").toJavaObject(Map.class);
            return map;
        }
        return null;

    }

    public List getBlocksByAccAddr(String accountAddress) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getBlocksByAccAddr");
        List params = new ArrayList<>();
        requestJson.setParams(params);
        params.add(accountAddress);
        params.add(0);
        params.add(10);

        JSONObject jsonObject = Generator.executeSync(rpcViteService.postRequest(requestJson));

        if (jsonObject.getJSONArray("result") != null) {
            List list = jsonObject.getJSONArray("result").toJavaList(AccountBlock.class);
            return list;
        }
        return null;
    }

    public String ledgerGetFittestSnapshotHash(String accountAddress, String sendBlockHash) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getFittestSnapshotHash");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        if (accountAddress != null) {
            parems.add(accountAddress);
        }
        if (sendBlockHash != null) {
            parems.add(sendBlockHash);
        }
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getString("result") != null) {
            return result.getString("result");
        }

        return null;
    }

    public SnapshotBlock getSnapshotBlockBeforeTime(long timeStamp) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getSnapshotBlockBeforeTime");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(timeStamp);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getJSONObject("result") != null) {
            return result.getJSONObject("result").toJavaObject(SnapshotBlock.class);
        }
        return null;
    }

    public String calcDifficulty(AccountBlock accountBlock) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("tx_calcPoWDifficulty");
        List params = new ArrayList<>();
        requestJson.setParams(params);

        Map map = new HashMap<>();
        map.put("selfAddr", accountBlock.getAccountAddress());
        map.put("prevHash", accountBlock.getPrevHash());
        map.put("blockType", accountBlock.getBlockType());
        map.put("toAddr", accountBlock.getToAddress());
        map.put("data", accountBlock.getData() != null ? accountBlock.getData() : null);
        map.put("usePledgeQuota", true);

        params.add(JSONObject.toJSON(map));

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getString("result") != null) {
            String diffculty = result.getJSONObject("result").getString("difficulty");
            return StringUtils.isNotEmpty(diffculty) ? diffculty : null;
        }

        return null;

    }

    public String getPowNonce(String address, String prevHash, String difficulty) {

        if (difficulty == null) {
            return null;
        }
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("pow_getPowNonce");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);

        byte[] sources = new byte[1];
        sources = ViteWalletHelper.generateRealByteAddress(address);

        try {
            sources = byteMerger(sources, Hex.decodeHex(prevHash));
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        byte[] hash = digest(32, sources);
        parems.add(difficulty);
        parems.add(Hex.encodeHexString(hash));

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getString("result") != null) {
            return result.getString("result");
        }
        return null;
    }

    public JSONObject txSendRawTx(AccountBlock accountBlock) {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("tx_sendRawTx");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(accountBlock);

        // System.out.println(JSONObject.toJSONString(requestJson));

        requestJson = JSONObject.parseObject(JSONObject.toJSONString(requestJson), RequestJson.class);

        System.out.println(JSONObject.toJSONString(requestJson));

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getJSONObject("error") == null) {
            result.put("hash", accountBlock.getHash());
        }
        return result;
    }

    /**
     * 获得链信息
     *
     * @return
     */
    public JSONObject getLastestAccountBlock() {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getLatestSnapshotChainHash");
        return Generator.executeSync(rpcViteService.postRequest(requestJson));
    }


    public SnapshotBlock getSnapshotBlockByHeight(long n) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getSnapshotBlockByHeight");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(n);

        System.out.println(JSONObject.toJSONString(requestJson));
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getJSONObject("result") != null) {
            return result.getJSONObject("result").toJavaObject(SnapshotBlock.class);
        }
        return null;
    }

    public SnapshotBlock getSnapshotBlockByHash(String hash) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getSnapshotBlockByHash");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(hash);

        System.out.println(JSONObject.toJSONString(requestJson));
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.getJSONObject("result") != null) {
            return result.getJSONObject("result").toJavaObject(SnapshotBlock.class);
        }
        return null;
    }


    public Long getSnapshotChainHeight() {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getSnapshotChainHeight");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);

        //  System.out.println(JSONObject.toJSONString(requestJson));
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getLong("result");
        }
        return null;
    }


    public List getChunks(long fromHeight, long toHeight) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("ledger_getChunks");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(fromHeight);
        parems.add(toHeight);

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONArray("result").toJavaList(Chunk.class);
        }
        return null;

    }


    public Long getRoundIndexFromTime(long timeStamp) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("sbpstats_time2Index");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);

        return null;
    }


    public List getTokenList(long start, long pageSize) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("mintage_getTokenInfoList");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(start);
        parems.add(pageSize);

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONObject("result").getJSONArray("tokenInfoList").toJavaList(Token.class);
        }
        return null;
    }

    public Long getTokenListCount() {

        /**
         * {
         *    "jsonrpc":"2.0",
         *    "id":1,
         *    "method":"mintage_getTokenInfoList",
         *    "params":[0, 0]
         * }
         */
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("mintage_getTokenInfoList");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(0);
        parems.add(0);

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONObject("result").getLong("totalCount");
        }
        return null;
    }


    public List getCurrentVoteInfos() {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("register_getCandidateList");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add("00000000000000000001");

        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONArray("result").toJavaList(VoteInfo.class);
        }
        return null;
    }


    public Long getRoundByTime(long curtime) {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("sbpstats_time2Index");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        String timeFormat = dateTimeFormatOfGoInput.print(curtime);
        parems.add(timeFormat);
        parems.add(0);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getLong("result");
        }
        return null;
    }


    public List getHourSBPStats(long fromHourindex, long toHourindex) {


        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("sbpstats_getHourSBPStats");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(fromHourindex);
        parems.add(toHourindex);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONArray("result").toJavaList(SuperNodeHourInfo.class);
        }
        return null;
    }


    public List getPeriodSBPStats(long fromRound, long toRound) {


        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("sbpstats_getPeriodSBPStats");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(fromRound);
        parems.add(toRound);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONArray("result").toJavaList(SuperNodeRoundInfo.class);
        }
        return null;
    }

    /**
     * get contract
     *
     * @param address
     * @return
     */
    public ContractInfo getContractInfo(String address) {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("contract_getContractInfo");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(address);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONObject("result").toJavaObject(ContractInfo.class);
        }

        return null;
    }


    public String getCreateContractData(ContractInput contractInput) {

        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("contract_getCreateContractData");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(contractInput);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getString("result");
        }
        return null;

    }

    /**
     * get contract send Block
     *
     * @param address
     * @return
     */
    public AccountBlock getContractCreatBlock(String address) {
        AccountBlock accountBlockFirst = getBlockByHeight(address, 1 + "");
        if (accountBlockFirst == null || StringUtils.isEmpty(accountBlockFirst.getFromBlockHash())) {
            return null;

        }
        AccountBlock accountBlockSend = getBlockByHash(accountBlockFirst.getFromBlockHash());

        return accountBlockSend;
    }


    public int getPledgeQuota(String address) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("pledge_getPledgeQuota");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(address);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONObject("result").getInteger("utps");
        }
        return -1;
    }

    public PledgeListResult getPledgeListByPage(String snapshotHash, String lastKey, int pageSize) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("data_getPledgeListByPage");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(snapshotHash);
        parems.add(lastKey);
        parems.add(pageSize);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONObject("result").toJavaObject(PledgeListResult.class);
        }
        return null;
    }


    public DexUserFundsResult getDexUserFundsByPage(String snapshotHash, String lastKey, int pageSize) {
        RequestJson requestJson = new RequestJson();
        requestJson.setMethod("data_getDexUserFundsByPage");
        List parems = new ArrayList<>();
        requestJson.setParams(parems);
        parems.add(snapshotHash);
        parems.add(lastKey);
        parems.add(pageSize);
        JSONObject result = Generator.executeSync(rpcViteService.postRequest(requestJson));
        if (result.get("result") != null) {
            return result.getJSONObject("result").toJavaObject(DexUserFundsResult.class);
        }
        return null;
    }

    public static void main(String[] args) {


//        ViteRpc viteRpc = new ViteRpc("http://118.24.129.159:48132");
//        String lastKey = null;
//        Map set = new HashMap<>();
//        long total = 0;
//
//
//        while (true) {
//            PledgeListResult pledgeListResult = viteRpc.getPledgeListByPage("19a8baf9f0b6f3171e3e5a59c3eb0aee148ec0b8d376d798af24942fe9ea7385", lastKey, 200);
//            if (pledgeListResult != null) {
//                System.out.println(pledgeListResult.getList().size());
//                total += pledgeListResult.getList().size();
//                for (int k = 0; k < pledgeListResult.getList().size(); k++) {
//
//                    PledgeListResult.PledgeInfo pledgeInfo = pledgeListResult.getList().get(k);
//                    String key = pledgeInfo.isAgent() + pledgeInfo.getPledgeAddr() + pledgeInfo.getBeneficialAddr() + pledgeInfo.getAgentAddr() + pledgeInfo.getBid();
//                    if (set.containsKey(key)) {
//                        System.err.println(JSONObject.toJSONString(pledgeInfo) + "----" + JSONObject.toJSONString(set.get(key)));
//
//                    } else {
//                        set.put(key, pledgeInfo);
//                    }
//                }
//                lastKey = pledgeListResult.getLastKey();
//
//                if (StringUtils.isEmpty(lastKey)) {
//                    break;
//                }
//
//            } else {
//                break;
//
//            }
//        }
//
//        PledgeListResult pledgeListResult = viteRpc.getPledgeListByPage("19a8baf9f0b6f3171e3e5a59c3eb0aee148ec0b8d376d798af24942fe9ea7385", lastKey, 8400);
//
//        System.out.println("total:" + total);
//
//        System.out.println(pledgeListResult.getList().size());





        ViteRpc viteRpc = new ViteRpc("http://118.24.129.159:48132");

        PledgeListResult pledgeListResult = viteRpc.getPledgeListByPage("19a8baf9f0b6f3171e3e5a59c3eb0aee148ec0b8d376d798af24942fe9ea7385", null, 200);

        System.out.println(JSONObject.toJSONString(pledgeListResult));


        DexUserFundsResult dexUserFundsByPage = viteRpc.getDexUserFundsByPage("19a8baf9f0b6f3171e3e5a59c3eb0aee148ec0b8d376d798af24942fe9ea7385", null, 200);

        System.out.println(JSONObject.toJSONString(dexUserFundsByPage));




      /*  String lastKey = null;
        Map set = new HashMap<>();
        long total = 0;


        while (true) {
            DexUserFundsResult dexUserFundsByPage = viteRpc.getDexUserFundsByPage("19a8baf9f0b6f3171e3e5a59c3eb0aee148ec0b8d376d798af24942fe9ea7385", lastKey, 200);
            if (dexUserFundsByPage != null && dexUserFundsByPage.getFunds() != null) {
                System.out.println(dexUserFundsByPage.getFunds().size());
                total += dexUserFundsByPage.getFunds().size();
                for (int k = 0; k < dexUserFundsByPage.getFunds().size(); k++) {

                    DexUserFundsResult.Fund fund = dexUserFundsByPage.getFunds().get(k);
                    String key = fund.getAddress();
                    if (set.containsKey(key)) {
                        System.err.println(JSONObject.toJSONString(fund) + "----" + JSONObject.toJSONString(set.get(key)));

                    } else {
                        set.put(key, fund);
                    }

                    if (k == dexUserFundsByPage.getFunds().size() - 1) {
                        lastKey = fund.getAddress();
                    }

                }

                if (StringUtils.isEmpty(lastKey)) {
                    break;
                }

            } else {
                break;

            }
        }

        DexUserFundsResult dexUserFundsByPage = viteRpc.getDexUserFundsByPage("19a8baf9f0b6f3171e3e5a59c3eb0aee148ec0b8d376d798af24942fe9ea7385", null, 8400);

        System.out.println("total:" + total);

        System.out.println(dexUserFundsByPage.getFunds().size());

        */


    }

}