-
-
Notifications
You must be signed in to change notification settings - Fork 276
feat: Implement FiatStrategy and getQuotes functionality
#8121
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
OGPoyraz
wants to merge
3
commits into
main
Choose a base branch
from
ogp/7069
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.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { TransactionType } from '@metamask/transaction-controller'; | ||
| import type { Hex } from '@metamask/utils'; | ||
|
|
||
| export const CONTROLLER_NAME = 'TransactionPayController'; | ||
|
|
@@ -14,6 +15,36 @@ export const ARBITRUM_USDC_ADDRESS = | |
| export const POLYGON_USDCE_ADDRESS = | ||
| '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174' as Hex; | ||
|
|
||
| export type TransactionPayFiatAsset = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should all of the be in |
||
| address: Hex; | ||
| caipAssetId: string; | ||
| chainId: Hex; | ||
| decimals: number; | ||
| }; | ||
|
|
||
| const POLYGON_POL_FIAT_ASSET: TransactionPayFiatAsset = { | ||
| address: '0x0000000000000000000000000000000000001010', | ||
| caipAssetId: 'eip155:137/slip44:966', | ||
| chainId: CHAIN_ID_POLYGON, | ||
| decimals: 18, | ||
| }; | ||
|
|
||
| const ARBITRUM_ETH_FIAT_ASSET: TransactionPayFiatAsset = { | ||
| address: NATIVE_TOKEN_ADDRESS, | ||
| caipAssetId: 'eip155:42161/slip44:60', | ||
| chainId: CHAIN_ID_ARBITRUM, | ||
| decimals: 18, | ||
| }; | ||
|
|
||
| // We might use feature flags to determine these later. | ||
| export const MMPAY_FIAT_ASSET_ID_BY_TX_TYPE: Partial< | ||
| Record<TransactionType, TransactionPayFiatAsset> | ||
| > = { | ||
| [TransactionType.predictDeposit]: POLYGON_POL_FIAT_ASSET, | ||
| [TransactionType.perpsDeposit]: ARBITRUM_ETH_FIAT_ASSET, | ||
| [TransactionType.perpsDepositAndOrder]: ARBITRUM_ETH_FIAT_ASSET, | ||
| }; | ||
|
|
||
| export const STABLECOINS: Record<Hex, Hex[]> = { | ||
| // Mainnet | ||
| '0x1': [ | ||
|
|
@@ -35,6 +66,7 @@ export const STABLECOINS: Record<Hex, Hex[]> = { | |
| export enum TransactionPayStrategy { | ||
| Across = 'across', | ||
| Bridge = 'bridge', | ||
| Fiat = 'fiat', | ||
| Relay = 'relay', | ||
| Test = 'test', | ||
| } | ||
|
|
||
53 changes: 53 additions & 0 deletions
53
packages/transaction-pay-controller/src/strategy/fiat/FiatStrategy.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,53 @@ | ||
| import type { TransactionMeta } from '@metamask/transaction-controller'; | ||
|
|
||
| import { getFiatQuotes } from './fiat-quotes'; | ||
| import { submitFiatQuotes } from './fiat-submit'; | ||
| import { FiatStrategy } from './FiatStrategy'; | ||
| import type { FiatOriginalQuote } from './types'; | ||
| import type { TransactionPayControllerMessenger } from '../..'; | ||
| import type { TransactionPayQuote } from '../../types'; | ||
|
|
||
| jest.mock('./fiat-quotes'); | ||
| jest.mock('./fiat-submit'); | ||
|
|
||
| const QUOTE_MOCK = { | ||
| estimatedDuration: 5, | ||
| } as TransactionPayQuote<FiatOriginalQuote>; | ||
|
|
||
| describe('FiatStrategy', () => { | ||
| const getFiatQuotesMock = jest.mocked(getFiatQuotes); | ||
| const submitFiatQuotesMock = jest.mocked(submitFiatQuotes); | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| getFiatQuotesMock.mockResolvedValue([QUOTE_MOCK]); | ||
| }); | ||
|
|
||
| describe('getQuotes', () => { | ||
| it('returns result from util', async () => { | ||
| const result = new FiatStrategy().getQuotes({ | ||
| messenger: {} as TransactionPayControllerMessenger, | ||
| requests: [], | ||
| transaction: {} as TransactionMeta, | ||
| }); | ||
|
|
||
| expect(await result).toStrictEqual([QUOTE_MOCK]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('execute', () => { | ||
| it('calls util', async () => { | ||
| await new FiatStrategy().execute({ | ||
| isSmartTransaction: () => false, | ||
| quotes: [QUOTE_MOCK], | ||
| messenger: {} as TransactionPayControllerMessenger, | ||
| transaction: { txParams: { from: '0x1' } } as TransactionMeta, | ||
| }); | ||
|
|
||
| expect(submitFiatQuotesMock).toHaveBeenCalledTimes(1); | ||
| expect( | ||
| submitFiatQuotesMock.mock.calls[0][0].transaction.txParams.from, | ||
| ).toBe('0x1'); | ||
| }); | ||
| }); | ||
| }); |
23 changes: 23 additions & 0 deletions
23
packages/transaction-pay-controller/src/strategy/fiat/FiatStrategy.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,23 @@ | ||
| import { getFiatQuotes } from './fiat-quotes'; | ||
| import { submitFiatQuotes } from './fiat-submit'; | ||
| import type { FiatOriginalQuote } from './types'; | ||
| import type { | ||
| PayStrategy, | ||
| PayStrategyExecuteRequest, | ||
| PayStrategyGetQuotesRequest, | ||
| TransactionPayQuote, | ||
| } from '../../types'; | ||
|
|
||
| export class FiatStrategy implements PayStrategy<FiatOriginalQuote> { | ||
| async getQuotes( | ||
| request: PayStrategyGetQuotesRequest, | ||
| ): Promise<TransactionPayQuote<FiatOriginalQuote>[]> { | ||
| return getFiatQuotes(request); | ||
| } | ||
|
|
||
| async execute( | ||
| request: PayStrategyExecuteRequest<FiatOriginalQuote>, | ||
| ): ReturnType<PayStrategy<FiatOriginalQuote>['execute']> { | ||
| return await submitFiatQuotes(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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.