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