ETH开发 JSONRPC

因为时间的原因,暂时没有对异常信息记性整理


package com.bib.wallet.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * JSONRPC 方法抽象
 *
 * @author 小呆呆
 * @create 2019-08-29 11:49
 **/
@Service
public class JsonRPC {

    private static final Integer ID = 1;

    private static final String JSONRPC = "2.0";
    private static final String LATEST = "latest";
    private static final String PENDING = "pending";

    private static final String PERSONAL_NEW_ACCOUNT = "personal_newAccount";
    private static final String ETH_ACCOUNTS = "eth_accounts";
    private static final String ETH_GETBALANCE = "eth_getBalance";
    private static final String PERSONAL_UNLOCK_ACCOUNT = "personal_unlockAccount";
    private static final String ETH_SEND_TRANSACTION = "eth_sendTransaction";
    private static final String ETH_GET_TRANSACTION_COUNT = "eth_getTransactionCount";
    private static final String ETH_SING_TRANSACTION = "eth_signTransaction";
    private static final String ETH_SEND_RAW_TRANSACTION = "eth_sendRawTransaction";
    private static final String ETH_ESTIMATE_GAS = "eth_estimateGas";
    private static final String ETH_GAS_PRICE = "eth_gasPrice";
    private static final String ETH_PENDING_TRANSACTIONS = "eth_pendingTransactions";
    private static final String ETH_BLOCK_NUMBER = "eth_blockNumber";
    private static final String ETH_GET_BLOCK_NUMBER = "eth_getBlockByNumber";
    private static final String ETH_CALL = "eth_call";

    // private static final String http_host = "http://47.56.160.110:8719";
    private static final String http_host = "http://127.0.0.1:8719";
    private static final String PASSWORD = "123456";

    /**
     * 获取所有的账户
     * @return
     */
    public static JSONArray getEthAccounts(){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("method",ETH_ACCOUNTS);
        map.put("id",ID);
        map.put("params",new ArrayList<String>());
        String postWithjson =null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null) {
            return null; // 调用接口失败
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);

        //JSONArray result = jsonObject.getJSONArray("result");
        return jsonObject.getJSONArray("result");
    }

    /**
     * 获取账户余额
     * @param address  钱包地址
     * @return
     */
    public static String getBalance(String address){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("method",ETH_GETBALANCE);
        map.put("id",ID);
        List<String> list = new ArrayList<>();
        list.add(address);
        list.add(LATEST);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){ // 访问失败
            return postWithjson;
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){  // 地址不存在
            return null;
        }
        return jsonObject.get("result").toString();
    }

    /**
     * 创建一个新的账户
     * @return
     */
    public static String createNewAccount(){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("method",PERSONAL_NEW_ACCOUNT);
        map.put("id",ID);
        List<String> list = new ArrayList<>();
        list.add(PASSWORD);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){ // 访问失败
            return postWithjson;
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){  // 生成地址失败
            return null;
        }
        return jsonObject.get("result").toString();
    }

    /**
     * 解锁制定的账户
     * @param address 钱包地址
     * @param time  解锁时间 s
     * @return boolean
     */
    public static Boolean personUnlockAccount(String address, Long time){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("method",PERSONAL_UNLOCK_ACCOUNT);
        map.put("id",ID);
        List<Object> list = new ArrayList<>();
        list.add(address);
        list.add(PASSWORD);
        list.add(time);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            System.out.printf("1");
            return false;  // 解锁用户信息失败
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            System.out.printf("2");
            return false; // 用户信息错误
        }
        return jsonObject.getBoolean("result");
    }

    /**
     * 直接交易的接口
     * @param from
     * @param to
     * @param gas
     * @param gasPrice
     * @param value
     * @param data
     * @return 返回交易hash
     */
    public static String ethSendTransaction(String from, String to, String gas, String gasPrice, String value, String data){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_SEND_TRANSACTION);
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> params = new HashMap<>();
        params.put("from",from);
        params.put("to",to);
        params.put("gas",gas);
        params.put("gasPrice",gasPrice);
        params.put("value",value);  // todo  ETH填金额  ERC 20 可以 不填
        params.put("data",data);
        list.add(params);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 交易异常,请重新交易
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2"; // 交易失败  具体原因另看
        }

        return jsonObject.get("result").toString(); // 返回交易hash
    }
    /**
     * todo 热钱包不需要设置
     * 直接交易的接口
     * @param from
     * @param to
     * @param gas
     * @param gasPrice
     * @param value
     * @param data
     * @return 返回交易hash
     */
    public static String ethSendTransaction1(String from, String to,  String value, String data){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_SEND_TRANSACTION);
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> params = new HashMap<>();
        params.put("from",from);
        params.put("to",to);
        params.put("value",value);  // todo  ETH填金额  ERC 20 可以 不填
        params.put("data",data);
        list.add(params);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 交易异常,请重新交易
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2"; // 交易失败  具体原因另看
        }

        return jsonObject.get("result").toString(); // 返回交易hash
    }

    /**
     * 查询地址发送交易的数量,下次交易的nonce从这个开始
     * @param address
     * @return 返回交易的数量
     */
    public static String ethGetTransactionCount(String address){
        Map<String,Object> map = new HashMap<>();
        map.put("method",ETH_GET_TRANSACTION_COUNT);
        map.put("id",ID);
        List<String> list = new ArrayList<>();
        list.add(address);
        list.add(PENDING);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 获取 nonce 出现异常
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2"; // 参数有误
        }
        return jsonObject.get("result").toString();
    }

    /**
     * 构建原始交易,返回原始交易的签名 ETH
     * @param from
     * @param to
     * @param gas
     * @param gasPrice
     * @param value
     * @param data
     * @param nonce
     * @return 从冷钱包到热钱包的hash
     */
    public static String ethSignTranscation(String from, String to, String gas, String gasPrice, String value, String data, String nonce){
        Map<String,Object> map = new HashMap<>();
        map.put("method",ETH_SING_TRANSACTION);
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> params = new HashMap<>();
        params.put("from",from);
        params.put("to",to);
        params.put("gas",gas);
        params.put("gasPrice",gasPrice);
        params.put("value",value);
        params.put("data",data);
        params.put("nonce",nonce);
        list.add(params);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 构建原始交易异常,请重试
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));

            return "2"; // 构建原始交易失败,具体未整理
        }
        return JSONObject.parseObject(String.valueOf(jsonObject.getJSONObject("result"))).getString("raw");
    }

    /**
     * 构建原始交易,返回原始交易的签名 合约地址
     * @param from
     * @param to
     * @param gas
     * @param gasPrice
     * @param value
     * @param data
     * @param nonce
     * @return 从冷钱包到热钱包的hash
     * todo to
     */
    // todo  这个接口重新改一遍
    public static String ethSignTranscationContract(String from, String to, String gas, String gasPrice, String value, String data, String nonce){
        Map<String,Object> map = new HashMap<>();
        map.put("method",ETH_SING_TRANSACTION);
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> params = new HashMap<>();
        params.put("from",from);
        params.put("to",to);
        params.put("gas",gas);
        params.put("gasPrice",gasPrice);
        params.put("value",value);
        params.put("data",data);
        params.put("nonce",nonce);
        list.add(params);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 构建原始交易异常,请重试
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2"; // 构建原始交易失败,具体未整理
        }
        return JSONObject.parseObject(String.valueOf(jsonObject.getJSONObject("result"))).getString("raw");
    }

    /**
     * 广播交易(判断有没有错误的情况,如果有,则写入数据 库)
     * @param hash_code  离线签名 hash
     * @return  交易成功的 hash
     */
    public static String ethSendRawTransaction(String hash_code){
        Map<String,Object> map = new HashMap<>();
        map.put("method",ETH_SEND_RAW_TRANSACTION);
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);

        List<String> list = new ArrayList<>();
        list.add(hash_code);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 广播交易异常,请重试
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2";  // 广播交易失败,请查看原因
        }
        return jsonObject.getString("result");
    }

    /**
     * 返回交易可能需要的 gas
     * @param from  发出的地址
     * @param to    接受的地址
     * @param gas   可不填
     * @param gasPrice  可不填
     * @param value 可不填
     * @param data  可不填写
     * @return
     */
    public static String ethEstimateGas(String from,String to,String gas,String gasPrice,String value,String data){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_ESTIMATE_GAS);
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> params = new HashMap<>();
        params.put("from",from);
        params.put("to",to);
        params.put("gas",gas);
        params.put("gasPrice",gasPrice);
        params.put("value",value);
        params.put("data",data);
        list.add(params);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 获取gas异常,请重新获取
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2"; // 获取gas失败,请查询原因
        }
        return jsonObject.getString("result"); // 返回 可能需要的gas
    }

    /**
     * 返回挂起的事务列表。
     * @return  返回 list 表示代处理的事物
     */
    public static List<Object> eth_pendingTransactions(){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_PENDING_TRANSACTIONS);
        List<String> list = new ArrayList<>();
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> list1 = new ArrayList<>();
        if (postWithjson == null){
            list1.add("10001");
            return list1;
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            list1.add("10002");
            return list1;
        }
        return jsonObject.getJSONArray("result");
    }
    // ETH_BLOCK_NUMBER

    /**
     * 获取当前区块的高度
     * @return
     */
    public static BigInteger ethBlockNumber(){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_BLOCK_NUMBER);
        List<String> list = new ArrayList<>();
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> list1 = new ArrayList<>();
        if (postWithjson == null){
            list1.add("10001");
            return new BigInteger("1");
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            list1.add("10002");
            return new BigInteger("2");
        }
        System.err.println(jsonObject.getString("result"));
        return new BigInteger(Decimal.haxToBingInt(jsonObject.getString("result").substring(2)));
    }

    /**
     * 获取指定高度的交易信息
     * @param hax
     * @return
     */
    public static Object ethGetBlockByNumber(String hax){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_GET_BLOCK_NUMBER);
        List<Object> list = new ArrayList<>();
        list.add(hax);
        list.add(true);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return new BigInteger("1");
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return new BigInteger("2");
        }
        JSONObject result = jsonObject.getJSONObject("result");
        return result.getJSONArray("transactions");
    }

    /**
     * 获取ERC20的名字
     * @param contract
     * @return
     */
    public static String getERC20Name(String contract){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_CALL);
        List<Object> list = new ArrayList<>();
        Map<String,Object> maps = new HashMap<>();
        maps.put("to",contract);
        maps.put("data","0x95d89b41000000000000000000000000");
        list.add(maps);
        list.add(LATEST);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1";
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2";
        }
        return Decimal.hexStringToString(jsonObject.getString("result").substring(130).replace("0",""));
    }

    /**
     * 获取 ERC20 需要程序 10 次方 number
     * @param contract
     * @return
     */
    public static String getERC20Number(String contract){
        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_CALL);
        List<Object> list = new ArrayList<>();
        Map<String,Object> maps = new HashMap<>();
        maps.put("to",contract);
        maps.put("data","0x313ce567000000000000000000000000");
        list.add(maps);
        list.add(LATEST);
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1";
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2";
        }
        System.err.println(jsonObject.getString("result").substring(2).replace("0",""));
        return Decimal.haxToBingInt(jsonObject.getString("result").substring(2).replace("0","").equals("") ? "0" : jsonObject.getString("result").substring(2).replace("0",""));
    }

    // ETH_GAS_PRICE

    public static String ethGasPrice(){

        Map<String,Object> map = new HashMap<>();
        map.put("jsonrpc",JSONRPC);
        map.put("id",ID);
        map.put("method",ETH_GAS_PRICE);
        List<Map<String,String>> list = new ArrayList<>();
        map.put("params",list);
        String postWithjson = null;
        try {
            postWithjson = HttpUtils.httpPostWithjson(http_host, new JSONObject(map).toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (postWithjson == null){
            return "1"; // 获取gas异常,请重新获取
        }
        JSONObject jsonObject = JSONObject.parseObject(postWithjson);
        if (jsonObject.get("error") != null){
            System.err.println(jsonObject.get("error"));
            return "2"; // 获取gas失败,请查询原因
        }
        return jsonObject.getString("result"); // 返回 可能需要的gas
    }

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值