> For the complete documentation index, see [llms.txt](https://docs.kubchain.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kubchain.com/quickstart/deploy-smart-contracts-on-kub.md).

# Deploy Smart Contracts on KUB

This walkthrough helps you set up your environment, deploy a smart contract to KUB Testnet, and verify it on [KUB Scan](https://www.kubscan.com/).

## What You'll Achieve

By the end of this quickstart, you'll be able to:

* Set up your development environment to deploy on the KUB Testnet
* Deploy your smart contracts to the KUB Testnet
* Verify your contract on KUB Scan

## Set Up Your Development Environment

Create a new project directory

```bash
mkdir my-kub-project && cd my-kub-project
```

Install Foundry

```bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
```

{% hint style="info" %}
If `curl | bash` installs are blocked in your environment; install via Homebrew instead: `brew install foundry`.
{% endhint %}

Initialize a new Solidity project

```bash
forge init
```

Your Foundry project is now ready. You'll find an example contract below, which we'll use for the rest of this guide.

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

contract Counter {
    uint256 public number;

    function setNumber(uint256 newNumber) public {
        number = newNumber;
    }

    function increment() public {
        number++;
    }
}
```

## Configure Foundry with KUB

To deploy to the KUB network, you need three things: an EVM version setting, a node connection, and a funded private key.

### Target the correct EVM version

The KUB EVM predates the Shanghai upgrade, so it rejects the `PUSH0` opcode that solc 0.8.20 and later emit by default. Without this setting, deployment fails with the cryptic error `invalid opcode: opcode 0x5f not defined`. Add the following to your `foundry.toml` under `[profile.default]`:

```toml
# foundry.toml
evm_version = "paris"
```

{% hint style="info" %}
This compiles your contracts without `PUSH0` while keeping the latest Solidity language features. It applies to every `forge` command in this guide, including verification, so the verified bytecode matches what you deployed.
{% endhint %}

### Set up your node connection

Create a `.env` file in your project root

```bash
KUB_RPC_URL="https://rpc.kubchain.io"
KUB_TESTNET_RPC_URL="https://rpc-testnet.kubchain.io"
```

Load your environment variables

```bash
source .env
```

{% hint style="info" %}
KUB Testnet is the test network we'll use for the rest of this guide. Get free tKUB from the [KUB Faucet](https://faucet.kubchain.com/); it dispenses 5 tKUB to your address every 24 hours. If you wish to deploy to other KUB networks, refer to this [guide](/about-kub/connect-to-kub.md).
{% endhint %}

### Secure your private key

Store your private key in Foundry's encrypted keystore

```bash
cast wallet import deployer --interactive
```

When prompted, enter your private key and a password. The key is stored in `~/.foundry/keystores`, which is not tracked by git.

{% hint style="warning" %}
Never share or commit your private key. Always keep it secure and handle it with care.
{% endhint %}

### Deploy Your Contracts

Compile and deploy the Counter contract to the KUB Testnet

```bash
forge create ./src/Counter.sol:Counter --rpc-url $KUB_TESTNET_RPC_URL --account deployer --broadcast --legacy
```

{% hint style="info" %}
The `--legacy` flag is required. KUB does not support EIP-1559 fee estimation, so `forge create` without it fails with "Failed to estimate EIP1559 fees". `--legacy` sends a pre-EIP-1559 transaction with a plain gas price instead.
{% endhint %}

After a successful deployment, the transaction hash and contract address are printed to the console. Add the address to your `.env`

```bash
COUNTER_CONTRACT_ADDRESS="0x..."
```

Reload the environment

```bash
source .env
```

### Verify Your Deployment

Check the transaction on [KUB Testnet](https://testnet.kubscan.com/) and read the initial counter value from the command line

```bash
cast call $COUNTER_CONTRACT_ADDRESS "number()(uint256)" --rpc-url $KUB_TESTNET_RPC_URL
```

This should return `0`, the initial value of the Counter's `number` storage variable.

## Verify the Source Code on KUB Scan

Verify the source code on KUB Scan with Foundry using the following command line

```bash
forge verify-contract $COUNTER_CONTRACT_ADDRESS ./src/Counter.sol:Counter \
  --verifier blockscout \
  --verifier-url https://testnet.kubscan.com/api \
  --rpc-url $KUB_TESTNET_RPC_URL
```

{% hint style="info" %}
For KUB Mainnet, swap the verifier URL to `https://www.kubscan.com/api`.
{% endhint %}

{% hint style="warning" %}
The KUB Scan API enforces a rate limit of 12 requests per minute per IP. Exceeding it blocks the IP for 1 minute.
{% endhint %}

Congratulations! You've deployed and verified a smart contract on KUB Testnet.

## Next Steps

* Use [wagmi](https://wagmi.sh) or [viem](https://viem.sh) to connect your frontend to your contracts (see this guide).
* Launch a token with the KAP standard (see this guide).
* Submit your app to the [KUB Developer Center](https://developers.kubchain.com/) to be showcased in the KUB Ecosystem.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kubchain.com/quickstart/deploy-smart-contracts-on-kub.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
