因为时间的原因,暂时没有对异常信息记性整理
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;
@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://127.0.0.1:8719";
private static final String PASSWORD = "123456";
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);
return jsonObject.getJSONArray("result");
}
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();
}
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();
}
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");
}
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);
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();
}
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);
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();
}
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";
}
JSONObject jsonObject = JSONObject.parseObject(postWithjson);
if (jsonObject.get("error") != null){
System.err.println(jsonObject.get("error"));
return "2";
}
return jsonObject.get("result").toString();
}
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");
}
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");
}
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");
}
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";
}
JSONObject jsonObject = JSONObject.parseObject(postWithjson);
if (jsonObject.get("error") != null){
System.err.println(jsonObject.get("error"));
return "2";
}
return jsonObject.getString("result");
}
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");
}
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)));
}
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");
}
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",""));
}
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",""));
}
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";
}
JSONObject jsonObject = JSONObject.parseObject(postWithjson);
if (jsonObject.get("error") != null){
System.err.println(jsonObject.get("error"));
return "2";
}
return jsonObject.getString("result");
}
}