Skip to content

Commit

Permalink
feat: add getTransactionsInRange api.
Browse files Browse the repository at this point in the history
Signed-off-by: grapebaba <281165273@qq.com>
  • Loading branch information
GrapeBaBa committed Nov 13, 2022
1 parent 56c5ab7 commit b92f34c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/integrationTest/java/io/sui/SuiClientImplIntTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,18 @@ void getTransaction() throws ExecutionException, InterruptedException {
assertEquals(
"0xb5e91320d3acc77b4d9e66a218031441b2be1bb3", moveEvent.getFields().get("object_id"));
}

/**
* Gets transactions in range.
*
* @throws ExecutionException the execution exception
* @throws InterruptedException the interrupted exception
*/
@Test
@DisplayName("Test getTransactionsInRange.")
void getTransactionsInRange() throws ExecutionException, InterruptedException {
CompletableFuture<List<String>> res = client.getTransactionsInRange(0L, 100L);
assertEquals(2, res.get().size());
assertEquals("GN9sW4hBVNFIc83VIfyn/J1n4a9tU9sQVq3+UkfgEKU=", res.get().get(1));
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/sui/SuiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,13 @@ public interface SuiClient {
* @return the transaction
*/
CompletableFuture<TransactionResponse> getTransaction(String digest);

/**
* Gets transactions in range.
*
* @param start the start
* @param end the end
* @return the transactions in range
*/
CompletableFuture<List<String>> getTransactionsInRange(Long start, Long end);
}
7 changes: 7 additions & 0 deletions src/main/java/io/sui/SuiClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public CompletableFuture<TransactionResponse> getTransaction(String digest) {
return call("/sui_getTransaction", request, new TypeToken<TransactionResponse>() {}.getType());
}

@Override
public CompletableFuture<List<String>> getTransactionsInRange(Long start, Long end) {
final JsonRpc20Request request =
createJsonRpc20Request("sui_getTransactionsInRange", Lists.newArrayList(start, end));
return call("/sui_getTransactionsInRange", request, new TypeToken<List<String>>() {}.getType());
}

private JsonRpc20Request createJsonRpc20Request(String method, List<?> params) {
final JsonRpc20Request request = new JsonRpc20Request();
request.setId(jsonRpcClientProvider.nextId());
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/io/sui/SuiClientImplTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ public MockResponse dispatch(RecordedRequest request) {
}
}

if ("/sui_getTransactionsInRange".equals(request.getPath())) {
JsonRpc20Request jsonRpc20Request =
((GsonJsonHandler) jsonHandler).getGson().fromJson(body, JsonRpc20Request.class);
if ((Long) jsonRpc20Request.getParams().get(1) == 100L) {
return getMockResponse("mockdata/getTransactionsInRange.json");
}
}

return new MockResponse().setResponseCode(404);
}
};
Expand Down Expand Up @@ -399,4 +407,18 @@ void getTransaction() throws ExecutionException, InterruptedException {
assertEquals(
"0xb5e91320d3acc77b4d9e66a218031441b2be1bb3", moveEvent.getFields().get("object_id"));
}

/**
* Gets transactions in range.
*
* @throws ExecutionException the execution exception
* @throws InterruptedException the interrupted exception
*/
@Test
@DisplayName("Test getTransactionsInRange.")
void getTransactionsInRange() throws ExecutionException, InterruptedException {
CompletableFuture<List<String>> res = client.getTransactionsInRange(0L, 100L);
assertEquals(2, res.get().size());
assertEquals("GN9sW4hBVNFIc83VIfyn/J1n4a9tU9sQVq3+UkfgEKU=", res.get().get(1));
}
}
8 changes: 8 additions & 0 deletions src/test/resources/mockdata/getTransactionsInRange.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"jsonrpc": "2.0",
"result": [
"3Dda4/74LXf6GmOxMxp3qdbW/WdQ6/8EHobZ1LvSyYk=",
"GN9sW4hBVNFIc83VIfyn/J1n4a9tU9sQVq3+UkfgEKU="
],
"id": 1
}

0 comments on commit b92f34c

Please sign in to comment.