-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Steve Corya
committed
Oct 15, 2020
1 parent
6a7b510
commit 399d2aa
Showing
5 changed files
with
255 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
pragma solidity ^0.6.0; | ||
|
||
import "./lib/openzeppelin/Math.sol"; | ||
import "./lib/openzeppelin/SafeMath.sol"; | ||
import "./lib/openzeppelin/IERC20.sol"; | ||
|
||
|
||
abstract contract ERC20WithoutTotalSupply is IERC20 { | ||
using SafeMath for uint256; | ||
|
||
mapping(address => uint256) private _balances; | ||
mapping(address => mapping(address => uint256)) private _allowances; | ||
|
||
function balanceOf(address account) public view override returns (uint256) { | ||
return _balances[account]; | ||
} | ||
|
||
function allowance(address owner, address spender) public view override returns (uint256) { | ||
return _allowances[owner][spender]; | ||
} | ||
|
||
function transfer(address recipient, uint256 amount) public override returns (bool) { | ||
_transfer(msg.sender, recipient, amount); | ||
return true; | ||
} | ||
|
||
function approve(address spender, uint256 amount) public override returns (bool) { | ||
_approve(msg.sender, spender, amount); | ||
return true; | ||
} | ||
|
||
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) { | ||
_transfer(sender, recipient, amount); | ||
uint256 allowed = _allowances[sender][msg.sender]; | ||
if ((allowed >> 255) == 0) { | ||
_approve(sender, msg.sender, allowed.sub(amount, "ERC20: transfer amount exceeds allowance")); | ||
} | ||
return true; | ||
} | ||
|
||
function _transfer(address sender, address recipient, uint256 amount) internal { | ||
_balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance"); | ||
_balances[recipient] = _balances[recipient].add(amount); | ||
emit Transfer(sender, recipient, amount); | ||
} | ||
|
||
function _approve(address owner, address spender, uint256 amount) internal { | ||
_allowances[owner][spender] = amount; | ||
emit Approval(owner, spender, amount); | ||
} | ||
|
||
function _mint(address account, uint256 amount) internal { | ||
_balances[account] = _balances[account].add(amount); | ||
emit Transfer(address(0), account, amount); | ||
} | ||
|
||
function _burn(address account, uint256 amount) internal { | ||
_balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance"); | ||
emit Transfer(account, address(0), amount); | ||
} | ||
|
||
function _burnFrom(address account, uint256 amount) internal { | ||
_burn(account, amount); | ||
uint256 allowed = _allowances[account][msg.sender]; | ||
if ((allowed >> 255) == 0) { | ||
_approve(account, msg.sender, allowed.sub(amount, "ERC20: burn amount exceeds allowance")); | ||
} | ||
} | ||
} | ||
|
||
|
||
contract ChiToken is IERC20, ERC20WithoutTotalSupply { | ||
string constant public name = "Chi Token by 1inch"; | ||
string constant public symbol = "CHI"; | ||
uint8 constant public decimals = 0; | ||
|
||
uint256 public totalMinted; | ||
uint256 public totalBurned; | ||
|
||
function totalSupply() public view override returns(uint256) { | ||
return totalMinted - totalBurned; | ||
} | ||
|
||
function mint(uint256 value) public { | ||
uint256 offset = totalMinted; | ||
assembly { | ||
mstore(0, 0x746d4946c0e9F43F4Dee607b0eF1fA1c3318585733ff6000526015600bf30000) | ||
|
||
for {let i := div(value, 32)} i {i := sub(i, 1)} { | ||
pop(create2(0, 0, 30, add(offset, 0))) pop(create2(0, 0, 30, add(offset, 1))) | ||
pop(create2(0, 0, 30, add(offset, 2))) pop(create2(0, 0, 30, add(offset, 3))) | ||
pop(create2(0, 0, 30, add(offset, 4))) pop(create2(0, 0, 30, add(offset, 5))) | ||
pop(create2(0, 0, 30, add(offset, 6))) pop(create2(0, 0, 30, add(offset, 7))) | ||
pop(create2(0, 0, 30, add(offset, 8))) pop(create2(0, 0, 30, add(offset, 9))) | ||
pop(create2(0, 0, 30, add(offset, 10))) pop(create2(0, 0, 30, add(offset, 11))) | ||
pop(create2(0, 0, 30, add(offset, 12))) pop(create2(0, 0, 30, add(offset, 13))) | ||
pop(create2(0, 0, 30, add(offset, 14))) pop(create2(0, 0, 30, add(offset, 15))) | ||
pop(create2(0, 0, 30, add(offset, 16))) pop(create2(0, 0, 30, add(offset, 17))) | ||
pop(create2(0, 0, 30, add(offset, 18))) pop(create2(0, 0, 30, add(offset, 19))) | ||
pop(create2(0, 0, 30, add(offset, 20))) pop(create2(0, 0, 30, add(offset, 21))) | ||
pop(create2(0, 0, 30, add(offset, 22))) pop(create2(0, 0, 30, add(offset, 23))) | ||
pop(create2(0, 0, 30, add(offset, 24))) pop(create2(0, 0, 30, add(offset, 25))) | ||
pop(create2(0, 0, 30, add(offset, 26))) pop(create2(0, 0, 30, add(offset, 27))) | ||
pop(create2(0, 0, 30, add(offset, 28))) pop(create2(0, 0, 30, add(offset, 29))) | ||
pop(create2(0, 0, 30, add(offset, 30))) pop(create2(0, 0, 30, add(offset, 31))) | ||
offset := add(offset, 32) | ||
} | ||
|
||
for {let i := and(value, 0x1F)} i {i := sub(i, 1)} { | ||
pop(create2(0, 0, 30, offset)) | ||
offset := add(offset, 1) | ||
} | ||
} | ||
|
||
_mint(msg.sender, value); | ||
totalMinted = offset; | ||
} | ||
|
||
function computeAddress2(uint256 salt) public pure returns (address child) { | ||
assembly { | ||
let data := mload(0x40) | ||
mstore(data, 0xff0000000000004946c0e9F43F4Dee607b0eF1fA1c0000000000000000000000) | ||
mstore(add(data, 21), salt) | ||
mstore(add(data, 53), 0x3c1644c68e5d6cb380c36d1bf847fdbc0c7ac28030025a2fc5e63cce23c16348) | ||
child := and(keccak256(data, 85), 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) | ||
} | ||
} | ||
|
||
function _destroyChildren(uint256 value) internal { | ||
assembly { | ||
let i := sload(totalBurned_slot) | ||
let end := add(i, value) | ||
sstore(totalBurned_slot, end) | ||
|
||
let data := mload(0x40) | ||
mstore(data, 0xff0000000000004946c0e9F43F4Dee607b0eF1fA1c0000000000000000000000) | ||
mstore(add(data, 53), 0x3c1644c68e5d6cb380c36d1bf847fdbc0c7ac28030025a2fc5e63cce23c16348) | ||
let ptr := add(data, 21) | ||
for { } lt(i, end) { i := add(i, 1) } { | ||
mstore(ptr, i) | ||
pop(call(gas(), keccak256(data, 85), 0, 0, 0, 0, 0)) | ||
} | ||
} | ||
} | ||
|
||
function free(uint256 value) public returns (uint256) { | ||
if (value > 0) { | ||
_burn(msg.sender, value); | ||
_destroyChildren(value); | ||
} | ||
return value; | ||
} | ||
|
||
function freeUpTo(uint256 value) public returns (uint256) { | ||
return free(Math.min(value, balanceOf(msg.sender))); | ||
} | ||
|
||
function freeFrom(address from, uint256 value) public returns (uint256) { | ||
if (value > 0) { | ||
_burnFrom(from, value); | ||
_destroyChildren(value); | ||
} | ||
return value; | ||
} | ||
|
||
function freeFromUpTo(address from, uint256 value) public returns (uint256) { | ||
return freeFrom(from, Math.min(Math.min(value, balanceOf(from)), allowance(from, msg.sender))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.6.0; | ||
|
||
/** | ||
* @dev Standard math utilities missing in the Solidity language. | ||
*/ | ||
library Math { | ||
/** | ||
* @dev Returns the largest of two numbers. | ||
*/ | ||
function max(uint256 a, uint256 b) internal pure returns (uint256) { | ||
return a >= b ? a : b; | ||
} | ||
|
||
/** | ||
* @dev Returns the smallest of two numbers. | ||
*/ | ||
function min(uint256 a, uint256 b) internal pure returns (uint256) { | ||
return a < b ? a : b; | ||
} | ||
|
||
/** | ||
* @dev Returns the average of two numbers. The result is rounded towards | ||
* zero. | ||
*/ | ||
function average(uint256 a, uint256 b) internal pure returns (uint256) { | ||
// (a + b) / 2 can overflow, so we distribute | ||
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); | ||
} | ||
} |