This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/2.0' into feature/truffle4-plus-solidity-0.5
- Loading branch information
Showing
7 changed files
with
251 additions
and
8 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
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,23 @@ | ||
/// Implements ERC 20 Token standard: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md | ||
pragma solidity ^0.4.21; | ||
|
||
|
||
/// @title Abstract token contract - Functions to be implemented by token contracts | ||
contract BadToken { | ||
|
||
/* | ||
* Events | ||
*/ | ||
event Transfer(address indexed from, address indexed to, uint value); | ||
event Approval(address indexed owner, address indexed spender, uint value); | ||
|
||
/* | ||
* Public functions | ||
*/ | ||
function transfer(address to, uint value) public; | ||
function transferFrom(address from, address to, uint value) public; | ||
function approve(address spender, uint value) public returns (bool); | ||
function balanceOf(address owner) public view returns (uint); | ||
function allowance(address owner, address spender) public view returns (uint); | ||
function totalSupply() public view returns (uint); | ||
} |
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,98 @@ | ||
pragma solidity ^0.4.21; | ||
import "./BadToken.sol"; | ||
import "@gnosis.pm/util-contracts/contracts/Math.sol"; | ||
import "@gnosis.pm/util-contracts/contracts/Proxy.sol"; | ||
import {StandardTokenData} from "@gnosis.pm/util-contracts/contracts/GnosisStandardToken.sol"; | ||
|
||
/// @title Standard token contract with overflow protection | ||
contract SubStandardToken is BadToken, StandardTokenData { | ||
using Math for *; | ||
|
||
/* | ||
* Public functions | ||
*/ | ||
/// @dev Transfers sender's tokens to a given address. Returns success | ||
/// @param to Address of token receiver | ||
/// @param value Number of tokens to transfer | ||
/// @return Was transfer successful? | ||
function transfer(address to, uint value) | ||
public | ||
// returns (bool) | ||
{ | ||
if ( !balances[msg.sender].safeToSub(value) | ||
|| !balances[to].safeToAdd(value)) | ||
return; | ||
// return false; | ||
balances[msg.sender] -= value; | ||
balances[to] += value; | ||
emit Transfer(msg.sender, to, value); | ||
// return true; | ||
} | ||
|
||
/// @dev Allows allowed third party to transfer tokens from one address to another. Returns success | ||
/// @param from Address from where tokens are withdrawn | ||
/// @param to Address to where tokens are sent | ||
/// @param value Number of tokens to transfer | ||
/// @return Was transfer successful? | ||
function transferFrom(address from, address to, uint value) | ||
public | ||
// returns (bool) | ||
{ | ||
if ( !balances[from].safeToSub(value) | ||
|| !allowances[from][msg.sender].safeToSub(value) | ||
|| !balances[to].safeToAdd(value)) | ||
return; | ||
// return false; | ||
balances[from] -= value; | ||
allowances[from][msg.sender] -= value; | ||
balances[to] += value; | ||
emit Transfer(from, to, value); | ||
// return true; | ||
} | ||
|
||
/// @dev Sets approved amount of tokens for spender. Returns success | ||
/// @param spender Address of allowed account | ||
/// @param value Number of approved tokens | ||
/// @return Was approval successful? | ||
function approve(address spender, uint value) | ||
public | ||
returns (bool) | ||
{ | ||
allowances[msg.sender][spender] = value; | ||
emit Approval(msg.sender, spender, value); | ||
return true; | ||
} | ||
|
||
/// @dev Returns number of allowed tokens for given address | ||
/// @param owner Address of token owner | ||
/// @param spender Address of token spender | ||
/// @return Remaining allowance for spender | ||
function allowance(address owner, address spender) | ||
public | ||
view | ||
returns (uint) | ||
{ | ||
return allowances[owner][spender]; | ||
} | ||
|
||
/// @dev Returns number of tokens owned by given address | ||
/// @param owner Address of token owner | ||
/// @return Balance of owner | ||
function balanceOf(address owner) | ||
public | ||
view | ||
returns (uint) | ||
{ | ||
return balances[owner]; | ||
} | ||
|
||
/// @dev Returns total supply of tokens | ||
/// @return Total supply | ||
function totalSupply() | ||
public | ||
view | ||
returns (uint) | ||
{ | ||
return totalTokens; | ||
} | ||
} |
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,18 @@ | ||
pragma solidity ^0.4.21; | ||
|
||
import "./SubStandardToken.sol"; | ||
|
||
contract BadGNO is SubStandardToken { | ||
string public constant symbol = "GNO"; | ||
string public constant name = "Gnosis"; | ||
uint8 public constant decimals = 18; | ||
|
||
function BadGNO( | ||
uint amount | ||
) | ||
public | ||
{ | ||
totalTokens = amount; | ||
balances[msg.sender] = amount; | ||
} | ||
} |
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,34 @@ | ||
|
||
pragma solidity ^0.4.21; | ||
|
||
import "@gnosis.pm/util-contracts/contracts/Token.sol"; | ||
|
||
interface BadToken { | ||
function transfer(address to, uint value) external; | ||
function transferFrom(address from, address to, uint value) external; | ||
} | ||
|
||
contract SafeTransfer { | ||
function safeTransfer(address token, address to, uint value, bool from) internal returns (bool result) { | ||
if (from) { | ||
BadToken(token).transferFrom(msg.sender, address(this), value); | ||
} else { | ||
BadToken(token).transfer(to, value); | ||
} | ||
|
||
assembly { | ||
switch returndatasize() | ||
case 0 { // This is our BadToken | ||
result := not(0) // result is true | ||
} | ||
case 32 { // This is our GoodToken | ||
returndatacopy(0, 0, 32) | ||
result := mload(0) // result == returndata of external call | ||
} | ||
default { // This is not an ERC20 token | ||
result := 0 | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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,70 @@ | ||
const { | ||
log: utilsLog, | ||
gasLogger | ||
} = require('./utils') | ||
|
||
const { getContracts } = require('./testFunctions') | ||
|
||
const BadGNO = artifacts.require('BadGNO') | ||
|
||
// Test VARS | ||
let dx, badGNO | ||
let contracts | ||
|
||
const separateLogs = () => utilsLog('\n ----------------------------------') | ||
const log = (...args) => utilsLog('\t', ...args) | ||
|
||
contract('DutchExchange - addTokenPair', accounts => { | ||
const [master, seller1] = accounts | ||
|
||
const startBal = { | ||
startingETH: 90.0.toWei(), | ||
startingGNO: 90.0.toWei(), | ||
ethUSDPrice: 1008.0.toWei(), | ||
sellingAmount: 50.0.toWei() | ||
} | ||
|
||
beforeEach(separateLogs) | ||
afterEach(gasLogger) | ||
|
||
before(async () => { | ||
// get contracts | ||
contracts = await getContracts(); | ||
// destructure contracts into upper state | ||
({ | ||
DutchExchange: dx | ||
} = contracts) | ||
|
||
badGNO = await BadGNO.new(1e22, { from: master }) | ||
await Promise.all([ | ||
badGNO.transfer(seller1, startBal.startingGNO, { from: master }), | ||
badGNO.approve(dx.address, startBal.startingGNO, { from: seller1 }) | ||
]) | ||
}) | ||
|
||
it('deposits bad ERC20 tokens', async () => { | ||
log('Depositing Bad GNO') | ||
const tx = await dx.deposit(badGNO.address, startBal.startingGNO, { from: seller1 }) | ||
log('tx: ', JSON.stringify(tx.logs, null, 2)) | ||
log('Succeeded Depositing Bad GNO') | ||
log('startBal.startingGNO: ', startBal.startingGNO) | ||
|
||
const deposited = await dx.balances(badGNO.address, seller1) | ||
log('deposited: ', deposited.toString()) | ||
|
||
assert(deposited.eq(startBal.startingGNO), 'deposited amount was exactly equal startingGNO') | ||
}) | ||
|
||
it('withdraws bad ERC20 tokens', async () => { | ||
log('Withdrawing Bad GNO') | ||
const tx = await dx.withdraw(badGNO.address, startBal.startingGNO, { from: seller1 }) | ||
log('tx: ', JSON.stringify(tx.logs, null, 2)) | ||
log('Succeeded Withdrawing Bad GNO') | ||
log('startBal.startingGNO: ', startBal.startingGNO) | ||
|
||
const deposited = await dx.balances(badGNO.address, seller1) | ||
log('deposited: ', deposited.toString()) | ||
|
||
assert(deposited.eq(0), 'deposited amount is exactly 0 after startingGNO was withdrawn') | ||
}) | ||
}) |