Skip to content

Commit

Permalink
Merge pull request #38 from OpenSTFoundation/jasonbanks/gh3/unit-tests
Browse files Browse the repository at this point in the history
Redeem minimal amount
  • Loading branch information
benjaminbollen authored Dec 5, 2017
2 parents d06b71e + 425038b commit d5c45fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
43 changes: 27 additions & 16 deletions test/OpenSTValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ const SimpleStake = artifacts.require("./SimpleStake.sol");
/// fails to reprocess
///
/// ConfirmRedemptionIntent
/// fails to confirm by non-registrar
/// fails to confirm when utility token does not have a simpleStake address
/// fails to confirm when amountUT is not > 0
/// fails to confirm when redemptionUnlockHeight is not > 0
/// fails to confirm when redemptionIntentHash is empty
/// fails to confirm when nonce is not exactly 1 greater than previously
/// fails to confirm when redemptionIntentHash does not match calculated hash
/// fails to confirm when token balance of stake is not >= amountST // Fails
/// successfully confirms // Fails
/// fails to confirm a replay
/// fails to confirm by non-registrar
/// fails to confirm when utility token does not have a simpleStake address
/// fails to confirm when amountUT is not > 0
/// fails to confirm when redemptionUnlockHeight is not > 0
/// fails to confirm when redemptionIntentHash is empty
/// fails to confirm when nonce is not exactly 1 greater than previously
/// fails to confirm when redemptionIntentHash does not match calculated hash
/// fails to confirm when token balance of stake is not >= amountST
/// successfully confirms
/// fails to confirm a replay
/// fails to confirm when amountUT does not convert into at least 1 STWei // Fails
///
/// ProcessUnstaking
/// when expirationHeight is > block number
Expand Down Expand Up @@ -326,7 +327,7 @@ contract('OpenSTValue', function(accounts) {
var redeemer = accounts[2];
var redemptionIntentHash = null;
var redemptionUnlockHeight = 80668;
var amountUT = 1;
var amountUT = conversionRate;

before(async () => {
contracts = await OpenSTValue_utils.deployOpenSTValue(artifacts, accounts);
Expand Down Expand Up @@ -377,16 +378,14 @@ contract('OpenSTValue', function(accounts) {
await Utils.expectThrow(openSTValue.confirmRedemptionIntent(checkUuid, redeemer, nonce.minus(1), amountUT, redemptionUnlockHeight, "bad hash", { from: registrar }));
})

// Fails because 1/10 == 0 in Solidity, and 0 balance >= 0 amountST
it('fails to confirm when token balance of stake is not >= amountST', async () => {
redemptionIntentHash = await openSTValue.hashRedemptionIntent.call(checkUuid, redeemer, nonce, amountUT, redemptionUnlockHeight);
await Utils.expectThrow(openSTValue.confirmRedemptionIntent(checkUuid, redeemer, nonce, amountUT, redemptionUnlockHeight, redemptionIntentHash, { from: registrar }));
})

// Fails because of and in relation to cause of preceding failure
it('successfully confirms', async () => {
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, 2, { from: accounts[0] });
result = await openSTValue.stake(checkUuid, 2, accounts[0], { from: accounts[0] });
stakingIntentHash = result.logs[0].args._stakingIntentHash;
await openSTValue.processStaking(stakingIntentHash, { from: accounts[0] });

Expand All @@ -396,13 +395,25 @@ contract('OpenSTValue', function(accounts) {
assert.equal(amountST, amountUT / conversionRate);

result = await openSTValue.confirmRedemptionIntent(checkUuid, redeemer, nonce, amountUT, redemptionUnlockHeight, redemptionIntentHash, { from: registrar });
await OpenSTValue_utils.checkRedemptionIntentConfirmedEvent(result.logs[0], checkUuid, redemptionIntentHash, redeemer, amountST, amountUT, redemptionUnlockHeight);
var BLOCKS_TO_WAIT_SHORT = 240;
var blockNumber = web3.eth.blockNumber;
var expirationHeight = blockNumber + BLOCKS_TO_WAIT_SHORT;
await OpenSTValue_utils.checkRedemptionIntentConfirmedEvent(result.logs[0], checkUuid, redemptionIntentHash, redeemer, amountST, amountUT, expirationHeight);
})

it('fails to confirm a replay', async () => {
redemptionIntentHash = await openSTValue.hashRedemptionIntent.call(checkUuid, redeemer, nonce, amountUT, redemptionUnlockHeight);
await Utils.expectThrow(openSTValue.confirmRedemptionIntent(checkUuid, redeemer, nonce, amountUT, redemptionUnlockHeight, redemptionIntentHash, { from: registrar }));
})

// Fails because logic does not prevent attempting to redeem 1 UTWei when the conversion rate is greater than 1
it('fails to confirm when amountUT does not convert into at least 1 STWei', async () => {
nonce = await openSTValue.getNextNonce.call(redeemer);

// 1 STWei == 10 UTWei at the given conversion rate
redemptionIntentHash = await openSTValue.hashRedemptionIntent.call(checkUuid, redeemer, nonce, 1, redemptionUnlockHeight);
await Utils.expectThrow(openSTValue.confirmRedemptionIntent(checkUuid, redeemer, nonce, 1, redemptionUnlockHeight, redemptionIntentHash, { from: registrar }));
})
})

describe('ProcessUnstaking', async () => {
Expand Down
8 changes: 4 additions & 4 deletions test/OpenSTValue_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports.checkProcessedStakeEvent = (event, _uuid, _stakingIntentHash, _st
assert.equal(event.args._amountUT.toNumber(), _amountUT.toNumber());
}

module.exports.checkRedemptionIntentConfirmedEvent = (event, _uuid, _redemptionIntentHash, _redeemer, _amountST, _amountUT, _unlockHeight) => {
module.exports.checkRedemptionIntentConfirmedEvent = (event, _uuid, _redemptionIntentHash, _redeemer, _amountST, _amountUT, _expirationHeight) => {
if (Number.isInteger(_amountST)) {
_amountST = new BigNumber(_amountST);
}
Expand All @@ -119,8 +119,8 @@ module.exports.checkRedemptionIntentConfirmedEvent = (event, _uuid, _redemptionI
_amountUT = new BigNumber(_amountUT);
}

if (Number.isInteger(_unlockHeight)) {
_unlockHeight = new BigNumber(_unlockHeight);
if (Number.isInteger(_expirationHeight)) {
_expirationHeight = new BigNumber(_expirationHeight);
}

assert.equal(event.event, "RedemptionIntentConfirmed");
Expand All @@ -129,7 +129,7 @@ module.exports.checkRedemptionIntentConfirmedEvent = (event, _uuid, _redemptionI
assert.equal(event.args._redeemer, _redeemer);
assert.equal(event.args._amountST.toNumber(), _amountST.toNumber());
assert.equal(event.args._amountUT.toNumber(), _amountUT.toNumber());
assert.equal(event.args._unlockHeight.toNumber(), _unlockHeight.toNumber());
assert.equal(event.args._expirationHeight.toNumber(), _expirationHeight.toNumber());
}

module.exports.checkProcessedUnstakeEvent = (event, _uuid, _redemptionIntentHash, stake, _redeemer, _amountST) => {
Expand Down

0 comments on commit d5c45fa

Please sign in to comment.