Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Cosman committed Apr 17, 2018
1 parent e2041cf commit d916896
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 26 deletions.
10 changes: 5 additions & 5 deletions contracts/CanDelegate.sol
Expand Up @@ -22,15 +22,15 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken {
if (delegate == address(0)) {
super.transferAllArgs(_from, _to, _value);
} else {
assert(delegate.delegateTransfer(_to, _value, _from));
require(delegate.delegateTransfer(_to, _value, _from));
}
}

function transferFromAllArgs(address _from, address _to, uint256 _value, address _spender) internal {
if (delegate == address(0)) {
super.transferFromAllArgs(_from, _to, _value, _spender);
} else {
assert(delegate.delegateTransferFrom(_from, _to, _value, _spender));
require(delegate.delegateTransferFrom(_from, _to, _value, _spender));
}
}

Expand All @@ -46,7 +46,7 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken {
if (delegate == address(0)) {
super.approveAllArgs(_spender, _value, _tokenHolder);
} else {
assert(delegate.delegateApprove(_spender, _value, _tokenHolder));
require(delegate.delegateApprove(_spender, _value, _tokenHolder));
}
}

Expand All @@ -70,15 +70,15 @@ contract CanDelegate is ModularMintableToken, ModularBurnableToken {
if (delegate == address(0)) {
super.increaseApprovalAllArgs(_spender, _addedValue, _tokenHolder);
} else {
assert(delegate.delegateIncreaseApproval(_spender, _addedValue, _tokenHolder));
require(delegate.delegateIncreaseApproval(_spender, _addedValue, _tokenHolder));
}
}

function decreaseApprovalAllArgs(address _spender, uint256 _subtractedValue, address _tokenHolder) internal {
if (delegate == address(0)) {
super.decreaseApprovalAllArgs(_spender, _subtractedValue, _tokenHolder);
} else {
assert(delegate.delegateDecreaseApproval(_spender, _subtractedValue, _tokenHolder));
require(delegate.delegateDecreaseApproval(_spender, _subtractedValue, _tokenHolder));
}
}

Expand Down
59 changes: 59 additions & 0 deletions contracts/mocks/OtherMocks.sol
@@ -0,0 +1,59 @@
pragma solidity ^0.4.18;

import "../DelegateBurnable.sol";

contract FailingDelegate is DelegateBurnable {
function delegateTotalSupply() public view returns (uint256) {
return 0;
}
function delegateBalanceOf(address) public view returns (uint256) {
return 0;
}
function delegateTransfer(address, uint256, address) public returns (bool) {
return false;
}
function delegateAllowance(address, address) public view returns (uint256) {
return 0;
}
function delegateTransferFrom(address, address, uint256, address) public returns (bool) {
return false;
}
function delegateApprove(address, uint256, address) public returns (bool) {
return false;
}
function delegateIncreaseApproval(address, uint, address) public returns (bool) {
return false;
}
function delegateDecreaseApproval(address, uint, address) public returns (bool) {
return false;
}
function delegateBurn(address, uint256) public {}
}

contract SucceedingDelegate is DelegateBurnable {
function delegateTotalSupply() public view returns (uint256) {
return 0;
}
function delegateBalanceOf(address) public view returns (uint256) {
return 0;
}
function delegateTransfer(address, uint256, address) public returns (bool) {
return true;
}
function delegateAllowance(address, address) public view returns (uint256) {
return 0;
}
function delegateTransferFrom(address, address, uint256, address) public returns (bool) {
return true;
}
function delegateApprove(address, uint256, address) public returns (bool) {
return true;
}
function delegateIncreaseApproval(address, uint, address) public returns (bool) {
return true;
}
function delegateDecreaseApproval(address, uint, address) public returns (bool) {
return true;
}
function delegateBurn(address, uint256) public {}
}
56 changes: 56 additions & 0 deletions test/CanDelegate.js
Expand Up @@ -3,6 +3,8 @@ import mintableTokenTests from './token/MintableToken';
import burnableTokenTests from './token/BurnableToken';
import standardTokenTests from './token/StandardToken';
import basicTokenTests from './token/BasicToken';
const FailingDelegate = artifacts.require('FailingDelegate')
const SucceedingDelegate = artifacts.require('SucceedingDelegate')

function canDelegateTests([owner, oneHundred, anotherAccount]) {
describe('--CanDelegate Tests--', function () {
Expand Down Expand Up @@ -33,6 +35,60 @@ function canDelegateTests([owner, oneHundred, anotherAccount]) {
await assertRevert(this.token.delegateToNewContract("0x1", { from: anotherAccount }))
})
})

describe('delegating to token that always fails', function () {
beforeEach(async function () {
this.failToken = await FailingDelegate.new({ from: owner })
await this.token.delegateToNewContract(this.failToken.address, { from: owner })
})

it('transfer', async function () {
await assertRevert(this.token.transfer(oneHundred, 50, { from: anotherAccount }))
})

it('transferFrom', async function () {
await assertRevert(this.token.transferFrom(oneHundred, oneHundred, 50, { from: anotherAccount }))
})

it('approve', async function () {
await assertRevert(this.token.approve(oneHundred, 50, { from: anotherAccount }))
})

it('increaseApproval', async function () {
await assertRevert(this.token.increaseApproval(oneHundred, 50, { from: anotherAccount }))
})

it('decreaseApproval', async function () {
await assertRevert(this.token.decreaseApproval(oneHundred, 50, { from: anotherAccount }))
})
})

describe('delegating to token that always succeeds', function () {
beforeEach(async function () {
this.passToken = await SucceedingDelegate.new({ from: owner })
await this.token.delegateToNewContract(this.passToken.address, { from: owner })
})

it('transfer', async function () {
await this.token.transfer(oneHundred, 50, { from: anotherAccount })
})

it('transferFrom', async function () {
await this.token.transferFrom(oneHundred, oneHundred, 50, { from: anotherAccount })
})

it('approve', async function () {
await this.token.approve(oneHundred, 50, { from: anotherAccount })
})

it('increaseApproval', async function () {
await this.token.increaseApproval(oneHundred, 50, { from: anotherAccount })
})

it('decreaseApproval', async function () {
await this.token.decreaseApproval(oneHundred, 50, { from: anotherAccount })
})
})
})
}

Expand Down
21 changes: 0 additions & 21 deletions test/CanDelegate.test.js
@@ -1,6 +1,5 @@
import canDelegateTests from './CanDelegate'
const CanDelegateMock = artifacts.require('CanDelegateMock')
const StandardDelegateMock = artifacts.require('StandardDelegateMock')

contract('CanDelegate', function (accounts) {
const _ = accounts[0]
Expand All @@ -10,27 +9,7 @@ contract('CanDelegate', function (accounts) {

beforeEach(async function () {
this.token = await CanDelegateMock.new(oneHundreds[0], 100, { from: owners[0] })
// this.token2 = await StandardDelegateMock.new(oneHundreds[1], 100, { from: owners[1] })
})

canDelegateTests([owners[0], oneHundreds[0], anotherAccounts[0]])

// describe('chaining three contracts', function () {
// beforeEach(async function () {
// this.token.delegateToNewContract(this.token2.address, { from: owners[0] })
// this.token2.setDelegatedFrom(this.token.address, { from: owners[1] })
// })

// describe('first contract behaves', function () {
// it('temp', async function () {
// const balance = await this.token.balanceOf(oneHundreds[1])
// assert.equal(balance, 100)
// await this.token.burn(100, { from: oneHundreds[1] })
// // await this.token.transfer(anotherAccounts[2], 100, { from: oneHundreds[2] })
// })

// // basicTokenTests([owners[2], oneHundreds[2], anotherAccounts[2]])
// })
// })

})

0 comments on commit d916896

Please sign in to comment.