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
Converts a buffer to BufferCV clarity type
value to be converted to clarity type
returns instance of type BufferCV
Converts a string to BufferCV clarity type
returns instance of type BufferCV
Calls a function as read-only from a contract interface It is not necessary that the function is defined as read-only in the contract
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 stx address in to ContractPrincipalCV clarity type
returns instance of type ContractPrincipalCV
Create ContractPrincipalCV from Address type
value to be converted to ContractPrincipalCV clarity type
returns instance of type ContractPrincipalCV
Converts a clarity value to a hex encoded string with 0x
prefix
the clarity value to convert
If true then ints and uints are returned as JSON serializable numbers when
less than or equal to 53 bit length, otherwise string wrapped integers when larger than 53 bits.
If false, they are returned as js native bigint
s which are not JSON serializable.
Deserializes clarity value to clarity type
returns the clarity type instance
Buffer or hex string
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
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 Stacks transaction
is an optional argument that provides the endpoint with an estimation of the final length (in bytes) of the transaction, including any post-conditions and signatures
the Stacks network to estimate transaction fees for
a promise that resolves to FeeEstimate
Estimates transaction byte length Context:
Estimated transaction byte length
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
Converts false to BooleanCV clarity type
returns instance of type BooleanCV
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
Converts IntegerType in to IntCV clarity type
returns instance of type IntCV
Create list of clarity types
returns instance of type ListCV
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
Create a null clarity type *
returns instance of type NoneCV
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
Use utils.randomBytes to replace randombytes dependency Generates a buffer with random bytes of given length
For return type compatibility converting utils.randomBytes return value to buffer
Converts ClarityValue to responseErrorCV
returns instance of type responseErrorCV
Converts ClarityValue to ResponseOkCV
returns instance of type ResponseOkCV
Serializes clarity value to buffer
returns the buffer instance
Signs a message using a private key. The resulting signature along with the original message can be verified using verifyMessageSignature
A recoverable signature (in RSV order)
Signs a structured message (ClarityValue) and a domain (ClarityValue) using a private key. The resulting signature along with the original message can be verified using verifyMessageSignature
A recoverable signature (in RSV order)
A recoverable signature (in VRS order)
Converts any ClarityValue in to OptionalCV clarity type
to be converted to OptionalCV clarity type
returns instance of type OptionalCV
Constructs and signs a sponsored transaction as the sponsor
the sponsor options object
Returns a signed sponsored transaction.
Converts stx address in to StandardPrincipalCV clarity type
returns instance of type StandardPrincipalCV
Converts stx address in to StandardPrincipalCV clarity type
returns instance of type StandardPrincipalCV
Converts ClarityValue to stringAsciiCV
returns instance of type StringAsciiCV
Converts ClarityValue to stringUtf8CV
returns instance of type stringUtf8CV
Converts true to BooleanCV clarity type
returns instance of type BooleanCV
Create tuple of clarity values
returns instance of type clarity tuple
Converts IntegerType in to IntCV clarity type
returns instance of type UIntCV
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.