Skip to content

Commit

Permalink
Merge branch 'develop' into gh_409
Browse files Browse the repository at this point in the history
  • Loading branch information
0xsarvesh authored Jan 22, 2019
2 parents 9184c42 + 0f2efaf commit b3bdf73
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 37 deletions.
35 changes: 17 additions & 18 deletions contracts/gateway/EIP20CoGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -585,22 +585,14 @@ contract EIP20CoGateway is GatewayBase {
uint256 amount_
)
{

require(
_messageHash != bytes32(0),
"Message hash must not be zero"
"Message hash must not be zero."
);

// Get the message object for the _messageHash.
MessageBus.Message storage message = messages[_messageHash];

require(message.intentHash != bytes32(0));

require(
message.intentHash != bytes32(0),
"RedeemIntentHash must not be zero"
);

require(
message.sender == msg.sender,
"Only redeemer can revert redeem."
Expand All @@ -611,23 +603,19 @@ contract EIP20CoGateway is GatewayBase {

require(
msg.value == penalty,
"msg.value must match the penalty amount"
"msg.value must match the penalty amount."
);

require(
messageBox.outbox[_messageHash] ==
MessageBus.MessageStatus.Undeclared,
"Message status must be Undeclared"
// Declare redeem revocation.
MessageBus.declareRevocationMessage(
messageBox,
message
);
// Update the message outbox status to declared.
messageBox.outbox[_messageHash] =
MessageBus.MessageStatus.DeclaredRevocation;

redeemer_ = message.sender;
redeemerNonce_ = message.nonce;
amount_ = redeems[_messageHash].amount;

// Emit RevertRedeemDeclared event.
emit RevertRedeemDeclared(
_messageHash,
redeemer_,
Expand Down Expand Up @@ -867,11 +855,22 @@ contract EIP20CoGateway is GatewayBase {
msg.value == bounty,
"Payable amount should be equal to the bounty amount."
);

require(
_amount > uint256(0),
"Redeem amount must not be zero."
);

/*
* Maximum reward possible is _gasPrice * _gasLimit, we check this
* upfront in this function to make sure that after unstake of the
* tokens it is possible to give the reward to the facilitator.
*/
require(
_amount > _gasPrice.mul(_gasLimit),
"Maximum possible reward must be less than the redeem amount."
);

// Get the redeem intent hash.
bytes32 intentHash = GatewayLib.hashRedeemIntent(
_amount,
Expand Down
15 changes: 13 additions & 2 deletions contracts/gateway/EIP20Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,22 @@ contract EIP20Gateway is GatewayBase {

require(
_amount > uint256(0),
"Stake amount must not be zero"
"Stake amount must not be zero."
);

require(
_beneficiary != address(0),
"Beneficiary address must not be zero"
"Beneficiary address must not be zero."
);

/*
* Maximum reward possible is _gasPrice * _gasLimit, we check this
* upfront in this function to make sure that after minting of the
* tokens it is possible to give the reward to the facilitator.
*/
require(
_amount > _gasPrice.mul(_gasLimit),
"Maximum possible reward must be less than the stake amount."
);

// Get the stake intent hash.
Expand Down
57 changes: 42 additions & 15 deletions test/gateway/eip20_cogateway/redeem.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ let eip20CoGateway,

let MessageStatusEnum = messageBus.MessageStatusEnum;

contract('EIP20CoGateway.redeem() ', function (accounts) {
contract('EIP20CoGateway.redeem()', function (accounts) {

let amount,
beneficiary = accounts[4],
gasPrice = new BN(10),
gasLimit = new BN(10),
gasPrice = new BN(1),
gasLimit = new BN(1000),
nonce = new BN(1),
hashLockObj = Utils.generateHashLock(),
hashLock = hashLockObj.l;
Expand All @@ -59,7 +59,7 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {
utilityToken = await MockToken.new({ from: owner });
bountyAmount = new BN(100);
redeemer = accounts[7];
redeemerBalance = new BN(1000);
redeemerBalance = new BN(100000);
burner = accounts[10];

eip20CoGateway = await EIP20CoGateway.new(
Expand All @@ -79,7 +79,7 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {
redeemerBalance,
{ from: redeemer },
);
amount = new BN(100);
amount = redeemerBalance;

});

Expand Down Expand Up @@ -119,7 +119,8 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {

it('should fail when redeem amount is zero', async function () {

amount = 0;
amount = new BN(0);

await Utils.expectRevert(
eip20CoGateway.redeem(
amount,
Expand All @@ -134,6 +135,27 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {
);
});

it('should fail when max reward amount is greater than the redeem amount', async function () {

amount = new BN(100);
gasPrice = new BN(1);
gasLimit = new BN(10000);

await Utils.expectRevert(
eip20CoGateway.redeem(
amount,
beneficiary,
gasPrice,
gasLimit,
nonce,
hashLock,
{ from: redeemer, value: bountyAmount },
),
'Maximum possible reward must be less than the redeem amount.',
);

});

it('should fail when redeem with same nonce is already initiated', async function () {

await eip20CoGateway.redeem(
Expand All @@ -146,7 +168,7 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {
{ from: redeemer, value: bountyAmount },
);

amount = new BN(200);
amount = new BN(200000);
await Utils.expectRevert(
eip20CoGateway.redeem(
amount,
Expand Down Expand Up @@ -191,11 +213,16 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {

it('should fail when cogateway is not approved with redeem amount', async function () {

amount = new BN(100000);
/*
* CoGateway is approved to spend the redeem amount in beforeEach, so by
* adding 1 to the approved(redeem) amount, the transfer will fail for this
* test case.
*/
let redeemAmount = amount.addn(1);

await Utils.expectRevert(
eip20CoGateway.redeem(
amount,
redeemAmount,
beneficiary,
gasPrice,
gasLimit,
Expand Down Expand Up @@ -227,17 +254,17 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {

it('should fail when the redeemer\'s BT balance is less than the redeem amount', async function () {

let amount = new BN(10000);
let redeemAmount = redeemerBalance.addn(1);

await utilityToken.approve(
eip20CoGateway.address,
amount,
redeemAmount,
{ from: redeemer },
);

await Utils.expectRevert(
eip20CoGateway.redeem(
amount,
redeemAmount,
beneficiary,
gasPrice,
gasLimit,
Expand Down Expand Up @@ -482,11 +509,11 @@ contract('EIP20CoGateway.redeem() ', function (accounts) {
);

let expectedBalance = redeemerBalance.sub(amount);

let redeemerTokenBalance = await utilityToken.balanceOf(redeemer);
assert.strictEqual(
(await utilityToken.balanceOf(redeemer)).eq(expectedBalance),
redeemerTokenBalance.eq(expectedBalance),
true,
"Redeemer EIP20 token balance should be equal to ${expectedBalance}",
`Redeemer's EIP20 token balance ${redeemerTokenBalance} should be equal to ${expectedBalance}`,
);

let expectedEvent = {
Expand Down
Loading

0 comments on commit b3bdf73

Please sign in to comment.