-
-
Notifications
You must be signed in to change notification settings - Fork 275
feat(transaction-pay): add Across strategy support #7886
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
Open
pedronfigueiredo
wants to merge
1
commit into
main
Choose a base branch
from
cor-6997-across-strategy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,047
−41
Open
Changes from all commits
Commits
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
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
188 changes: 188 additions & 0 deletions
188
packages/transaction-pay-controller/src/strategy/across/AcrossStrategy.test.ts
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,188 @@ | ||
| import type { TransactionMeta } from '@metamask/transaction-controller'; | ||
| import { | ||
| TransactionStatus, | ||
| TransactionType, | ||
| } from '@metamask/transaction-controller'; | ||
| import type { Hex } from '@metamask/utils'; | ||
|
|
||
| import { getAcrossQuotes } from './across-quotes'; | ||
| import { submitAcrossQuotes } from './across-submit'; | ||
| import { AcrossStrategy } from './AcrossStrategy'; | ||
| import type { AcrossQuote } from './types'; | ||
| import type { | ||
| PayStrategyExecuteRequest, | ||
| PayStrategyGetQuotesRequest, | ||
| TransactionPayQuote, | ||
| } from '../../types'; | ||
| import { getPayStrategiesConfig } from '../../utils/feature-flags'; | ||
|
|
||
| jest.mock('./across-quotes'); | ||
| jest.mock('./across-submit'); | ||
| jest.mock('../../utils/feature-flags'); | ||
|
|
||
| describe('AcrossStrategy', () => { | ||
| const getPayStrategiesConfigMock = jest.mocked(getPayStrategiesConfig); | ||
| const getAcrossQuotesMock = jest.mocked(getAcrossQuotes); | ||
| const submitAcrossQuotesMock = jest.mocked(submitAcrossQuotes); | ||
|
|
||
| const messenger = {} as never; | ||
|
|
||
| const TRANSACTION_META_MOCK = { | ||
| id: 'tx-1', | ||
| chainId: '0x1', | ||
| networkClientId: 'mainnet', | ||
| status: TransactionStatus.unapproved, | ||
| time: Date.now(), | ||
| txParams: { | ||
| from: '0xabc', | ||
| }, | ||
| } as TransactionMeta; | ||
|
|
||
| const baseRequest = { | ||
| messenger, | ||
| transaction: TRANSACTION_META_MOCK, | ||
| requests: [ | ||
| { | ||
| from: '0xabc' as Hex, | ||
| sourceBalanceRaw: '100', | ||
| sourceChainId: '0x1' as Hex, | ||
| sourceTokenAddress: '0xabc' as Hex, | ||
| sourceTokenAmount: '100', | ||
| targetAmountMinimum: '100', | ||
| targetChainId: '0x2' as Hex, | ||
| targetTokenAddress: '0xdef' as Hex, | ||
| }, | ||
| ], | ||
| } as PayStrategyGetQuotesRequest; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| getPayStrategiesConfigMock.mockReturnValue({ | ||
| across: { | ||
| allowSameChain: false, | ||
| apiBase: 'https://across.test', | ||
| enabled: true, | ||
| }, | ||
| relay: { | ||
| enabled: true, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns false when across is disabled', () => { | ||
| getPayStrategiesConfigMock.mockReturnValue({ | ||
| across: { | ||
| allowSameChain: false, | ||
| apiBase: 'https://across.test', | ||
| enabled: false, | ||
| }, | ||
| relay: { | ||
| enabled: true, | ||
| }, | ||
| }); | ||
|
|
||
| const strategy = new AcrossStrategy(); | ||
| expect(strategy.supports(baseRequest)).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true for perps deposits when other constraints are met', () => { | ||
| const strategy = new AcrossStrategy(); | ||
| expect( | ||
| strategy.supports({ | ||
| ...baseRequest, | ||
| transaction: { | ||
| ...TRANSACTION_META_MOCK, | ||
| type: TransactionType.perpsDeposit, | ||
| } as TransactionMeta, | ||
| }), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true when same-chain swaps are allowed', () => { | ||
| getPayStrategiesConfigMock.mockReturnValue({ | ||
| across: { | ||
| allowSameChain: true, | ||
| apiBase: 'https://across.test', | ||
| enabled: true, | ||
| }, | ||
| relay: { | ||
| enabled: true, | ||
| }, | ||
| }); | ||
|
|
||
| const strategy = new AcrossStrategy(); | ||
| expect( | ||
| strategy.supports({ | ||
| ...baseRequest, | ||
| requests: [ | ||
| { | ||
| from: '0xabc' as Hex, | ||
| sourceBalanceRaw: '100', | ||
| sourceChainId: '0x1' as Hex, | ||
| sourceTokenAddress: '0xabc' as Hex, | ||
| sourceTokenAmount: '100', | ||
| targetAmountMinimum: '100', | ||
| targetChainId: '0x1' as Hex, | ||
| targetTokenAddress: '0xdef' as Hex, | ||
| }, | ||
| ], | ||
| }), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false when same-chain swaps are not allowed', () => { | ||
| const strategy = new AcrossStrategy(); | ||
| expect( | ||
| strategy.supports({ | ||
| ...baseRequest, | ||
| requests: [ | ||
| { | ||
| from: '0xabc' as Hex, | ||
| sourceBalanceRaw: '100', | ||
| sourceChainId: '0x1' as Hex, | ||
| sourceTokenAddress: '0xabc' as Hex, | ||
| sourceTokenAmount: '100', | ||
| targetAmountMinimum: '100', | ||
| targetChainId: '0x1' as Hex, | ||
| targetTokenAddress: '0xdef' as Hex, | ||
| }, | ||
| ], | ||
| }), | ||
| ).toBe(false); | ||
| }); | ||
|
|
||
| it('returns true when all requests are cross-chain', () => { | ||
| const strategy = new AcrossStrategy(); | ||
| expect(strategy.supports(baseRequest)).toBe(true); | ||
| }); | ||
|
|
||
| it('delegates getQuotes to across quotes', async () => { | ||
| const strategy = new AcrossStrategy(); | ||
| const quote = { strategy: 'across' } as TransactionPayQuote<AcrossQuote>; | ||
| getAcrossQuotesMock.mockResolvedValue([quote]); | ||
|
|
||
| const result = await strategy.getQuotes(baseRequest); | ||
|
|
||
| expect(result).toStrictEqual([quote]); | ||
| expect(getAcrossQuotesMock).toHaveBeenCalledWith(baseRequest); | ||
| }); | ||
|
|
||
| it('delegates execute to across submit', async () => { | ||
| const strategy = new AcrossStrategy(); | ||
| const request = { | ||
| messenger, | ||
| quotes: [], | ||
| transaction: TRANSACTION_META_MOCK, | ||
| isSmartTransaction: jest.fn(), | ||
| } as PayStrategyExecuteRequest<AcrossQuote>; | ||
|
|
||
| submitAcrossQuotesMock.mockResolvedValue({ transactionHash: '0xhash' }); | ||
|
|
||
| const result = await strategy.execute(request); | ||
|
|
||
| expect(result).toStrictEqual({ | ||
| transactionHash: '0xhash', | ||
| }); | ||
| expect(submitAcrossQuotesMock).toHaveBeenCalledWith(request); | ||
| }); | ||
| }); |
42 changes: 42 additions & 0 deletions
42
packages/transaction-pay-controller/src/strategy/across/AcrossStrategy.ts
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,42 @@ | ||
| import { getAcrossQuotes } from './across-quotes'; | ||
| import { submitAcrossQuotes } from './across-submit'; | ||
| import type { AcrossQuote } from './types'; | ||
| import type { | ||
| PayStrategy, | ||
| PayStrategyExecuteRequest, | ||
| PayStrategyGetQuotesRequest, | ||
| TransactionPayQuote, | ||
| } from '../../types'; | ||
| import { getPayStrategiesConfig } from '../../utils/feature-flags'; | ||
|
|
||
| export class AcrossStrategy implements PayStrategy<AcrossQuote> { | ||
| supports(request: PayStrategyGetQuotesRequest): boolean { | ||
| const config = getPayStrategiesConfig(request.messenger); | ||
|
|
||
| if (!config.across.enabled) { | ||
| return false; | ||
| } | ||
|
|
||
| if (config.across.allowSameChain) { | ||
| return true; | ||
| } | ||
|
|
||
| // Across doesn't support same-chain swaps (e.g. mUSD conversions). | ||
| return request.requests.every( | ||
| (singleRequest) => | ||
| singleRequest.sourceChainId !== singleRequest.targetChainId, | ||
| ); | ||
| } | ||
|
|
||
| async getQuotes( | ||
| request: PayStrategyGetQuotesRequest, | ||
| ): Promise<TransactionPayQuote<AcrossQuote>[]> { | ||
| return getAcrossQuotes(request); | ||
| } | ||
|
|
||
| async execute( | ||
| request: PayStrategyExecuteRequest<AcrossQuote>, | ||
| ): ReturnType<PayStrategy<AcrossQuote>['execute']> { | ||
| return submitAcrossQuotes(request); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.