diff --git a/src/main/java/org/starcoin/api/StateRPCClient.java b/src/main/java/org/starcoin/api/StateRPCClient.java index b54e3ca..9f880ff 100644 --- a/src/main/java/org/starcoin/api/StateRPCClient.java +++ b/src/main/java/org/starcoin/api/StateRPCClient.java @@ -148,7 +148,7 @@ private String tokenInfoParameter(String code) { return "0x1::Token::TokenInfo<" + code + ">"; } - private ListResource getListResource(String address,List resourceTypes,String stateRoot,Integer startIndex,Integer maxSize) throws JSONRPC2SessionException{ + private ListResource getListResource(String address, List resourceTypes, String stateRoot, Integer startIndex, Integer maxSize) throws JSONRPC2SessionException { JsonRPCClient client = new JsonRPCClient<>(); List parameter = new ArrayList<>(); parameter.add(address); @@ -164,46 +164,67 @@ private ListResource getListResource(String address,List 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 resourceTypes) throws JSONRPC2SessionException{ - return this.getListResource(address,resourceTypes,null,null,null); + public ListResource getAllResourcesByResourceTypes(String address, List 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 typeTags) throws JSONRPC2SessionException{ - return this.getListResource(address,typeTags,null,null,null); + public ListResource getAllResourcesByTypeTags(String address, List 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 client = new JsonRPCClient<>(); + List 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); } -} +} \ No newline at end of file diff --git a/src/main/java/org/starcoin/bean/TableInfo.java b/src/main/java/org/starcoin/bean/TableInfo.java new file mode 100644 index 0000000..3a8fbea --- /dev/null +++ b/src/main/java/org/starcoin/bean/TableInfo.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/test/java/org/starcoin/api/StateRPCClientTest.java b/src/test/java/org/starcoin/api/StateRPCClientTest.java index ad2f94c..291d233 100644 --- a/src/test/java/org/starcoin/api/StateRPCClientTest.java +++ b/src/test/java/org/starcoin/api/StateRPCClientTest.java @@ -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; @@ -89,7 +90,7 @@ public void testGetAddressAmountValue() { } @Test - public void testGetAllResourcesByAddress() throws JSONRPC2SessionException{ + public void testGetAllResourcesByAddress() throws JSONRPC2SessionException { //查询所有 ListResource listResource = stateRPCClient.getAllResourcesByAddress("0x00000000000000000000000000000001"); Map resources = listResource.getResources(); @@ -97,41 +98,50 @@ public void testGetAllResourcesByAddress() throws JSONRPC2SessionException{ } @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 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 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 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 typeTags2 = Arrays.asList("0x00000000000000000000000000000001::Account::Balance","0x00000000000000000000000000000001::Account::Account"); - ListResource listResource2 = stateRPCClient.getAllResourcesByTypeTags(address,typeTags2); - System.out.println(listResource2.toString()); + List 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); + } + } + }