Broadcast Transactions
The process of broadcasting transactions is fundamental for interacting with blockchains, whether you're transferring tokens, deploying contracts, or executing contract functions.
In this guide you will:
Install the required packages
Connect to a user's wallet
Sign and broadcast different transaction types
Handle transaction results
Setup and installation
Install the required packages to start building and broadcasting transactions.
npm install @stacks/connect @stacks/transactionsConnect to a user's wallet
Before signing transactions, users need to connect their wallet to your application. Use the connect function to initiate a wallet connection:
import { connect, isConnected } from '@stacks/connect';
async function connectWallet() {
if (!isConnected()) {
const response = await connect();
console.log('Connected with addresses:', response);
}
}Sign and broadcast transactions
There are three common transaction flows you can build:
STX transfer
Use stx_transferStx to send tokens:
import { request } from '@stacks/connect';
async function transferStx() {
const response = await request('stx_transferStx', {
recipient: 'ST2EB9WEQNR9P0K28D2DC352TM75YG3K0GT7V13CV',
amount: '100',
memo: 'Reimbursement',
});
console.log('Transaction ID:', response.txId);
}Contract deployment
Deploy a contract with stx_deployContract:
import { request } from '@stacks/connect';
async function deployContract() {
const codeBody = '(define-public (say-hi) (ok "hello world"))';
const response = await request('stx_deployContract', {
name: 'my-contract',
code: codeBody,
clarityVersion: 3,
});
console.log('Transaction ID:', response.txId);
}Contract execution
Call contract functions with stx_callContract:
(define-public (say-hi)
(print "hi")
(ok u0)
)import { request } from '@stacks/connect';
async function callContract() {
const response = await request('stx_callContract', {
contractAddress: 'ST22T6ZS7HVWEMZHHFK77H4GTNDTWNPQAX8WZAKHJ',
contractName: 'my-contract',
functionName: 'say-hi',
functionArgs: [],
});
console.log('Transaction ID:', response.txId);
}When passing arguments, construct Clarity values via Cl:
import { Cl } from '@stacks/transactions';
const functionArgs = [
Cl.uint(123),
Cl.stringAscii('hello'),
Cl.standardPrincipalCV('ST1X..'),
];Handle transaction results
When a transaction is signed and broadcast, the request method returns a response object containing information about the transaction:
interface TransactionResponse {
txId: string; // The transaction ID
txRaw: string; // The raw transaction hex
}You can use the transaction ID to create a link to view the transaction in the explorer:
import { request } from '@stacks/connect';
async function handleTransaction() {
const response = await request('stx_transferStx', {
recipient: 'ST2EB9WEQNR9P0K28D2DC352TM75YG3K0GT7V13CV',
amount: '100',
});
const explorerUrl = `https://explorer.stacks.co/txid/${response.txId}`;
console.log('View transaction in explorer:', explorerUrl);
}Was this helpful?
