Fix/handle other errors (#13)

* 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 <simsbluebox@gmail.com>
Co-authored-by: david weil <david.weil@coinfabrik.com>
This commit is contained in:
ignaciopenia
2024-07-24 00:50:15 -03:00
committed by GitHub
parent 38e997e8c6
commit f673f2d280
3 changed files with 12 additions and 8 deletions

View File

@@ -64,9 +64,13 @@ export async function fetchBalanceForAccount(
stxAddress: string, stxAddress: string,
tokenMappings: TokenInfo[] tokenMappings: TokenInfo[]
): Promise<Partial<{ [currency in Currency]: bigint }>> { ): Promise<Partial<{ [currency in Currency]: bigint }>> {
const response: AddressBalanceResponse = await fetch( const response = await fetch(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances` `${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( return fromEntries(
await Promise.all( await Promise.all(
tokenMappings.map(async (a) => { tokenMappings.map(async (a) => {
@@ -95,10 +99,10 @@ export async function fetchBalanceForAccount(
]; ];
} }
if (a.id === Currency.STX) { 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 = const fungibleToken =
response.fungible_tokens[a.underlyingToken]?.balance; balanceData.fungible_tokens[a.underlyingToken]?.balance;
if (fungibleToken == null) { if (fungibleToken == null) {
return [a.id, BigInt(0)]; return [a.id, BigInt(0)];
} }

View File

@@ -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 () => { it('Attempt to Get Balances with incorrect data', async () => {
await expect( await expect(
fetchBalanceForAccount(stxAddress, tokenMappings) 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)', () => { describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Gateway Timeout)', () => {
@@ -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 () => { it('Attempt to Get Balances with incorrect data', async () => {
await expect( await expect(
fetchBalanceForAccount(stxAddress, tokenMappings) 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)', () => { describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Not Found)', () => {
@@ -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 () => { it('Attempt to Get Balances with incorrect data', async () => {
await expect( await expect(
fetchBalanceForAccount(stxAddress, tokenMappings) fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow('Unexpected'); ).rejects.toThrow('Failed to fetch account balances');
}); });
}); });
describe('Transfer Factory', () => { describe('Transfer Factory', () => {

View File

@@ -297,7 +297,7 @@ describe('AlexSDK', () => {
// TODO: Implement principal address verification in the SDK methods. // TODO: Implement principal address verification in the SDK methods.
const wrongAddress = 'ABC'; const wrongAddress = 'ABC';
await expect(sdk.getBalances(wrongAddress)).rejects.toThrow( await expect(sdk.getBalances(wrongAddress)).rejects.toThrow(
"Cannot read properties of undefined (reading 'balance')" 'Failed to fetch account balances'
); );
}, 10000); }, 10000);