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
5 changes: 5 additions & 0 deletions packages/transaction-pay-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- **BREAKING:** Include transactions in Relay quotes via EIP-7702 and delegation ([#7122](https://github.com/MetaMask/core/pull/7122))
- Requires new `getDelegationTransaction` constructor option.

### Fixed

- Read Relay provider fees directly from response ([#7098](https://github.com/MetaMask/core/pull/7098))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('TransactionPayController', () => {
*/
function createController() {
return new TransactionPayController({
getDelegationTransaction: jest.fn(),
messenger,
});
}
Expand Down Expand Up @@ -85,6 +86,7 @@ describe('TransactionPayController', () => {

it('returns callback value if provided', async () => {
new TransactionPayController({
getDelegationTransaction: jest.fn(),
getStrategy: async () => TransactionPayStrategy.Test,
messenger,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { updatePaymentToken } from './actions/update-payment-token';
import { CONTROLLER_NAME, TransactionPayStrategy } from './constants';
import { QuoteRefresher } from './helpers/QuoteRefresher';
import type {
GetDelegationTransactionCallback,
TransactionData,
TransactionPayControllerMessenger,
TransactionPayControllerOptions,
Expand Down Expand Up @@ -36,11 +37,14 @@ export class TransactionPayController extends BaseController<
TransactionPayControllerState,
TransactionPayControllerMessenger
> {
readonly #getDelegationTransaction: GetDelegationTransactionCallback;

readonly #getStrategy?: (
transaction: TransactionMeta,
) => Promise<TransactionPayStrategy>;

constructor({
getDelegationTransaction,
getStrategy,
messenger,
state,
Expand All @@ -52,6 +56,7 @@ export class TransactionPayController extends BaseController<
state: { ...getDefaultState(), ...state },
});

this.#getDelegationTransaction = getDelegationTransaction;
this.#getStrategy = getStrategy;

this.#registerActionHandlers();
Expand Down Expand Up @@ -127,6 +132,11 @@ export class TransactionPayController extends BaseController<
}

#registerActionHandlers() {
this.messenger.registerActionHandler(
'TransactionPayController:getDelegationTransaction',
this.#getDelegationTransaction.bind(this),
);

this.messenger.registerActionHandler(
'TransactionPayController:getStrategy',
this.#getStrategy ?? (async () => TransactionPayStrategy.Relay),
Expand Down
1 change: 1 addition & 0 deletions packages/transaction-pay-controller/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type {
TransactionPayControllerActions,
TransactionPayControllerEvents,
TransactionPayControllerGetDelegationTransactionAction,
TransactionPayControllerGetStateAction,
TransactionPayControllerGetStrategyAction,
TransactionPayControllerMessenger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const ARBITRUM_USDC_ADDRESS =
'0xaf88d065e77c8cC2239327C5EDb3A432268e5831';
export const CHAIN_ID_ARBITRUM = '0xa4b1';
export const CHAIN_ID_POLYGON = '0x89';
export const CHAIN_ID_HYPERCORE = '0x539';
export const RELAY_URL_BASE = 'https://api.relay.link';
export const RELAY_URL_QUOTE = `${RELAY_URL_BASE}/quote`;
export const RELAY_FALLBACK_GAS_LIMIT = 900000;
export const RELAY_POLLING_INTERVAL = 1000; // 1 Second
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { successfulFetch } from '@metamask/controller-utils';
import type { TransactionMeta } from '@metamask/transaction-controller';
import type { Hex } from '@metamask/utils';
import { cloneDeep } from 'lodash';

import {
Expand All @@ -12,7 +13,10 @@ import { getRelayQuotes } from './relay-quotes';
import type { RelayQuote } from './types';
import { NATIVE_TOKEN_ADDRESS } from '../../constants';
import { getMessengerMock } from '../../tests/messenger-mock';
import type { QuoteRequest } from '../../types';
import type {
GetDelegationTransactionCallback,
QuoteRequest,
} from '../../types';
import { calculateGasCost, calculateTransactionGasCost } from '../../utils/gas';
import { getNativeToken, getTokenFiatRate } from '../../utils/token';

Expand All @@ -25,14 +29,14 @@ jest.mock('@metamask/controller-utils', () => ({
}));

const QUOTE_REQUEST_MOCK: QuoteRequest = {
from: '0x123',
from: '0x1234567890123456789012345678901234567891',
sourceBalanceRaw: '10000000000000000000',
sourceChainId: '0x1',
sourceTokenAddress: '0xabc',
sourceTokenAmount: '1000000000000000000',
targetAmountMinimum: '123',
targetChainId: '0x2',
targetTokenAddress: '0xdef',
targetTokenAddress: '0x1234567890123456789012345678901234567890',
};

const QUOTE_MOCK = {
Expand Down Expand Up @@ -62,12 +66,12 @@ const QUOTE_MOCK = {
},
data: {
chainId: 1,
data: '0x123',
from: '0x1',
data: '0x123' as Hex,
from: '0x1' as Hex,
gas: '21000',
maxFeePerGas: '1000000000',
maxPriorityFeePerGas: '2000000000',
to: '0x2',
to: '0x2' as Hex,
value: '300000',
},
status: 'complete',
Expand All @@ -78,7 +82,20 @@ const QUOTE_MOCK = {
],
} as RelayQuote;

const TRANSACTION_META_MOCK = {} as TransactionMeta;
const TRANSACTION_META_MOCK = { txParams: {} } as TransactionMeta;

const DELEGATION_RESULT_MOCK = {
authorizationList: [
{
chainId: '0x1' as Hex,
nonce: '0x2' as Hex,
yParity: '0x1' as Hex,
},
],
data: '0x111' as Hex,
to: '0x222' as Hex,
value: '0x333' as Hex,
} as Awaited<ReturnType<GetDelegationTransactionCallback>>;

describe('Relay Quotes Utils', () => {
const successfulFetchMock = jest.mocked(successfulFetch);
Expand All @@ -90,8 +107,11 @@ describe('Relay Quotes Utils', () => {
calculateTransactionGasCost,
);

const { messenger, getRemoteFeatureFlagControllerStateMock } =
getMessengerMock();
const {
messenger,
getDelegationTransactionMock,
getRemoteFeatureFlagControllerStateMock,
} = getMessengerMock();

beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -115,6 +135,8 @@ describe('Relay Quotes Utils', () => {
cacheTimestamp: 0,
remoteFeatureFlags: {},
});

getDelegationTransactionMock.mockResolvedValue(DELEGATION_RESULT_MOCK);
});

describe('getRelayQuotes', () => {
Expand Down Expand Up @@ -164,6 +186,52 @@ describe('Relay Quotes Utils', () => {
);
});

it('includes transactions in request', async () => {
successfulFetchMock.mockResolvedValue({
json: async () => QUOTE_MOCK,
} as never);

await getRelayQuotes({
messenger,
requests: [QUOTE_REQUEST_MOCK],
transaction: {
...TRANSACTION_META_MOCK,
txParams: {
data: '0xabc' as Hex,
},
} as TransactionMeta,
});

const body = JSON.parse(
successfulFetchMock.mock.calls[0][1]?.body as string,
);

expect(body).toStrictEqual(
expect.objectContaining({
authorizationList: [
{
chainId: 1,
nonce: 2,
yParity: 1,
},
],
tradeType: 'EXACT_OUTPUT',
txs: [
{
to: QUOTE_REQUEST_MOCK.targetTokenAddress,
data: '0xa9059cbb0000000000000000000000001234567890123456789012345678901234567891000000000000000000000000000000000000000000000000000000000000007b',
value: '0x0',
},
{
to: DELEGATION_RESULT_MOCK.to,
data: DELEGATION_RESULT_MOCK.data,
value: DELEGATION_RESULT_MOCK.value,
},
],
}),
);
});

it('sends request to url from feature flag', async () => {
successfulFetchMock.mockResolvedValue({
json: async () => QUOTE_MOCK,
Expand Down Expand Up @@ -325,8 +393,8 @@ describe('Relay Quotes Utils', () => {
});

expect(result[0].fees.targetNetwork).toStrictEqual({
usd: '1.23',
fiat: '2.34',
usd: '0',
fiat: '0',
});
});

Expand Down
Loading
Loading