Skip to content

Commit

Permalink
unneccesary revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Corya committed Oct 15, 2020
1 parent 6a7b510 commit 399d2aa
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 13 deletions.
169 changes: 169 additions & 0 deletions ChiToken.sol
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)));
}
}
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@

SOLC_ABI=solc --abi --output-dir ./abi --overwrite --optimize-runs $(OPTIMIZATION)
SOLC_BIN=solc --bin --output-dir ./bin --overwrite --optimize-runs $(OPTIMIZATION)

SOLC5_ABI=solc5 --abi --output-dir ./abi --overwrite --optimize-runs $(OPTIMIZATION)
SOLC5_BIN=solc5 --bin --output-dir ./bin --overwrite --optimize-runs $(OPTIMIZATION)

OPTIMIZATION=2000

all: proxy aave bzx dydx runner
all: proxy chi aave bzx dydx runner

clean:
rm -r ./bin && rm -r ./abi

proxy:
$(SOLC_ABI) ./TransactionProxy.sol && $(SOLC_BIN) ./TransactionProxy.sol

chi:
$(SOLC_ABI) ./ChiToken.sol && $(SOLC_BIN) ./ChiToken.sol

aave:
$(SOLC_ABI) ./aaveFlashLoan.sol && $(SOLC_BIN) ./aaveFlashLoan.sol

Expand Down
57 changes: 46 additions & 11 deletions TransactionProxy.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

pragma solidity >= 0.6.0;
pragma solidity >= 0.6.0 < 0.7.0;
pragma experimental ABIEncoderV2;

import "./lib/dapphub/auth.sol";
import "./lib/dapphub/note.sol";

import "./lib/DSProxyCache.sol";
// import "./lib/DSProxyCache.sol";
import "./lib/ChiGasSaver.sol";
import "./lib/TokenInterface.sol";

contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
DSProxyCache public cache;
address gasTank;

receive() external payable {
}
// receive() external payable {
// }

fallback() external payable {
}
Expand All @@ -25,12 +25,25 @@ contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
setGasTank(_gasTank);
}

// proxy actions
function deploy(bytes memory _code)
public
payable
returns (address target)
{
target = cache.read(_code);
if (target == address(0)) {
// deploy contract & store its address in cache
target = cache.write(_code);
}
// return target;
}


// proxy actions
function execute(bytes memory _code, bytes memory _data)
public
payable
auth
returns (address target, bytes memory response)
{
target = cache.read(_code);
Expand All @@ -39,13 +52,12 @@ contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
target = cache.write(_code);
}

response = _execute(target, _data);
response = execute(target, _data);
}

function executeAndBurn(bytes memory _code, bytes memory _data)
public
payable
auth
saveGas(payable(gasTank))
returns (address target, bytes memory response)
{
Expand All @@ -55,10 +67,10 @@ contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
target = cache.write(_code);
}

response = _execute(target, _data);
response = execute(target, _data);
}

function _execute(address _target, bytes memory _data)
function execute(address _target, bytes memory _data)
public
auth
note
Expand All @@ -70,6 +82,8 @@ contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
// call contract in current context
assembly {
let succeeded := delegatecall(sub(gas(), 5000), _target, add(_data, 0x20), mload(_data), 0, 0)
// let succeeded := delegatecall(gas(), _target, add(_data, 0x20), mload(_data), 0, 0)

let size := returndatasize()

response := mload(0x40)
Expand All @@ -87,13 +101,13 @@ contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {


// address updates
function setCache (address _cacheAddr) public auth returns (bool) {
function setCache (address _cacheAddr) public auth note returns (bool) {
require (_cacheAddr != address(0), "ds-proxy-cache-address-required");
cache = DSProxyCache(_cacheAddr); // overwrite cache
return true;
}

function setGasTank (address _gasTank) public auth returns (bool) {
function setGasTank (address _gasTank) public auth note returns (bool) {
require (_gasTank != address(0), "gasTank address required");
gasTank = _gasTank;
return true;
Expand All @@ -118,3 +132,24 @@ contract TransactionProxy is ChiGasSaver, DSAuth, DSNote {
}
}

contract DSProxyCache {
mapping(bytes32 => address) cache;

function read(bytes memory _code) public view returns (address) {
bytes32 hash = keccak256(_code);
return cache[hash];
}

function write(bytes memory _code) public returns (address target) {
assembly {
target := create(0, add(_code, 0x20), mload(_code))
switch iszero(extcodesize(target))
case 1 {
// throw if contract failed to deploy
revert(0, 0)
}
}
bytes32 hash = keccak256(_code);
cache[hash] = target;
}
}
2 changes: 1 addition & 1 deletion lib/DSProxyCache.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

pragma solidity >= 0.5.0;
pragma solidity >= 0.6.0;

contract DSProxyCache {
mapping(bytes32 => address) cache;
Expand Down
31 changes: 31 additions & 0 deletions lib/openzeppelin/Math.sol
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);
}
}

0 comments on commit 399d2aa

Please sign in to comment.