|
| 1 | +import { _buildSlowRelayRoot, _getRefundLeaves } from "../src/dataworker/DataworkerUtils"; |
| 2 | +import { BundleSlowFills, DepositWithBlock } from "../src/interfaces"; |
| 3 | +import { BigNumber, bnOne, bnZero, toBNWei, ZERO_ADDRESS } from "../src/utils"; |
| 4 | +import { repaymentChainId } from "./constants"; |
| 5 | +import { assert, expect, randomAddress } from "./utils"; |
| 6 | + |
| 7 | +describe("RelayerRefund utils", function () { |
| 8 | + it("Removes zero value refunds from relayer refund root", async function () { |
| 9 | + const recipient1 = randomAddress(); |
| 10 | + const recipient2 = randomAddress(); |
| 11 | + const repaymentToken = randomAddress(); |
| 12 | + const maxRefundsPerLeaf = 2; |
| 13 | + const result = _getRefundLeaves( |
| 14 | + { |
| 15 | + [recipient1]: bnZero, |
| 16 | + [recipient2]: bnOne, |
| 17 | + }, |
| 18 | + bnZero, |
| 19 | + repaymentChainId, |
| 20 | + repaymentToken, |
| 21 | + maxRefundsPerLeaf |
| 22 | + ); |
| 23 | + expect(result.length).to.equal(1); |
| 24 | + expect(result[0].refundAddresses.length).to.equal(1); |
| 25 | + }); |
| 26 | + it("No more than maxRefundsPerLeaf number of refunds in a leaf", async function () { |
| 27 | + const recipient1 = randomAddress(); |
| 28 | + const recipient2 = randomAddress(); |
| 29 | + const repaymentToken = randomAddress(); |
| 30 | + const amountToReturn = bnOne; |
| 31 | + const maxRefundsPerLeaf = 1; |
| 32 | + const result = _getRefundLeaves( |
| 33 | + { |
| 34 | + [recipient1]: bnOne, |
| 35 | + [recipient2]: bnOne, |
| 36 | + }, |
| 37 | + amountToReturn, |
| 38 | + repaymentChainId, |
| 39 | + repaymentToken, |
| 40 | + maxRefundsPerLeaf |
| 41 | + ); |
| 42 | + expect(result.length).to.equal(2); |
| 43 | + // Only the first leaf should have an amount to return. |
| 44 | + expect(result[0].groupIndex).to.equal(0); |
| 45 | + expect(result[0].amountToReturn).to.equal(amountToReturn); |
| 46 | + expect(result[1].groupIndex).to.equal(1); |
| 47 | + expect(result[1].amountToReturn).to.equal(0); |
| 48 | + }); |
| 49 | + it("Sorts refunds by amount in descending order", async function () { |
| 50 | + const recipient1 = randomAddress(); |
| 51 | + const recipient2 = randomAddress(); |
| 52 | + const repaymentToken = randomAddress(); |
| 53 | + const maxRefundsPerLeaf = 2; |
| 54 | + const result = _getRefundLeaves( |
| 55 | + { |
| 56 | + [recipient1]: bnOne, |
| 57 | + [recipient2]: bnOne.mul(2), |
| 58 | + }, |
| 59 | + bnZero, |
| 60 | + repaymentChainId, |
| 61 | + repaymentToken, |
| 62 | + maxRefundsPerLeaf |
| 63 | + ); |
| 64 | + expect(result.length).to.equal(1); |
| 65 | + expect(result[0].refundAddresses[0]).to.equal(recipient2); |
| 66 | + expect(result[0].refundAddresses[1]).to.equal(recipient1); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("SlowFill utils", function () { |
| 71 | + /** |
| 72 | + * @notice Returns dummy slow fill leaf that you can insert into a BundleSlowFills object. |
| 73 | + * @dev The leaf returned will not actually be executable so its good for testing functions |
| 74 | + * that produce but do not execute merkle leaves. |
| 75 | + * @param depositId This is used to sort slow fill leaves so allow caller to set. |
| 76 | + * @param amountToFill This will be set to the deposit's inputAmount because the slow fill pays out |
| 77 | + * inputAmount * (1 - lpFeePct). |
| 78 | + * @param lpFeePct Amount to charge on the amountToFill. |
| 79 | + * @param message 0-value, empty-message slow fills should be ignored by dataworker so allow caller |
| 80 | + * to set this to non-empty to test logic. |
| 81 | + * @param originChainId This is used to sort slow fill leaves so allow caller to set. |
| 82 | + */ |
| 83 | + |
| 84 | + function createSlowFillLeaf( |
| 85 | + depositId: number, |
| 86 | + originChainId: number, |
| 87 | + amountToFill: BigNumber, |
| 88 | + message: string, |
| 89 | + _lpFeePct: BigNumber |
| 90 | + ): DepositWithBlock & { lpFeePct: BigNumber } { |
| 91 | + assert(message.slice(0, 2) === "0x", "Need to specify message beginning with 0x"); |
| 92 | + const destinationChainId = originChainId + 1; |
| 93 | + const deposit: DepositWithBlock = { |
| 94 | + inputAmount: amountToFill, |
| 95 | + inputToken: randomAddress(), |
| 96 | + outputAmount: bnOne, |
| 97 | + outputToken: randomAddress(), |
| 98 | + depositor: randomAddress(), |
| 99 | + depositId, |
| 100 | + originChainId: 1, |
| 101 | + recipient: randomAddress(), |
| 102 | + exclusiveRelayer: ZERO_ADDRESS, |
| 103 | + exclusivityDeadline: 0, |
| 104 | + message, |
| 105 | + destinationChainId, |
| 106 | + fillDeadline: 0, |
| 107 | + quoteBlockNumber: 0, |
| 108 | + blockNumber: 0, |
| 109 | + transactionHash: "", |
| 110 | + logIndex: 0, |
| 111 | + transactionIndex: 0, |
| 112 | + quoteTimestamp: 0, |
| 113 | + fromLiteChain: false, |
| 114 | + toLiteChain: false, |
| 115 | + }; |
| 116 | + return { |
| 117 | + ...deposit, |
| 118 | + lpFeePct: _lpFeePct, |
| 119 | + }; |
| 120 | + } |
| 121 | + it("Filters out 0-value empty-message slowfills", async function () { |
| 122 | + const zeroValueSlowFillLeaf = createSlowFillLeaf(0, 1, bnZero, "0x", bnZero); |
| 123 | + const oneWeiSlowFillLeaf = createSlowFillLeaf(1, 1, bnOne, "0x", bnZero); |
| 124 | + const zeroValueNonEmptyMessageSlowFillLeaf = createSlowFillLeaf(2, 1, bnZero, "0x12", bnZero); |
| 125 | + const bundleSlowFills: BundleSlowFills = { |
| 126 | + [zeroValueSlowFillLeaf.destinationChainId]: { |
| 127 | + [zeroValueSlowFillLeaf.outputToken]: [ |
| 128 | + zeroValueSlowFillLeaf, |
| 129 | + oneWeiSlowFillLeaf, |
| 130 | + zeroValueNonEmptyMessageSlowFillLeaf, |
| 131 | + ], |
| 132 | + }, |
| 133 | + }; |
| 134 | + |
| 135 | + // Should return two out of three leaves, sorted by deposit ID. |
| 136 | + const { leaves } = _buildSlowRelayRoot(bundleSlowFills); |
| 137 | + expect(leaves.length).to.equal(2); |
| 138 | + expect(leaves[0].relayData.depositId).to.equal(1); |
| 139 | + expect(leaves[1].relayData.depositId).to.equal(2); |
| 140 | + }); |
| 141 | + it("Applies LP fee to input amount", async function () { |
| 142 | + const slowFillLeaf = createSlowFillLeaf(0, 1, toBNWei("4"), "0x", toBNWei("0.25")); |
| 143 | + const bundleSlowFills: BundleSlowFills = { |
| 144 | + [slowFillLeaf.destinationChainId]: { |
| 145 | + [slowFillLeaf.outputToken]: [slowFillLeaf], |
| 146 | + }, |
| 147 | + }; |
| 148 | + |
| 149 | + // Should return two out of three leaves, sorted by deposit ID. |
| 150 | + const { leaves } = _buildSlowRelayRoot(bundleSlowFills); |
| 151 | + expect(leaves.length).to.equal(1); |
| 152 | + // updatedOutputAmount should be equal to inputAmount * (1 - lpFee) so 4 * (1 - 0.25) = 3 |
| 153 | + expect(leaves[0].updatedOutputAmount).to.equal(toBNWei("3")); |
| 154 | + }); |
| 155 | +}); |
0 commit comments