Skip to content

Commit

Permalink
Add mempool endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing committed Nov 26, 2024
1 parent fe9f794 commit 4b862dc
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/stacks-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ export type * as StackingPool from "./stacking-pool/index.js";
import { transactions } from "./transactions/index.js";
export type * as Transactions from "./transactions/index.js";

import { mempool } from "./mempool/index.js";
export type * as Mempool from "./mempool/index.js";

export const stacksApi = {
accounts,
blocks,
faucets,
info,
mempool,
proofOfTransfer,
stackingPool,
transactions,
Expand Down
6 changes: 6 additions & 0 deletions src/stacks-api/mempool/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { transactionFeePriorities } from "./transaction-fee-priorities.js";
export type * as TransactionFeePriorities from "./transaction-fee-priorities.js";

export const mempool = {
transactionFeePriorities,
};
79 changes: 79 additions & 0 deletions src/stacks-api/mempool/transaction-fee-priorities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
error,
safePromise,
success,
type Result,
type SafeError,
} from "../../utils/safe.js";
import type { ApiRequestOptions } from "../types.js";

export type FeePrioritiesResponse = {
all: {
no_priority: number;
low_priority: number;
medium_priority: number;
high_priority: number;
};
token_transfer: {
no_priority: number;
low_priority: number;
medium_priority: number;
high_priority: number;
};
smart_contract: {
no_priority: number;
low_priority: number;
medium_priority: number;
high_priority: number;
};
contract_call: {
no_priority: number;
low_priority: number;
medium_priority: number;
high_priority: number;
};
};

export async function transactionFeePriorities(
opts: ApiRequestOptions,
): Promise<
Result<
FeePrioritiesResponse,
SafeError<
"FetchFeePrioritiesError" | "ParseBodyError" | "ValidateDataError"
>
>
> {
const init: RequestInit = {};
if (opts.apiKeyConfig) {
init.headers = {
[opts.apiKeyConfig.header]: opts.apiKeyConfig.key,
};
}

const endpoint = `${opts.baseUrl}/extended/v2/mempool/fees`;
const res = await fetch(endpoint, init);

if (!res.ok) {
return error({
name: "FetchFeePrioritiesError",
message: "Failed to fetch transaction fee priorities.",
data: {
status: res.status,
statusText: res.statusText,
bodyParseResult: await safePromise(res.text()),
},
});
}

const [jsonError, data] = await safePromise(res.json());
if (jsonError) {
return error({
name: "ParseBodyError",
message: "Failed to parse response body as JSON.",
data: jsonError,
});
}

return success(data as FeePrioritiesResponse);
}

0 comments on commit 4b862dc

Please sign in to comment.