fetchPushCScriptNum
Calls the pox-5 push-c-script-num read-only and returns the Bitcoin Script push of the number n, the form pox-5 uses for the unlock height in a lockup script.
Usage
import { bytesToHex } from '@stacks/common';
import { fetchPushCScriptNum } from '@stacks/bitcoin-staking';
bytesToHex(await fetchPushCScriptNum({ n: 16, network: 'mainnet' })); // '60' (OP_16)
bytesToHex(await fetchPushCScriptNum({ n: 1000, network: 'mainnet' })); // '02e803'Notes
Wraps
push-c-script-num.0returns0x00(OP_0),1to16return the single opcodes0x51to0x60(OP_1toOP_16), and larger values return the fetchSerializeCScriptNum bytes with a length prefix.nof 2^39 (549,755,813,888) or more makes the contract returnERR_INVALID_UNLOCK_HEIGHT (u52). The SDK then throws anErrorwhose message starts withpush-c-script-num returned (err u52), followed by the error's name and description.The SDK applies the same encoding locally when it builds a lockup script. Call this to cross-check that encoding against the deployed contract.
Throws an
Errorif the node returns a non-2xx status (the message starts withError calling read-only function.) or answersokay: false(the message is the node'scause).
Signature
function fetchPushCScriptNum(
opts: { n: number | bigint } & NetworkClientParam
): Promise<Uint8Array>;opts is a NetworkClientParam plus n.
Returns
Promise<Uint8Array>
The script bytes that push n.
Parameters
opts.n (required)
Type:
number | bigint
Non-negative integer to encode, below 2^39. Sent as a Clarity uint.
opts.network (optional)
Type:
StacksNetworkName | StacksNetwork
The network to query: a name such as 'mainnet' or 'testnet', or a StacksNetwork object. Defaults to 'mainnet'.
opts.client (optional)
Type:
{ baseUrl?: string; fetch?: FetchFn }
Overrides the API base URL or the fetch function, for example to query your own node. Fields you set replace the network's defaults.
Was this helpful?