Skip to content

Commit

Permalink
Fix/handle other errors (#13)
Browse files Browse the repository at this point in the history
* add tests to mock-externals suite

* tests: complete custom and alternative routes coverage

* handle STACKS_API_HOST error

* tests: lint fix

* tests: empty route coverage

* refactor mock-externals suite

* tests: coverage on fungible token balances and null token cases

* refactor mock-externals suite

* tests: remove unused structure

* temp commit

* remove redundant describe

* lint fix

---------

Co-authored-by: simsbluebox <[email protected]>
Co-authored-by: david weil <[email protected]>
  • Loading branch information
3 people authored Jul 24, 2024
1 parent 38e997e commit f673f2d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/utils/fetchData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ export async function fetchBalanceForAccount(
stxAddress: string,
tokenMappings: TokenInfo[]
): Promise<Partial<{ [currency in Currency]: bigint }>> {
const response: AddressBalanceResponse = await fetch(
const response = await fetch(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`
).then((a) => a.json());
);
if (!response.ok) {
throw new Error('Failed to fetch account balances');
}
const balanceData: AddressBalanceResponse = await response.json();
return fromEntries(
await Promise.all(
tokenMappings.map(async (a) => {
Expand Down Expand Up @@ -95,10 +99,10 @@ export async function fetchBalanceForAccount(
];
}
if (a.id === Currency.STX) {
return [a.id, BigInt(response.stx.balance) * BigInt(100)];
return [a.id, BigInt(balanceData.stx.balance) * BigInt(100)];
}
const fungibleToken =
response.fungible_tokens[a.underlyingToken]?.balance;
balanceData.fungible_tokens[a.underlyingToken]?.balance;
if (fungibleToken == null) {
return [a.id, BigInt(0)];
}
Expand Down
6 changes: 3 additions & 3 deletions test/alexSDK.mock-externals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP
it('Attempt to Get Balances with incorrect data', async () => {
await expect(
fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow('Unexpected');
).rejects.toThrow('Failed to fetch account balances');
});
});
describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Gateway Timeout)', () => {
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP
it('Attempt to Get Balances with incorrect data', async () => {
await expect(
fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow('Unexpected');
).rejects.toThrow('Failed to fetch account balances');
});
});
describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Not Found)', () => {
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_AP
it('Attempt to Get Balances with incorrect data', async () => {
await expect(
fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow('Unexpected');
).rejects.toThrow('Failed to fetch account balances');
});
});
describe('Transfer Factory', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/alexSDK.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('AlexSDK', () => {
// TODO: Implement principal address verification in the SDK methods.
const wrongAddress = 'ABC';
await expect(sdk.getBalances(wrongAddress)).rejects.toThrow(
"Cannot read properties of undefined (reading 'balance')"
'Failed to fetch account balances'
);
}, 10000);

Expand Down

0 comments on commit f673f2d

Please sign in to comment.