diff --git a/test/fee.t.sol b/test/fee.t.sol index 564f229f..b4b94289 100644 --- a/test/fee.t.sol +++ b/test/fee.t.sol @@ -17,23 +17,46 @@ contract FeeTest is PacketSenderTest { PacketFee[] packetFees; + modifier useFeeTestCases() { + // initialize an array of PacketFee with different values + packetFees.push(PacketFee(0, 0, 0)); + packetFees.push(PacketFee(1, 0, 0)); + packetFees.push(PacketFee(0, 1, 0)); + packetFees.push(PacketFee(0, 0, 1)); + packetFees.push(PacketFee(1, 1, 1)); + packetFees.push(PacketFee(1, 2, 3)); + packetFees.push(PacketFee(2, 2, 1)); + packetFees.push(PacketFee(1, 1, 2)); + packetFees.push(PacketFee(1, 2, 2)); + packetFees.push(PacketFee(3, 2, 1)); + + for (uint i = 0; i < packetFees.length; i++) { + fee = packetFees[i]; + _; + } + } + // pay extra packet fee after packet creation - function test_payPacketFeeAsync() public { - PacketFee memory accu = mergeFees(fee, fee); + function test_payPacketFeeAsync() public useFeeTestCases { + // merge fees to calculate expected packet fee + PacketFee memory extraFee = PacketFee(1, 2, 3); + PacketFee memory expectedFee = mergeFees(fee, extraFee); + for (uint64 index = 0; index < 3; index++) { - uint64 packetSeq = index + 1; + // create packet and pay escrow fee + sendPacket(); - mars.greet{value: Ibc.calcEscrowFee(fee)}( - IbcDispatcher(dispatcher), - payloadStr, + // pay packet fee asynchronously + dispatcher.payPacketFeeAsync{value: Ibc.calcEscrowFee(extraFee)}( + address(mars), channelId, - maxTimeout, - fee + sentPacket.sequence, + extraFee ); - dispatcher.payPacketFeeAsync{value: Ibc.calcEscrowFee(fee)}(address(mars), channelId, packetSeq, fee); - PacketFee memory packetFee = dispatcher.getTotalPacketFees(address(mars), channelId, packetSeq); - assertEq(keccak256(abi.encode(packetFee)), keccak256(abi.encode(accu))); + // check that the total packet fee is correct + PacketFee memory actualFee = dispatcher.getTotalPacketFees(address(mars), channelId, sentPacket.sequence); + assertEq(keccak256(abi.encode(actualFee)), keccak256(abi.encode(expectedFee)), 'Packet fee is incorrect'); } } @@ -49,25 +72,6 @@ contract FeeTest is PacketSenderTest { ); } - modifier useFeeTestCases() { - // initialize an array of PacketFee with different values - packetFees.push(PacketFee(0, 0, 0)); - packetFees.push(PacketFee(1, 0, 0)); - packetFees.push(PacketFee(0, 1, 0)); - packetFees.push(PacketFee(0, 0, 1)); - packetFees.push(PacketFee(1, 1, 1)); - packetFees.push(PacketFee(1, 2, 3)); - packetFees.push(PacketFee(2, 2, 1)); - packetFees.push(PacketFee(1, 1, 2)); - packetFees.push(PacketFee(1, 2, 2)); - packetFees.push(PacketFee(3, 2, 1)); - - for (uint i = 0; i < packetFees.length; i++) { - fee = packetFees[i]; - _; - } - } - // claim ack fee on receving ack function test_ack_fee() public useFeeTestCases { // save balances of forward and reverse relayer payees; assert balances changes after ack @@ -99,21 +103,22 @@ contract FeeTest is PacketSenderTest { } // claim timeout fee on receving timeout - function test_timeout_fee() public { - assertEq(address(forwardRelayerPayee).balance, 0); - assertEq(address(reverseRelayerPayee).balance, 0); + function test_timeout_fee() public useFeeTestCases { + uint balanceReverseRelayer1 = address(reverseRelayerPayee).balance; + address refundPayee = address(mars); + uint balanceRefund1 = refundPayee.balance; + vm.startPrank(relayer); sendPacket(); - uint balance1 = address(mars).balance; - vm.startPrank(reverseRelayerPayee, reverseRelayerPayee); dispatcher.timeout(IbcReceiver(mars), sentPacket, validProof); - assertEq(address(reverseRelayerPayee).balance, fee.timeoutFee); + assertEq(address(reverseRelayerPayee).balance, fee.timeoutFee + balanceReverseRelayer1); + // no forward relayer is invovled in timeout case assertEq(address(forwardRelayerPayee).balance, 0); + assertEq(refundPayee.balance, Ibc.timeoutRefundAmount(fee) + balanceRefund1); assertEq(escrow.balance, 0); - assertEq(address(mars).balance, balance1 + Ibc.timeoutRefundAmount(fee)); // cannot claim timeout fee twice vm.expectRevert();