|
| 1 | +# Architecture |
| 2 | + |
| 3 | +The ultimate purpose of the `TransactionPayController` is to automatically transfer ERC-20 tokens to the appropriate chains and accounts, in order to enable and simplify EVM transactions. |
| 4 | + |
| 5 | +This functionality is referred to as `MetaMask Pay` in the MetaMask clients. |
| 6 | + |
| 7 | +## Required Tokens |
| 8 | + |
| 9 | +The ERC-20 tokens required by a transaction are automatically identified from various sources, this includes: |
| 10 | + |
| 11 | +- ERC-20 Token Transfers |
| 12 | + - Identified from the call data using the `0xa9059cbb` four-byte prefix. |
| 13 | + - Supports EIP-7702 transactions where the token transfer is a nested call. |
| 14 | +- Gas Fees |
| 15 | + - A required native token is generated from the gas limit and gas fee parameters, including estimates from the `GasFeeController`. |
| 16 | + |
| 17 | +## Pay Strategies |
| 18 | + |
| 19 | +The mechanism by which the tokens are moved is abstracted into a `PayStrategy`. |
| 20 | + |
| 21 | +Each `PayStrategy` dictates how the `quotes` are retrieved, which detail the associated fees and strategy specific data, and how those quotes are actioned or "submitted". |
| 22 | + |
| 23 | +### Bridge |
| 24 | + |
| 25 | +The `BridgeStrategy` retrieves quotes from the MetaMask Bridge API via the `BridgeController`, and submits quotes via the `BridgeStatusController`. |
| 26 | + |
| 27 | +The `BridgeStatusController` generates suitable transactions via the `TransactionController` that target the MetaMask Bridge contract which in turn communicates with a specific Bridge provider. |
| 28 | + |
| 29 | +### Relay |
| 30 | + |
| 31 | +The `RelayStrategy` retrieves quotes from the [Relay API](https://docs.relay.link/what-is-relay), then submits the necessary transactions directly to the `TransactionController`. |
| 32 | + |
| 33 | +The resulting transaction deposits the necessary funds (on the source network), then a Relayer on the target chain immediately transfers the necessary funds and optionally executes any requested call data. |
| 34 | + |
| 35 | +## Client Lifecycle |
| 36 | + |
| 37 | +The high level interaction with the `TransactionPayController` is as follows: |
| 38 | + |
| 39 | +1. Controller subscribes to `TranasctionController` state changes during initialisation. |
| 40 | +2. An unapproved transaction is created in a MetaMask client, either internally or via a dApp. |
| 41 | +3. Controller identifies any required tokens and adds them to its state. |
| 42 | +4. If a client confirmation is using `MetaMask Pay`, the user selects a payment token (or it is done automatically) which invokes the `updatePaymentToken` action. |
| 43 | +5. Controllers selects an appropriate `PayStrategy` using the `getStrategy` action. |
| 44 | +6. Controller requests quotes from the `PayStrategy` and persists them in state, including associated totals. |
| 45 | +7. Resulting fees and totals are presented in the client transaction confirmation. |
| 46 | +8. If approved by the user, the target transaction is signed and published. |
| 47 | +9. The `TransactionPayPublishHook` is invoked as this is specified by the client during `TransactionController` initialisation. |
| 48 | +10. Publish hook submits the quotes via the same `PayStrategy`. |
| 49 | +11. Depending on the pay strategy and required tokens, the original target transaction is also published as the required funds are now in place on the user's account on the target chain. |
| 50 | +12. Target transaction is finalized and relevant controller state is removed. |
| 51 | + |
| 52 | +## State |
| 53 | + |
| 54 | +State is grouped according to the associated transaction ID in the `transactionData` property. |
| 55 | + |
| 56 | +This transaction specific data includes any required tokens, selected payment token, retrieved quotes, and calculated totals. |
0 commit comments