Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests: consistent use of Token units versus TokenWei units #82

Merged
merged 7 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ experience to build mainstream applications on Ethereum.

Detailed changelog:

- Unit tests: Consistent use of Token units versus TokenWei units ([openst-protocol#1](https://github.com/OpenSTFoundation/openst-protocol/pull/82))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can already be under new header v0.9.2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes Done

- Generate documentation in /docs ([openst-protocol#78](https://github.com/OpenSTFoundation/openst-protocol/pull/78))
- Unit tests for Owned, OpsManaged, SafeMath (carry over from SimpleTokenSale) ([openst-protocol#73](https://github.com/OpenSTFoundation/openst-protocol/pull/73))
- Add mock ERC20 token for dryrun on Ethereum mainnet ([openst-protocol#71](https://github.com/OpenSTFoundation/openst-protocol/pull/71))
Expand Down
3 changes: 2 additions & 1 deletion test/BrandedToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ contract('BrandedToken', function(accounts) {
})

it('fails to burn by openSTProtocol if msg.value != 0', async () => {
await Utils.expectThrow(token.burn(beneficiary, ST1, { from: openSTProtocol, value: 1 }));
const amountBT = new BigNumber(web3.toWei(1, "ether"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation

await Utils.expectThrow(token.burn(beneficiary, ST1, { from: openSTProtocol, value: amountBT }));
})

it('successfully burns', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/MockToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ contract('MockToken', (accounts) => {
const SYMBOL = "MOCK"
const NAME = "Mock Token"
const DECIMALS = 18
const TOTAL_SUPPLY = new BigNumber('800000000').mul(DECIMALSFACTOR)
const TOTAL_SUPPLY = new BigNumber(web3.toWei(800000000, "ether"));
const admin = accounts[1];


Expand Down
128 changes: 67 additions & 61 deletions test/OpenSTUtility.js

Large diffs are not rendered by default.

77 changes: 43 additions & 34 deletions test/OpenSTValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ contract('OpenSTValue', function(accounts) {
})

it('fails to add core by non-registrar', async () => {
await Utils.expectThrow(openSTValue.addCore(core.address, { from: accounts[0] }));
await Utils.expectThrow(openSTValue.addCore(core.address, { from: accounts[0] }));
})

it('fails to add core by registrar when core is null', async () => {
await Utils.expectThrow(openSTValue.addCore(0, { from: registrar }));
await Utils.expectThrow(openSTValue.addCore(0, { from: registrar }));
})

it('fails to add core when registrar != core.registrar', async () => {
Expand All @@ -170,7 +170,7 @@ contract('OpenSTValue', function(accounts) {
valueToken = contracts.valueToken;
openSTValue = contracts.openSTValue;
core = await Core.new(registrar, chainIdValue, chainIdRemote, openSTRemote);
await openSTValue.addCore(core.address, { from: registrar });
await openSTValue.addCore(core.address, { from: registrar });
checkUuid = await openSTValue.hashUuid.call(symbol, name, chainIdValue, chainIdRemote, openSTRemote, conversionRate);
})

Expand Down Expand Up @@ -211,6 +211,7 @@ contract('OpenSTValue', function(accounts) {
})

describe('Stake', async () => {
const amountST = new BigNumber(web3.toWei(1, "ether"));
context('when the staking account is null', async () => {
before(async () => {
contracts = await OpenSTValue_utils.deployOpenSTValue(artifacts, accounts);
Expand All @@ -225,66 +226,71 @@ contract('OpenSTValue', function(accounts) {
})

it('fails to stake when tx.origin has not approved it to transfer at least the amount', async () => {
await Utils.expectThrow(openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[0] }));
await Utils.expectThrow(openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[0] }));
})

it('fails to stake when the SimpleStake address for the given UUID is null', async () => {
await valueToken.approve(openSTValue.address, 1, { from: accounts[0] });
await Utils.expectThrow(openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[0] }));
await valueToken.approve(openSTValue.address, amountST, { from: accounts[0] });
await Utils.expectThrow(openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[0] }));
})

it('fails to stake when the beneficiary is null', async () => {
checkUuid = await openSTValue.hashUuid.call(symbol, name, chainIdValue, chainIdRemote, openSTRemote, conversionRate);
await openSTValue.registerUtilityToken(symbol, name, conversionRate, chainIdRemote, 0, checkUuid, { from: registrar });
await Utils.expectThrow(openSTValue.stake(checkUuid, 1, 0, { from: accounts[0] }));
await Utils.expectThrow(openSTValue.stake(checkUuid, amountST, 0, { from: accounts[0] }));
})

it('successfully stakes', async () => {
var stakeReturns = await openSTValue.stake.call(checkUuid, 1, accounts[0], { from: accounts[0] });
var stakeReturns = await openSTValue.stake.call(checkUuid, amountST, accounts[0], { from: accounts[0] });
var amountUT = stakeReturns[0].toNumber();
nonce = stakeReturns[1].toNumber();

// call block number is one less than send block number
var unlockHeight = stakeReturns[2].plus(1);
stakingIntentHash = await openSTValue.hashStakingIntent.call(checkUuid, accounts[0], nonce, accounts[0], 1, amountUT, unlockHeight);
result = await openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[0] });
stakingIntentHash = await openSTValue.hashStakingIntent.call(checkUuid, accounts[0], nonce, accounts[0], amountST, amountUT, unlockHeight);
result = await openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[0] });

await OpenSTValue_utils.checkStakingIntentDeclaredEvent(result.logs[0], checkUuid, accounts[0], nonce, accounts[0], 1, amountUT, unlockHeight, stakingIntentHash, chainIdRemote);
await OpenSTValue_utils.checkStakingIntentDeclaredEvent(result.logs[0], checkUuid, accounts[0], nonce, accounts[0],
amountST, amountUT, unlockHeight, stakingIntentHash, chainIdRemote);
})
})

context('when the staking account is not null', async () => {

before(async () => {
contracts = await OpenSTValue_utils.deployOpenSTValue(artifacts, accounts);
valueToken = contracts.valueToken;
openSTValue = contracts.openSTValue;
core = await Core.new(registrar, chainIdValue, chainIdRemote, openSTRemote);
await openSTValue.addCore(core.address, { from: registrar });
checkUuid = await openSTValue.hashUuid.call(symbol, name, chainIdValue, chainIdRemote, openSTRemote, conversionRate);
await openSTValue.registerUtilityToken(symbol, name, conversionRate, chainIdRemote, accounts[0], checkUuid, { from: registrar });
await valueToken.approve(openSTValue.address, 1, { from: accounts[0] });
await openSTValue.registerUtilityToken(symbol, name, conversionRate, chainIdRemote, accounts[0], checkUuid, { from: registrar });
await valueToken.approve(openSTValue.address,amountST, { from: accounts[0] });
})

it('fails to stake when msg.sender is not the stakingAccount', async () => {
await Utils.expectThrow(openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[1] }));
await Utils.expectThrow(openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[1] }));
})

it('successfully stakes', async () => {
var stakeReturns = await openSTValue.stake.call(checkUuid, 1, accounts[0], { from: accounts[0] });
var stakeReturns = await openSTValue.stake.call(checkUuid, amountST, accounts[0], { from: accounts[0] });
var amountUT = stakeReturns[0].toNumber();
nonce = stakeReturns[1].toNumber();

// call block number is one less than send block number
// call block number is one less than send block number
var unlockHeight = stakeReturns[2].plus(1);
stakingIntentHash = await openSTValue.hashStakingIntent.call(checkUuid, accounts[0], nonce, accounts[0], 1, amountUT, unlockHeight);
result = await openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[0] });
stakingIntentHash = await openSTValue.hashStakingIntent.call(checkUuid, accounts[0], nonce, accounts[0], amountST, amountUT, unlockHeight);
result = await openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[0] });

await OpenSTValue_utils.checkStakingIntentDeclaredEvent(result.logs[0], checkUuid, accounts[0], nonce, accounts[0], 1, amountUT, unlockHeight, stakingIntentHash, chainIdRemote);
await OpenSTValue_utils.checkStakingIntentDeclaredEvent(result.logs[0], checkUuid, accounts[0], nonce, accounts[0],
amountST, amountUT, unlockHeight, stakingIntentHash, chainIdRemote);
})
})
})

describe('ProcessStaking', async () => {
const amountST = new BigNumber(web3.toWei(1, "ether")),
amountUT = amountST * conversionRate;
before(async () => {
contracts = await OpenSTValue_utils.deployOpenSTValue(artifacts, accounts);
valueToken = contracts.valueToken;
Expand All @@ -294,8 +300,8 @@ contract('OpenSTValue', function(accounts) {
checkUuid = await openSTValue.hashUuid.call(symbol, name, chainIdValue, chainIdRemote, openSTRemote, conversionRate);
result = await openSTValue.registerUtilityToken(symbol, name, conversionRate, chainIdRemote, 0, checkUuid, { from: registrar });
stake = result.logs[0].args.stake;
await valueToken.approve(openSTValue.address, 1, { from: accounts[0] });
result = await openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[0] });
await valueToken.approve(openSTValue.address, amountST, { from: accounts[0] });
result = await openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[0] });
stakingIntentHash = result.logs[0].args._stakingIntentHash;
})

Expand All @@ -311,15 +317,15 @@ contract('OpenSTValue', function(accounts) {
it('successfully processes', async () => {
var openSTValueBal = await valueToken.balanceOf.call(openSTValue.address);
var stakeBal = await valueToken.balanceOf.call(stake);
assert.equal(openSTValueBal.toNumber(), 1);
assert.equal(openSTValueBal.toNumber(), amountST);
assert.equal(stakeBal.toNumber(), 0);
result = await openSTValue.processStaking(stakingIntentHash, { from: accounts[0] });

openSTValueBal = await valueToken.balanceOf.call(openSTValue.address);
stakeBal = await valueToken.balanceOf.call(stake);
assert.equal(openSTValueBal.toNumber(), 0);
assert.equal(stakeBal.toNumber(), 1);
await OpenSTValue_utils.checkProcessedStakeEvent(result.logs[0], checkUuid, stakingIntentHash, stake, accounts[0], 1, 10);
assert.equal(stakeBal.toNumber(), amountST);
await OpenSTValue_utils.checkProcessedStakeEvent(result.logs[0], checkUuid, stakingIntentHash, stake, accounts[0], amountST, amountUT);
})

it('fails to reprocess', async () => {
Expand All @@ -328,32 +334,35 @@ contract('OpenSTValue', function(accounts) {
})

describe('ProcessStaking with fallback', async () => {
const amountST = new BigNumber(web3.toWei(1, "ether")),
amountUT = amountST * conversionRate;

before(async () => {
contracts = await OpenSTValue_utils.deployOpenSTValue(artifacts, accounts);
valueToken = contracts.valueToken;
openSTValue = contracts.openSTValue;
core = await Core.new(registrar, chainIdValue, chainIdRemote, openSTRemote);
await openSTValue.addCore(core.address, { from: registrar });
await openSTValue.addCore(core.address, { from: registrar });
checkUuid = await openSTValue.hashUuid.call(symbol, name, chainIdValue, chainIdRemote, openSTRemote, conversionRate);
result = await openSTValue.registerUtilityToken(symbol, name, conversionRate, chainIdRemote, 0, checkUuid, { from: registrar });
stake = result.logs[0].args.stake;
await valueToken.approve(openSTValue.address, 1, { from: accounts[0] });
result = await openSTValue.stake(checkUuid, 1, accounts[0], { from: accounts[0] });
await valueToken.approve(openSTValue.address, amountST, { from: accounts[0] });
result = await openSTValue.stake(checkUuid, amountST, accounts[0], { from: accounts[0] });
stakingIntentHash = result.logs[0].args._stakingIntentHash;
})

it('successfully processes by registrar', async () => {
var openSTValueBal = await valueToken.balanceOf.call(openSTValue.address);
var stakeBal = await valueToken.balanceOf.call(stake);
assert.equal(openSTValueBal.toNumber(), 1);
assert.equal(openSTValueBal.toNumber(), amountST);
assert.equal(stakeBal.toNumber(), 0);
result = await openSTValue.processStaking(stakingIntentHash, { from: registrar });

openSTValueBal = await valueToken.balanceOf.call(openSTValue.address);
stakeBal = await valueToken.balanceOf.call(stake);
assert.equal(openSTValueBal.toNumber(), 0);
assert.equal(stakeBal.toNumber(), 1);
await OpenSTValue_utils.checkProcessedStakeEvent(result.logs[0], checkUuid, stakingIntentHash, stake, accounts[0], 1, 10);
assert.equal(stakeBal.toNumber(), amountST);
await OpenSTValue_utils.checkProcessedStakeEvent(result.logs[0], checkUuid, stakingIntentHash, stake, accounts[0], amountST, amountUT);
})
})

Expand Down Expand Up @@ -517,7 +526,7 @@ contract('OpenSTValue', function(accounts) {

var staker = accounts[0];
var owner = accounts[1];
var amountST = 126;
var amountST = new BigNumber(web3.toWei(126, "ether"));
var amountUT = null;
var unlockHeight = null;

Expand Down Expand Up @@ -586,7 +595,7 @@ contract('OpenSTValue', function(accounts) {
context('Revert Staking After ProcessStaking', async () => {
var staker = accounts[0];
var owner = accounts[1];
var amountST = 126;
var amountST = new BigNumber(web3.toWei(126, "ether"));
var amountUT = null;
var unlockHeight = null;

Expand Down Expand Up @@ -645,8 +654,8 @@ contract('OpenSTValue', function(accounts) {
var redeemer = accounts[2];
var redemptionIntentHash = null;
var redemptionUnlockHeight = 80668;
var amountST = 1;
var amountUT = 1 * conversionRate;
var amountST = new BigNumber(web3.toWei(1, "ether"));
var amountUT = new BigNumber(amountST * conversionRate);
var externalUser = accounts[7];

context('Revert Unstaking before ProcessUnstaking ', async () => {
Expand Down
Loading