Skip to content

Commit

Permalink
Merge pull request artemii235#23 from artemii235/allow-receiver-spend…
Browse files Browse the repository at this point in the history
…-any-time

Allow receiver spend any time artemii235#18
  • Loading branch information
artemii235 authored Dec 13, 2019
2 parents 455eab8 + 92eb9e8 commit 330275d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/EtomicSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ contract EtomicSwap {
_amount
));

require(paymentHash == payments[_id].paymentHash && now < payments[_id].lockTime);
require(paymentHash == payments[_id].paymentHash);
payments[_id].state = PaymentState.ReceivedSpent;
if (_tokenAddress == address(0)) {
msg.sender.transfer(_amount);
Expand Down
69 changes: 69 additions & 0 deletions test/EtomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,73 @@ contract('EtomicSwap', function(accounts) {
// should not allow to spend again
await this.swap.receiverSpend(id, web3.utils.toWei('1'), secretHex, this.token.address, accounts[0], { from: accounts[1], gasPrice }).should.be.rejectedWith(EVMThrow);
});

it('should allow receiver to spend ETH payment by revealing a secret even after locktime', async function () {
const lockTime = await currentEvmTime() + 1000;
const params = [
id,
accounts[1],
secretHash,
lockTime
];

await this.swap.ethPayment(...params, { value: web3.utils.toWei('1') }).should.be.fulfilled;

await increaseTime(1000);

// success spend
const balanceBefore = web3.utils.toBN(await web3.eth.getBalance(accounts[1]));
const gasPrice = web3.utils.toWei('100', 'gwei');

const tx = await this.swap.receiverSpend(id, web3.utils.toWei('1'), secretHex, zeroAddr, accounts[0], { from: accounts[1], gasPrice }).should.be.fulfilled;
const txFee = web3.utils.toBN(gasPrice).mul(web3.utils.toBN(tx.receipt.gasUsed));

const balanceAfter = web3.utils.toBN(await web3.eth.getBalance(accounts[1]));

// check receiver balance
assert.equal(balanceAfter.sub(balanceBefore).add(txFee).toString(), web3.utils.toWei('1'));

const payment = await this.swap.payments(id);

// status
assert.equal(payment[2].valueOf(), RECEIVER_SPENT);

// should not allow to spend again
await this.swap.receiverSpend(id, web3.utils.toWei('1'), secretHex, zeroAddr, accounts[0], { from: accounts[1], gasPrice }).should.be.rejectedWith(EVMThrow);
});

it('should allow receiver to spend ERC20 payment by revealing a secret', async function () {
const lockTime = await currentEvmTime() + 1000;
const params = [
id,
web3.utils.toWei('1'),
this.token.address,
accounts[1],
secretHash,
lockTime
];

await this.token.approve(this.swap.address, web3.utils.toWei('1'));
await this.swap.erc20Payment(...params).should.be.fulfilled;

await increaseTime(1000);

// success spend
const balanceBefore = web3.utils.toBN(await this.token.balanceOf(accounts[1]));

const gasPrice = web3.utils.toWei('100', 'gwei');
await this.swap.receiverSpend(id, web3.utils.toWei('1'), secretHex, this.token.address, accounts[0], { from: accounts[1], gasPrice }).should.be.fulfilled;
const balanceAfter = web3.utils.toBN(await this.token.balanceOf(accounts[1]));

// check receiver balance
assert.equal(balanceAfter.sub(balanceBefore).toString(), web3.utils.toWei('1'));

const payment = await this.swap.payments(id);

// status
assert.equal(payment[2].valueOf(), RECEIVER_SPENT);

// should not allow to spend again
await this.swap.receiverSpend(id, web3.utils.toWei('1'), secretHex, this.token.address, accounts[0], { from: accounts[1], gasPrice }).should.be.rejectedWith(EVMThrow);
});
});

0 comments on commit 330275d

Please sign in to comment.