Skip to content

Commit

Permalink
feat: change stacks client requests priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Aug 16, 2024
1 parent 6d61f17 commit 1178a85
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions packages/query/src/stacks/stacks-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ export interface CallReadOnlyFunctionArgs {
signal?: AbortSignal;
}

const priorityLevels = {
getNetworkStatus: 6,
getAccountTransactionsWithTransfers: 5,
getNamesOwnedByAddress: 5,
getAccountBalance: 4,
getAccountNonces: 4,
getNameInfo: 4,
getNftHoldings: 4,
getAddressMempoolTransactions: 4,
getRawTransactionById: 4,
getTransactionById: 4,
getContractInterface: 4,
getNetworkBlockTimes: 3,
postFeeTransaction: 2,
getFtMetadata: 2,
getNftMetadata: 2,
callReadOnlyFunction: 2,
getStx20Balances: 1,
};

export function stacksClient(basePath: string) {
const rateLimiter = getHiroApiRateLimiter(basePath);

Expand All @@ -49,15 +69,15 @@ export function stacksClient(basePath: string) {
axios.get<AddressBalanceResponse>(`${basePath}/extended/v1/address/${address}/balances`, {
signal,
}),
{ priority: 1, signal, throwOnTimeout: true }
{ priority: priorityLevels.getAccountBalance, signal, throwOnTimeout: true }
);
return resp.data;
},
async getAccountNonces(address: string, signal: AbortSignal) {
const resp = await rateLimiter.add(
() =>
axios.get<AddressNonces>(`${basePath}/extended/v1/address/${address}/nonces`, { signal }),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getAccountNonces, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -69,7 +89,11 @@ export function stacksClient(basePath: string) {
`${basePath}/extended/v1/address/${address}/transactions_with_transfers?limit=${DEFAULT_LIST_LIMIT}`,
{ signal }
),
{ signal, throwOnTimeout: true }
{
priority: priorityLevels.getAccountTransactionsWithTransfers,
signal,
throwOnTimeout: true,
}
);
return resp.data;
},
Expand All @@ -80,18 +104,15 @@ export function stacksClient(basePath: string) {
estimated_len: estimatedLen,
transaction_payload: transactionPayload,
}),
{
priority: 2,
throwOnTimeout: true,
}
{ priority: priorityLevels.postFeeTransaction, throwOnTimeout: true }
);
return resp.data;
},
async getNetworkBlockTimes() {
const resp = await rateLimiter.add(
() =>
axios.get<NetworkBlockTimesResponse>(`${basePath}/extended/v1/info/network_block_times`),
{ throwOnTimeout: true }
{ priority: priorityLevels.getNetworkBlockTimes, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -101,7 +122,7 @@ export function stacksClient(basePath: string) {
axios.get<BnsNamesOwnByAddressResponse>(`${basePath}/v1/addresses/stacks/${address}`, {
signal,
}),
{ signal, priority: 2, throwOnTimeout: true }
{ priority: priorityLevels.getNamesOwnedByAddress, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -111,16 +132,15 @@ export function stacksClient(basePath: string) {
axios.get<BnsGetNameInfoResponse>(`${basePath}/v1/names/${name}`, {
signal,
}),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getNameInfo, signal, throwOnTimeout: true }
);
return resp.data;
},
async getNetworkStatus(url: string) {
const resp = await rateLimiter.add(() => axios.get(url, { timeout: 30000 }), {
priority: priorityLevels.getNetworkStatus,
throwOnTimeout: true,
priority: 3,
});

return resp.data;
},
async getNftHoldings(address: string, signal: AbortSignal) {
Expand All @@ -130,7 +150,7 @@ export function stacksClient(basePath: string) {
`${basePath}/extended/v1/tokens/nft/holdings?principal=${address}&limit=${DEFAULT_LIST_LIMIT}`,
{ signal }
),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getNftHoldings, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -141,15 +161,15 @@ export function stacksClient(basePath: string) {
`${basePath}/extended/v1/tx/mempool?address=${address}&limit=${DEFAULT_LIST_LIMIT}`,
{ signal }
),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getAddressMempoolTransactions, signal, throwOnTimeout: true }
);
return resp.data;
},
async getRawTransactionById(txid: string, signal: AbortSignal) {
const resp = await rateLimiter.add(
() =>
axios.get<GetRawTransactionResult>(`${basePath}/extended/v1/tx/${txid}/raw`, { signal }),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getRawTransactionById, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -159,14 +179,14 @@ export function stacksClient(basePath: string) {
axios.get<MempoolTransaction | Transaction>(`${basePath}/extended/v1/tx/${txid}`, {
signal,
}),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getTransactionById, signal, throwOnTimeout: true }
);
return resp.data;
},
async getFtMetadata(address: string, signal: AbortSignal) {
const resp = await rateLimiter.add(
() => axios.get<FtMetadataResponse>(`${basePath}/metadata/v1/ft/${address}`, { signal }),
{ signal, priority: 2, throwOnTimeout: true }
{ priority: priorityLevels.getFtMetadata, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -176,7 +196,7 @@ export function stacksClient(basePath: string) {
axios.get<NftMetadataResponse>(`${basePath}/metadata/v1/nft/${address}/${tokenId}`, {
signal,
}),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getNftMetadata, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -197,7 +217,7 @@ export function stacksClient(basePath: string) {
signal,
}
),
{ priority: 2, signal, throwOnTimeout: true }
{ priority: priorityLevels.callReadOnlyFunction, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -210,11 +230,9 @@ export function stacksClient(basePath: string) {
() =>
axios.get<ContractInterfaceResponseWithFunctions>(
`${basePath}/v2/contracts/interface/${contractAddress}/${contractName}`,
{
signal,
}
{ signal }
),
{ signal, throwOnTimeout: true }
{ priority: priorityLevels.getContractInterface, signal, throwOnTimeout: true }
);
return resp.data;
},
Expand All @@ -224,7 +242,7 @@ export function stacksClient(basePath: string) {
axios.get<Stx20BalanceResponse>(`${STX20_API_BASE_URL_MAINNET}/balance/${address}`, {
signal,
}),
{ signal, priority: 1, throwOnTimeout: true }
{ priority: priorityLevels.getStx20Balances, signal, throwOnTimeout: true }
);
return resp.data.balances;
},
Expand All @@ -235,6 +253,5 @@ export type StacksClient = ReturnType<typeof stacksClient>;

export function useStacksClient() {
const network = useLeatherNetwork();

return stacksClient(network.chain.stacks.url);
}

0 comments on commit 1178a85

Please sign in to comment.