티스토리 뷰
728x90
ganache-cli 로 로컬 네트워크 생성
ganache-cli --db ~/dev/blockchain/ganacheDB -m "stereo consider quality wild fat farm symptom bundle laundry side one lemon" -i 1640314894363
- ganache를 재실행하면 이전에 배포해놓은 컨트랙트들이 리셋되는 것을 방지하고자 db와 chain id 옵션을 주었다.
- 지갑 주소를 고정하기 위해서 mnemonic 옵션을 사용했다.
truffle을 이용해서 erc20 토큰 배포
truffle init
으로 트러플 프로젝트 생성
// erc20 토큰 컨트랙트 코드
// contracts/SimpleToken.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.10;
interface ERC20Interface {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount)
external
returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender)
external
view
returns (uint256);
function transferFrom(
address spender,
address recipient,
uint256 amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 amount);
event Transfer(
address indexed spender,
address indexed from,
address indexed to,
uint256 amount
);
event Approval(
address indexed owner,
address indexed spender,
uint256 oldAmount,
uint256 amount
);
}
contract SimpleToken is ERC20Interface {
mapping(address => uint256) private _balances;
mapping(address => mapping(address => uint256)) public _allowances;
uint256 public _totalSupply;
string public _name;
string public _symbol;
uint8 public _decimals;
constructor(string memory getName, string memory getSymbol) {
_name = getName;
_symbol = getSymbol;
_decimals = 18;
_totalSupply = 100000000e18;
_balances[msg.sender] = _totalSupply;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() external view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account)
external
view
virtual
override
returns (uint256)
{
return _balances[account];
}
function transfer(address recipient, uint256 amount)
public
virtual
override
returns (bool)
{
_transfer(msg.sender, recipient, amount);
emit Transfer(msg.sender, recipient, amount);
return true;
}
function allowance(address owner, address spender)
external
view
override
returns (uint256)
{
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount)
external
virtual
override
returns (bool)
{
uint256 currentAllownace = _allowances[msg.sender][spender];
require(
currentAllownace >= amount,
"ERC20: Transfer amount exceeds allowance"
);
_approve(msg.sender, spender, currentAllownace, amount);
return true;
}
function transferFrom(
address sender,
address recipient,
uint256 amount
) external virtual override returns (bool) {
_transfer(sender, recipient, amount);
emit Transfer(msg.sender, sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][msg.sender];
require(
currentAllowance >= amount,
"ERC20: transfer amount exceeds allowance"
);
_approve(
sender,
msg.sender,
currentAllowance,
currentAllowance - amount
);
return true;
}
function _transfer(
address sender,
address recipient,
uint256 amount
) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
uint256 senderBalance = _balances[sender];
require(
senderBalance >= amount,
"ERC20: transfer amount exceeds balance"
);
_balances[sender] = senderBalance - amount;
_balances[recipient] += amount;
}
function _approve(
address owner,
address spender,
uint256 currentAmount,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
require(
currentAmount == _allowances[owner][spender],
"ERC20: invalid currentAmount"
);
_allowances[owner][spender] = amount;
emit Approval(owner, spender, currentAmount, amount);
}
}
truffle compile
명령어로 컨트랙트 컴파일 =>build
디렉토리 생성
// 배포 스크립트
// scripts/2_deploy_contracts.js
var simpleToken = artifacts.require("SimpleToken");
module.exports = function (deployer) {
deployer.deploy(simpleToken, "SimpleToken", "SMT");
};
// truffle-config.js
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*",
}
}
};
truffle migrate
명령어로development
네트워크(ganache 네트워크)에 컨트랙트 배포
truffle 콘솔에서 배포된 erc20 토큰 확인
truffle console --network development
명령어로 네트워크 접속
let tokenContract = await SimpleToken.deployed()
tokenContract.address
로 컨트랙트 주소를 얻을 수 있다.
'블록체인 개발 > DApp' 카테고리의 다른 글
인센티브(토큰) 기반 커뮤니티 개발 (0) | 2022.01.03 |
---|---|
스마트 컨트랙트 개발 도구(truffle, hardhat) 기본 사용법 (0) | 2021.12.27 |
[DApp] 오픈씨 일부 기능 구현(NFT 민팅, 전송 등) (0) | 2021.12.20 |
[web3.js] web3.js로 이더스캔 기능 구현 (0) | 2021.12.18 |
[라이브러리] web3 vs. ethers (0) | 2021.12.18 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Hardhat
- interface
- Call
- 스마트 컨트랙트
- caver-js
- typescript
- 블록 탐색기
- avalanchego
- Truffle
- web3.js
- class
- eslint
- metamask-extension
- Proxy Pattern
- caver.js
- web3
- JWT
- 이더리움
- nft
- ethers.js
- 앱 아이콘
- Android
- Flutter
- web3-token
- erc20
- Upgradeable Contracts
- ganache
- 블록체인
- swr
- ERC721
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함