> For the complete documentation index, see [llms.txt](https://archive-vn.docs.klaytn.foundation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://archive-vn.docs.klaytn.foundation/content/smart-contract/sample-contracts/erc-721/2-erc721.md).

# 2. Triển khai hợp đồng thông minh

Bạn có thể sử dụng Remix Online IDE hoặc Truffle để triển khai hợp đồng thông minh `MyERC721Card` trên đây.

## 2.1 Triển khai hợp đồng thông minh bằng Remix Online IDE <a href="#id-2-1-deploying-smart-contract-using-klaytn-ide" id="id-2-1-deploying-smart-contract-using-klaytn-ide"></a>

* Vui lòng truy cập [Klaytn Plugin for Remix](https://ide.klaytn.foundation) và tạo hợp đồng `MyERC721Card`. Mã nguồn hoàn chỉnh được cung cấp tại mục [Soạn hợp đồng thông minh ERC-721](/content/smart-contract/sample-contracts/erc-721/1-erc721.md).
* Tạo một tài khoản để triển khai hợp đồng.
  * Nếu bạn chưa có tài khoản, hãy tạo một tài khoản tại <https://baobab.wallet.klaytn.foundation/create> hoặc \[<https://toolkit.klaytn.foundation/tài> khoản/tài khoảnKeyLegacy]\(<https://toolkit.klaytn.foundation/tài> khoản/tài khoảnKeyLegacy).
  * Nhận một số KLAY thử nghiệm từ faucet - <https://baobab.wallet.klaytn.foundation/faucet>
* Hãy triển khai hợp đồng `MyERC721Card.sol` như dưới đây.

![ERC721-1-deploy](/files/SQHgez2ktWtQcOyWNd9M)

Giờ đây `MyERC721Card` đã hoạt động! Bạn có thể tạo và chuyển đổi các thẻ là các token không thể thay thế tương thích với ERC-721.

Hãy tạo hai thẻ, cụ thể là thẻ `King` và `Queen`, cho tài khoản `0x2645BA5Be42FfEe907ca8e9d88f6Ee6dAd8c1410` như dưới đây.

![ERC721-2-mint-king](/files/jSHYr8WALGcey0yeUM8c) ![ERC721-3-mint-queen](/files/eHeLgem6p8zmlKqcyfuk)

Giờ chúng ta đã tạo được hai thẻ và hãy kiểm tra trạng thái của các token không thể thay thế `MyERC721Card` này.

![ERC721-4-cards-trạng thái](/files/l2QZjgAfSGO1wnCmiiuX)

* `balanceOf` thể hiện rằng tài khoản `0x2645BA5Be42FfEe907ca8e9d88f6Ee6dAd8c1410` có hai thẻ.
* `cards` có tham số `1` thể hiện rằng `MyERC721Card` với ID token `1` là thẻ `Queen` cấp độ 1.
* `ownerOf` có tham số `0` thể hiện rằng chủ sở hữu của `MyERC721Card` với ID token `0` là `0x2645BA5Be42FfEe907ca8e9d88f6Ee6dAd8c1410`.

## 2.2 Triển khai hợp đồng thông minh bằng truffle <a href="#id-2-2-deploying-smart-contract-using-truffle" id="id-2-2-deploying-smart-contract-using-truffle"></a>

Bạn nên cài đặt xong [node.js](https://nodejs.org/) trong môi trường của mình. Hãy xem [Hướng dẫn cài đặt Node.js qua trình quản lý gói](https://nodejs.org/en/download/package-manager/) để cài đặt node.js bằng trình quản lý gói trong các môi trường khác nhau.

```
$ mkdir klaytn
$ cd klaytn
$ npm init # initialize npm at the erc20token directory
$ npm install truffle@4.1.15
$ npm install caver-js@latest # installing caver-js
$ ln -s node_modules/truffle/build/cli.bundled.js truffle
$ export PATH=`pwd`:$PATH
```

Giờ đây, bạn đã cài đặt truffle và caver-js, hai công cụ cần thiết để triển khai hợp đồng thông minh.

Hãy chuẩn bị `truffle` và một hợp đồng thông minh `MyERC721Card.sol`.

```
$ mkdir myerc721
$ cd myerc721
$ truffle init
```

Giờ bạn sẽ có các cấu trúc thư mục như sau.

```
.
├── contracts
│   ├── Migrations.sol
├── migrations
│   └── 1_initial_migration.js
└── truffle-config.js
```

Sau khi soạn hợp đồng `MyERC721Card.sol` và đặt nó vào thư mục `contracts`, cấu trúc thư mục sẽ như dưới đây.

Giờ bạn sẽ có các cấu trúc thư mục như sau.

```
.
├── contracts
│   ├── Migrations.sol
│   └── MyERC721Card.sol
├── migrations
│   └── 1_initial_migration.js
└── truffle-config.js
```

Ngoài ra, chỉnh sửa `1_initial_migration.js` như trình bày dưới đây để triển khai hợp đồng `MyERC721Card`.

```javascript
const Migrations = artifacts.require("./Migrations.sol");
const MyERC721Card = artifacts.require("./MyERC721Card.sol");
module.exports = function(deployer) {
  deployer.deploy(Migrations);
  deployer.deploy(MyERC721Card)
};
```

Bạn cũng phải cấu hình tập tin `truffle-config.js` để triển khai hợp đồng thông minh lên mạng lưới Klaytn. Bước này giống như bước được mô tả tại mục [Triển khai Hợp đồng thông minh bằng Truffle](/content/getting-started/quick-start/deploy-a-smart-contract.md#deploying-a-smart-contract-using-truffle)

```
// truffle-config.js
module.exports = {
    networks: {
        baobab: {
            host: '127.0.0.1',
            port: 8551,
            from: '0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd', // nhập địa chỉ hợp đồng của bạn
            network_id: '1001', // id mạng Baobab
            gas: 20000000, // giới hạn gas của giao dịch
            gasPrice: 250000000000, // gasPrice của Baobab là 250 ston
        },
    },
    compilers: {
      solc: {
        version: "0.5.12"    // Chỉ định phiên bản trình biên dịch là 0.5.12
      }
  }
};
```

Giờ đây bạn đã sẵn sàng, hãy cùng triển khai `MyERC721Card.sol` bằng lệnh như sau.

```
$ truffle deploy --network baobab --reset
Compiling ./contracts/MyERC721Card.sol...
Soạn các tập tin artifact trong thư mục ./build/contracts

Sử dụng mạng lưới 'baobab'.

Running migration: 1_initial_migration.js
  Replacing Migrations...
  ... 0x5a947f076f4570dff8ff18b1ae3557e27dd69c92ce38a3c97fad8f5355914066
  Migrations: 0x0d737e9865e5fc4c1ff53744fd2c13c52a44b9bc
  Deploying MyERC721Card...
  ... 0x1571e80552dab1d67260e8914e06d9b16ccae16fb698c750f6a09aab12517bc1
  MyERC721Card: 0xc3d282926871c505f334d0f2c85ad52758347831
Lưu quá trình di chuyển thành công lên mạng...
  ... 0x5b984b3f79c425d80470a96d5badb857fc05e7f31d94423044ae3119c639aa77
Lưu các tập tin artifact...
```

Đầu ra bảng điều khiển thể hiện hàm băm giao dịch để triển khai hợp đồng `MyERC721Card` là `0x1571e80552dab1d67260e8914e06d9b16ccae16fb698c750f6a09aab12517bc1` và địa chỉ của `MyERC721Card` là `0xc3d282926871c505f334d0f2c85ad52758347831`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://archive-vn.docs.klaytn.foundation/content/smart-contract/sample-contracts/erc-721/2-erc721.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
