# Download, Compile, Store

## Download

We're going to grab the `cosmwasm-examples` repo and compile our chosen contract.

```bash
# get the code
git clone https://github.com/CosmWasm/cosmwasm-examples
cd cosmwasm-examples
git fetch
git checkout 44d6a256cd99e66849e550185c98671d4109d78b # current at time of writing, should be cw 1.0.0-beta
cd contracts/erc20
```

## Compile

We can compile our contract like so:

```
# compile the wasm contract with stable toolchain
rustup default stable
cargo wasm
```

However, we want to create an optimised version to limit gas usage, so we're going to run:

```bash
sudo docker run --rm -v "$(pwd)":/code \
    --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \
    --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
    cosmwasm/rust-optimizer:0.12.6
```

This will result in an artifact called `cw_erc20.wasm` being created in the `artifacts` directory.

## Uploading

You can now upload, or 'store' this to the chain via your local node.

```bash
cd artifacts
sourced tx wasm store cw_erc20.wasm  --from <your-key> --chain-id=<chain-id> \
  --gas-prices 0.1usource --gas auto --gas-adjustment 1.3 -b block -y
```

{% hint style="info" %}
You will need to look in the output for this command for the code ID of the contract. In the JSON, it will look like `{"key":"code_id","value":"6"}` in the output.
{% endhint %}

Alternatively, you can capture the output of the command run above, by doing these steps instead, and use the `jq` tool installed earlier to get the `code_id` value:

```bash
cd artifacts
TX=$(sourced tx wasm store cw_erc20.wasm  --from <your-key> --chain-id=<chain-id> --gas-prices 0.1usource --gas auto --gas-adjustment 1.3 -b block --output json -y | jq -r '.txhash')
CODE_ID=$(sourced query tx $TX --output json | jq -r '.logs[0].events[-1].attributes[0].value')
```

You can now see this value with:

```bash
echo $CODE_ID
```


---

# 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.sourceprotocol.io/smart-contracts-and-sourced-development/cw-20-erc-20-tutorial/download-compile-store.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.
