Skip to content

Instantly share code, notes, and snippets.

@fromjyk
Created November 28, 2018 16:24
Show Gist options
  • Save fromjyk/9bbd51dcfb0a5f57edc26ca8bda6d249 to your computer and use it in GitHub Desktop.
Save fromjyk/9bbd51dcfb0a5f57edc26ca8bda6d249 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.16;
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
contract TokenERC20 { // 토큰의 이름과 심볼을 변경할 수 있습니다.
string public name;
string public symbol;
uint8 public decimals = 18; // 18자릿수가 권장되는 기본값이므로 변경하지 마십시오.
uint256 public totalSupply;
// 모든 잔액에 대한 배열을 만듭니다.
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
// 블록체인에서 클라이언트에게 알려주는 이벤트를 생성합니다.
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
// 이 금액을 고객에게 알려줍니다.
event Burn(address indexed from, uint256 value);
/* 컨트렉터 함수 */
// 컨트렉터에게 초기공급 토큰과의 계약을 초기화합니다.
function TokenERC20(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // 10진수로 총 공급량을 업데이트 합니다.
balanceOf[msg.sender] = totalSupply; // 컨트렉터에게 모든 초기 토큰을 제공합니다.
name = tokenName; // 토큰에 표기될 이름을 정합니다. (ex. Ethereum)
symbol = tokenSymbol; // 토큰에 표기될 심볼을 정합니다. (ex. ETH)
}
/*
내부 전송, 이 계약을 통해서만 호출 될 수 있습니다.
*/
function _transfer(address _from, address _to, uint _value) internal {
// 0x0 주소로 전송됨을 방지합니다. 사용된다면 burn()을 사용합니다.
require(_to != 0x0); // 보낸사람의 요건이 충분한지 확인합니다.
require(balanceOf[_from] >= _value); // 자산보다 넘게 보내는지 확인합니다.
require(balanceOf[_to] + _value >= balanceOf[_to]);
// 해당 내용을 저장해 놓습니다.
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// 발신자에게서 뺍니다.
balanceOf[_from] -= _value;
// 받는 사람에게 같은 조건을 추가합니다.
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts 는 정적 분석을 사용하여 우리의 코드에서 버그를 찾는데 사용됩니다. 이것은 절대 생략하지 말아야 합니다.
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/* 토큰 전송
* 계정에서 `_to` 에 `_value` 토큰을 보냅니다.
* @param _to 수신자의 주소
* @param _value 보내는 사람의 양
*/
function transfer(address _to, uint256 _value) public returns (bool success) {
_transfer(msg.sender, _to, _value);
return true;
}
/* 다른 주소에서 토큰 전송
* `_from` 을 대신하여 `_to` 에 `_value` 토큰을 보냅니다.
* @param _from 보낸 사람의 주소
* @param _to 받는 사람의 주소
* @param _value 보낼 금액
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // 비용을 확인
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
/*
* 다른 주소에 대한 허용 설정
* `_spender` 가 당신을 대신하여 `_value` 토큰을 소비하는 것을 허용합니다.
* @param _spender 지출 권한이 있는 주소
* @param _value 지출할 수 있는 최대 금액
*/
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
/* 다른 주소에 대한 허용치 설정 및 알림
* `_spender` 가 당신을 대신하여 `_value` 토큰 이상을 소비하지 못하도록 허용한 다음 그것에 대한 계약서를 작성합니다.
* @param _spender 지출 권한이 있는 주소
* @param _value 지출할수 있는 최대 금액
* @param _extraData 승인된 계약서로 보낼 추가정보
*/
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/* 토큰 파괴
* 시스템에서 `_value` 토큰을 되돌릴 수 없습니다.
* @param 소각(태울) 금액을 ‘_value’로 지정하십시오.
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // 보낸 사람의 밸류가 충분한지 확인
balanceOf[msg.sender] -= _value; // 발신자에게서 뺍니다.
totalSupply -= _value; // TotalSupply 업데이트
emit Burn(msg.sender, _value);
return true;
}
/* 다른 계정에서 토큰 삭제
* `_from`을 대신하여 시스템에서 `_value` 토큰을 비가역적으로 제거합니다.
* @param _from 보낸 사람의 주소
* @param _value 소각(태움)의 금액
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // 목표 잔액이 충분한지 확인합니다.
require(_value <= allowance[_from][msg.sender]);
balanceOf[_from] -= _value; // 목표 잔액에서 차감
allowance[_from][msg.sender] -= _value; // 보낸 사람의 수당에서 뺍니다.
totalSupply -= _value; // TotalSupply 업데이트
emit Burn(_from, _value);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment