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

vite.ViteClient Maven / Gradle / Ivy

The newest version!
package vite;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.util.encoders.Hex;
import vite.api.service.ViteOpService;
import vite.api.vo.AccountBlock;
import vite.api.vo.AccountDetail;
import vite.bean.Key;
import vite.utils.abi.Abi;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class ViteClient {
    private ViteRpc rpc;

    public ViteClient(String baseurl) {
        this.rpc = new ViteRpc(baseurl);
    }

    // data is string
    public JSONObject transfer(String fromAddress, String toAddress, String tokenId, String amount, String data, boolean needPOW, String priKey) {
        String dataBase64 = null;
        try {
            if (data != null) {
                dataBase64 = Base64.encodeBase64String(data.getBytes("utf-8"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ViteOpService.transfer(fromAddress, toAddress, tokenId, amount, priKey, dataBase64, needPOW, rpc);
    }

    // data is byte array
    public JSONObject transferArray(String fromAddress, String toAddress, String tokenId, String amount, byte[] data, boolean needPOW, String priKey) {
        String dataBase64 = null;

        if (data != null) {
            dataBase64 = Base64.encodeBase64String(data);
        }
        return ViteOpService.transfer(fromAddress, toAddress, tokenId, amount, priKey, dataBase64, needPOW, rpc);
    }


    public JSONObject callContract(String fromAddress, String toAddress, String tokenId, String amount, String abi, String methodName, List params, boolean needPOW, String priKey) {
        Abi abiService = Abi.fromJson(abi);
        byte[] abibytes = abiService.encodeFunction(methodName, params.toArray());

        System.out.println(Hex.toHexString(abibytes));
        //  String data = new String(abibytes, "UTF-8");

        Base64 base64 = new Base64();
        String dataBase64 = base64.encodeAsString(abibytes);
        return ViteOpService.transfer(fromAddress, toAddress, tokenId, amount, priKey, dataBase64, needPOW, rpc);
    }

    // receive all onroad txs
    public List receiveAllOnroadTx(String toAddress, boolean needPow, String priKey) {

        List result = new ArrayList<>();

        try {
            List tmp = rpc.getOnroadBlocksByAddress(toAddress);
            if (tmp != null) {
                for (int i = 0; i < tmp.size(); i++) {
                    AccountBlock accountBlock = tmp.get(i);
                    String fromBlockHash = accountBlock.getHash();
                    toAddress = accountBlock.getToAddress();

                    JSONObject object = ViteOpService.receive(accountBlock.getAccountAddress(),
                            toAddress, fromBlockHash, priKey, needPow, rpc);
                    result.add(object);

                    Thread.sleep(3000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return result;
    }

    // receive single tx by hash
    public JSONObject receiveSingleTx(String blockHash, boolean needPOW, String privKey) {
        try {
            AccountBlock accountBlock = rpc.getBlockByHash(blockHash);
            if (accountBlock == null) {
                return null;
            }

            System.out.println(JSONObject.toJSONString(accountBlock));
            return ViteOpService.receive(
                    accountBlock.getAccountAddress(),
                    accountBlock.getToAddress(),
                    accountBlock.getHash(),
                    privKey,
                    needPOW,
                    rpc);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public List getOnroadBlocksByAddress(String address) {
        return rpc.getOnroadBlocksByAddress(address);
    }

    public AccountBlock getBlockByHash(String blockHash) {
        return rpc.getBlockByHash(blockHash);
    }

    public AccountDetail getAccountByAccAddr(String accountAddress) {
        return rpc.getAccountByAccAddr(accountAddress);
    }

    public List getBlocksByAccAddr(String accountAddress) {
        return rpc.getBlocksByAccAddr(accountAddress);
    }

    public static void main(String[] args) throws Exception {

        Key key = WalletClient.getKeyPairFromMnemonics("humble category output craft giant reform weapon business dinner gentle club diagram goat recycle cactus leopard library ship offer output history lake harvest struggle", 0);

        ViteClient viteClient = new ViteClient("https://api.vitewallet.com/ios/");


        System.out.println(key.getHexAddress());

//        AccountBlock result = viteClient.getBlockByHash("e3b4c93786f8c9586335d944b5e2c4ab8ce66f46a81addfb707b8053c23b101d");

        JSONObject result = viteClient.transfer(key.getHexAddress(), key.getHexAddress(),
                "tti_5649544520544f4b454e6e40", "0", "", true, key.getHexPriKey());


        //  List result = viteClient.getBlocksByAccAddr("vite_000000000000000000000000000000000000000309508ba646");
        //  System.out.println(JSONObject.toJSONString(result));

        //    List result2 = viteClient.receiveAllOnroadTx(key.getHexAddress(),true, key.getHexPriKey());
        //  List result = viteClient.getOnroadBlocksByAddress(key.getHexAddress());
        //   JSONObject result = viteClient.receiveSingleTx("e1bfecbaad96b7aaef95701fe58b921d570f21c88b0ab44d5a030cf56290ad94", true, key.getHexPriKey());


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


        List params = new ArrayList<>();

        params.add(key.getHexAddress());


        //   {"type":"function","name":"Mint",
        // "inputs":[{"name":"isReIssuable","type":"bool"}
        // ,{"name":"tokenId","type":"tokenId"},
        // {"name":"tokenName","type":"string"},
        // {"name":"tokenSymbol","type":"string"}
        // ,{"name":"totalSupply","type":"uint256"},
        // {"name":"decimals","type":"uint8"},
        // {"name":"maxSupply","type":"uint256"},
        // {"name":"ownerBurnOnly","type":"bool"}]},

        //  JSONObject order = viteClient.callContract(key.getHexAddress(), "vite_000000000000000000000000000000000000000309508ba646", "tti_5649544520544f4b454e6e40", "1000000000000000000000", ContractsUtil.abiMap.get("vite_000000000000000000000000000000000000000309508ba646").toJson(), "Pledge", params, true, key.getHexPriKey());

        // System.out.println(order.toJSONString());


        //String fromAddress, String toAddress, String tokenId, String amount, String abi, String methodName, List params, String priKey

        // test demo

//        System.err.println(System.currentTimeMillis());
//        Random r = new Random();
//
//        int balance = 10;
//        int delta = 1;
//        long count = 0;
//        while (true) {
//            System.err.println(System.currentTimeMillis());
//
//            count++;
//            String abi = "[{\n" +
//                    "\t\"type\": \"function\",\n" +
//                    "\t\"name\": \"DexFundNewOrder\",\n" +
//                    "\t\"inputs\": [{\n" +
//                    "\t\t\"name\": \"orderId\",\n" +
//                    "\t\t\"type\": \"bytes\"\n" +
//                    "\t}, {\n" +
//                    "\t\t\"name\": \"tradeToken\",\n" +
//                    "\t\t\"type\": \"tokenId\"\n" +
//                    "\t}, {\n" +
//                    "\t\t\"name\": \"quoteToken\",\n" +
//                    "\t\t\"type\": \"tokenId\"\n" +
//                    "\t}, {\n" +
//                    "\t\t\"name\": \"side\",\n" +
//                    "\t\t\"type\": \"bool\"\n" +
//                    "\t}, {\n" +
//                    "\t\t\"name\": \"orderType\",\n" +
//                    "\t\t\"type\": \"uint32\"\n" +
//                    "\t}, {\n" +
//                    "\t\t\"name\": \"price\",\n" +
//                    "\t\t\"type\": \"string\"\n" +
//                    "\t}, {\n" +
//                    "\t\t\"name\": \"quantity\",\n" +
//                    "\t\t\"type\": \"uint256\"\n" +
//                    "\t}]\n" +
//                    "}]S";
//
//
//            balance += delta;
//            if (count % 100 == 0) {
//                delta = delta * -1;
//                if(delta>0) {
//                    balance =+20;
//                }
//            }
//
//            String method = "DexFundNewOrder";
//            List params = new ArrayList<>();
//            params.add("eedefc9fdedd534f284a54be1ea" + System.currentTimeMillis());
//            params.add("tti_c2695839043cf966f370ac84");
//            params.add("tti_5649544520544f4b454e6e40");
//            params.add(false);
//            params.add(0);
//            params.add(balance + "");
//            params.add(100);
//
//            JSONObject order = viteClient.callContract(key.getHexAddress(), "vite_000000000000000000000000000000000000000617d47459a8", "tti_5649544520544f4b454e6e40", "0", abi, method, params, false, key.getHexPriKey());
//
//            Thread.sleep(100);
//            //9000000000000000000
//
//
//            System.out.println(JSONObject.toJSONString(order));
//
//
//            params.clear();
//            params.add("eedefc9fdedd534f284a54be1ea" + System.currentTimeMillis());
//            params.add("tti_c2695839043cf966f370ac84");
//            params.add("tti_5649544520544f4b454e6e40");
//            params.add(true);
//            params.add(0);
//            params.add(balance + "");
//            params.add(100);
//            order = viteClient.callContract(key.getHexAddress(), "vite_000000000000000000000000000000000000000617d47459a8", "tti_5649544520544f4b454e6e40", "0", abi, method, params, false, key.getHexPriKey());
//
//
//            Thread.sleep(100);
//            //9000000000000000000
//
//            System.out.println(JSONObject.toJSONString(order));
//        }
    }


}