mirror of
https://github.com/alexgo-io/alex-sdk.git
synced 2026-01-12 22:12:16 +08:00
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:
@@ -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) => {
|
||||
@@ -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)];
|
||||
}
|
||||
|
||||
@@ -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)', () => {
|
||||
@@ -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)', () => {
|
||||
@@ -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', () => {
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user