Translates the tx auth hash mode to the corresponding address version.
Broadcast the signed transaction to a core node
the raw serialized transaction buffer to broadcast
the broadcast endpoint URL
that resolves to a response if the operation succeeds
Broadcast the signed transaction to a core node
the token transfer transaction to broadcast
the Stacks network to broadcast transaction to
that resolves to a response if the operation succeeds
Calls a read only function from a contract interface
the options object
Returns an object with a status bool (okay) and a result string that is a serialized clarity value in hex format.
Converts a clarity value to a hex encoded string with 0x
prefix
the clarity value to convert
Buffer or hex string
Estimate the total transaction fee in microstacks for a contract deploy
the token transfer transaction to estimate fees for
the Stacks network to estimate transaction for
a promise that resolves to number of microstacks per byte
Estimate the total transaction fee in microstacks for a contract function call
the token transfer transaction to estimate fees for
the Stacks network to estimate transaction for
a promise that resolves to number of microstacks per byte
Estimate the total transaction fee in microstacks for a token transfer
the token transfer transaction to estimate fees for
the Stacks network to estimate transaction for
a promise that resolves to number of microstacks per byte
Fetch a contract's ABI
the contracts address
the contracts name
the Stacks network to broadcast transaction to
that resolves to a ClarityAbi if the operation succeeds
Creates a P2PKH address string from the given private key and tx version.
Creates a P2PKH address string from the given public key and tx version.
Lookup the nonce for an address from a core node
the c32check address to look up
the Stacks network to look up address on
a promise that resolves to an integer
Converts a hex encoded string to a clarity value
the hex encoded string with or without 0x
prefix
Generates a Clarity smart contract function call transaction
an options object for the contract function call
Returns a signed Stacks smart contract function call transaction.
Generates a Clarity smart contract deploy transaction
an options object for the contract deploy
Returns a signed Stacks smart contract deploy transaction.
Generates a fungible token post condition with a contract principal
Returns a fungible token post condition object
the c32check address
the name of the contract
the condition code
the amount of fungible tokens
asset info describing the fungible token
Generates a non-fungible token post condition with a contract principal
Returns a non-fungible token post condition object
the c32check address
the name of the contract
the condition code
asset info describing the non-fungible token
asset name describing the non-fungible token
Generates a STX post condition with a contract principal
Returns a STX post condition object
the c32check address of the contract
the name of the contract
the condition code
the amount of STX tokens
Generates a signed Stacks token transfer transaction
Returns a signed Stacks token transfer transaction.
an options object for the token transfer
Generates a fungible token post condition with a standard principal
Returns a fungible token post condition object
the c32check address
the condition code
the amount of fungible tokens
asset info describing the fungible token
Generates a non-fungible token post condition with a standard principal
Returns a non-fungible token post condition object
the c32check address
the condition code
asset info describing the non-fungible token
asset name describing the non-fungible token
Generates a STX post condition with a standard principal
Returns a STX post condition object
the c32check address
the condition code
the amount of STX tokens
Generates an unsigned Clarity smart contract function call transaction
an options object for the contract call
Generates an unsigned Stacks token transfer transaction
Returns a Stacks token transfer transaction.
an options object for the token transfer
Parse a fully qualified string that identifies the token type.
String in the format {address}.{contractName}::{assetName}
Parses a principal string for either a standard principal or contract principal.
String in the format {address}.{contractName}
Converts the response of a read-only function call into its Clarity Value
Convert string input to Clarity value based on contract ABI data. Only handles Clarity primitives and buffers. Responses, optionals, tuples and lists are not supported.
string to be parsed into Clarity value
the contract function argument object
returns a Clarity value
Constructs and signs a sponsored transaction as the sponsor
the sponsor options object
Returns a signed sponsored transaction.
Validates a contract-call payload with a contract ABI
a contract-call payload
a contract ABI
true if the payloads functionArgs type check against those in the ABI
Generated using TypeDoc
@stacks/transactions
Construct, decode transactions and work with Clarity smart contracts on the Stacks blockchain.
Installation
Overview
This library supports the creation of the following Stacks transaction types:
Key Generation
STX Token Transfer Transaction
Smart Contract Deploy Transaction
Smart Contract Function Call
In this example we construct a
contract-call
transaction with a post condition. We have set thevalidateWithAbi
option totrue
, so themakeContractCall
builder will attempt to fetch this contracts ABI from the specified Stacks network, and validate that the provided functionArgs match what is described in the ABI. This should help you avoid constructing invalid contract-call transactions. If you would prefer to provide your own ABI instead of fetching it from the network, thevalidateWithABI
option also accepts ClarityABI objects, which can be constructed from ABI files like so:Sponsoring Transactions
To generate a sponsored transaction, first create and sign the transaction as the origin. The
sponsored
property in the options object must be set to true.The serialized transaction can now be passed to the sponsoring party which will sign the sponsor portion of the transaction and set the fee.
Supporting multi-signature transactions
To generate a multi-sig transaction, first create an unsigned transaction. The
numSignatures
andpublicKeys
properties in the options object must be set:This transaction payload can be passed along to other participants to sign. In addition to meeting the numSignatures requirement, the public keys of the parties who did not sign the transaction must be appended to the signature.
Calling Read-only Contract Functions
Read-only contract functions can be called without generating or broadcasting a transaction. Instead it works via a direct API call to a Stacks node.
Constructing Clarity Values
Building transactions that call functions in deployed clarity contracts requires you to construct valid Clarity Values to pass to the function as arguments. The Clarity type system contains the following types:
(tuple (key-name-0 key-type-0) (key-name-1 key-type-1) ...)
(list max-len entry-type)
(response ok-type err-type)
(optional some-type)
(buff max-len)
principal
bool
int
uint
This library contains Typescript types and classes that map to the Clarity types, in order to make it easy to construct well-typed Clarity values in Javascript. These types all extend the abstract class
ClarityValue
.If you develop in Typescript, the type checker can help prevent you from creating wrongly-typed Clarity values. For example, the following code won't compile since in Clarity lists are homogeneous, meaning they can only contain values of a single type. It is important to include the type variable
BooleanCV
in this example, otherwise the typescript type checker won't know which type the list is of and won't enforce homogeneity.Post Conditions
Three types of post conditions can be added to transactions:
For details see: https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-005-blocks-and-transactions.md#transaction-post-conditions
STX post condition
Fungible token post condition
Non-fungible token post condition
Helper functions
Conversion of Clarity Values to JSON
Clarity Values represent values of Clarity contracts. If a JSON format is required the helper function
cvToJSON
can be used.