Skip to content

Commit

Permalink
feat: solution to query the mirror node for the account balance, acco…
Browse files Browse the repository at this point in the history
…unt info, and contract info data (#1698)

Signed-off-by: Nikita Lebedev <[email protected]>
  • Loading branch information
thenswan authored May 23, 2024
1 parent 4301900 commit 8243cf6
Show file tree
Hide file tree
Showing 48 changed files with 758 additions and 171 deletions.
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));
}
}
4 changes: 4 additions & 0 deletions examples/src/main/java/CreateAccountThresholdKeyExample.java
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

0 comments on commit 8243cf6

Please sign in to comment.