Skip to content

Commit

Permalink
Add test spending ERC20 v2 payment and fix bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vitae committed Feb 7, 2024
1 parent b679c76 commit 9ec27e2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
27 changes: 1 addition & 26 deletions contracts/EtomicSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -191,36 +191,11 @@ contract EtomicSwap is ERC165, IERC1155Receiver, IERC721Receiver {
token.transfer(msg.sender, amount), "ERC20 transfer failed: Contract may lack balance or token transfer was rejected"
);
require(
token.transfer(dexFeeAddress, amount), "ERC20 transfer failed: Contract may lack balance or token transfer was rejected"
token.transfer(dexFeeAddress, dexFee), "ERC20 transfer failed: Contract may lack balance or token transfer was rejected"
);
}
}

function updatePaymentState(
bytes32 id,
uint256 amount,
uint256 dexFee,
address receiver,
bytes20 takerSecretHash,
bytes20 makerSecretHash,
address tokenAddress
) external {
bytes20 paymentHash = ripemd160(
abi.encodePacked(
amount,
dexFee,
receiver,
msg.sender,
takerSecretHash,
makerSecretHash,
tokenAddress
)
);
require(paymentHash == payments_v2[id].paymentHash, "Invalid paymentHash");

payments_v2[id].state = PaymentStateV2.SenderRefunded;
}

function erc20Payment(
bytes32 id,
uint256 amount,
Expand Down
63 changes: 52 additions & 11 deletions test/EtomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,6 @@ describe("EtomicSwap", function() {
await etomicSwap.connect(accounts[0]).ethTakerPaymentV2(...params, {
value: ethers.parseEther('1')
}).should.be.rejectedWith("ETH v2 payment is already initialized");

const update_params = [
id,
ethers.parseEther('0.9'),
ethers.parseEther('0.1'),
accounts[1].address,
secretHash,
secretHash,
zeroAddr
];
await etomicSwap.connect(accounts[0]).updatePaymentState(...update_params).should.be.fulfilled;
});

it('should allow to spend ETH taker payment v2', async function() {
Expand Down Expand Up @@ -210,6 +199,58 @@ describe("EtomicSwap", function() {
expect(Number(payment[3])).to.equal(RECEIVER_V2_SPENT);
});

it('should allow to spend ERC20 taker payment v2', async function() {
let current_time = await currentEvmTime();
const immediateRefundLockTime = current_time + 100;
const paymentLockTime = current_time + 100;
const payment_params = [
id,
ethers.parseEther('0.9'), // amount
ethers.parseEther('0.1'), // dexFee
token.target,
accounts[1].address, // receiver
secretHash,
secretHash,
immediateRefundLockTime,
paymentLockTime
];

// Make the ERC20 payment
await token.approve(etomicSwap.target, ethers.parseEther('1'));
await etomicSwap.connect(accounts[0]).erc20TakerPaymentV2(...payment_params).should.be.fulfilled;

const contractBalance = await token.balanceOf(etomicSwap.target);
expect(contractBalance).to.equal(ethers.parseEther('1'));

const spend_params = [
id,
ethers.parseEther('0.9'), // amount
ethers.parseEther('0.1'), // dexFee
secret,
accounts[0].address, // sender
secretHash,
token.target, // tokenAddress
];

const balanceBefore = await token.balanceOf(accounts[1].address);

const gasPrice = ethers.parseUnits('100', 'gwei');
await etomicSwap.connect(accounts[1]).spendTakerPaymentV2(...spend_params, {
gasPrice
}).should.be.fulfilled;

const balanceAfter = await token.balanceOf(accounts[1].address);
// Check receiver balance
expect((balanceAfter - balanceBefore)).to.equal(ethers.parseEther('0.9'));

const dexFeeAddrBalance = await token.balanceOf(dexFeeAddr);
expect(dexFeeAddrBalance).to.equal(ethers.parseEther('0.1'));

const payment = await etomicSwap.payments_v2(id);

expect(Number(payment[3])).to.equal(RECEIVER_V2_SPENT);
});

it('should allow to send ERC20 payment', async function() {
const lockTime = await currentEvmTime() + 1000;
const amount = ethers.parseEther('1');
Expand Down

0 comments on commit 9ec27e2

Please sign in to comment.