Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds handling for fetching more than 10 addresses #545

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/__test__/e2e/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
fetchActiveWallets,
fetchAddress,
fetchAddresses,
fetchBtcLegacyAddresses,
fetchBtcSegwitAddresses,
pair,
signBtcLegacyTx,
signBtcSegwitTx,
Expand Down Expand Up @@ -152,19 +154,43 @@ describe('API', () => {
});

describe('addresses', () => {
test('fetchAddresses', async () => {
const addresses = await fetchAddresses();
expect(addresses).toHaveLength(10);
});
describe('fetchAddresses', () => {
test('fetchAddresses', async () => {
const addresses = await fetchAddresses();
expect(addresses).toHaveLength(10);
});

test('fetchAddresses[1]', async () => {
const addresses = await fetchAddresses({ n: 1 });
expect(addresses).toHaveLength(1);
});

test('fetchAddresses[12]', async () => {
const addresses = await fetchAddresses({ n: 12 });
expect(addresses).toHaveLength(12);
});

test('fetchAddress', async () => {
const address = await fetchAddress();
expect(address).toBeTruthy();
test('fetchBtcLegacyAddresses', async () => {
const addresses = await fetchBtcLegacyAddresses();
expect(addresses).toHaveLength(10);
});

test('fetchBtcSegwitAddresses[12]', async () => {
const addresses = await fetchBtcSegwitAddresses({ n: 12 });
expect(addresses).toHaveLength(12);
});

test('fetchLedgerLiveAddresses', async () => {
const addresses = await fetchLedgerLiveAddresses();
expect(addresses).toHaveLength(10);
});
});

test('fetchLedgerLiveAddresses', async () => {
const addresses = await fetchLedgerLiveAddresses();
expect(addresses).toHaveLength(10);
describe('fetchAddress', () => {
test('fetchAddress', async () => {
const address = await fetchAddress();
expect(address).toBeTruthy();
});
});
});

Expand Down
92 changes: 61 additions & 31 deletions src/api/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,36 @@ import {
} from '../constants';
import { getStartPath, queue } from './utilities';

export const fetchAddresses = async (
overrides?: GetAddressesRequestParams,
): Promise<string[]> => {
return queue((client) =>
client
.getAddresses({
startPath: DEFAULT_ETH_DERIVATION,
n: MAX_ADDR,
...overrides,
})
.then((addrs) => addrs.map((addr) => `${addr}`)),
);
type FetchAddressesParams = {
n?: number;
startPathIndex?: number;
};

export const fetchAddresses = async (overrides?: GetAddressesRequestParams) => {
let allAddresses: string[] = [];
let totalFetched = 0;
const totalToFetch = overrides?.n || MAX_ADDR;

while (totalFetched < totalToFetch) {
const batchSize = Math.min(MAX_ADDR, totalToFetch - totalFetched);
const startPath = getStartPath(DEFAULT_ETH_DERIVATION, totalFetched);
await queue((client) =>
client
.getAddresses({
startPath,
...overrides,
n: batchSize,
})
.then((addresses: string[]) => {
if (addresses.length > 0) {
allAddresses = [...allAddresses, ...addresses];
totalFetched += addresses.length;
netbonus marked this conversation as resolved.
Show resolved Hide resolved
}
}),
);
}

return allAddresses;
};

/**
Expand All @@ -43,49 +61,59 @@ export const fetchAddress = async (
};

export const fetchBtcLegacyAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(BTC_LEGACY_DERIVATION, startPathIndex),
n,
});
};

export const fetchBtCSegwitAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
export const fetchBtcSegwitAddresses = async (
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(BTC_SEGWIT_DERIVATION, startPathIndex),
n,
});
};

export const fetchBtcWrappedSegwitAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(BTC_WRAPPED_SEGWIT_DERIVATION, startPathIndex),
n,
});
};

export const fetchSolanaAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
return fetchAddresses({
startPath: getStartPath(SOLANA_DERIVATION, startPathIndex, 2),
n,
});
};

export const fetchLedgerLiveAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
const addresses = [];
for (let i = 0; i < n; i++) {
addresses.push(
Expand All @@ -107,9 +135,11 @@ export const fetchLedgerLiveAddresses = async (
};

export const fetchLedgerLegacyAddresses = async (
n = MAX_ADDR,
startPathIndex?: number,
): Promise<string[]> => {
{ n, startPathIndex }: FetchAddressesParams = {
n: MAX_ADDR,
startPathIndex: 0,
},
) => {
const addresses = [];
for (let i = 0; i < n; i++) {
addresses.push(
Expand Down
Loading