Skip to content

Commit

Permalink
fix: Updated the Mirror Node Client class to ensure compatibility wit…
Browse files Browse the repository at this point in the history
…h the Swagger specs
  • Loading branch information
ivaylogarnev-limechain committed Sep 30, 2024
1 parent 45bfb98 commit 4569c72
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
39 changes: 24 additions & 15 deletions mirrorNodeClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from 'axios';
import axios from "axios";

class MirrorNodeClient {
constructor() {
Expand All @@ -7,33 +7,42 @@ class MirrorNodeClient {
}

async getAccountData(accountId) {
const url = `${this.mirrorNodeRestUrl}/api/v1/accounts?account.id=${accountId}`;
return this.retryUntilData(url, 'accounts');
const url = `${this.mirrorNodeRestUrl}/api/v1/accounts/${accountId}`;
return this.retryUntilData(url);
}

async getBalanceData(accountId) {
const url = `${this.mirrorNodeRestUrl}/api/v1/balances?account.id=${accountId}`;
return this.retryUntilData(url, 'balances');
async getBalanceData() {
const url = `${this.mirrorNodeRestUrl}/api/v1/balances`;
return this.retryUntilData(url);
}

async retryUntilData(url, dataKey) {
async getTokenData(tokenId) {
const url = `${this.mirrorNodeRestUrl}/api/v1/tokens/${tokenId}`;
return this.retryUntilData(url);
}

async retryUntilData(url) {
const maxRetries = Math.floor(this.NODE_TIMEOUT / 1000); // retry once per second
let retries = 0;

while(retries < maxRetries) {
const response = await axios.get(url);

// If the array is not empty, return the data
if(response.data[dataKey] && response.data[dataKey].length > 0) {
return response.data;
while (retries < maxRetries) {
try {
const response = await axios.get(url);

if (response.data) {
return response.data;
}
} catch (error) {
// Uncomment if you want to see the error message
// console.log(error);
}

// If the array is empty, delay for a second before the next try
await new Promise((resolve) => setTimeout(resolve, 1000));
retries++;
}
throw new Error('Max retries reached without data');

throw new Error("Max retries reached without data");
}
}

Expand Down
6 changes: 2 additions & 4 deletions test/crypto-service/test_accountCreateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,7 @@ describe("AccountCreateTransaction", function () {

it("(#4) Creates an account with an initial balance higher than the operator account balance", async function () {
// Get the operator account balance.
const operatorBalanceData = await mirrorNodeClient.getBalanceData(process.env.OPERATOR_ACCOUNT_ID);
const operatorAccountBalance = Number(operatorBalanceData.balances[0].balance);

const operatorBalanceData = await mirrorNodeClient.getAccountData(process.env.OPERATOR_ACCOUNT_ID);
// Generate a valid key for the account.
const key = await JSONRPCRequest("generateKey", {
type: "ecdsaSecp256k1PrivateKey"
Expand All @@ -289,7 +287,7 @@ describe("AccountCreateTransaction", function () {
// Attempt to create an account with an initial balance of the operator account balance + 1. The network should respond with an INSUFFICIENT_PAYER_BALANCE status.
const response = await JSONRPCRequest("createAccount", {
key: key.key,
initialBalance: operatorAccountBalance + 1,
initialBalance: operatorBalanceData.balance.balance + 1,
});
if (response.status === "NOT_IMPLEMENTED") this.skip();
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions test/crypto-service/test_accountUpdateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe("AccountUpdateTransaction", function () {
// Account info should remain the same
let mirrorNodeData = await mirrorNodeClient.getAccountData(accountId);
let consensusNodeData = await consensusInfoClient.getAccountInfo(accountId);
expect(accountId).to.be.equal(mirrorNodeData.accounts[0].account);
expect(accountId).to.be.equal(mirrorNodeData.account);
expect(accountId).to.be.equal(consensusNodeData.accountId.toString());
});

Expand Down Expand Up @@ -598,7 +598,7 @@ describe("AccountUpdateTransaction", function () {
it("(#3) Updates the expiration time of an account to 1 second less than its current expiration time", async function () {
// Get the account's expiration time.
let accountInfo = await mirrorNodeClient.getAccountData(accountId);
let expirationTimeSeconds = accountInfo.accounts[0].expiry_timestamp;
let expirationTimeSeconds = accountInfo.expiry_timestamp;

// Attempt to update the expiration time to 1 second less than its current expiration time. The network should respond with an EXPIRATION_REDUCTION_NOT_ALLOWED status.
try {
Expand Down

0 comments on commit 4569c72

Please sign in to comment.