Contract Calls

Contract calls allow you to execute state-changing functions in smart contracts. Unlike read-only calls, these create transactions that must be signed and broadcast to the network.

Basic contract call

Execute a simple contract function by creating a transaction with the required parameters.

import { 
  makeContractCall,
  broadcastTransaction,
} from '@stacks/transactions';

async function callContract() {
  const txOptions = {
    contractAddress: 'SPQR8VS42ZCYH73W1T495CDCESYD360Y1D2N0AMJ',
    contractName: 'counter',
    functionName: 'increment',
    functionArgs: [],
    senderKey: 'your-private-key',
    network: 'mainnet',
  };
  
  const transaction = await makeContractCall(txOptions);
  const broadcastResponse = await broadcastTransaction({ transaction });
  
  console.log('Transaction ID:', broadcastResponse.txid);
}

The makeContractCall function creates a transaction that will execute the specified function when confirmed on-chain.

Passing function arguments

Most contract functions require arguments. Use Clarity value constructors to match the expected parameter types.

Each Clarity type has a corresponding constructor function that ensures proper encoding for the blockchain.

Complex argument types

Optional and response values have dedicated constructors for proper type safety.

Handling contract responses

Process transaction results and contract responses:

Last updated

Was this helpful?