pragma solidity ^0.5.6;
// IKIP7
interface IKIP7 {
/// @dev Emitted when value
tokens are moved from one account (from
) to
/// another (to
) and created (from
== 0) and destroyed(to
== 0).
///
/// Note that value
may be zero.
event Transfer(address indexed from, address indexed to, uint256 value);
/// @dev Emitted when the allowance of a `spender` for an `owner` is set by
/// a call to {approve}. `value` is the new allowance.
event Approval(address indexed owner, address indexed spender, uint256 value);
/// @notice Returns the amount of tokens in existence.
/// @return the total supply of this token.
function totalSupply() external view returns (uint256);
/// @notice Returns the amount of tokens owned by `account`.
/// @param account An address for whom to query the balance
/// @return the amount of tokens owned by `account.
function balanceOf(address account) external view returns (uint256);
/// @notice Moves `amount` tokens from the caller's account to `recipient`.
/// @dev Throws if the message caller's balance does not have enough tokens to spend.
/// Throws if the contract is pausable and paused.
///
/// Emits a {Transfer} event.
/// @param recipient The owner will receive the tokens.
/// @param amount The token amount will be transferred.
/// @return A boolean value indicating whether the operation succeeded.
function transfer(address recipient, uint256 amount) external returns (bool);
/// @notice Returns the remaining number of tokens that `spender` will be
/// allowed to spend on behalf of `owner` through {transferFrom}. This is
/// zero by default.
/// @dev Throws if the contract is pausable and paused.
///
/// This value changes when {approve} or {transferFrom} are called.
/// @param owner The account allowed `spender` to withdraw the tokens from the account.
/// @param spender The address is approved to withdraw the tokens.
/// @return An amount of spender's token approved by owner.
function allowance(address owner, address spender) external view returns (uint256);
/// @notice Sets `amount` as the allowance of `spender` over the caller's tokens.
/// @dev Throws if the contract is pausable and paused.
///
/// IMPORTANT: Beware that changing an allowance with this method brings the risk
/// that someone may use both the old and the new allowance by unfortunate
/// transaction ordering. One possible solution to mitigate this race
/// condition is to first reduce the spender's allowance to 0 and set the
/// desired value afterwards:
/// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
///
/// Emits an {Approval} event.
/// @param spender The address is approved to withdraw the tokens.
/// @param amount The token amount will be approved.
/// @return a boolean value indicating whether the operation succeeded.
function approve(address spender, uint256 amount) external returns (bool);
/// @notice Moves `amount` tokens from `sender` to `recipient` using the
/// allowance mechanism. `amount` is then deducted from the caller's
/// allowance.
/// @dev Throw unless the `sender` account has deliberately authorized the sender of the message via some mechanism.
/// Throw if `sender` or `recipient` is the zero address.
/// Throws if the contract is pausable and paused.
///
/// Emits a {Transfer} event.
/// Emits an `Approval` event indicating the updated allowance.
/// @param sender The current owner of the tokens.
/// @param recipient The owner will receive the tokens.
/// @param amount The token amount will be transferred.
/// @return A boolean value indicating whether the operation succeeded.
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/// @notice Moves `amount` tokens from the caller's account to `recipient`.
/// @dev Throws if the message caller's balance does not have enough tokens to spend.
/// Throws if the contract is pausable and paused.
/// Throws if `_to` is the zero address.
/// When transfer is complete, this function checks if `_to` is a smart
/// contract (code size > 0). If so, it calls
/// `onKIP7Received` on `_to` and throws if the return value is not
/// `bytes4(keccak256("onKIP7Received(address,address,uint256,bytes)"))`.
/// @param recipient The owner will receive the tokens.
/// @param amount The token amount will be transferred.
/// @param data Additional data with no specified format, sent in call to `_to`
function safeTransfer(address recipient, uint256 amount, bytes calldata data) external; // SK
/// @notice Moves `amount` tokens from the caller's account to `recipient`.
/// @dev This works identically to the other function with an extra data parameter,
/// except this function just sets data to "".
/// @param recipient The owner will receive the tokens.
/// @param amount The token amount will be transferred.
function safeTransfer(address recipient, uint256 amount) external;
/// @notice Moves `amount` tokens from `sender` to `recipient` using the
/// allowance mechanism. `amount` is then deducted from the caller's
/// allowance.
/// @dev Throw unless the `sender` account has deliberately authorized the sender of the message via some mechanism.
/// Throw if `sender` or `recipient` is the zero address.
/// Throws if the contract is pausable and paused.
/// When transfer is complete, this function checks if `_to` is a smart
/// contract (code size > 0). If so, it calls
/// `onKIP7Received` on `_to` and throws if the return value is not
/// `bytes4(keccak256("onKIP7Received(address,address,uint256,bytes)"))`.
/// Emits a {Transfer} event.
/// Emits an `Approval` event indicating the updated allowance.
/// @param sender The current owner of the tokens.
/// @param recipient The owner will receive the tokens.
/// @param amount The token amount will be transferred.
/// @param data Additional data with no specified format, sent in call to `_to`
function safeTransferFrom(address sender, address recipient, uint256 amount, bytes calldata data) external;
/// @notice Moves `amount` tokens from `sender` to `recipient` using the
/// allowance mechanism. `amount` is then deducted from the caller's
/// allowance.
/// @dev This works identically to the other function with an extra data parameter,
/// except this function just sets data to "".
/// @param sender The current owner of the tokens.
/// @param recipient The owner will receive the tokens.
/// @param amount The token amount will be transferred.
function safeTransferFrom(address sender, address recipient, uint256 amount) external;
}
/// @title KIP-7 Fungible Token Standard, optional wallet interface
/// @dev Note: the KIP-13 identifier for this interface is 0x9d188c22.
interface IKIP7TokenReceiver {
/// @notice Handle the receipt of KIP-7 token
/// @dev The KIP-7 smart contract calls this function on the recipient
/// after a safeTransfer
. This function MAY throw to revert and reject the
/// transfer. Return of other than the magic value MUST result in the
/// transaction being reverted.
/// Note: the contract address is always the message sender.
/// @param _operator The address which called safeTransferFrom
function
/// @param _from The address which previously owned the token
/// @param _amount The token amount which is being transferred.
/// @param _data Additional data with no specified format
/// @return bytes4(keccak256("onKIP7Received(address,address,uint256,bytes)"))
/// unless throwing
function onKIP7Received(address _operator, address _from, uint256 _amount, bytes calldata _data) external returns(bytes4); // SK
}
library Address {
/**
* @dev Returns true if account
is a contract.
*
* This test is non-exhaustive, and there may be false-negatives: during the
* execution of a contract’s constructor, its address will be reported as
* not containing a contract.
*
* > It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*/
function isContract(address account) internal view returns (bool) {
// This method relies in extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly { size := extcodesize(account) }
return size > 0;
}
}
contract IKIP7Receiver {
/**
* @notice Handle the receipt of KIP-7 token
* @dev The KIP-7 smart contract calls this function on the recipient
* after a safeTransfer
. This function MAY throw to revert and reject the
* transfer. Return of other than the magic value MUST result in the
* transaction being reverted.
* Note: the contract address is always the message sender.
* @param _operator The address which called safeTransferFrom
function
* @param _from The address which previously owned the token
* @param _amount The token amount which is being transferred.
* @param _data Additional data with no specified format
* @return bytes4(keccak256("onKIP7Received(address,address,uint256,bytes)"))
* unless throwing
*/
function onKIP7Received(address _operator, address _from, uint256 _amount, bytes memory _data) public returns (bytes4);
}
interface IKIP13 {
/**
* @dev Returns true if this contract implements the interface defined by
* interfaceId
. See the corresponding
* KIP-13 section
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
contract KIP13 is IKIP13 {
/*
* bytes4(keccak256(‘supportsInterface(bytes4)’)) == 0x01ffc9a7
*/
bytes4 private constant _INTERFACE_ID_KIP13 = 0x01ffc9a7;
/**
* @dev Mapping of interface ids to whether or not it's supported.
*/
mapping(bytes4 => bool) private _supportedInterfaces;
constructor () internal {
// Derived contracts need only register support for their own interfaces,
// we register support for KIP13 itself here
_registerInterface(_INTERFACE_ID_KIP13);
}
/**
* @dev See `IKIP13.supportsInterface`.
*
* Time complexity O(1), guaranteed to always use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool) {
return _supportedInterfaces[interfaceId];
}
/**
* @dev Registers the contract as an implementer of the interface defined by
* `interfaceId`. Support of the actual KIP13 interface is automatic and
* registering its interface id is not required.
*
* See `IKIP13.supportsInterface`.
*
* Requirements:
*
* - `interfaceId` cannot be the KIP13 invalid interface (`0xffffffff`).
*/
function _registerInterface(bytes4 interfaceId) internal {
require(interfaceId != 0xffffffff, "KIP13: invalid interface id");
_supportedInterfaces[interfaceId] = true;
}
}
library SafeMath {
/**
* @dev 두 부호 없는 정수의 합을 반환합니다.
* 오버플로우 발생 시 예외처리합니다.
*
* 솔리디티의 +
연산자를 대체합니다.
*
* 요구사항:
* - 덧셈은 오버플로우될 수 없습니다.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, “SafeMath: addition overflow”);
return c;
}
/**
* @dev 두 부호 없는 정수의 차를 반환합니다.
* 결과가 음수일 경우 오버플로우입니다.
*
* 솔리디티의 `-` 연산자를 대체합니다.
*
* 요구사항:
* - 뺄셈은 오버플로우될 수 없습니다.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
/**
* @dev 두 부호 없는 정수의 곱을 반환합니다.
* 오버플로우 발생 시 예외처리합니다.
*
* 솔리디티의 `*` 연산자를 대체합니다.
*
* 요구사항:
* - 곱셈은 오버플로우될 수 없습니다.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// 가스 최적화: 이는 'a'가 0이 아님을 요구하는 것보다 저렴하지만,
// 'b'도 테스트할 경우 이점이 없어집니다.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev 두 부호 없는 정수의 몫을 반환합니다. 0으로 나누기를 시도할 경우
* 예외처리합니다. 결과는 0의 자리에서 반올림됩니다.
*
* 솔리디티의 `/` 연산자를 대체합니다. 참고: 이 함수는
* `revert` 명령코드(잔여 가스를 건들지 않음)를 사용하는 반면, 솔리디티는
* 유효하지 않은 명령코드를 사용해 복귀합니다(남은 모든 가스를 소비).
*
* 요구사항:
* - 0으로 나눌 수 없습니다.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// 솔리디티는 0으로 나누기를 자동으로 검출하고 중단합니다.
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
// assert(a == b * c + a % b); // 이를 만족시키지 않는 경우가 없어야 합니다.
return c;
}
/**
* @dev 두 부호 없는 정수의 나머지를 반환합니다. (부호 없는 정수 모듈로 연산),
* 0으로 나눌 경우 예외처리합니다.
*
* 솔리디티의 `%` 연산자를 대체합니다. 이 함수는 `revert`
* 명령코드(잔여 가스를 건들지 않음)를 사용하는 반면, 솔리디티는
* 유효하지 않은 명령코드를 사용해 복귀합니다(남은 모든 가스를 소비).
*
* 요구사항:
* - 0으로 나눌 수 없습니다.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, "SafeMath: modulo by zero");
return a % b;
}
}
contract KIP7 is KIP13, IKIP7 {
using SafeMath for uint256;
using Address for address;
// Equals to `bytes4(keccak256("onKIP7Received(address,address,uint256,bytes)"))`
// which can be also obtained as `IKIP7Receiver(0).onKIP7Received.selector`
bytes4 private constant _KIP7_RECEIVED = 0x9d188c22;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
/*
* bytes4(keccak256('totalSupply()')) == 0x18160ddd
* bytes4(keccak256('balanceOf(address)')) == 0x70a08231
* bytes4(keccak256('transfer(address,uint256)')) == 0xa9059cbb
* bytes4(keccak256('allowance(address,address)')) == 0xdd62ed3e
* bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
* bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
* bytes4(keccak256("safeTransfer(address,uint256)")) == 0x423f6cef
* bytes4(keccak256("safeTransfer(address,uint256,bytes)")) == 0xeb795549
* bytes4(keccak256("safeTransferFrom(address,address,uint256)")) == 0x42842e0e
* bytes4(keccak256("safeTransferFrom(address,address,uint256,bytes)")) == 0xb88d4fde
*
* => 0x18160ddd ^ 0x70a08231 ^ 0xa9059cbb ^ 0xdd62ed3e ^ 0x095ea7b3 ^ 0x23b872dd ^ 0x423f6cef ^ 0xeb795549 ^ 0x42842e0e ^ 0xb88d4fde == 0x65787371
*/
bytes4 private constant _INTERFACE_ID_KIP7 = 0x65787371;
constructor () public {
// register the supported interfaces to conform to KIP7 via KIP13
_registerInterface(_INTERFACE_ID_KIP7);
}
/**
* @dev See `IKIP7.totalSupply`.
*/
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
/**
* @dev See `IKIP7.balanceOf`.
*/
function balanceOf(address account) public view returns (uint256) {
return _balances[account];
}
/**
* @dev See `IKIP7.transfer`.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}
/**
* @dev See `IKIP7.allowance`.
*/
function allowance(address owner, address spender) public view returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See `IKIP7.approve`.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 value) public returns (bool) {
_approve(msg.sender, spender, value);
return true;
}
/**
* @dev See `IKIP7.transferFrom`.
*
* Emits an `Approval` event indicating the updated allowance. This is not
* required by the KIP. See the note at the beginning of `KIP7`;
*
* Requirements:
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `value`.
* - the caller must have allowance for `sender`'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) public returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount));
return true;
}
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*/
function safeTransfer(address recipient, uint256 amount) public {
// safeTransfer(recipient, amount, "");
safeTransfer(recipient, amount); // modified
}
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*/
// function safeTransfer(address recipient, uint256 amount, bytes memory data) public {
function safeTransfer(address recipient, uint256 amount, bytes calldata data) external { // SK
transfer(recipient, amount);
require(_checkOnKIP7Received(msg.sender, recipient, amount, data), "KIP7: transfer to non KIP7Receiver implementer");
}
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the allowance mechanism.
* `amount` is then deducted from the caller's allowance.
*/
function safeTransferFrom(address sender, address recipient, uint256 amount) public {
// safeTransferFrom(sender, recipient, amount, "");
safeTransferFrom(sender, recipient, amount); // SK
}
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the allowance mechanism.
* `amount` is then deducted from the caller's allowance.
*/
// function safeTransferFrom(address sender, address recipient, uint256 amount, bytes memory data) public {
function safeTransferFrom(address sender, address recipient, uint256 amount, bytes calldata data) external { // SK
transferFrom(sender, recipient, amount);
require(_checkOnKIP7Received(sender, recipient, amount, data), "KIP7: transfer to non KIP7Receiver implementer");
}
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to `transfer`, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a `Transfer` event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer(address sender, address recipient, uint256 amount) internal {
require(sender != address(0), "KIP7: transfer from the zero address");
require(recipient != address(0), "KIP7: transfer to the zero address");
_balances[sender] = _balances[sender].sub(amount);
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a `Transfer` event with `from` set to the zero address.
*
* Requirements
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal {
require(account != address(0), "KIP7: mint to the zero address");
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a `Transfer` event with `to` set to the zero address.
*
* Requirements
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 value) internal {
require(account != address(0), "KIP7: burn from the zero address");
_totalSupply = _totalSupply.sub(value);
_balances[account] = _balances[account].sub(value);
emit Transfer(account, address(0), value);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an `Approval` event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 value) internal {
require(owner != address(0), "KIP7: approve from the zero address");
require(spender != address(0), "KIP7: approve to the zero address");
_allowances[owner][spender] = value;
emit Approval(owner, spender, value);
}
/**
* @dev Destroys `amount` tokens from `account`.`amount` is then deducted
* from the caller's allowance.
*
* See `_burn` and `_approve`.
*/
function _burnFrom(address account, uint256 amount) internal {
_burn(account, amount);
_approve(account, msg.sender, _allowances[account][msg.sender].sub(amount));
}
/**
* @dev Internal function to invoke `onKIP7Received` on a target address.
* The call is not executed if the target address is not a contract.
*/
function _checkOnKIP7Received(address sender, address recipient, uint256 amount, bytes memory _data)
internal returns (bool)
{
if (!recipient.isContract()) {
return true;
}
bytes4 retval = IKIP7Receiver(recipient).onKIP7Received(msg.sender, sender, amount, _data);
return (retval == _KIP7_RECEIVED);
}
}