Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/transaction-pay-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add Gas Station support for Across source transactions when native balance is insufficient ([#8588](https://github.com/MetaMask/core/pull/8588))

### Fixed

- Pass explicit `assetId`, `providers`, and `fiat` to `RampsController:getQuotes` and persist the selected ramps quote on `TransactionFiatPayment` ([#8628](https://github.com/MetaMask/core/pull/8628))

## [20.2.0]

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
NATIVE_TOKEN_ADDRESS,
} from '../../constants';

export const DEFAULT_FIAT_CURRENCY = 'USD';

export type TransactionPayFiatAsset = {
address: Hex;
caipAssetId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { Hex } from '@metamask/utils';
import { TransactionPayStrategy } from '../../constants';
import type {
PayStrategyGetQuotesRequest,
TransactionFiatPayment,
TransactionPayQuote,
TransactionPayRequiredToken,
} from '../../types';
Expand All @@ -17,7 +18,7 @@ import { getRelayQuotes } from '../relay/relay-quotes';
import type { RelayQuote } from '../relay/types';
import type { TransactionPayFiatAsset } from './constants';
import { getFiatQuotes } from './fiat-quotes';
import { deriveFiatAssetForFiatPayment, pickBestFiatQuote } from './utils';
import { deriveFiatAssetForFiatPayment } from './utils';

jest.mock('../relay/relay-quotes');
jest.mock('../../utils/token');
Expand Down Expand Up @@ -67,6 +68,8 @@ const FIAT_QUOTE_MOCK: RampsQuote = {
},
};

const SELECTED_PROVIDER_ID = '/providers/transak-native-staging';

const FIAT_QUOTES_RESPONSE_MOCK: RampsQuotesResponse = {
customActions: [],
error: [],
Expand Down Expand Up @@ -123,47 +126,69 @@ function getRequest({
amountFiat = '10',
fiatPaymentMethod = '/payments/debit-credit-card',
rampsQuotes = FIAT_QUOTES_RESPONSE_MOCK,
selectedProviderId = SELECTED_PROVIDER_ID as string | null,
tokens = [REQUIRED_TOKEN_MOCK],
throwsOnRampsQuotes,
}: {
amountFiat?: string;
fiatPaymentMethod?: string;
rampsQuotes?: RampsQuotesResponse;
selectedProviderId?: string | null;
tokens?: TransactionPayRequiredToken[];
throwsOnRampsQuotes?: Error;
} = {}): {
callMock: jest.Mock;
request: PayStrategyGetQuotesRequest;
} {
const callMock = jest.fn((action: string) => {
if (action === 'TransactionPayController:getState') {
return {
transactionData: {
[TRANSACTION_ID]: {
fiatPayment: {
amountFiat,
const callMock = jest.fn(
(action: string, requestArg?: Record<string, unknown>) => {
if (action === 'TransactionPayController:getState') {
return {
transactionData: {
[TRANSACTION_ID]: {
fiatPayment: {
amountFiat,
},
isLoading: false,
tokens,
},
isLoading: false,
tokens,
},
},
};
}
};
}

if (action === 'RampsController:getQuotes') {
if (throwsOnRampsQuotes) {
throw throwsOnRampsQuotes;
if (action === 'RampsController:getState') {
return {
providers: {
selected: selectedProviderId ? { id: selectedProviderId } : null,
},
};
}

return rampsQuotes;
}
if (action === 'RampsController:getQuotes') {
if (throwsOnRampsQuotes) {
throw throwsOnRampsQuotes;
}

throw new Error(`Unexpected action: ${action}`);
});
return rampsQuotes;
}

if (action === 'TransactionPayController:updateFiatPayment') {
const { callback } = requestArg as unknown as {
callback: (fiatPayment: TransactionFiatPayment) => void;
};
const fiatPayment: TransactionFiatPayment = {};
callback(fiatPayment);
return undefined;
}

throw new Error(`Unexpected action: ${action}`);
},
);

return {
callMock,
request: {
accountSupports7702: false,
fiatPaymentMethod,
messenger: {
call: callMock,
Expand All @@ -181,7 +206,6 @@ describe('getFiatQuotes', () => {
const deriveFiatAssetForFiatPaymentMock = jest.mocked(
deriveFiatAssetForFiatPayment,
);
const pickBestFiatQuoteMock = jest.mocked(pickBestFiatQuote);

beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -193,7 +217,6 @@ describe('getFiatQuotes', () => {
});
computeRawFromFiatAmountMock.mockReturnValue('5000000000000000000');
getRelayQuotesMock.mockResolvedValue([getRelayQuoteMock()]);
pickBestFiatQuoteMock.mockReturnValue(FIAT_QUOTE_MOCK);
});

it('returns combined fiat quote and calls ramps with adjusted amount', async () => {
Expand All @@ -219,11 +242,22 @@ describe('getFiatQuotes', () => {
'RampsController:getQuotes',
expect.objectContaining({
amount: 20,
assetId: FIAT_ASSET_MOCK.caipAssetId,
fiat: 'USD',
paymentMethods: ['/payments/debit-credit-card'],
providers: [SELECTED_PROVIDER_ID],
walletAddress: WALLET_ADDRESS,
}),
);

expect(callMock).toHaveBeenCalledWith(
'TransactionPayController:updateFiatPayment',
expect.objectContaining({
callback: expect.any(Function),
transactionId: TRANSACTION_ID,
}),
);

expect(result).toHaveLength(1);
expect(result[0].strategy).toBe(TransactionPayStrategy.Fiat);
// provider = relay(1) + ramps(0.7) = 1.7
Expand Down Expand Up @@ -301,16 +335,15 @@ describe('getFiatQuotes', () => {
throw new Error(`Unexpected action: ${action}`);
});

const request: PayStrategyGetQuotesRequest = {
const result = await getFiatQuotes({
accountSupports7702: false,
fiatPaymentMethod: '/payments/debit-credit-card',
messenger: {
call: callMock,
} as unknown as PayStrategyGetQuotesRequest['messenger'],
requests: [],
transaction: TRANSACTION_MOCK,
};

const result = await getFiatQuotes(request);
});

expect(result).toStrictEqual([]);
expect(getRelayQuotesMock).not.toHaveBeenCalled();
Expand Down Expand Up @@ -425,17 +458,22 @@ describe('getFiatQuotes', () => {
);
});

it('returns empty array if preferred fiat quote is missing', async () => {
pickBestFiatQuoteMock.mockReturnValue(undefined);
const { request } = getRequest();
it('returns empty array if no quotes in success array', async () => {
const { request } = getRequest({
rampsQuotes: {
customActions: [],
error: [],
sorted: [],
success: [],
},
});

const result = await getFiatQuotes(request);

expect(result).toStrictEqual([]);
});

it('handles ramps response without success property', async () => {
pickBestFiatQuoteMock.mockReturnValue(undefined);
const { request } = getRequest({
rampsQuotes: {
customActions: [],
Expand All @@ -449,6 +487,72 @@ describe('getFiatQuotes', () => {
expect(result).toStrictEqual([]);
});

it('passes undefined providers when no provider is selected', async () => {
const { callMock, request } = getRequest({
selectedProviderId: null,
});

await getFiatQuotes(request);

expect(callMock).toHaveBeenCalledWith(
'RampsController:getQuotes',
expect.objectContaining({
providers: undefined,
}),
);
});

it('stores rampsQuote on fiat payment state via updateFiatPayment', async () => {
const fiatPaymentState: TransactionFiatPayment = {};
const callMock = jest.fn(
(action: string, requestArg?: Record<string, unknown>) => {
if (action === 'TransactionPayController:getState') {
return {
transactionData: {
[TRANSACTION_ID]: {
fiatPayment: { amountFiat: '10' },
isLoading: false,
tokens: [REQUIRED_TOKEN_MOCK],
},
},
};
}

if (action === 'RampsController:getState') {
return {
providers: { selected: { id: SELECTED_PROVIDER_ID } },
};
}

if (action === 'RampsController:getQuotes') {
return FIAT_QUOTES_RESPONSE_MOCK;
}

if (action === 'TransactionPayController:updateFiatPayment') {
const { callback } = requestArg as unknown as {
callback: (fp: TransactionFiatPayment) => void;
};
callback(fiatPaymentState);
return undefined;
}

throw new Error(`Unexpected action: ${action}`);
},
);

await getFiatQuotes({
accountSupports7702: false,
fiatPaymentMethod: '/payments/debit-credit-card',
messenger: {
call: callMock,
} as unknown as PayStrategyGetQuotesRequest['messenger'],
requests: [],
transaction: TRANSACTION_MOCK,
});

expect(fiatPaymentState.rampsQuote).toStrictEqual(FIAT_QUOTE_MOCK);
});

it('returns empty array if ramps quotes fetch throws', async () => {
const { request } = getRequest({
throwsOnRampsQuotes: new Error('ramps failed'),
Expand All @@ -474,15 +578,22 @@ describe('getFiatQuotes', () => {
});

it('sets providerFiat fee to zero when ramps provider/network fees are missing', async () => {
pickBestFiatQuoteMock.mockReturnValue({
const quoteWithoutFees: RampsQuote = {
provider: '/providers/transak-native-staging',
quote: {
amountIn: 20,
amountOut: 5,
paymentMethod: '/payments/debit-credit-card',
},
} as RampsQuote);
const { request } = getRequest();
};
const { request } = getRequest({
rampsQuotes: {
customActions: [],
error: [],
sorted: [],
success: [quoteWithoutFees],
},
});

const result = await getFiatQuotes(request);

Expand Down
Loading
Loading