# 在 Titan链上 部署智能合约

## 如何将 WASM 二进制文件部署到 Titan 链上

***注意： 此过程需要较强的专业知识。本教程专注于使用命令行部署智能合约，其他方式请自行探索。***

### 步骤 1: 准备 WASM 合约文件

* 编译您的 WASM 合约，并生成名为 `<project_name>.wasm` 的二进制文件。

### 步骤 2: 上传 WASM 二进制文件到 Titan 链

* 执行上传操作:

{% code overflow="wrap" %}

```bash
titand tx wasm store <project_name>.wasm --from <account> --gas 10000000 --gas-prices 0.0025uttnt
```

{% endcode %}

`<account>` *代表您的账户名称，请替换为您的实际账户名。*

* 检查交易是否上链成功并获取 code id:

```bash
titand query tx <tx_hash>
```

`<tx_hash>`*是您在上传操作后生成的交易哈希，用于查询交易状态。*

* 或者通过以下命令获取 code id:

```bash
titand query wasm list-code
```

*通过 list-code 命令可以查看所有已上传的合约代码及其 code id。*

### 步骤 3: 实例化 WASM 合约

* 执行合约实例化操作:

{% code overflow="wrap" %}

```bash
titand tx wasm instantiate <code_id> <json_encoded_init_args> --admin="$(titand keys show <account> -a)" --from <account> --gas 10000000 --gas-prices 0.0025uttnt --label <label>
```

{% endcode %}

`<code_id>` *是您在步骤 2 中获取到的代码 ID。*

&#x20;`<json_encoded_init_args>` *是合约初始化时需要传入的参数，需以 JSON 格式编码。*

&#x20;`<label>` *是合约的标签，用于标识此实例化的合约。*

* 检查交易是否上链成功并获取合约地址:

```bash
titand query tx <tx_hash>
```

*成功后，您将获得合约地址，用于后续操作。*

### 步骤 4: 查询合约状态

* 通过命令行查询合约状态:

```bash
titand query wasm contract-state smart <contract_address> <query_args>
```

`<contract_address>` *是您在步骤 3 中获得的合约地址。*

`<query_args>` *是查询时需要传入的参数，需以 JSON 格式编码。*

* 通过 Titan 区块浏览器查看合约状态:
  * 您也可以通过 Titan 的区块浏览器查看合约状态：[Titan 区块浏览器](https://testnet.titan.explorers.guru/contracts)。
  * 在浏览器中输入您的合约地址，即可查看合约详情和状态。

### 步骤 5: 执行合约

* 执行合约调用:

{% code overflow="wrap" %}

```bash
titand tx wasm execute <contract_address> <exec_args> --fees 500uttnt --from <account>
```

{% endcode %}

`<exec_args>` *是执行合约时传入的参数，需以 JSON 格式编码。*

#### 提示：

* 每个步骤中的 <...> 部分需要替换为您的实际值。
* 在执行各步骤前，建议仔细检查命令中的参数设置，确保合约正确部署和运行。


---

# 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://titannet.gitbook.io/titan-network-cn/qu-kuai-lian-ce-shi/zai-titan-lian-shang-bu-shu-zhi-neng-he-yue.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.
