Skip to main content

Deploy Your Smart Contracts

To deploy your smart contracts to a live network, there are a few things you need to adjust.

1. Configure your networkโ€‹

Scaffold-Stark 2 comes with a selection of predefined networks: Devnet, Sepolia, and Mainnet.

Here are the Starknet docs for information on Sepolia network providers.

2. Use pre-funded accounts and add one of them to deploy the contract(s) from.โ€‹

The deployer account is the account that will deploy your contracts. Additionally, the deployer account will be used to execute any function calls that are part of your deployment script.

You can use any pre-funded account / private key or add your crypto wallet's private key.

Configure Sepolia:โ€‹

To use the Sepolia, set your own private key and configure the necessary environment variables by adding and replacing yourWalletPrivateKey, yourWalletAddress in the following:

Add the following to the packages/snfoundry/.env file:

If you are working with Sepolia, you need to use the setup:

PRIVATE_KEY_SEPOLIA=yourWalletPrivateKey
RPC_URL_SEPOLIA=https://starknet-sepolia.g.alchemy.com/starknet/version/rpc/v0_8
ACCOUNT_ADDRESS_SEPOLIA=yourWalletAddress

Configure Mainnet:โ€‹

To use the Mainnet, set your own private key and configure the necessary environment variables by adding and replacing yourWalletPrivateKey, yourWalletAddress in the following:

Add the following to the packages/snfoundry/.env file:

If you are working with Mainnet, you need to use the setup:

PRIVATE_KEY_MAINNET=yourWalletPrivateKey
RPC_URL_MAINNET=https://starknet-mainnet.g.alchemy.com/starknet/version/rpc/v0_8
ACCOUNT_ADDRESS_MAINNET=yourWalletAddress

3. Changing the smart contract nameโ€‹

Steps to Change the Contract Name:โ€‹

  • Change the package name:

    Rename package name (pub mod ...) in .cairo and lib.cairo file in the contracts directory to the desired name. For example, if you rename pub mod YourContract to pub mod MyNewContract, make sure rename it in lib.cairo too.

  • Update the deploy.ts Script:

    After renaming the package name, open the deploy.ts script and change the contract name from YourContract to the new contract name youโ€™ve chosen. The relevant section in the deploy.ts file will look like this:

    const deployScript = async (): Promise<void> => {
    await deployContract({
    contract: "MyNewContract", // Change this to your renamed contract
    constructorArgs: {
    owner: deployer.address,
    },
    });
    };
  • (Optional) Deploy the same contract under a different name:

    If you want to deploy the same contract but reference it under a different name, you can use contractName:

    await deployContract({
    contract: "MyNewContract",
    contractName: "CustomDisplayName", // Allows multiple deployments with unique references
    constructorArgs: {
    owner: deployer.address,
    },
    });

    This can be useful if you want to:

    • Deploy multiple instances of the same contract with different identifiers.
    • Reference the deployed contract under a custom name for clarity in deployment logs.

4. Deploying Multiple Contractsโ€‹

Scaffold-Stark makes it easy to deploy multiple contracts in a single deployment script. This is useful when you have contracts that depend on each other or when you want to deploy a complete dApp with multiple components.

Basic Multiple Contract Deploymentโ€‹

To deploy multiple contracts, call deployContract multiple times within your deployScript function:

const deployScript = async (): Promise<void> => {
// Deploy first contract
await deployContract({
contract: "TokenContract",
constructorArgs: {
name: "MyToken",
symbol: "MTK",
initial_supply: 1000000n,
recipient: deployer.address,
},
});

// Deploy second contract
await deployContract({
contract: "VaultContract",
constructorArgs: {
owner: deployer.address,
},
});

// Deploy third contract
await deployContract({
contract: "GovernanceContract",
constructorArgs: {
admin: deployer.address,
},
});
};

Deploying Dependent Contractsโ€‹

When one contract needs the address of another contract (e.g., a vault that interacts with a token), deploy them in order and capture the address:

const deployScript = async (): Promise<void> => {
// Deploy the token contract first
const { address: tokenAddress } = await deployContract({
contract: "TokenContract",
constructorArgs: {
name: "MyToken",
symbol: "MTK",
initial_supply: 1000000n,
recipient: deployer.address,
},
});

// Deploy the vault contract using the token address
await deployContract({
contract: "TokenVault",
constructorArgs: {
token_address: tokenAddress, // Reference the deployed token
owner: deployer.address,
},
});
};

Deploying Multiple Instances of the Same Contractโ€‹

Use the contractName parameter to deploy multiple instances of the same contract with unique identifiers:

const deployScript = async (): Promise<void> => {
// Deploy a vault for STRK tokens
await deployContract({
contract: "TokenVault",
contractName: "StrkVault", // Custom name for this instance
constructorArgs: {
token_address: STRK_TOKEN_ADDRESS,
owner: deployer.address,
},
});

// Deploy another vault for ETH tokens using the same contract
await deployContract({
contract: "TokenVault",
contractName: "EthVault", // Different custom name
constructorArgs: {
token_address: ETH_TOKEN_ADDRESS,
owner: deployer.address,
},
});
};

Both vaults will be available in your frontend with their respective names (StrkVault and EthVault).

Preserving Existing Deploymentsโ€‹

By default, yarn deploy resets the deployedContracts.ts file. To add new contracts without losing existing deployments:

yarn deploy:no-reset

This is useful when iteratively adding contracts to your dApp.

5. Deploy your smart contract(s)โ€‹

By default, yarn deploy will deploy the contract to the local network. You can change defaultNetwork in scaffold.config.ts.

Run the command below to deploy the smart contract to the target network. Make sure to have some funds in your deployer account to pay for the transaction.

yarn deploy --network <NETWORK_NAME>

e.g. yarn deploy --network sepolia

6. Configuration of Third-Party Services for Production-Grade Appsโ€‹

By default, Scaffold-Stark 2 provides predefined RPC endpoints for popular services such as Alchemy. This allows you to begin developing and testing your applications more easily, avoiding the need to register for this service.

For production-grade applications, it's recommended to obtain your own API keys (to prevent rate limiting issues). You can configure these at:

  • RPC_URL_SEPOLIA variable in packages/snfoundry/.env and packages/nextjs/.env.local. You can create API keys from the Alchemy dashboard or Infura.
Hint

It's recommended to store environment variables for Next.js in Vercel/system environment variable configuration for live apps and use .env.local for local testing.