@stacks/transactions

The @stacks/transactions package provides comprehensive functionality for creating, signing, and broadcasting transactions on the Stacks blockchain, including STX transfers, contract deployments, and contract calls.

Installation

npm install @stacks/transactions

Transaction functions

makeSTXTokenTransfer

makeSTXTokenTransfer creates a signed STX token transfer transaction.

Signature

function makeSTXTokenTransfer(options: SignedTokenTransferOptions): Promise<StacksTransaction>

Parameters

Name
Type
Required
Description

recipient

string

Yes

Recipient STX address

amount

bigint

Yes

Amount to transfer in microSTX

senderKey

string

Yes

Sender's private key

network

StacksNetwork | string

Yes

Network configuration

memo

string

No

Optional memo field

fee

bigint

No

Transaction fee in microSTX

nonce

bigint

No

Account nonce

anchorMode

AnchorMode

No

Block anchoring mode

Examples

Basic transfer

Transfer with memo

makeUnsignedSTXTokenTransfer

makeUnsignedSTXTokenTransfer creates an unsigned STX token transfer transaction.

Signature

Parameters

Name
Type
Required
Description

recipient

string

Yes

Recipient STX address

amount

bigint

Yes

Amount to transfer in microSTX

publicKey

string

Yes

Sender's public key

network

StacksNetwork | string

Yes

Network configuration

memo

string

No

Optional memo field

fee

bigint

No

Transaction fee in microSTX

nonce

bigint

No

Account nonce

Example

makeContractDeploy

makeContractDeploy creates a signed smart contract deployment transaction.

Signature

Parameters

Name
Type
Required
Description

contractName

string

Yes

Name for the contract

codeBody

string

Yes

Clarity contract source code

senderKey

string

Yes

Deployer's private key

network

StacksNetwork | string

Yes

Network configuration

clarityVersion

ClarityVersion

No

Clarity version (defaults to latest)

fee

bigint

No

Transaction fee in microSTX

nonce

bigint

No

Account nonce

Examples

Deploy a contract

Deploy with Clarity version

makeContractCall

makeContractCall creates a signed contract function call transaction.

Signature

Parameters

Name
Type
Required
Description

contractAddress

string

Yes

Contract address

contractName

string

Yes

Contract name

functionName

string

Yes

Function to call

functionArgs

ClarityValue[]

Yes

Function arguments

senderKey

string

Yes

Caller's private key

network

StacksNetwork | string

Yes

Network configuration

postConditions

PostCondition[]

No

Post conditions

validateWithAbi

boolean | ClarityAbi

No

Validate arguments against ABI

fee

bigint

No

Transaction fee in microSTX

nonce

bigint

No

Account nonce

Examples

Basic contract call

Call with post conditions

sponsorTransaction

sponsorTransaction adds a sponsor signature to a transaction, enabling sponsored transactions.

Signature

Parameters

Name
Type
Required
Description

transaction

StacksTransaction

Yes

Transaction to sponsor

sponsorPrivateKey

string

Yes

Sponsor's private key

fee

bigint

Yes

Fee to be paid by sponsor

sponsorNonce

bigint

No

Sponsor account nonce

Example

fetchCallReadOnlyFunction

fetchCallReadOnlyFunction calls a read-only contract function without creating a transaction.

Signature

Parameters

Name
Type
Required
Description

contractAddress

string

Yes

Contract address

contractName

string

Yes

Contract name

functionName

string

Yes

Function to call

functionArgs

ClarityValue[]

Yes

Function arguments

network

StacksNetwork

Yes

Network configuration

senderAddress

string

Yes

Address of the caller

Example

broadcastTransaction

broadcastTransaction broadcasts a signed transaction to the network.

Signature

Parameters

Name
Type
Required
Description

transaction

StacksTransaction

Yes

Signed transaction to broadcast

network

StacksNetwork | string

Yes

Network to broadcast to

Example

Clarity value construction

Primitive values

Complex values

Post conditions

STX post conditions

Fungible token post conditions

Non-fungible token post conditions

Multi-signature transactions

Multi-signature transactions require multiple signatures before broadcasting.

1

Create an unsigned multi-sig transaction

2

Deserialize and prepare signer

3

Add required signatures

4

Broadcast the multi-sig transaction

Key generation utilities

randomPrivateKey

randomPrivateKey generates a new random private key.

privateKeyToPublicKey

privateKeyToPublicKey derives a public key from a private key.

Utility functions

deserializeTransaction

deserializeTransaction converts a serialized transaction back to a transaction object.

cvToJSON

cvToJSON converts Clarity values to JSON format for easier manipulation.

Was this helpful?