# Deploying Smart Contracts

### 1. Create Foundry Project & Write Contracts

Install Foundry:

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

Initialize a new project:

```
forge init memecore-contract-demo
cd memecore-contract-demo
```

Open the folder in your preferred IDE (e.g. VS Code), and create  `src/Counter.sol`:

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

contract Counter {
    uint256 public number;

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

Compile:

```
forge build
```

Test code example `test/Counter.t.sol`:&#x20;

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

import "forge-std/Test.sol";
import "../src/Counter.sol";

contract CounterTest is Test {
    Counter public counter;

    function setUp() public {
        counter = new Counter();
        counter.setNumber(0);
    }

    function testIncrement() public {
        counter.increment();
        assertEq(counter.number(), 1);
    }

    function testSetNumber(uint256 x) public {
        counter.setNumber(x);
        assertEq(counter.number(), x);
    }
}
```

Run tests:

```powershell
forge test
```

### 2. **Deploying Contracts and Checking Results**

> The following examples are based on MemeCore’s **testnet**, the Insectarium network.

#### Requirements

* Your EOA (e.g. MetaMask) wallet must have M tokens and your private key is needed (for signing transactions and paying fees).
  * If you are connecting to a testnet, you can obtain tokens from the [testnet faucet](https://docs.memecore.com/developer-guide/quickstart/receive-test-tokens-from-faucet); *If you are connecting to the mainnet, you will need to prepare the necessary funds separately.*
* Keep your private key secure and never expose it.

#### Deploying Contracts

Deployment command:

```
forge create --rpc-url <YOUR_RPC_URL>
             --private-key <YOUR_PRIVATE_KEY>
             src/Example.sol:Example
```

For example:

```
forge create --rpc-url https://rpc.insectarium.memecore.net
             --private-key 0x5e2b...b9b6b6
             src/Counter.sol:Counter
```

{% hint style="info" %}
If you are connecting to the mainnet, please refer to the Mainnet RPC URL in the [Memecore Network Information](https://docs.memecore.com/memecore/connect-to-memecore).
{% endhint %}

If a constructor takes arguments:

```
forge create --rpc-url <YOUR_RPC_URL>
             --constructor-args "Arg1" "Arg2"
             --private-key <YOUR_PRIVATE_KEY>
             src/Example.sol:Example
```

#### **Checking Results**

* After running the deployment command, the contract address and transaction hash will be displayed in the console upon success.
* You can search for the contract address on the Block Explorer (e.g. [Insectarium Explorer](https://insectarium.blockscout.memecore.com/)) to view the contract code, transactions, and event logs.
  * You can find the Block Explorer links for each network in the [Memecore Network Information](https://docs.memecore.com/memecore/connect-to-memecore).

### 3. Common Issues & Solutions

* Network connection failure: Ensure the RPC URL and PRIVATE KEY are correct.
* Insufficient gas: Make sure your deployment account (with M tokens) has sufficient balance.
* Transaction failure/Compile error: Review your contract code, command options, and environment variables for potential problems.
