# 基于本地测试网的开发流程

本教程描述了使用本地测试网进行开发的端到端流程。

* 注意：本教程使用的 Aptos-CLI 来自源代码编译，而不是 github release 的版本

  如果你使用来自 GitHub 发布的 `aptos` CLI 或来自 `cargo install` 的 `aptos` CLI，本指南是不正确的，只有当你按照下面的描述从 aptos-core 中自己构建它的时候才是正确的。

请仔细阅读本指南。本指南特别针对本地测试网的开发流程。如果你在 devnet 上构建，这个流程将无法工作。

###

### 基于 `aptos-core` 运行一个本地测试网

clone 代码并且进入 `aptos-core` 工程路径：

```bash
 git clone git@github.com:aptos-labs/aptos-core.git ~/aptos-core && cd ~/aptos-core
```

运行本地测试网：

```bash
cargo run -p aptos -- node run-local-testnet --with-faucet --faucet-port 8081 --force-restart --assume-yes
```

如果你想启动一个基于 CLI 的发布版来运行本地测试网，你可以在`cargo run` 后面加上

`--release` 标志。

现在你正在运行一个由 `aptos-core` main 构建的本地测试网。

### Typescript: 使用 `aptos-core` 的 SDK

**重要提示：**&#x5728;这个开发流程中，你必须不使用 npmjs 的 SDK。相反，你必须使用与 `aptos` CLI 构建来源的相同的 SDK，我们将在下面介绍。

本指南假设你已经完成了前面的本地测试网步骤。我们还假设你已经安装了 `yarn`。

首先：进入 `aptos-core` 路径，构建 SDK

```
cd ~/aptos-core/ecosystem/typescript/sdk
yarn install
yarn build
```

如果你没有的话，创建一个新工程路径

```
mkdir ~/project && cd ~/project
yarn init
```

使你的项目指向本地 `aptos-core` 的SDK。

```
yarn add ../aptos-core/ecosystem/typescript/sdk
```

你也可以使用绝对路径，例如 `/home/daniel/aptos-core/ecosystem/typescript/sdk`。

安装所有东西。

```
yarn install
```

现在你已经准备好了! 你应该在 `package.json` 中看到，你的项目指向你的本地 `aptos-core` 路径。

```
{
  "name": "project",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "aptos": "../a/core/ecosystem/typescript/sdk/"
  }
}
```

这样你的本地测试网和你所使用的 SDK 就会匹配，这意味着你不会看到任何兼容性问题。

现在你可以在你的代码中像这样使用 aptos 模块。

```
import { AptosClient, AptosAccount, FaucetClient } from "aptos";

const NODE_URL = "https://127.0.0.1:8080/v1";
const FAUCET_URL = "https://127.0.0.1:8081";

(async () => {
  const client = new AptosClient(NODE_URL);
  const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL);
})();
```

{% hint style="info" %}
注意：请看这段代码构建的客户端是与你的本地 testnet 交互，而不是与 devnet。
{% endhint %}
