Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: solution to query the mirror node for the account balance, account info, and contract info data #1698

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
238e60f
feat: AccountBalanceQuery via mirror node
thenswan Jan 5, 2024
9d4feb4
chore: revert GetAccountBalanceExample
thenswan Jan 5, 2024
d0a8b4b
chore: minor AccountBalanceQuery.mapResponse refactoring
thenswan Jan 5, 2024
1a3d3c1
chore: use `/api/v1/accounts/{idOrAliasOrEvmAddress}/tokens` endpoint…
thenswan Jan 5, 2024
5cf53eb
feat: updates AccountInfoQuery and ContractInfoQuery to fetch some da…
thenswan Jan 8, 2024
4fb16d1
refactor: moved logic related to mirror node querying from `EntityIdH…
thenswan Jan 8, 2024
4e7b265
Merge branch 'develop' into 01697-add-account-balanceinfo-contract-in…
thenswan Mar 15, 2024
de95077
fix: resolve merge conflicts
thenswan Mar 15, 2024
18d60d5
feat: add decimals
thenswan Mar 15, 2024
b31195e
test: ClientTest#testExecuteAsyncTimeout() and ClientTest#testExecute…
thenswan Mar 18, 2024
a900806
Merge branch 'develop' into 01697-add-account-balanceinfo-contract-in…
thenswan Mar 18, 2024
17df585
test: attempt to fix ClientTest#testExecuteAsyncTimeout() and ClientT…
thenswan Mar 18, 2024
7c80531
test: MockingTest is passing
thenswan Mar 18, 2024
19959c3
chore: update WireMock dependency to fix unit tests
thenswan Mar 19, 2024
205c3ba
test: fix integration tests
thenswan Mar 19, 2024
8463b21
test: fix some integration tests are not working with testnet or prev…
thenswan Mar 19, 2024
a47e052
test: fix some integration tests are not working with testnet or prev…
thenswan Mar 19, 2024
1e4ec7c
fix: add wait time in examples before affected queries
thenswan Mar 19, 2024
3087f08
fix: DEFAULT_LOCAL_MIRROR_NODE_ADDRESS in examples
thenswan Mar 19, 2024
1d5a67b
chore: temp disable ConsensusPubSubExample
thenswan Mar 19, 2024
e9e7971
fix: queries are now working with accounts which have alias
thenswan Mar 20, 2024
2b3bf86
fix: local grpc queries
thenswan Mar 20, 2024
da626c5
fix: LOCAL_MIRROR_NODE_REST_API_ENDPOINT const in ClientHelper
thenswan Mar 20, 2024
8d6e461
fix: add wait time to CustomFeesExample
thenswan Mar 20, 2024
0a8992b
fix: mirror network config in examples
thenswan Mar 20, 2024
aec59ac
fix: mirror network config for integration tests
thenswan Mar 20, 2024
5dc4a54
fix: local queries
thenswan Mar 21, 2024
8a5a26a
chore: update comments
thenswan Mar 21, 2024
37a8aec
chore: integration tests clean-up
thenswan Mar 21, 2024
018b724
chore: add unit test comment
thenswan Mar 21, 2024
2b3755f
Merge branch 'develop' into 01697-add-account-balanceinfo-contract-in…
thenswan Mar 21, 2024
2315e14
chore: address PR comments
thenswan Mar 24, 2024
1c43963
chore: address PR comments
thenswan Mar 25, 2024
c50946e
Merge branch 'main' into 01697-add-account-balanceinfo-contract-info-…
thenswan Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/src/main/java/AccountAliasExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public static void main(String[] args)
.execute(client)
.getReceipt(client);

// `AccountBalanceQuery` and `AccountInfoQuery` also query the mirror node.
// Wait until the mirror node is updated with the new data.
Thread.sleep(5000);

AccountBalance balance = new AccountBalanceQuery()
.setAccountId(aliasAccountId)
.execute(client);
Expand Down
9 changes: 7 additions & 2 deletions examples/src/main/java/AccountAllowanceExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ private AccountAllowanceExample()
printBalances();
}

private void printBalances() throws PrecheckStatusException, TimeoutException {
private void printBalances() throws PrecheckStatusException, TimeoutException, InterruptedException {
// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

System.out.println(
"Alice's balance: " +
new AccountBalanceQuery().setAccountId(aliceId).execute(client).hbars
Expand All @@ -126,7 +130,8 @@ private void printBalances() throws PrecheckStatusException, TimeoutException {
);
}

private void demonstrateAllowances() throws PrecheckStatusException, TimeoutException, ReceiptStatusException {
private void demonstrateAllowances()
throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InterruptedException {
System.out.println("Approving an allowance of 2 Hbar with owner Alice and spender Bob");

new AccountAllowanceApproveTransaction()
Expand Down
10 changes: 10 additions & 0 deletions examples/src/main/java/AccountCreateWithHtsExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public static void main(String[] args) throws Exception {
System.out.println("Current owner account id: " + nftOwnerAccountId);

// Step 6 - Show the new account ID owns the NFT

// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

String accountId = new AccountInfoQuery()
.setAccountId(aliasAccountId)
.execute(client)
Expand Down Expand Up @@ -208,6 +213,11 @@ public static void main(String[] args) throws Exception {
tokenTransferSubmit.getReceipt(client);

// Step 4 - Return the new account ID in the child record

// `AccountBalanceQuery` and `AccountInfoQuery` also query the mirror node.
// Wait until the mirror node is updated with the new data.
Thread.sleep(5000);

String accountId2 = new AccountInfoQuery()
.setAccountId(aliasAccountId2)
.execute(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ public static void main(String[] args) throws Exception {
- The alias property of the account does not have the public address
- Referred to as a hollow account
*/
// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo accountInfo = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down
16 changes: 10 additions & 6 deletions examples/src/main/java/ClientHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
import java.util.List;

public class ClientHelper {

public static final String LOCAL_NETWORK_NAME = "localhost";
private static final String DEFAULT_LOCAL_NODE_ADDRESS = "127.0.0.1:50211";
private static final String DEFAULT_LOCAL_MIRROR_NODE_ADDRESS = "127.0.0.1:5600";

private static final String LOCAL_CONSENSUS_NODE_ENDPOINT = "127.0.0.1:50211";

private static final String LOCAL_MIRROR_NODE_GRPC_ENDPOINT = "127.0.0.1:5600";

private static final AccountId LOCAL_CONSENSUS_NODE_ACCOUNT_ID = new AccountId(3);

public static Client forName(String network) throws InterruptedException {
if (network.equals(LOCAL_NETWORK_NAME)) {
Expand All @@ -18,10 +23,9 @@ public static Client forName(String network) throws InterruptedException {

public static Client forLocalNetwork() throws InterruptedException {
var network = new HashMap<String, AccountId>();
network.put(DEFAULT_LOCAL_NODE_ADDRESS, new AccountId(3));
network.put(LOCAL_CONSENSUS_NODE_ENDPOINT, LOCAL_CONSENSUS_NODE_ACCOUNT_ID);

return Client
.forNetwork(network)
.setMirrorNetwork(List.of(DEFAULT_LOCAL_MIRROR_NODE_ADDRESS));
return Client.forNetwork(network)
.setMirrorNetwork(List.of(LOCAL_MIRROR_NODE_GRPC_ENDPOINT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public static void main(String[] args)
// (important!) wait for the transfer to go to consensus
transferTransactionResponse.getReceipt(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Hbar balanceAfter = new AccountBalanceQuery()
.setAccountId(newAccountId)
.execute(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public static void main(String[] args)
* Step 6
* Get the `AccountInfo` and show that the account has contractAccountId
*/
// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo accountInfo = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down
4 changes: 4 additions & 0 deletions examples/src/main/java/CreateAccountWithAliasExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public static void main(String[] args) throws Exception {
* Step 6
* Get the `AccountInfo` and show that the account has contractAccountId
*/
// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo accountInfo = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down
16 changes: 16 additions & 0 deletions examples/src/main/java/CustomFeesExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ public static void main(String[] args)
.execute(client)
.getReceipt(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Hbar aliceHbar1 = new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client)
Expand All @@ -193,6 +197,10 @@ public static void main(String[] args)
.execute(client)
.getRecord(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Hbar aliceHbar2 = new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client)
Expand Down Expand Up @@ -251,6 +259,10 @@ public static void main(String[] args)
.execute(client)
.getRecord(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Map<TokenId, Long> aliceTokens4 = new AccountBalanceQuery()
.setAccountId(aliceId)
.execute(client)
Expand Down Expand Up @@ -285,6 +297,10 @@ public static void main(String[] args)
.execute(client)
.getReceipt(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

// wipe token on created accounts
Map<TokenId, Long> charlieTokensBeforeWipe = new AccountBalanceQuery()
.setAccountId(charlieId)
Expand Down
4 changes: 4 additions & 0 deletions examples/src/main/java/ExemptCustomFeesExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public static void main(String[] args) throws TimeoutException, PrecheckStatusEx
* of the token that was created was not charged a custom fee in the transfer
*/

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Long firstAccountBalanceAfter = new AccountBalanceQuery()
.setAccountId(firstAccountId)
.execute(client)
Expand Down
7 changes: 6 additions & 1 deletion examples/src/main/java/MultiAppTransferExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public final class MultiAppTransferExample {
private MultiAppTransferExample() {
}

public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException {
public static void main(String[] args)
throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException, InterruptedException {
// the exchange creates an account for the user to transfer funds to
AccountId exchangeAccountId = new AccountCreateTransaction()
// the exchange only accepts transfers that it validates through a side channel (e.g. REST API)
Expand Down Expand Up @@ -120,6 +121,10 @@ public static void main(String[] args) throws ReceiptStatusException, TimeoutExc
// (important!) wait for consensus by querying for the receipt
transactionResponse.getReceipt(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Hbar senderBalanceAfter = new AccountBalanceQuery()
.setAccountId(userAccountId)
.execute(client)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public static void main(String[] args)

System.out.println("3-of-4 multi-sig account ID: " + multiSigAccountId);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountBalance balance = new AccountBalanceQuery()
.setAccountId(multiSigAccountId)
.execute(client);
Expand Down
11 changes: 11 additions & 0 deletions examples/src/main/java/ScheduledTransferExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public static void main(String[] args)
System.out.println("Alice's ID: " + client.getOperatorAccountId());
System.out.println("Bob's ID: " + bobsId);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountBalance bobsInitialBalance = new AccountBalanceQuery()
.setAccountId(bobsId)
.execute(client);
Expand Down Expand Up @@ -141,6 +145,9 @@ public static void main(String[] args)
* Bob's balance should be unchanged. The transfer has been scheduled, but it hasn't been executed yet
* because it requires Bob's signature.
*/
// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);
AccountBalance bobsBalanceAfterSchedule = new AccountBalanceQuery()
.setAccountId(bobsId)
.execute(client);
Expand Down Expand Up @@ -179,6 +186,10 @@ public static void main(String[] args)
.execute(client)
.getReceipt(client);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountBalance balanceAfterSigning = new AccountBalanceQuery()
.setAccountId(bobsId)
.execute(client);
Expand Down
5 changes: 5 additions & 0 deletions examples/src/main/java/StakingExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public static void main(String[] args)

// Query the account info, it should show the staked account ID
// to be 0.0.3 just like what we set it to

// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo info = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down
10 changes: 10 additions & 0 deletions examples/src/main/java/StakingWithUpdateExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public static void main(String[] args)

// Query the account info, it should show the staked account ID
// to be 0.0.3 just like what we set it to

// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo info = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand All @@ -104,6 +109,11 @@ public static void main(String[] args)

// Query the account info, it should show the staked account ID
// to be 0.0.3 just like what we set it to

// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

info = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down
4 changes: 4 additions & 0 deletions examples/src/main/java/TransactionSerializationExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public static void main(String[] args) throws Exception {

System.out.println("transferred " + amount + "...");

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Hbar senderBalanceAfter = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
Expand Down
4 changes: 4 additions & 0 deletions examples/src/main/java/TransferCryptoExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public static void main(String[] args)

System.out.println("transferred " + amount + "...");

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

Hbar senderBalanceAfter = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
Expand Down
8 changes: 8 additions & 0 deletions examples/src/main/java/TransferUsingEvmAddressExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public static void main(String[] args) throws PrecheckStatusException, TimeoutEx
* Step 6
* Get the `AccountInfo` on the new account and show it is a hollow account by not having a public key
*/
// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo accountInfo = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down Expand Up @@ -120,6 +124,10 @@ public static void main(String[] args) throws PrecheckStatusException, TimeoutEx
* Step 9
* Get the `AccountInfo` of the account and show the account is now a complete account by returning the public key on the account
*/
// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo accountInfo2 = new AccountInfoQuery()
.setAccountId(newAccountId)
.execute(client);
Expand Down
4 changes: 4 additions & 0 deletions examples/src/main/java/UpdateAccountPublicKeyExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public static void main(String[] args)
// Now we fetch the account information to check if the key was changed
System.out.println(" :: getAccount and check our current key");

// `AccountInfoQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

AccountInfo info = new AccountInfoQuery()
.setAccountId(accountId)
.execute(client);
Expand Down
1 change: 1 addition & 0 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies {
testImplementation "io.github.json-snapshot:json-snapshot:1.0.17"
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.10.0"
testImplementation "org.junit.jupiter:junit-jupiter-params:5.10.0"
testImplementation "org.wiremock:wiremock-standalone:3.4.2"

testRuntimeOnly "org.slf4j:slf4j-simple:2.0.9"
testRuntimeOnly "io.grpc:grpc-netty-shaded:1.57.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ void canFetchTokenBalancesForClientOperator() throws Exception {

var tokenId = Objects.requireNonNull(response.getReceipt(testEnv.client).tokenId);

// `AccountBalanceQuery` also queries the mirror node.
// Wait until the mirror node updates with the new data.
Thread.sleep(5000);

var query = new AccountBalanceQuery();
var balance = query
.setAccountId(testEnv.operatorId)
Expand Down
Loading
Loading