-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { ClarityAbi } from "@stacks/transactions"; | ||
import type { ApiRequestOptions, ProofAndTip } from "../../stacks-api/types.js"; | ||
import { error, safePromise, success, type Result } from "../../utils/safe.js"; | ||
|
||
export type Args = { | ||
contractAddress: string; | ||
contractName: string; | ||
} & ApiRequestOptions & | ||
ProofAndTip; | ||
|
||
export type InterfaceResponse = ClarityAbi; | ||
|
||
export async function contractInterface( | ||
args: Args, | ||
): Promise<Result<InterfaceResponse>> { | ||
const search = new URLSearchParams(); | ||
if (args.proof === 0) search.append("proof", "0"); | ||
if (args.tip) search.append("tip", args.tip); | ||
|
||
const init: RequestInit = {}; | ||
if (args.apiKeyConfig) { | ||
init.headers = { | ||
[args.apiKeyConfig.header]: args.apiKeyConfig.key, | ||
}; | ||
} | ||
|
||
const endpoint = `${args.baseUrl}/v2/contracts/interface/${args.contractAddress}/${args.contractName}`; | ||
const res = await fetch(endpoint, init); | ||
if (!res.ok) { | ||
return error({ | ||
name: "FetcContractInterfaceError", | ||
message: "Failed to fetch contract interface.", | ||
data: { | ||
init, | ||
status: res.status, | ||
statusText: res.statusText, | ||
endpoint, | ||
bodyText: 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 InterfaceResponse); | ||
} |