# eosRpcJ
**Repository Path**: warrior666/eosRpcJ
## Basic Information
- **Project Name**: eosRpcJ
- **Description**: eos rpc java
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2018-12-10
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# eosRpcJ
#### 项目介绍
对接eos rpc api,包括以下功能:
- 1.获取区块链信息
- 2.创建新账号
- 3.查询账户余额
- 4.充值处理
- 5.离线签名转账
- 6.资源抵押、赎回
- 7.ram购买、售出
#### 安装教程
1. 将项目打包成jar,导入本地仓库
2. 添加一下maven:
`
com.squareup.retrofit2
retrofit
2.4.0
com.squareup.retrofit2
converter-jackson
2.4.0
com.warrior.eos
eosRpcJ
1.0-SNAPSHOT
`
#### 使用说明
1. 测试类如下:
```
public class EosApiTest {
//测试网络
final static EosApi eosRpcService = new EosApi("https://api-kylin.eosasia.one");
//正式网络
//final static EosApi eosRpcService = new EosApi("https://proxy.eosnode.tools");
final static String privateKey = "5KezfC7aGLn9K6DGumST6iCzTdDZvd1Tt6F2T8gDKCVJVu9PX9p";//填写你的账户私钥
//1.获取区块链信息
@Test
public void getChainInfo() {
long t1 = System.currentTimeMillis();
ChainInfo chainInfo = eosRpcService.getChainInfo();
long t2 = System.currentTimeMillis();
System.out.println(JSON.toJSON(chainInfo));
System.out.println("time:" + (t2 - t1));
}
//2.创建新账号
@Test
public void createAccount() {
Transaction transaction = null;
try {
//生成新的秘钥对
String owner = "";
Map keyMap = eosRpcService.generateKey();
if (keyMap != null) {
owner = keyMap.get(EosApi.K_publicKey).toString();
}
transaction = eosRpcService.createAccount(privateKey,
"linqihong123", "linqihong115", owner, owner, 4096l);
} catch (ApiException e) {
ApiError apiError = e.getError();
System.out.println("code:" + apiError.getError().getCode());
System.out.println("msg:" + apiError.getError().getWhat());
if (apiError.getError().getCode().equals("3050001")) {
System.out.println("该账户名已经被占用");
}
} catch (EException e) {
System.out.println(e.getCode());
System.out.println(e.getMsg());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(JSON.toJSON(transaction));
}
//3.查询账户余额
@Test
public void balance() {
double balance = eosRpcService.getBalance("linqihong123");
System.out.println(balance);
}
//5.充值处理
@Test
public void recharge() throws Exception {
List accountList = new ArrayList<>();
accountList.add("linqihong123");
List actions = eosRpcService.getActions(accountList);
System.out.println(JSON.toJSONString(actions));
}
//6.生成秘钥对
@Test
public void generateKey() throws Exception {
Map resultMap = eosRpcService.generateKey();
System.out.println("resultMap:" + resultMap);
}
//7.获取账户信息
@Test
public void getAccount() {
Account account = eosRpcService.getAccount("linqihong123");
System.out.println(JSON.toJSONString(account));
}
//8.转账
@Test
public void transfer() {
Transaction transaction = null;
try {
transaction = eosRpcService.transfer(privateKey, "linqihong123", "linqihong124", 1, "hhahah");
} catch (ApiException e) {
ApiError apiError = e.getError();
if (apiError.getError().getCode().equals("3080002")) {
System.out.println("交易超出了对交易强加的当前网络使用限制");
}
System.out.println("code:" + apiError.getError().getCode());
System.out.println("msg:" + apiError.getError().getWhat());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(JSON.toJSONString(transaction));
}
//9.计算创建账户的费用:fee=1.005*内存(kb)*汇率+ 抵押cpu的EOS + 抵押net的EOS
@Test
public void getRate() {
double rate = eosRpcService.getRate();
double fee = 1.005 * 3 * rate;
System.out.println(fee);
}
//10.抵押资源-以租借的方式
@Test
public void delegatebw1() throws Exception {
Transaction transaction = eosRpcService.delegatebw(privateKey, "linqihong123", "linqihong124", 1, 1, 0L);
System.out.println(JSON.toJSONString(transaction));
}
//11.抵押资源-以赠送的方式
@Test
public void delegatebw2() throws Exception {
Transaction transaction = eosRpcService.delegatebw(privateKey, "linqihong123", "linqihong124", 1, 0, 1L);
System.out.println(JSON.toJSONString(transaction));
}
//12.赎回资源
@Test
public void unDelegatebw() throws Exception {
Transaction transaction = eosRpcService.unDelegatebw(privateKey, "linqihong123", "linqihong124", 1, 1);
System.out.println(JSON.toJSONString(transaction));
}
//13.购买内存
@Test
public void buyRam() throws Exception {
Transaction transaction = eosRpcService.buyRam(privateKey, "linqihong123", "linqihong124", 2*1024L);
System.out.println(JSON.toJSONString(transaction));
}
//14.销售内存
@Test
public void sellRam() throws Exception {
Transaction transaction = eosRpcService.sellRam(privateKey, "linqihong123", 1024L);
System.out.println(JSON.toJSONString(transaction));
}
}
```