Mã thông báo ERC20 là đại diện của một thứ gì đó trên Chuỗi khối Ethereum. Mã thông báo ERC20 đã trở nên rất hữu ích và đã tìm thấy các ứng dụng trong nhiều lĩnh vực
mục tiêu
Trong bài viết này, chúng tôi sẽ xem xét những gì bạn cần để triển khai và giao dịch mã thông báo ERC20 của riêng bạn, bằng cách thực hiện như sau
- Tạo mã thông báo ERC20 của riêng bạn
- Triển khai mã thông báo của bạn vào một chuỗi khối cục bộ
- Thực hiện giao dịch bằng mã thông báo của bạn
- Tạo một hợp đồng thông minh tương tác với mã thông báo của bạn
điều kiện tiên quyết
- Đã cài đặt Visual Studio Code [VS Code]
- Đã cài đặt môi trường Brownie Python
- Một số kiến thức cơ bản về ngôn ngữ lập trình Python
- Một số kiến thức trước về ngôn ngữ lập trình Solidity
Mục lục
- Khởi tạo
- Triển khai mã thông báo
- Giao dịch bằng token
- Tạo Hợp đồng thông minh giao dịch Mã thông báo
Khởi tạo
Tạo một thư mục mới ERC20. Mở nó trong Mã VS. mở thiết bị đầu cuối
brownie init
Triển khai mã thông báo
Tạo một tệp trong thư mục
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
1 có tên là //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
2. Nó sẽ chứa tất cả các cấu hình của bạnCách dễ nhất để triển khai hợp đồng ERC20 là sử dụng thư viện của
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
3. Hợp đồng thông minh của họ được kiểm tra kỹ lưỡng và tin cậy bởi các công ty tiền điện tử hàng đầuCác phụ thuộc từ
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
3 sẽ được chứa trong tệp //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
2dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
Bây giờ vào sự kiện chính. Tạo mã thông báo MyToken của riêng bạn. sol. Lưu tệp trong thư mục hợp đồng
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
Để đảm bảo hợp đồng của bạn đã được thiết lập xong, hãy chạy
________số 8
Mã thông báo của bạn có 2 tham số.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
6 và //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
7. Người sáng lập sẽ nhận được tất cả các mã thông báo từ nguồn cung cấp ban đầuMã thông báo của bạn kế thừa từ ERC20 của OpenZeppelin. Cần 2 tham số.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
8 và //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
9, đã được đặt tên là brownie compile
0 VÀ brownie compile
1Khi khởi tạo, hợp đồng này cấp mã thông báo
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
7 cho //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
6, thông qua chức năng brownie compile
4Theo mặc định, mã thông báo ERC20 này sử dụng 18 số thập phân
Tạo một tệp Python có tên là transact_token. py trong thư mục script
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
8MyToken. triển khai [] có 3 tham số
- Người sáng lập sẽ có mã thông báo được đúc cho họ
- Nguồn cung cấp ban đầu là số lượng mã thông báo được đúc cho người sáng lập
- Tài khoản triển khai Hợp đồng thông minh mã thông báo [{“từ”. x}]
Điều quan trọng là tài khoản của người sáng lập phải là tài khoản triển khai hợp đồng thông minh. Điều này là do các chức năng trong hợp đồng ERC20 sẽ cho rằng tiền sẽ đến từ tài khoản đã triển khai Hợp đồng thông minh mã thông báo
brownie compile
5 đã được đặt thành 21 triệu mã thông báobrownie compile
6 đã được sử dụng để chuyển đổi 21 triệu thành 18 chữ số thập phân bắt buộcTài khoản bạn sẽ sử dụng để triển khai mã thông báo này sẽ là
brownie compile
7. Đây là một địa chỉ trên mạng chuỗi khối cục bộ được triển khai bằng cách sử dụng brownie compile
8. Brownie có thiết lập brownie compile
8 theo mặc địnhTrong thiết bị đầu cuối, chạy
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
4Giao dịch bằng Token
Bạn đã triển khai mã thông báo của mình trên chuỗi khối. Bây giờ bạn cần kiểm tra số dư. Trong giao dịch_token. py tạo một chức năng mới
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
5//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
80 là một chức năng khác được triển khai bởi //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
3 để kiểm tra số dư của mã thông báo được giữ bởi một địa chỉKiểm tra số dư mã thông báo được cung cấp cho
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
82, người là brownie compile
7//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
0Chạy
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
4Biết rằng
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
84 của bạn có đủ mã thông báo, bây giờ hãy gửi nó đến các tài khoản khác. Trong giao dịch_token. py tạo một chức năng mới//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
3//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
85 nhận được hợp đồng thông minh //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
86 được triển khai gần đây nhấtMã thông báo được chuyển từ tài khoản đã triển khai Hợp đồng thông minh mã thông báo. Sau đó, nó được gửi đến địa chỉ người nhận được chỉ định
Trong hàm
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
87dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
0Trong thiết bị đầu cuối, chạy
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
4Tạo hợp đồng thông minh giao dịch mã thông báo
Trong thư mục hợp đồng, tạo một tệp mới QuizReward. sol
dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
2//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
88 chứa số được so sánh với. //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
89 chứa số lượng mã thông báo sẽ được gửi cho bất kỳ ai trả lời đúngHàm
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
40 chuyển đổi số đầu vào thành 18 chữ số thập phânHàm
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
41 có 3 tham số. Câu trả lời nhận được, địa chỉ được thanh toán và mã thông báo để thưởng. Phần thưởng chỉ được chuyển đến địa chỉ nếu câu trả lời đúngTrong thiết bị đầu cuối, chạy
________số 8
Triển khai hợp đồng thông minh này trong transact_token. py
dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
4Có 2 cách mà hợp đồng thông minh này có thể thưởng cho người dùng
Cách đầu tiên là chuyển mã thông báo từ người sáng lập [
brownie compile
7] sang hợp đồng thông minh. Loại chuyển đổi này đã được chứng minhCách thứ hai là người sáng lập cung cấp cho hợp đồng thông minh một khoản trợ cấp. Một khoản trợ cấp để tiêu tiền của người sáng lập. Hãy coi đó là việc người sáng lập cấp thẻ ghi nợ của cô ấy, nhưng cô ấy có thể giới hạn số tiền mà hợp đồng thông minh có thể chi tiêu. Đây là những gì chúng ta sẽ làm
Trong giao dịch_token. py tạo một chức năng
dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
5Người chi tiêu trong trường hợp này sẽ là hợp đồng thông minh
dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
6Hợp đồng thông minh phần thưởng bài kiểm tra có quyền truy cập vào 6.000.000 mã thông báo của người sáng lập [
brownie compile
7]. Hợp đồng thông minh không nhất thiết phải sở hữu 6.000.000 mã thông báo. Nó chỉ được phép chi tiêu nóTiếp tục trong
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
87dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
7giao dịch cuối cùng_token. py sẽ trông như sau
dependencies:
- OpenZeppelin/openzeppelin-contracts@4.5.0
compiler:
solc:
remappings:
- '@openzeppelin=OpenZeppelin/openzeppelin-contracts@4.5.0'
8Cuối cùng, chạy
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
4Bạn sẽ nhận được đầu ra này
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
0Rõ ràng, hợp đồng thông minh đã có thể chuyển 1000000 mã thông báo vào tài khoản theo yêu cầu. Cũng có thể thấy rằng hợp đồng thông minh không có mã thông báo mà nó sở hữu. Chỉ được phép sử dụng mã thông báo trong tài khoản của người sáng lập
Đọc thêm
Để xem tất cả các chức năng ERC20 được kế thừa từ thư viện OpenZeppelin, hãy theo liên kết này. OpenZeppelin ERC20
Có một vài chức năng chưa được đề cập ở đây
Ví dụ.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
45. Điều này trái ngược với brownie compile
4. Nó phá hủy các mã thông báo trong tài khoản và giảm tổng nguồn cung đang lưu thông//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
47. Điều này trái ngược với //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
48. Nó làm giảm các mã thông báo mà một địa chỉ được phép chi tiêu trên một tài khoảnVới những kiến thức cơ bản đã có, bạn có thể dễ dàng điều chỉnh các chức năng này vào mã của mình mà không gặp nhiều rắc rối
Phần kết luận
Trong hướng dẫn này, bạn đã học cách tạo các mã thông báo ERC20 này và triển khai chúng vào chuỗi khối cục bộ bằng cách sử dụng ganache, nhưng bạn có thể thử thách bản thân bằng cách triển khai nó vào mạng thử nghiệm như
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
49 hoặc thậm chí là //SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
//Instead of WILLIAM and BILL you can use your names to make this token your own!
constructor [address _founder, uint256 _initialSupply] ERC20["WILLIAM","BILL"]{
_mint[_founder,_initialSupply];
}
}
50. Bạn cũng đã học tất cả các cách khác nhau mà bạn có thể giao dịch bằng cách sử dụng mã thông báo của mình và cả cách tạo hợp đồng thông minh để giao dịch mã thông báo của bạn