# Smart Contracts (CosmWasm)

### Getting Started

If you're new to **CosmWasm** and **Smart Contracts**, we recommend starting with the [CW-Template repository](https://github.com/InterWasm/cw-template). This template will help you understand the basic interfaces and models of CosmWasm smart contracts.

When you're ready to deploy your smart contract, you’ll need a local build of the chain core. This will allow you to store your code on-chain and submit governance proposals to instantiate it.

#### Installation Steps

1. Clone the Rujira core repository:

   ```bash
   git clone https://gitlab.com/thornode/thorchain/app-layer/
   cd core
   make install
   ```

### Deployment

Now that you've set up the environment, you're ready to store your code on-chain.

#### Step 1: Optimize the Code

To optimize your contract's code, run the following command:

```bash
cargo run-script optimize
```

#### Step 2: Create a Local Account

You’ll need a local account to handle transactions.

```bash
rujirad keys add <local-account>
```

This will generate a seed phrase and an associated Kujira address for your account. Make sure to store the seed phrase safely. You’ll also need to fund this address with **KUJI tokens** before proceeding to the next step.

#### Step 3: Store Code on Chain

To store the optimized code on-chain, run the following:

```bash
rujirad tx wasm store <./path/to/optimized/code.wasm> --from <local-account>
```

Upon successful execution, logs will be generated. These logs contain the `code_id` of your contract, which will look something like this:

```bash
logs:
- events:
  - attributes:
    - key: action
      value: /cosmwasm.wasm.v1.MsgStoreCode
    - key: module
      value: wasm
    - key: sender
      value: rujira1...
    type: message
  - attributes:
    - key: code_id
      value: "4"
    type: store_code
```

### Instantiation

Your code is now stored on-chain and behaves similarly to a class in an object-oriented language. Next, you need to instantiate the contract so you can interact with it.

On Kujira, instantiation must be done via a governance proposal. This ensures the quality and security of code and dApps on the network.

#### Step 1: Submit a Governance Proposal

Run the following command to submit an instantiation proposal:

```bash
rujirad tx gov submit-proposal instantiate-contract 4 \
  '{"count": 0}' \
  --title "Instantiate CW-Template" \
  --description "A more detailed description of this proposal" \
  --label "CW-Template" \
  --from <local-account> \
  --admin <local-account-address> \
  --run-as <local-account-address> \
  --gas 1000000 \
  --fees 1250ukuji
```

This creates a proposal at the funding stage. You can then visit Rujira Governance to deposit funds and open it for voting.

#### Step 2: Retrieve Contract Address

After the proposal passes and the contract is instantiated, the contract will have its own unique address. You can find the contract address with the following query:

```bash
rujirad query wasm list-contract-by-code 4
```

Once the contract is instantiated, you're ready to move on to building the UI for your newly deployed smart contract.


---

# Agent Instructions: 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:

```
GET https://docs.rujira.network/developers/getting-started/smart-contracts-cosmwasm.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
