-
Notifications
You must be signed in to change notification settings - Fork 96
improve(Dataworker): Remove 0-value refunds and 0-value empty-message slow fills #1957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f24f709
improve(Dataworker): Remove 0-value refunds and 0-value empty-message…
nicholaspai 65ab014
WIP
nicholaspai 1e69ffd
Update src/dataworker/DataworkerUtils.ts
nicholaspai 2121c0e
Update DataworkerUtils.ts
nicholaspai 9e87e28
Merge branch 'master' into zero-value-deposits
nicholaspai 36307a5
Merge branch 'master' into zero-value-deposits
nicholaspai 5fc53bf
Merge branch 'master' into zero-value-deposits
nicholaspai b9b206e
Merge branch 'master' into zero-value-deposits
nicholaspai 4c458bc
Merge branch 'master' into zero-value-deposits
nicholaspai 382c50e
Merge branch 'master' into zero-value-deposits
nicholaspai ac28433
Use SDK isZeroValueDeposit
nicholaspai 1a23767
Merge branch 'master' into zero-value-deposits
nicholaspai 6deda15
Merge branch 'master' into zero-value-deposits
nicholaspai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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")); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.