Skip to main content

πŸ”¨ Deploy with Foundry

Foundry is a smart contract development toolchain. It manages your dependencies, compiles your project, runs tests, deploys, and lets you interact with the chain from the command-line and via Solidity scripts.

1. Prerequisites​

Before starting, ensure you have the following tools installed:

You’ll also need the following:

  • Corn Network RPC URLs: You can use one of Corn's public RPCs for Maizenet or testnet, although obtaining a private RPC with one of Corn's RPC Providers is highly recommended to ensure the highest reliability and performance.
  • Private Key: The private key of the account you’ll be using to deploy the contract, this account must have BTCN on the network you are deploying to.
warning

Public RPCs are rate limited and not suitable for production environments. For applications with high traffic, it is recommended to run a node locally or use a private RPC provider.

2. Create a Foundry Project​

To create a new Foundry project, run the following command:

forge init my-corn-project
cd my-corn-project

This will set up a new Foundry project directory with a default structure and files.

3. Configure Foundry to Deploy to Corn​

Inside your project directory, edit the foundry.toml file to include the Corn network configurations for Maizenet and testnet. Below is an example configuration:

[rpc_endpoints]
corn_maizenet = "https://your-corn-maizenet-rpc-url.com"
corn_testnet = "https://your-corn-testnet-rpc-url.com"
warning

Make sure not to commit sensitive information, such as private keys, into version control. You can alternatively set environment variables for these values.

Setting Environment Variables​

Instead of including RPC endpoints in foundry.toml, you can set them as environment variables:

export FOUNDRY_DEPLOYER_PRIVATE_KEY="your-private-key"
export CORN_MAIZENET_RPC_URL="https://your-corn-maizenet-rpc-url.com"
export CORN_TESTNET_RPC_URL="https://your-corn-testnet-rpc-url.com"

4. Write and Compile the Smart Contract​

Create a simple smart contract under the src directory:

src/HelloCorn.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract HelloCorn {
string public greeting = "Welcome to Corn!";

function setGreeting(string calldata _greeting) external {
greeting = _greeting;
}
}

Compile the contract using Foundry:

forge build

5. Deploy from the command line​

For simple deployments, you can deploy directly from the command line using forge. Note that which network you deploy to is determined by the RPC URL you provide:

forge create --rpc-url $CORN_TESTNET_RPC_URL src/HelloCorn.sol:HelloCorn --
private-key $FOUNDRY_DEPLOYER_PRIVATE_KEY

6. Deploy using a script​

Alternatively, you can deploy using a script. Deploying via a script allows you to add additional functionality, such as dynamically generatting and setting the constructor arguments or interacting with the deployed contract after it has been deployed.

For this example, create a new file under the script directory.

script/DeployHelloCorn.s.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "forge-std/Script.sol";
import "../src/HelloCorn.sol";

contract DeployHelloCorn is Script {
function run() external {
vm.startBroadcast();

// Deploy contract
HelloCorn helloCorn = new HelloCorn();

console.log("HelloCorn deployed at:", address(helloCorn));

vm.stopBroadcast();
}
}

To deploy, use the following command, specifying the network:

Deploy to Corn Testnet​

forge script script/DeployHelloCorn.s.sol --rpc-url $CORN_TESTNET_RPC_URL --private-key $FOUNDRY_DEPLOYER_PRIVATE_KEY --broadcast

Deploy to Corn Maizenet​

forge script script/DeployHelloCorn.s.sol --rpc-url $CORN_MAIZENET_RPC_URL --private-key $FOUNDRY_DEPLOYER_PRIVATE_KEY --broadcast

After deployment, Foundry will display the contract address on the specified network.

7. Notes on Foundry Development​

  • Gas Estimates: Foundry gas usage and estimates are denominated on ETH regardless of the network. Bare in mind that the although the estimated values are accurate, the gas paid on Corn is denominated in BTCN.

  • Testing: Foundry’s forge test is a powerful tool for running unit tests locally. You can simulate contract deployment and interactions before deploying to Corn to ensure smooth operations.

  • Environment Variables: It’s best practice to keep sensitive information like private keys in environment variables. Consider using tools like .env files with libraries like dotenv to manage these securely.