Skip to content

Commit 397339e

Browse files
committed
Setup java examples
1 parent 90ce567 commit 397339e

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.onflow.examples.java.getAccountBalance;
2+
3+
import org.onflow.flow.sdk.FlowAccessApi;
4+
import org.onflow.flow.sdk.FlowAddress;
5+
6+
public class GetAccountBalanceAccessAPIConnector {
7+
private final FlowAccessApi accessAPI;
8+
9+
public GetAccountBalanceAccessAPIConnector(FlowAccessApi accessAPI) {
10+
this.accessAPI = accessAPI;
11+
}
12+
13+
public long getBalanceAtLatestBlock(FlowAddress address) {
14+
FlowAccessApi.AccessApiCallResponse<Long> response = accessAPI.getAccountBalanceAtLatestBlock(address);
15+
16+
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
17+
return ((FlowAccessApi.AccessApiCallResponse.Success<Long>) response).getData();
18+
} else {
19+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
20+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
21+
}
22+
}
23+
24+
public long getBalanceAtBlockHeight(FlowAddress address, long height) {
25+
FlowAccessApi.AccessApiCallResponse<Long> response = accessAPI.getAccountBalanceAtBlockHeight(address, height);
26+
27+
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) {
28+
return ((FlowAccessApi.AccessApiCallResponse.Success<Long>) response).getData();
29+
} else {
30+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response;
31+
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable());
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.onflow.examples.java.getAccountBalance;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.onflow.flow.common.test.FlowEmulatorProjectTest;
7+
import org.onflow.flow.common.test.FlowServiceAccountCredentials;
8+
import org.onflow.flow.common.test.FlowTestClient;
9+
import org.onflow.flow.common.test.TestAccount;
10+
import org.onflow.flow.sdk.FlowAccessApi;
11+
import org.onflow.flow.sdk.FlowAddress;
12+
import org.onflow.flow.sdk.FlowBlock;
13+
14+
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json")
15+
public class GetAccountBalanceAccessAPIConnectorTest {
16+
17+
@FlowTestClient
18+
private FlowAccessApi accessAPI;
19+
20+
@FlowServiceAccountCredentials
21+
private TestAccount serviceAccount;
22+
23+
private GetAccountBalanceAccessAPIConnector balanceAPIConnector;
24+
25+
@BeforeEach
26+
public void setup() {
27+
balanceAPIConnector = new GetAccountBalanceAccessAPIConnector(accessAPI);
28+
}
29+
30+
@Test
31+
public void testCanFetchBalanceAtLatestBlock() {
32+
FlowAddress address = serviceAccount.getFlowAddress();
33+
long balance = balanceAPIConnector.getBalanceAtLatestBlock(address);
34+
35+
Assertions.assertTrue(balance >= 0, "Balance at the latest block should be non-negative");
36+
}
37+
38+
@Test
39+
public void testCanFetchBalanceAtSpecificBlockHeight() {
40+
FlowAddress address = serviceAccount.getFlowAddress();
41+
42+
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true);
43+
44+
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) {
45+
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData();
46+
long blockHeight = latestBlock.getHeight();
47+
long balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight);
48+
49+
Assertions.assertTrue(balanceAtHeight >= 0, "Balance at specific block height should be non-negative");
50+
} else {
51+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) latestBlockResponse;
52+
Assertions.fail("Failed to fetch the latest block: " + errorResponse.getMessage());
53+
}
54+
}
55+
56+
@Test
57+
public void testBalancesAtLatestBlockAndSpecificHeightShouldMatch() {
58+
FlowAddress address = serviceAccount.getFlowAddress();
59+
60+
long balanceAtLatest = balanceAPIConnector.getBalanceAtLatestBlock(address);
61+
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true);
62+
63+
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) {
64+
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData();
65+
long blockHeight = latestBlock.getHeight();
66+
67+
// Fetch balance at the same block height
68+
long balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight);
69+
70+
// Ensure balances match
71+
Assertions.assertEquals(balanceAtLatest, balanceAtHeight, "Balance at latest block and specific block height should match");
72+
} else {
73+
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) latestBlockResponse;
74+
Assertions.fail("Failed to fetch the latest block: " + errorResponse.getMessage());
75+
}
76+
}
77+
}

sdk/src/intTest/org/onflow/flow/sdk/transaction/TransactionIntegrationTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ class TransactionIntegrationTest {
210210
assertThat(normalizedBalance).isEqualTo(account.balance.toBigInteger().longValueExact())
211211
}
212212

213-
214213
@Test
215214
fun `Can get latest block`() {
216215
val latestBlock = try {

0 commit comments

Comments
 (0)