Deploy a contract
Was this helpful?
Deploy a Clarity smart contract to the Stacks blockchain using Stacks.js
Deploying new smart contracts to mainnet or testnet
Automating contract deployments in CI/CD pipelines
Programmatic contract deployment for dApp initialization
Deploying contract upgrades or new versions
Contract name - Unique identifier for your contract (letters, numbers, hyphens)
Code body - The Clarity contract code as a string
Sender key - Private key of the account deploying the contract
Network - Target network (mainnet, testnet, or devnet)
Was this helpful?
Was this helpful?
import { STACKS_TESTNET } from "@stacks/network";
import {
makeContractDeploy,
broadcastTransaction,
AnchorMode,
PostConditionMode
} from "@stacks/transactions";
const contractCode = `
(define-data-var counter uint u0)
(define-public (increment)
(ok (var-set counter (+ (var-get counter) u1))))
(define-read-only (get-counter)
(ok (var-get counter)))
`;
const txOptions = {
contractName: "my-counter",
codeBody: contractCode,
senderKey: "753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601",
network: STACKS_TESTNET,
anchorMode: AnchorMode.Any,
postConditionMode: PostConditionMode.Allow,
fee: 100000n, // Set an appropriate fee
};
const transaction = await makeContractDeploy(txOptions);
const broadcastResponse = await broadcastTransaction({ transaction });
console.log("Contract deployed!");
console.log("Transaction ID:", broadcastResponse.txid);