Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions src/main/java/org/starcoin/api/StateRPCClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private String tokenInfoParameter(String code) {
return "0x1::Token::TokenInfo<" + code + ">";
}

private ListResource getListResource(String address,List<String> resourceTypes,String stateRoot,Integer startIndex,Integer maxSize) throws JSONRPC2SessionException{
private ListResource getListResource(String address, List<String> resourceTypes, String stateRoot, Integer startIndex, Integer maxSize) throws JSONRPC2SessionException {
JsonRPCClient<ListResource> client = new JsonRPCClient<>();
List<Object> parameter = new ArrayList<>();
parameter.add(address);
Expand All @@ -164,46 +164,67 @@ private ListResource getListResource(String address,List<String> resourceTypes,S

/**
* 通过 address 查询当前账户下的所有资源集合
*
* @param address 地址
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getAllResourcesByAddress(String address) throws JSONRPC2SessionException{
return this.getListResource(address,null,null,null,null);
public ListResource getAllResourcesByAddress(String address) throws JSONRPC2SessionException {
return this.getListResource(address, null, null, null, null);
}

/**
* 通过 address 和 resourceTypes 查询当前账户下的所有资源集合
* @param address 地址
*
* @param address 地址
* @param resourceTypes 资源类型集合
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getAllResourcesByResourceTypes(String address,List<String> resourceTypes) throws JSONRPC2SessionException{
return this.getListResource(address,resourceTypes,null,null,null);
public ListResource getAllResourcesByResourceTypes(String address, List<String> resourceTypes) throws JSONRPC2SessionException {
return this.getListResource(address, resourceTypes, null, null, null);
}

/**
* 通过 address 和 TypeTags 查询当前账户下的所有资源集合
*
* @param address
* @param typeTags
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getAllResourcesByTypeTags(String address,List<String> typeTags) throws JSONRPC2SessionException{
return this.getListResource(address,typeTags,null,null,null);
public ListResource getAllResourcesByTypeTags(String address, List<String> typeTags) throws JSONRPC2SessionException {
return this.getListResource(address, typeTags, null, null, null);
}

/**
* 通过 address 分页查询当前账户下的所有资源集合
*
* @param address
* @param startIndex
* @param maxSize
* @return
* @throws JSONRPC2SessionException
*/
public ListResource getResourceListByPage(String address,int startIndex,int maxSize) throws JSONRPC2SessionException{
return this.getListResource(address,null,null,startIndex,maxSize);
public ListResource getResourceListByPage(String address, int startIndex, int maxSize) throws JSONRPC2SessionException {
return this.getListResource(address, null, null, startIndex, maxSize);
}

/**
* 通过 address 查询 TableInfo
*
* @param address
* @return
* @throws JSONRPC2SessionException
*/
public TableInfo getTableInfo(String address) throws JSONRPC2SessionException, JsonProcessingException {
JsonRPCClient<TableInfo> client = new JsonRPCClient<>();
List<Object> param = new ArrayList<>();
param.add(address);
ListResourceOption option = new ListResourceOption();
option.setDecode(true);
param.add(option);
return client.getSubObject(session, "state.get_table_info", param, 0, "json", TableInfo.class);
}

}
}
30 changes: 30 additions & 0 deletions src/main/java/org/starcoin/bean/TableInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.starcoin.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;

public class TableInfo {
@JSONField(name = "key_type")
@JsonProperty("key_type")
private String keyType;

@JSONField(name = "value_type")
@JsonProperty("value_type")
private String valueType;

public String getKeyType() {
return keyType;
}

public String getValueType() {
return valueType;
}

@Override
public String toString() {
return "TableInfo{" +
"keyType='" + keyType + '\'' +
", valueType='" + valueType + '\'' +
'}';
}
}
40 changes: 25 additions & 15 deletions src/test/java/org/starcoin/api/StateRPCClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Test;
import org.starcoin.bean.ListResource;
import org.starcoin.bean.Resource;
import org.starcoin.bean.TableInfo;
import org.starcoin.bean.TokenInfo;
import org.starcoin.jsonrpc.client.JSONRPC2SessionException;

Expand Down Expand Up @@ -89,49 +90,58 @@ public void testGetAddressAmountValue() {
}

@Test
public void testGetAllResourcesByAddress() throws JSONRPC2SessionException{
public void testGetAllResourcesByAddress() throws JSONRPC2SessionException {
//查询所有
ListResource listResource = stateRPCClient.getAllResourcesByAddress("0x00000000000000000000000000000001");
Map<String, Resource> resources = listResource.getResources();
System.out.println(resources.size());
}

@Test
public void testGetResourceListByPage() throws JSONRPC2SessionException{
public void testGetResourceListByPage() throws JSONRPC2SessionException {
//分页查询 10 个
ListResource listResource = stateRPCClient.getResourceListByPage("0x00000000000000000000000000000001",1,10);
ListResource listResource = stateRPCClient.getResourceListByPage("0x00000000000000000000000000000001", 1, 10);
System.out.println(listResource.getResources().size());
}

@Test
public void testGetAllResourcesByResourceTypes() throws JSONRPC2SessionException{
public void testGetAllResourcesByResourceTypes() throws JSONRPC2SessionException {
//指定资源类型查询
String address = "0x86fDDFFbBB603C428e5c74442CE1e966";
List<String> resTypes1 = Arrays.asList("0x00000000000000000000000000000001::Account::Balance<0x00000000000000000000000000000001::STC::STC>");
ListResource listResource = stateRPCClient.getAllResourcesByResourceTypes(address,resTypes1);
System.out.println(listResource.toString());
ListResource listResource = stateRPCClient.getAllResourcesByResourceTypes(address, resTypes1);
System.out.println(listResource);
System.out.println(listResource.getResources().size());

List<String> resTypes2 = Arrays.asList("0x00000000000000000000000000000001::Account::Balance<0x00000000000000000000000000000001::STC::STC>"
,"0x00000000000000000000000000000001::Account::Balance<0x8c109349c6bd91411d6bc962e080c4a3::STAR::STAR>");
ListResource listResource2 = stateRPCClient.getAllResourcesByResourceTypes(address,resTypes2);
System.out.println(listResource2.toString());
, "0x00000000000000000000000000000001::Account::Balance<0x8c109349c6bd91411d6bc962e080c4a3::STAR::STAR>");
ListResource listResource2 = stateRPCClient.getAllResourcesByResourceTypes(address, resTypes2);
System.out.println(listResource2);
System.out.println(listResource2.getResources().size());
}

@Test
public void testGetAllResourcesByTypeTags() throws JSONRPC2SessionException{
public void testGetAllResourcesByTypeTags() throws JSONRPC2SessionException {
//指定 TypeTags 查询
String address = "0x86fDDFFbBB603C428e5c74442CE1e966";
List<String> typeTags1 = Arrays.asList("0x00000000000000000000000000000001::Account::Balance");
ListResource listResource = stateRPCClient.getAllResourcesByTypeTags(address,typeTags1);
System.out.println(listResource.toString());
ListResource listResource = stateRPCClient.getAllResourcesByTypeTags(address, typeTags1);
System.out.println(listResource);
System.out.println(listResource.getResources().size());

List<String> typeTags2 = Arrays.asList("0x00000000000000000000000000000001::Account::Balance","0x00000000000000000000000000000001::Account::Account");
ListResource listResource2 = stateRPCClient.getAllResourcesByTypeTags(address,typeTags2);
System.out.println(listResource2.toString());
List<String> typeTags2 = Arrays.asList("0x00000000000000000000000000000001::Account::Balance", "0x00000000000000000000000000000001::Account::Account");
ListResource listResource2 = stateRPCClient.getAllResourcesByTypeTags(address, typeTags2);
System.out.println(listResource2);
System.out.println(listResource2.getResources().size());
}

@Test
public void testGetTableInfo() throws JSONRPC2SessionException, JsonProcessingException {
String address = "0x86fDDFFbBB603C428e5c74442CE1e966";
TableInfo tableInfo = stateRPCClient.getTableInfo(address);
if (tableInfo != null) {
System.out.println(tableInfo);
}
}

}