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.
Merge pull request #134 from onflow/get-account-balance
Get account balance endpoints
- Loading branch information
Showing
12 changed files
with
559 additions
and
0 deletions.
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()); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...otlin/org/onflow/examples/kotlin/getAccountBalance/GetAccountBalanceAccessAPIConnector.kt
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,19 @@ | ||
package org.onflow.examples.kotlin.getAccountBalance | ||
|
||
import org.onflow.flow.sdk.* | ||
|
||
internal class GetAccountBalanceAccessAPIConnector( | ||
private val accessAPI: FlowAccessApi | ||
) { | ||
fun getBalanceAtLatestBlock(address: FlowAddress): Long = | ||
when (val response = accessAPI.getAccountBalanceAtLatestBlock(address)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
|
||
fun getBalanceAtBlockHeight(address: FlowAddress, height: Long): Long = | ||
when (val response = accessAPI.getAccountBalanceAtBlockHeight(address, height)) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> response.data | ||
is FlowAccessApi.AccessApiCallResponse.Error -> throw Exception(response.message, response.throwable) | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...n/org/onflow/examples/kotlin/getAccountBalance/GetAccountBalanceAccessAPIConnectorTest.kt
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,73 @@ | ||
package org.onflow.examples.kotlin.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 | ||
|
||
@FlowEmulatorProjectTest(flowJsonLocation = "../flow/flow.json") | ||
internal class GetAccountBalanceAccessAPIConnectorTest { | ||
@FlowServiceAccountCredentials | ||
lateinit var serviceAccount: TestAccount | ||
|
||
@FlowTestClient | ||
lateinit var accessAPI: FlowAccessApi | ||
|
||
private lateinit var balanceAPIConnector: GetAccountBalanceAccessAPIConnector | ||
|
||
@BeforeEach | ||
fun setup() { | ||
balanceAPIConnector = GetAccountBalanceAccessAPIConnector(accessAPI) | ||
} | ||
|
||
@Test | ||
fun `Can fetch account balance at the latest block`() { | ||
val address = serviceAccount.flowAddress | ||
val balance = balanceAPIConnector.getBalanceAtLatestBlock(address) | ||
|
||
Assertions.assertNotNull(balance, "Balance should not be null") | ||
Assertions.assertTrue(balance >= 0, "Balance should be non-negative") | ||
} | ||
|
||
@Test | ||
fun `Can fetch account balance at a specific block height`() { | ||
val address = serviceAccount.flowAddress | ||
val latestBlock = accessAPI.getLatestBlock(true) // Fetch the latest sealed block | ||
|
||
when (latestBlock) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> { | ||
val balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, latestBlock.data.height) | ||
|
||
Assertions.assertNotNull(balanceAtHeight, "Balance at specific block height should not be null") | ||
Assertions.assertTrue(balanceAtHeight >= 0, "Balance at specific block height should be non-negative") | ||
} | ||
is FlowAccessApi.AccessApiCallResponse.Error -> Assertions.fail("Failed to retrieve the latest block: ${latestBlock.message}") | ||
} | ||
} | ||
|
||
@Test | ||
fun `Balances at the latest block and specific block height should match`() { | ||
val address = serviceAccount.flowAddress | ||
|
||
// Fetch balance at latest block | ||
val balanceAtLatest = balanceAPIConnector.getBalanceAtLatestBlock(address) | ||
|
||
// Fetch latest block height | ||
val latestBlock = accessAPI.getLatestBlock(true) | ||
when (latestBlock) { | ||
is FlowAccessApi.AccessApiCallResponse.Success -> { | ||
val blockHeight = latestBlock.data.height | ||
|
||
// Fetch balance at the same block height | ||
val balanceAtHeight = balanceAPIConnector.getBalanceAtBlockHeight(address, blockHeight) | ||
|
||
Assertions.assertEquals(balanceAtLatest, balanceAtHeight, "Balance at latest block and specific block height should match") | ||
} | ||
is FlowAccessApi.AccessApiCallResponse.Error -> Assertions.fail("Failed to retrieve the latest block: ${latestBlock.message}") | ||
} | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.