forked from the-nft-company/flow-jvm-sdk
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
90ce567
commit 397339e
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
.../java/org/onflow/examples/java/getAccountBalance/GetAccountBalanceAccessAPIConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.onflow.examples.java.getAccountBalance; | ||
|
||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowAddress; | ||
|
||
public class GetAccountBalanceAccessAPIConnector { | ||
private final FlowAccessApi accessAPI; | ||
|
||
public GetAccountBalanceAccessAPIConnector(FlowAccessApi accessAPI) { | ||
this.accessAPI = accessAPI; | ||
} | ||
|
||
public long getBalanceAtLatestBlock(FlowAddress address) { | ||
FlowAccessApi.AccessApiCallResponse<Long> response = accessAPI.getAccountBalanceAtLatestBlock(address); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<Long>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
|
||
public long getBalanceAtBlockHeight(FlowAddress address, long height) { | ||
FlowAccessApi.AccessApiCallResponse<Long> response = accessAPI.getAccountBalanceAtBlockHeight(address, height); | ||
|
||
if (response instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
return ((FlowAccessApi.AccessApiCallResponse.Success<Long>) response).getData(); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) response; | ||
throw new RuntimeException(errorResponse.getMessage(), errorResponse.getThrowable()); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...a/org/onflow/examples/java/getAccountBalance/GetAccountBalanceAccessAPIConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package org.onflow.examples.java.getAccountBalance; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.onflow.flow.common.test.FlowEmulatorProjectTest; | ||
import org.onflow.flow.common.test.FlowServiceAccountCredentials; | ||
import org.onflow.flow.common.test.FlowTestClient; | ||
import org.onflow.flow.common.test.TestAccount; | ||
import org.onflow.flow.sdk.FlowAccessApi; | ||
import org.onflow.flow.sdk.FlowAddress; | ||
import org.onflow.flow.sdk.FlowBlock; | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
public class GetAccountBalanceAccessAPIConnectorTest { | ||
|
||
@FlowTestClient | ||
private FlowAccessApi accessAPI; | ||
|
||
@FlowServiceAccountCredentials | ||
private TestAccount serviceAccount; | ||
|
||
private GetAccountBalanceAccessAPIConnector balanceAPIConnector; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
balanceAPIConnector = new GetAccountBalanceAccessAPIConnector(accessAPI); | ||
} | ||
|
||
@Test | ||
public void testCanFetchBalanceAtLatestBlock() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
long balance = balanceAPIConnector.getBalanceAtLatestBlock(address); | ||
|
||
Assertions.assertTrue(balance >= 0, "Balance at the latest block should be non-negative"); | ||
} | ||
|
||
@Test | ||
public void testCanFetchBalanceAtSpecificBlockHeight() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
|
||
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true); | ||
|
||
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); | ||
long blockHeight = latestBlock.getHeight(); | ||
long balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight); | ||
|
||
Assertions.assertTrue(balanceAtHeight >= 0, "Balance at specific block height should be non-negative"); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) latestBlockResponse; | ||
Assertions.fail("Failed to fetch the latest block: " + errorResponse.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testBalancesAtLatestBlockAndSpecificHeightShouldMatch() { | ||
FlowAddress address = serviceAccount.getFlowAddress(); | ||
|
||
long balanceAtLatest = balanceAPIConnector.getBalanceAtLatestBlock(address); | ||
FlowAccessApi.AccessApiCallResponse<FlowBlock> latestBlockResponse = accessAPI.getLatestBlock(true); | ||
|
||
if (latestBlockResponse instanceof FlowAccessApi.AccessApiCallResponse.Success) { | ||
FlowBlock latestBlock = ((FlowAccessApi.AccessApiCallResponse.Success<FlowBlock>) latestBlockResponse).getData(); | ||
long blockHeight = latestBlock.getHeight(); | ||
|
||
// Fetch balance at the same block height | ||
long balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight); | ||
|
||
// Ensure balances match | ||
Assertions.assertEquals(balanceAtLatest, balanceAtHeight, "Balance at latest block and specific block height should match"); | ||
} else { | ||
FlowAccessApi.AccessApiCallResponse.Error errorResponse = (FlowAccessApi.AccessApiCallResponse.Error) latestBlockResponse; | ||
Assertions.fail("Failed to fetch the latest block: " + errorResponse.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters