@metamask-previews/transaction-pay-middleware 0.0.0-stage → 1.0.0-preview-55fcf3ef7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/LICENSE +6 -0
- package/LICENSE.APACHE2 +201 -0
- package/LICENSE.MIT +21 -0
- package/README.md +85 -2
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +4 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors.d.ts +12 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +24 -0
- package/dist/errors.js.map +1 -0
- package/dist/flows/index.d.ts +9 -0
- package/dist/flows/index.d.ts.map +1 -0
- package/dist/flows/index.js +19 -0
- package/dist/flows/index.js.map +1 -0
- package/dist/flows/perps/deposit.d.ts +8 -0
- package/dist/flows/perps/deposit.d.ts.map +1 -0
- package/dist/flows/perps/deposit.js +26 -0
- package/dist/flows/perps/deposit.js.map +1 -0
- package/dist/flows/perps/shared.d.ts +16 -0
- package/dist/flows/perps/shared.d.ts.map +1 -0
- package/dist/flows/perps/shared.js +33 -0
- package/dist/flows/perps/shared.js.map +1 -0
- package/dist/flows/perps/withdraw.d.ts +9 -0
- package/dist/flows/perps/withdraw.d.ts.map +1 -0
- package/dist/flows/perps/withdraw.js +28 -0
- package/dist/flows/perps/withdraw.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/methods/wallet_pay.d.ts +14 -0
- package/dist/methods/wallet_pay.d.ts.map +1 -0
- package/dist/methods/wallet_pay.js +79 -0
- package/dist/methods/wallet_pay.js.map +1 -0
- package/dist/types.d.ts +43 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/feature-flags.d.ts +36 -0
- package/dist/utils/feature-flags.d.ts.map +1 -0
- package/dist/utils/feature-flags.js +79 -0
- package/dist/utils/feature-flags.js.map +1 -0
- package/package.json +76 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [1.0.0]
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- Initial release ([#10786](https://github.com/MetaMask/core/pull/10786))
|
|
15
|
+
- Add `walletPay`, the handler for the `wallet_pay` JSON-RPC method, which lets a connected dapp request a MetaMask Pay transaction and resolves with its `transactionHash`.
|
|
16
|
+
- Support the `perpsDeposit` and `perpsWithdraw` types.
|
|
17
|
+
- Gate each type per site with the `confirmations_pay_rpc` remote feature flag.
|
|
18
|
+
- Add the `TransactionPayMiddlewareMessenger`, `WalletPayHooks`, `WalletPayJsonRpcRequest` and `WalletPayResult` types.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- Bump `@metamask/network-controller` from `^37.1.0` to `^37.1.1` ([#10982](https://github.com/MetaMask/core/pull/10982))
|
|
23
|
+
- Bump `@metamask/perps-controller` from `^20.1.0` to `^21.0.0` ([#10988](https://github.com/MetaMask/core/pull/10988))
|
|
24
|
+
|
|
25
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/transaction-pay-middleware@1.0.0...HEAD
|
|
26
|
+
[1.0.0]: https://github.com/MetaMask/core/releases/tag/@metamask/transaction-pay-middleware@1.0.0
|
package/LICENSE
ADDED
package/LICENSE.APACHE2
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/LICENSE.MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 MetaMask
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@metamask/transaction-pay-middleware`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Implements the `wallet_pay` JSON-RPC method, which lets dapps request MetaMask Pay transactions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
`yarn add @metamask/transaction-pay-middleware`
|
|
8
|
+
|
|
9
|
+
or
|
|
10
|
+
|
|
11
|
+
`npm install @metamask/transaction-pay-middleware`
|
|
12
|
+
|
|
13
|
+
## JSON-RPC Methods
|
|
14
|
+
|
|
15
|
+
### wallet_pay
|
|
16
|
+
|
|
17
|
+
Asks the user to confirm a MetaMask Pay transaction of the given type, and resolves once the transfer completes.
|
|
18
|
+
|
|
19
|
+
**Parameters:**
|
|
20
|
+
|
|
21
|
+
- `type` (string): The transaction type. Supported: `perpsDeposit`, `perpsWithdraw`.
|
|
22
|
+
- `from` (string): The account to use. Must be connected to the requesting site.
|
|
23
|
+
- `amount` (string, optional): The amount to prefill, as a decimal string with up to 6 decimals (for example `"10"` or `"12.5"`).
|
|
24
|
+
|
|
25
|
+
Unknown fields are rejected.
|
|
26
|
+
|
|
27
|
+
**Returns:**
|
|
28
|
+
|
|
29
|
+
- `transactionHash` (string, optional): The hash to monitor. Omitted when the transfer completes without one.
|
|
30
|
+
|
|
31
|
+
**Example:**
|
|
32
|
+
|
|
33
|
+
```json
|
|
34
|
+
{
|
|
35
|
+
"method": "wallet_pay",
|
|
36
|
+
"params": [
|
|
37
|
+
{
|
|
38
|
+
"type": "perpsDeposit",
|
|
39
|
+
"from": "0x1234567890123456789012345678901234567890",
|
|
40
|
+
"amount": "10"
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**Errors:**
|
|
47
|
+
|
|
48
|
+
| Code | When |
|
|
49
|
+
| -------- | --------------------------------------------------------------------------------------- |
|
|
50
|
+
| `-32602` | The params are invalid. The message names each failing field. |
|
|
51
|
+
| `-32601` | The type is unknown, or not enabled for the requesting site. |
|
|
52
|
+
| `4100` | `from` is not connected to the site. |
|
|
53
|
+
| `-32004` | The user is not eligible for perps in their region (`data.reason: "perpsNotEligible"`). |
|
|
54
|
+
| `4001` | The user rejected the request. |
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
`walletPay` is a JSON-RPC method handler. The client supplies a hook for the requesting site's connected accounts, and a messenger for the controllers it uses:
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { walletPay } from '@metamask/transaction-pay-middleware';
|
|
62
|
+
|
|
63
|
+
const middleware = createScaffoldMiddleware({
|
|
64
|
+
wallet_pay: createAsyncMiddleware(async (req, res) => {
|
|
65
|
+
await walletPay(
|
|
66
|
+
req,
|
|
67
|
+
res,
|
|
68
|
+
{
|
|
69
|
+
getPermittedAccountsForOrigin: async () =>
|
|
70
|
+
getPermittedAccounts(req.origin),
|
|
71
|
+
},
|
|
72
|
+
transactionPayMiddlewareMessenger,
|
|
73
|
+
);
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The messenger must allow:
|
|
79
|
+
|
|
80
|
+
- `RemoteFeatureFlagController:getState`: `wallet_pay` is enabled per site and per type by the `confirmations_pay_rpc` flag, and `perpsWithdraw` also requires the in-wallet withdraw flag, `confirmations_pay_post_quote`.
|
|
81
|
+
- `TransactionController:addTransaction` and `NetworkController:findNetworkClientIdByChainId`: the transaction is added for the requesting site. Its chain must already be configured; the dapp is expected to add it first.
|
|
82
|
+
- `PerpsController:getState` and `PerpsController:refreshEligibility`: perps types check the user's eligibility.
|
|
83
|
+
|
|
84
|
+
## Contributing
|
|
85
|
+
|
|
86
|
+
This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;aACvB,UAAU,EAAE,YAAY;CAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,YAAY,GAAG;IAC1B,UAAU,EAAE,YAAY;CAChB,CAAC","sourcesContent":["export const METHOD_NAMES = {\n WALLET_PAY: 'wallet_pay',\n} as const;\n"]}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { JsonRpcError } from '@metamask/rpc-errors';
|
|
2
|
+
/** Every error `wallet_pay` returns, besides ones from adding the transaction. */
|
|
3
|
+
export declare const walletPayErrors: {
|
|
4
|
+
invalidParams: (details: string) => JsonRpcError<never>;
|
|
5
|
+
unsupportedType: (type: string) => JsonRpcError<never>;
|
|
6
|
+
unauthorizedAccount: () => JsonRpcError<never>;
|
|
7
|
+
networkUnavailable: (chainId: string) => JsonRpcError<never>;
|
|
8
|
+
perpsNotEligible: () => JsonRpcError<{
|
|
9
|
+
reason: string;
|
|
10
|
+
}>;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAOzD,kFAAkF;AAClF,eAAO,MAAM,eAAe;IAC1B,aAAa,YAAY,MAAM,KAAG,YAAY,CAAC,KAAK,CAAC;IAKrD,eAAe,SAAS,MAAM,KAAG,YAAY,CAAC,KAAK,CAAC;IAKpD,mBAAmB,QAAM,YAAY,CAAC,KAAK,CAAC;IAK5C,kBAAkB,YAAY,MAAM,KAAG,YAAY,CAAC,KAAK,CAAC;IAM1D,gBAAgB,QAAM,YAAY,CAAC;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;CAKvD,CAAC"}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { providerErrors, rpcErrors } from '@metamask/rpc-errors';
|
|
2
|
+
import { METHOD_NAMES } from './constants.js';
|
|
3
|
+
const PREFIX = `${METHOD_NAMES.WALLET_PAY}:`;
|
|
4
|
+
/** Every error `wallet_pay` returns, besides ones from adding the transaction. */
|
|
5
|
+
export const walletPayErrors = {
|
|
6
|
+
invalidParams: (details) => rpcErrors.invalidParams({
|
|
7
|
+
message: `${PREFIX} invalid params.\n\n${details}`,
|
|
8
|
+
}),
|
|
9
|
+
unsupportedType: (type) => rpcErrors.methodNotFound({
|
|
10
|
+
message: `${PREFIX} type "${type}" is not available.`,
|
|
11
|
+
}),
|
|
12
|
+
unauthorizedAccount: () => providerErrors.unauthorized({
|
|
13
|
+
message: `${PREFIX} "from" is not an account connected to this site.`,
|
|
14
|
+
}),
|
|
15
|
+
networkUnavailable: (chainId) => rpcErrors.internal({
|
|
16
|
+
message: `${PREFIX} network ${chainId} is not configured.`,
|
|
17
|
+
}),
|
|
18
|
+
// Provisional code; to be confirmed with the Perps team before release.
|
|
19
|
+
perpsNotEligible: () => rpcErrors.methodNotSupported({
|
|
20
|
+
message: `${PREFIX} perps is not available in your region.`,
|
|
21
|
+
data: { reason: 'perpsNotEligible' },
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjE,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,MAAM,GAAG,GAAG,YAAY,CAAC,UAAU,GAAG,CAAC;AAE7C,kFAAkF;AAClF,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,aAAa,EAAE,CAAC,OAAe,EAAuB,EAAE,CACtD,SAAS,CAAC,aAAa,CAAC;QACtB,OAAO,EAAE,GAAG,MAAM,uBAAuB,OAAO,EAAE;KACnD,CAAC;IAEJ,eAAe,EAAE,CAAC,IAAY,EAAuB,EAAE,CACrD,SAAS,CAAC,cAAc,CAAC;QACvB,OAAO,EAAE,GAAG,MAAM,UAAU,IAAI,qBAAqB;KACtD,CAAC;IAEJ,mBAAmB,EAAE,GAAwB,EAAE,CAC7C,cAAc,CAAC,YAAY,CAAC;QAC1B,OAAO,EAAE,GAAG,MAAM,mDAAmD;KACtE,CAAC;IAEJ,kBAAkB,EAAE,CAAC,OAAe,EAAuB,EAAE,CAC3D,SAAS,CAAC,QAAQ,CAAC;QACjB,OAAO,EAAE,GAAG,MAAM,YAAY,OAAO,qBAAqB;KAC3D,CAAC;IAEJ,wEAAwE;IACxE,gBAAgB,EAAE,GAAqC,EAAE,CACvD,SAAS,CAAC,kBAAkB,CAAC;QAC3B,OAAO,EAAE,GAAG,MAAM,yCAAyC;QAC3D,IAAI,EAAE,EAAE,MAAM,EAAE,kBAAkB,EAAE;KACrC,CAAC;CACL,CAAC","sourcesContent":["import type { JsonRpcError } from '@metamask/rpc-errors';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\n\nimport { METHOD_NAMES } from './constants.js';\n\nconst PREFIX = `${METHOD_NAMES.WALLET_PAY}:`;\n\n/** Every error `wallet_pay` returns, besides ones from adding the transaction. */\nexport const walletPayErrors = {\n invalidParams: (details: string): JsonRpcError<never> =>\n rpcErrors.invalidParams({\n message: `${PREFIX} invalid params.\\n\\n${details}`,\n }),\n\n unsupportedType: (type: string): JsonRpcError<never> =>\n rpcErrors.methodNotFound({\n message: `${PREFIX} type \"${type}\" is not available.`,\n }),\n\n unauthorizedAccount: (): JsonRpcError<never> =>\n providerErrors.unauthorized({\n message: `${PREFIX} \"from\" is not an account connected to this site.`,\n }),\n\n networkUnavailable: (chainId: string): JsonRpcError<never> =>\n rpcErrors.internal({\n message: `${PREFIX} network ${chainId} is not configured.`,\n }),\n\n // Provisional code; to be confirmed with the Perps team before release.\n perpsNotEligible: (): JsonRpcError<{ reason: string }> =>\n rpcErrors.methodNotSupported({\n message: `${PREFIX} perps is not available in your region.`,\n data: { reason: 'perpsNotEligible' },\n }),\n};\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WalletPayFlow } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns the flow for a transaction type, ignoring inherited object keys.
|
|
4
|
+
*
|
|
5
|
+
* @param transactionType - The requested transaction type.
|
|
6
|
+
* @returns The flow, or `undefined` if the type isn't supported.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getWalletPayFlow(transactionType: string): WalletPayFlow | undefined;
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/flows/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AASjD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,eAAe,EAAE,MAAM,GACtB,aAAa,GAAG,SAAS,CAI3B"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { TransactionType } from '@metamask/transaction-controller';
|
|
2
|
+
import { perpsDepositFlow } from './perps/deposit.js';
|
|
3
|
+
import { perpsWithdrawFlow } from './perps/withdraw.js';
|
|
4
|
+
const WALLET_PAY_FLOWS = {
|
|
5
|
+
[TransactionType.perpsDeposit]: perpsDepositFlow,
|
|
6
|
+
[TransactionType.perpsWithdraw]: perpsWithdrawFlow,
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Returns the flow for a transaction type, ignoring inherited object keys.
|
|
10
|
+
*
|
|
11
|
+
* @param transactionType - The requested transaction type.
|
|
12
|
+
* @returns The flow, or `undefined` if the type isn't supported.
|
|
13
|
+
*/
|
|
14
|
+
export function getWalletPayFlow(transactionType) {
|
|
15
|
+
return Object.hasOwn(WALLET_PAY_FLOWS, transactionType)
|
|
16
|
+
? WALLET_PAY_FLOWS[transactionType]
|
|
17
|
+
: undefined;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/flows/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAGnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,MAAM,gBAAgB,GAAkC;IACtD,CAAC,eAAe,CAAC,YAAY,CAAC,EAAE,gBAAgB;IAChD,CAAC,eAAe,CAAC,aAAa,CAAC,EAAE,iBAAiB;CACnD,CAAC;AAEF;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAC9B,eAAuB;IAEvB,OAAO,MAAM,CAAC,MAAM,CAAC,gBAAgB,EAAE,eAAe,CAAC;QACrD,CAAC,CAAC,gBAAgB,CAAC,eAAe,CAAC;QACnC,CAAC,CAAC,SAAS,CAAC;AAChB,CAAC","sourcesContent":["import { TransactionType } from '@metamask/transaction-controller';\n\nimport type { WalletPayFlow } from '../types.js';\nimport { perpsDepositFlow } from './perps/deposit.js';\nimport { perpsWithdrawFlow } from './perps/withdraw.js';\n\nconst WALLET_PAY_FLOWS: Record<string, WalletPayFlow> = {\n [TransactionType.perpsDeposit]: perpsDepositFlow,\n [TransactionType.perpsWithdraw]: perpsWithdrawFlow,\n};\n\n/**\n * Returns the flow for a transaction type, ignoring inherited object keys.\n *\n * @param transactionType - The requested transaction type.\n * @returns The flow, or `undefined` if the type isn't supported.\n */\nexport function getWalletPayFlow(\n transactionType: string,\n): WalletPayFlow | undefined {\n return Object.hasOwn(WALLET_PAY_FLOWS, transactionType)\n ? WALLET_PAY_FLOWS[transactionType]\n : undefined;\n}\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { WalletPayFlow } from '../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* `perpsDeposit`: a USDC transfer to the Hyperliquid bridge on Arbitrum,
|
|
4
|
+
* matching the in-wallet deposit. Uses the mainnet route directly, while the
|
|
5
|
+
* in-wallet flow takes it from the active perps provider.
|
|
6
|
+
*/
|
|
7
|
+
export declare const perpsDepositFlow: WalletPayFlow;
|
|
8
|
+
//# sourceMappingURL=deposit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deposit.d.ts","sourceRoot":"","sources":["../../../src/flows/perps/deposit.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAGpD;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,EAAE,aAiB9B,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { toHex } from '@metamask/controller-utils';
|
|
2
|
+
import { ARBITRUM_MAINNET_CHAIN_ID_HEX, DEPOSIT_CONFIG, HYPERLIQUID_BRIDGE_CONTRACTS, USDC_ARBITRUM_MAINNET_ADDRESS, } from '@metamask/perps-controller/constants/hyperLiquidConfig';
|
|
3
|
+
import { generateERC20TransferData } from '@metamask/perps-controller/utils/transferData';
|
|
4
|
+
import { TransactionType } from '@metamask/transaction-controller';
|
|
5
|
+
import { assertPerpsEligible, toUsdcAmountRaw } from './shared.js';
|
|
6
|
+
/**
|
|
7
|
+
* `perpsDeposit`: a USDC transfer to the Hyperliquid bridge on Arbitrum,
|
|
8
|
+
* matching the in-wallet deposit. Uses the mainnet route directly, while the
|
|
9
|
+
* in-wallet flow takes it from the active perps provider.
|
|
10
|
+
*/
|
|
11
|
+
export const perpsDepositFlow = {
|
|
12
|
+
assertPreconditions: assertPerpsEligible,
|
|
13
|
+
build: ({ from, amount }) => ({
|
|
14
|
+
chainId: ARBITRUM_MAINNET_CHAIN_ID_HEX,
|
|
15
|
+
transactionParams: {
|
|
16
|
+
from,
|
|
17
|
+
to: USDC_ARBITRUM_MAINNET_ADDRESS,
|
|
18
|
+
value: '0x0',
|
|
19
|
+
data: generateERC20TransferData(HYPERLIQUID_BRIDGE_CONTRACTS.mainnet.contractAddress, toUsdcAmountRaw(amount)),
|
|
20
|
+
gas: toHex(DEPOSIT_CONFIG.EstimatedGasLimit),
|
|
21
|
+
},
|
|
22
|
+
type: TransactionType.perpsDeposit,
|
|
23
|
+
skipInitialGasEstimate: true,
|
|
24
|
+
}),
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=deposit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deposit.js","sourceRoot":"","sources":["../../../src/flows/perps/deposit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AACnD,OAAO,EACL,6BAA6B,EAC7B,cAAc,EACd,4BAA4B,EAC5B,6BAA6B,GAC9B,MAAM,wDAAwD,CAAC;AAChE,OAAO,EAAE,yBAAyB,EAAE,MAAM,+CAA+C,CAAC;AAC1F,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAGnE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnE;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAkB;IAC7C,mBAAmB,EAAE,mBAAmB;IACxC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;QAC5B,OAAO,EAAE,6BAA6B;QACtC,iBAAiB,EAAE;YACjB,IAAI;YACJ,EAAE,EAAE,6BAA6B;YACjC,KAAK,EAAE,KAAK;YACZ,IAAI,EAAE,yBAAyB,CAC7B,4BAA4B,CAAC,OAAO,CAAC,eAAe,EACpD,eAAe,CAAC,MAAM,CAAC,CACxB;YACD,GAAG,EAAE,KAAK,CAAC,cAAc,CAAC,iBAAiB,CAAC;SAC7C;QACD,IAAI,EAAE,eAAe,CAAC,YAAY;QAClC,sBAAsB,EAAE,IAAI;KAC7B,CAAC;CACH,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport {\n ARBITRUM_MAINNET_CHAIN_ID_HEX,\n DEPOSIT_CONFIG,\n HYPERLIQUID_BRIDGE_CONTRACTS,\n USDC_ARBITRUM_MAINNET_ADDRESS,\n} from '@metamask/perps-controller/constants/hyperLiquidConfig';\nimport { generateERC20TransferData } from '@metamask/perps-controller/utils/transferData';\nimport { TransactionType } from '@metamask/transaction-controller';\n\nimport type { WalletPayFlow } from '../../types.js';\nimport { assertPerpsEligible, toUsdcAmountRaw } from './shared.js';\n\n/**\n * `perpsDeposit`: a USDC transfer to the Hyperliquid bridge on Arbitrum,\n * matching the in-wallet deposit. Uses the mainnet route directly, while the\n * in-wallet flow takes it from the active perps provider.\n */\nexport const perpsDepositFlow: WalletPayFlow = {\n assertPreconditions: assertPerpsEligible,\n build: ({ from, amount }) => ({\n chainId: ARBITRUM_MAINNET_CHAIN_ID_HEX,\n transactionParams: {\n from,\n to: USDC_ARBITRUM_MAINNET_ADDRESS,\n value: '0x0',\n data: generateERC20TransferData(\n HYPERLIQUID_BRIDGE_CONTRACTS.mainnet.contractAddress,\n toUsdcAmountRaw(amount),\n ),\n gas: toHex(DEPOSIT_CONFIG.EstimatedGasLimit),\n },\n type: TransactionType.perpsDeposit,\n skipInitialGasEstimate: true,\n }),\n};\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Hex } from '@metamask/utils';
|
|
2
|
+
import type { TransactionPayMiddlewareMessenger } from '../../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Converts a USDC amount to its raw hex value.
|
|
5
|
+
*
|
|
6
|
+
* @param amount - The decimal amount, or `undefined` for no amount.
|
|
7
|
+
* @returns The raw amount, or `0x0` when no amount is given.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toUsdcAmountRaw(amount: string | undefined): Hex;
|
|
10
|
+
/**
|
|
11
|
+
* Throws the geo-block error when the user isn't eligible for perps.
|
|
12
|
+
*
|
|
13
|
+
* @param messenger - The middleware messenger.
|
|
14
|
+
*/
|
|
15
|
+
export declare function assertPerpsEligible(messenger: TransactionPayMiddlewareMessenger): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../src/flows/perps/shared.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAI3C,OAAO,KAAK,EAAE,iCAAiC,EAAE,MAAM,gBAAgB,CAAC;AAExE;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,GAAG,CAM/D;AAED;;;;GAIG;AACH,wBAAsB,mBAAmB,CACvC,SAAS,EAAE,iCAAiC,GAC3C,OAAO,CAAC,IAAI,CAAC,CAYf"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { USDC_DECIMALS } from '@metamask/perps-controller/constants/hyperLiquidConfig';
|
|
2
|
+
import { add0x } from '@metamask/utils';
|
|
3
|
+
import { BigNumber } from 'bignumber.js';
|
|
4
|
+
import { walletPayErrors } from '../../errors.js';
|
|
5
|
+
/**
|
|
6
|
+
* Converts a USDC amount to its raw hex value.
|
|
7
|
+
*
|
|
8
|
+
* @param amount - The decimal amount, or `undefined` for no amount.
|
|
9
|
+
* @returns The raw amount, or `0x0` when no amount is given.
|
|
10
|
+
*/
|
|
11
|
+
export function toUsdcAmountRaw(amount) {
|
|
12
|
+
if (amount === undefined) {
|
|
13
|
+
return '0x0';
|
|
14
|
+
}
|
|
15
|
+
return add0x(new BigNumber(amount).shiftedBy(USDC_DECIMALS).toString(16));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Throws the geo-block error when the user isn't eligible for perps.
|
|
19
|
+
*
|
|
20
|
+
* @param messenger - The middleware messenger.
|
|
21
|
+
*/
|
|
22
|
+
export async function assertPerpsEligible(messenger) {
|
|
23
|
+
if (messenger.call('PerpsController:getState').isEligible) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// `isEligible` starts as false until the first check completes, so refresh
|
|
27
|
+
// once before blocking.
|
|
28
|
+
await messenger.call('PerpsController:refreshEligibility');
|
|
29
|
+
if (!messenger.call('PerpsController:getState').isEligible) {
|
|
30
|
+
throw walletPayErrors.perpsNotEligible();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../../../src/flows/perps/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,wDAAwD,CAAC;AACvF,OAAO,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAGlD;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAC,MAA0B;IACxD,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,SAAS,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AAC5E,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,SAA4C;IAE5C,IAAI,SAAS,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC,UAAU,EAAE,CAAC;QAC1D,OAAO;IACT,CAAC;IAED,2EAA2E;IAC3E,wBAAwB;IACxB,MAAM,SAAS,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAC;IAE3D,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC,UAAU,EAAE,CAAC;QAC3D,MAAM,eAAe,CAAC,gBAAgB,EAAE,CAAC;IAC3C,CAAC;AACH,CAAC","sourcesContent":["import { USDC_DECIMALS } from '@metamask/perps-controller/constants/hyperLiquidConfig';\nimport { add0x } from '@metamask/utils';\nimport type { Hex } from '@metamask/utils';\nimport { BigNumber } from 'bignumber.js';\n\nimport { walletPayErrors } from '../../errors.js';\nimport type { TransactionPayMiddlewareMessenger } from '../../types.js';\n\n/**\n * Converts a USDC amount to its raw hex value.\n *\n * @param amount - The decimal amount, or `undefined` for no amount.\n * @returns The raw amount, or `0x0` when no amount is given.\n */\nexport function toUsdcAmountRaw(amount: string | undefined): Hex {\n if (amount === undefined) {\n return '0x0';\n }\n\n return add0x(new BigNumber(amount).shiftedBy(USDC_DECIMALS).toString(16));\n}\n\n/**\n * Throws the geo-block error when the user isn't eligible for perps.\n *\n * @param messenger - The middleware messenger.\n */\nexport async function assertPerpsEligible(\n messenger: TransactionPayMiddlewareMessenger,\n): Promise<void> {\n if (messenger.call('PerpsController:getState').isEligible) {\n return;\n }\n\n // `isEligible` starts as false until the first check completes, so refresh\n // once before blocking.\n await messenger.call('PerpsController:refreshEligibility');\n\n if (!messenger.call('PerpsController:getState').isEligible) {\n throw walletPayErrors.perpsNotEligible();\n }\n}\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WalletPayFlow } from '../../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* `perpsWithdraw`: the same placeholder USDC transfer to `from` the in-wallet
|
|
4
|
+
* withdraw uses; the confirmation and Pay turn it into the Hyperliquid
|
|
5
|
+
* withdraw. Gas isn't estimated upfront: the funds are on Hyperliquid, so the
|
|
6
|
+
* placeholder transfer would fail to estimate.
|
|
7
|
+
*/
|
|
8
|
+
export declare const perpsWithdrawFlow: WalletPayFlow;
|
|
9
|
+
//# sourceMappingURL=withdraw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../../../src/flows/perps/withdraw.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAIpD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,EAAE,aAgB/B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ARBITRUM_MAINNET_CHAIN_ID_HEX, USDC_ARBITRUM_MAINNET_ADDRESS, } from '@metamask/perps-controller/constants/hyperLiquidConfig';
|
|
2
|
+
import { generateERC20TransferData } from '@metamask/perps-controller/utils/transferData';
|
|
3
|
+
import { TransactionType } from '@metamask/transaction-controller';
|
|
4
|
+
import { isPayFlowEnabled } from '../../utils/feature-flags.js';
|
|
5
|
+
import { assertPerpsEligible, toUsdcAmountRaw } from './shared.js';
|
|
6
|
+
/**
|
|
7
|
+
* `perpsWithdraw`: the same placeholder USDC transfer to `from` the in-wallet
|
|
8
|
+
* withdraw uses; the confirmation and Pay turn it into the Hyperliquid
|
|
9
|
+
* withdraw. Gas isn't estimated upfront: the funds are on Hyperliquid, so the
|
|
10
|
+
* placeholder transfer would fail to estimate.
|
|
11
|
+
*/
|
|
12
|
+
export const perpsWithdrawFlow = {
|
|
13
|
+
// Reuses the in-wallet withdraw flow, so it also needs that flow's flag.
|
|
14
|
+
isEnabled: (remoteFeatureFlags) => isPayFlowEnabled(remoteFeatureFlags, TransactionType.perpsWithdraw),
|
|
15
|
+
assertPreconditions: assertPerpsEligible,
|
|
16
|
+
build: ({ from, amount }) => ({
|
|
17
|
+
chainId: ARBITRUM_MAINNET_CHAIN_ID_HEX,
|
|
18
|
+
transactionParams: {
|
|
19
|
+
from,
|
|
20
|
+
to: USDC_ARBITRUM_MAINNET_ADDRESS,
|
|
21
|
+
value: '0x0',
|
|
22
|
+
data: generateERC20TransferData(from, toUsdcAmountRaw(amount)),
|
|
23
|
+
},
|
|
24
|
+
type: TransactionType.perpsWithdraw,
|
|
25
|
+
skipInitialGasEstimate: true,
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=withdraw.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withdraw.js","sourceRoot":"","sources":["../../../src/flows/perps/withdraw.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6BAA6B,EAC7B,6BAA6B,GAC9B,MAAM,wDAAwD,CAAC;AAChE,OAAO,EAAE,yBAAyB,EAAE,MAAM,+CAA+C,CAAC;AAC1F,OAAO,EAAE,eAAe,EAAE,MAAM,kCAAkC,CAAC;AAGnE,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAkB;IAC9C,yEAAyE;IACzE,SAAS,EAAE,CAAC,kBAAkB,EAAE,EAAE,CAChC,gBAAgB,CAAC,kBAAkB,EAAE,eAAe,CAAC,aAAa,CAAC;IACrE,mBAAmB,EAAE,mBAAmB;IACxC,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;QAC5B,OAAO,EAAE,6BAA6B;QACtC,iBAAiB,EAAE;YACjB,IAAI;YACJ,EAAE,EAAE,6BAA6B;YACjC,KAAK,EAAE,KAAK;YACZ,IAAI,EAAE,yBAAyB,CAAC,IAAI,EAAE,eAAe,CAAC,MAAM,CAAC,CAAC;SAC/D;QACD,IAAI,EAAE,eAAe,CAAC,aAAa;QACnC,sBAAsB,EAAE,IAAI;KAC7B,CAAC;CACH,CAAC","sourcesContent":["import {\n ARBITRUM_MAINNET_CHAIN_ID_HEX,\n USDC_ARBITRUM_MAINNET_ADDRESS,\n} from '@metamask/perps-controller/constants/hyperLiquidConfig';\nimport { generateERC20TransferData } from '@metamask/perps-controller/utils/transferData';\nimport { TransactionType } from '@metamask/transaction-controller';\n\nimport type { WalletPayFlow } from '../../types.js';\nimport { isPayFlowEnabled } from '../../utils/feature-flags.js';\nimport { assertPerpsEligible, toUsdcAmountRaw } from './shared.js';\n\n/**\n * `perpsWithdraw`: the same placeholder USDC transfer to `from` the in-wallet\n * withdraw uses; the confirmation and Pay turn it into the Hyperliquid\n * withdraw. Gas isn't estimated upfront: the funds are on Hyperliquid, so the\n * placeholder transfer would fail to estimate.\n */\nexport const perpsWithdrawFlow: WalletPayFlow = {\n // Reuses the in-wallet withdraw flow, so it also needs that flow's flag.\n isEnabled: (remoteFeatureFlags) =>\n isPayFlowEnabled(remoteFeatureFlags, TransactionType.perpsWithdraw),\n assertPreconditions: assertPerpsEligible,\n build: ({ from, amount }) => ({\n chainId: ARBITRUM_MAINNET_CHAIN_ID_HEX,\n transactionParams: {\n from,\n to: USDC_ARBITRUM_MAINNET_ADDRESS,\n value: '0x0',\n data: generateERC20TransferData(from, toUsdcAmountRaw(amount)),\n },\n type: TransactionType.perpsWithdraw,\n skipInitialGasEstimate: true,\n }),\n};\n"]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AACpD,YAAY,EACV,iCAAiC,EACjC,cAAc,EACd,uBAAuB,EACvB,eAAe,GAChB,MAAM,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC","sourcesContent":["export { walletPay } from './methods/wallet_pay.js';\nexport type {\n TransactionPayMiddlewareMessenger,\n WalletPayHooks,\n WalletPayJsonRpcRequest,\n WalletPayResult,\n} from './types.js';\n"]}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { PendingJsonRpcResponse } from '@metamask/utils';
|
|
2
|
+
import type { TransactionPayMiddlewareMessenger, WalletPayHooks, WalletPayJsonRpcRequest } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The RPC method handler for `wallet_pay`: validates the request, checks the
|
|
5
|
+
* flags and the account, then adds the transaction for the requesting dapp and
|
|
6
|
+
* waits for its hash.
|
|
7
|
+
*
|
|
8
|
+
* @param req - The JSON-RPC request.
|
|
9
|
+
* @param res - The pending JSON-RPC response.
|
|
10
|
+
* @param hooks - Client hooks.
|
|
11
|
+
* @param messenger - The middleware messenger.
|
|
12
|
+
*/
|
|
13
|
+
export declare function walletPay(req: WalletPayJsonRpcRequest, res: PendingJsonRpcResponse, hooks: WalletPayHooks, messenger: TransactionPayMiddlewareMessenger): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=wallet_pay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet_pay.d.ts","sourceRoot":"","sources":["../../src/methods/wallet_pay.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAO,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAKnE,OAAO,KAAK,EACV,iCAAiC,EAEjC,cAAc,EACd,uBAAuB,EAIxB,MAAM,aAAa,CAAC;AAMrB;;;;;;;;;GASG;AACH,wBAAsB,SAAS,CAC7B,GAAG,EAAE,uBAAuB,EAC5B,GAAG,EAAE,sBAAsB,EAC3B,KAAK,EAAE,cAAc,EACrB,SAAS,EAAE,iCAAiC,GAC3C,OAAO,CAAC,IAAI,CAAC,CAef"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { validate } from '@metamask/superstruct';
|
|
2
|
+
import { walletPayErrors } from '../errors.js';
|
|
3
|
+
import { getWalletPayFlow } from '../flows/index.js';
|
|
4
|
+
import { WalletPayParamsStruct } from '../types.js';
|
|
5
|
+
import { isWalletPayTypeAllowed } from '../utils/feature-flags.js';
|
|
6
|
+
// Some providers resolve with this placeholder when there is no hash.
|
|
7
|
+
const FALLBACK_TRANSACTION_HASH = '0x0';
|
|
8
|
+
/**
|
|
9
|
+
* The RPC method handler for `wallet_pay`: validates the request, checks the
|
|
10
|
+
* flags and the account, then adds the transaction for the requesting dapp and
|
|
11
|
+
* waits for its hash.
|
|
12
|
+
*
|
|
13
|
+
* @param req - The JSON-RPC request.
|
|
14
|
+
* @param res - The pending JSON-RPC response.
|
|
15
|
+
* @param hooks - Client hooks.
|
|
16
|
+
* @param messenger - The middleware messenger.
|
|
17
|
+
*/
|
|
18
|
+
export async function walletPay(req, res, hooks, messenger) {
|
|
19
|
+
const request = validateRequest(req.params);
|
|
20
|
+
const flow = getAllowedFlow(messenger, req.origin, request.type);
|
|
21
|
+
await assertPermittedAccount(hooks, request.from);
|
|
22
|
+
await flow.assertPreconditions?.(messenger);
|
|
23
|
+
const hash = await addTransaction(messenger, flow.build(request), req);
|
|
24
|
+
const result = hash && hash !== FALLBACK_TRANSACTION_HASH
|
|
25
|
+
? { transactionHash: hash }
|
|
26
|
+
: {};
|
|
27
|
+
res.result = result;
|
|
28
|
+
}
|
|
29
|
+
function validateRequest(params) {
|
|
30
|
+
const [error, validated] = validate(params, WalletPayParamsStruct);
|
|
31
|
+
if (error) {
|
|
32
|
+
throw walletPayErrors.invalidParams(formatValidationError(error));
|
|
33
|
+
}
|
|
34
|
+
return validated[0];
|
|
35
|
+
}
|
|
36
|
+
function getAllowedFlow(messenger, origin, transactionType) {
|
|
37
|
+
const flow = getWalletPayFlow(transactionType);
|
|
38
|
+
const { remoteFeatureFlags } = messenger.call('RemoteFeatureFlagController:getState');
|
|
39
|
+
if (!flow ||
|
|
40
|
+
!isWalletPayTypeAllowed(remoteFeatureFlags, origin, transactionType) ||
|
|
41
|
+
!(flow.isEnabled?.(remoteFeatureFlags) ?? true)) {
|
|
42
|
+
throw walletPayErrors.unsupportedType(transactionType);
|
|
43
|
+
}
|
|
44
|
+
return flow;
|
|
45
|
+
}
|
|
46
|
+
async function assertPermittedAccount(hooks, from) {
|
|
47
|
+
const permittedAccounts = await hooks.getPermittedAccountsForOrigin();
|
|
48
|
+
const isPermitted = permittedAccounts.some((account) => account.toLowerCase() === from.toLowerCase());
|
|
49
|
+
if (!isPermitted) {
|
|
50
|
+
throw walletPayErrors.unauthorizedAccount();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function addTransaction(messenger, { chainId, transactionParams, type, skipInitialGasEstimate, }, req) {
|
|
54
|
+
const { result } = await messenger.call('TransactionController:addTransaction', transactionParams, {
|
|
55
|
+
networkClientId: getNetworkClientId(messenger, chainId),
|
|
56
|
+
origin: req.origin,
|
|
57
|
+
requestId: String(req.id),
|
|
58
|
+
method: req.method,
|
|
59
|
+
requireApproval: true,
|
|
60
|
+
type,
|
|
61
|
+
skipInitialGasEstimate,
|
|
62
|
+
});
|
|
63
|
+
return await result;
|
|
64
|
+
}
|
|
65
|
+
function getNetworkClientId(messenger, chainId) {
|
|
66
|
+
try {
|
|
67
|
+
return messenger.call('NetworkController:findNetworkClientIdByChainId', chainId);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw walletPayErrors.networkUnavailable(chainId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function formatValidationError(error) {
|
|
74
|
+
return error
|
|
75
|
+
.failures()
|
|
76
|
+
.map(({ path, message }) => `${path.join(' > ')}${path.length ? ' - ' : ''}${message}`)
|
|
77
|
+
.join('\n');
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=wallet_pay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wallet_pay.js","sourceRoot":"","sources":["../../src/methods/wallet_pay.ts"],"names":[],"mappings":"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","sourcesContent":["import type { StructError } from '@metamask/superstruct';\nimport { validate } from '@metamask/superstruct';\nimport type { Hex, PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { walletPayErrors } from '../errors.js';\nimport { getWalletPayFlow } from '../flows/index.js';\nimport { WalletPayParamsStruct } from '../types.js';\nimport type {\n TransactionPayMiddlewareMessenger,\n WalletPayFlow,\n WalletPayHooks,\n WalletPayJsonRpcRequest,\n WalletPayRequest,\n WalletPayResult,\n WalletPayTransaction,\n} from '../types.js';\nimport { isWalletPayTypeAllowed } from '../utils/feature-flags.js';\n\n// Some providers resolve with this placeholder when there is no hash.\nconst FALLBACK_TRANSACTION_HASH = '0x0';\n\n/**\n * The RPC method handler for `wallet_pay`: validates the request, checks the\n * flags and the account, then adds the transaction for the requesting dapp and\n * waits for its hash.\n *\n * @param req - The JSON-RPC request.\n * @param res - The pending JSON-RPC response.\n * @param hooks - Client hooks.\n * @param messenger - The middleware messenger.\n */\nexport async function walletPay(\n req: WalletPayJsonRpcRequest,\n res: PendingJsonRpcResponse,\n hooks: WalletPayHooks,\n messenger: TransactionPayMiddlewareMessenger,\n): Promise<void> {\n const request = validateRequest(req.params);\n const flow = getAllowedFlow(messenger, req.origin, request.type);\n\n await assertPermittedAccount(hooks, request.from);\n await flow.assertPreconditions?.(messenger);\n\n const hash = await addTransaction(messenger, flow.build(request), req);\n\n const result: WalletPayResult =\n hash && hash !== FALLBACK_TRANSACTION_HASH\n ? { transactionHash: hash as Hex }\n : {};\n\n res.result = result;\n}\n\nfunction validateRequest(params: unknown): WalletPayRequest {\n const [error, validated] = validate(params, WalletPayParamsStruct);\n\n if (error) {\n throw walletPayErrors.invalidParams(formatValidationError(error));\n }\n\n return validated[0];\n}\n\nfunction getAllowedFlow(\n messenger: TransactionPayMiddlewareMessenger,\n origin: string,\n transactionType: string,\n): WalletPayFlow {\n const flow = getWalletPayFlow(transactionType);\n const { remoteFeatureFlags } = messenger.call(\n 'RemoteFeatureFlagController:getState',\n );\n\n if (\n !flow ||\n !isWalletPayTypeAllowed(remoteFeatureFlags, origin, transactionType) ||\n !(flow.isEnabled?.(remoteFeatureFlags) ?? true)\n ) {\n throw walletPayErrors.unsupportedType(transactionType);\n }\n\n return flow;\n}\n\nasync function assertPermittedAccount(\n hooks: WalletPayHooks,\n from: Hex,\n): Promise<void> {\n const permittedAccounts = await hooks.getPermittedAccountsForOrigin();\n const isPermitted = permittedAccounts.some(\n (account) => account.toLowerCase() === from.toLowerCase(),\n );\n\n if (!isPermitted) {\n throw walletPayErrors.unauthorizedAccount();\n }\n}\n\nasync function addTransaction(\n messenger: TransactionPayMiddlewareMessenger,\n {\n chainId,\n transactionParams,\n type,\n skipInitialGasEstimate,\n }: WalletPayTransaction,\n req: WalletPayJsonRpcRequest,\n): Promise<string> {\n const { result } = await messenger.call(\n 'TransactionController:addTransaction',\n transactionParams,\n {\n networkClientId: getNetworkClientId(messenger, chainId),\n origin: req.origin,\n requestId: String(req.id),\n method: req.method,\n requireApproval: true,\n type,\n skipInitialGasEstimate,\n },\n );\n\n return await result;\n}\n\nfunction getNetworkClientId(\n messenger: TransactionPayMiddlewareMessenger,\n chainId: Hex,\n): string {\n try {\n return messenger.call(\n 'NetworkController:findNetworkClientIdByChainId',\n chainId,\n );\n } catch {\n throw walletPayErrors.networkUnavailable(chainId);\n }\n}\n\nfunction formatValidationError(error: StructError): string {\n return error\n .failures()\n .map(\n ({ path, message }) =>\n `${path.join(' > ')}${path.length ? ' - ' : ''}${message}`,\n )\n .join('\\n');\n}\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Messenger } from '@metamask/messenger';
|
|
2
|
+
import type { NetworkControllerFindNetworkClientIdByChainIdAction } from '@metamask/network-controller';
|
|
3
|
+
import type { PerpsControllerGetStateAction, PerpsControllerRefreshEligibilityAction } from '@metamask/perps-controller';
|
|
4
|
+
import type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';
|
|
5
|
+
import type { Infer } from '@metamask/superstruct';
|
|
6
|
+
import type { TransactionControllerAddTransactionAction, TransactionParams, TransactionType } from '@metamask/transaction-controller';
|
|
7
|
+
import type { Hex, Json, JsonRpcRequest } from '@metamask/utils';
|
|
8
|
+
import type { RemoteFeatureFlags } from './utils/feature-flags.js';
|
|
9
|
+
type AllowedActions = NetworkControllerFindNetworkClientIdByChainIdAction | PerpsControllerGetStateAction | PerpsControllerRefreshEligibilityAction | RemoteFeatureFlagControllerGetStateAction | TransactionControllerAddTransactionAction;
|
|
10
|
+
export type TransactionPayMiddlewareMessenger = Messenger<'TransactionPayMiddleware', AllowedActions>;
|
|
11
|
+
export declare const WalletPayParamsStruct: import("@metamask/superstruct").Struct<[{
|
|
12
|
+
type: string;
|
|
13
|
+
from: `0x${string}`;
|
|
14
|
+
amount?: string | undefined;
|
|
15
|
+
}], null>;
|
|
16
|
+
export type WalletPayRequest = Infer<typeof WalletPayParamsStruct>[0];
|
|
17
|
+
/** Result of `wallet_pay`, returned once the transfer completes. */
|
|
18
|
+
export type WalletPayResult = {
|
|
19
|
+
transactionHash?: Hex;
|
|
20
|
+
};
|
|
21
|
+
/** A transaction built for a `wallet_pay` request, ready to add. */
|
|
22
|
+
export type WalletPayTransaction = {
|
|
23
|
+
chainId: Hex;
|
|
24
|
+
transactionParams: TransactionParams;
|
|
25
|
+
type: TransactionType;
|
|
26
|
+
skipInitialGasEstimate?: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** How one `wallet_pay` type is enabled, checked and built. */
|
|
29
|
+
export type WalletPayFlow = {
|
|
30
|
+
/** Flag checks beyond `confirmations_pay_rpc`, if the type needs any. */
|
|
31
|
+
isEnabled?: (remoteFeatureFlags: RemoteFeatureFlags) => boolean;
|
|
32
|
+
assertPreconditions?: (messenger: TransactionPayMiddlewareMessenger) => Promise<void>;
|
|
33
|
+
build: (request: WalletPayRequest) => WalletPayTransaction;
|
|
34
|
+
};
|
|
35
|
+
export type WalletPayJsonRpcRequest = JsonRpcRequest<Json[]> & {
|
|
36
|
+
origin: string;
|
|
37
|
+
};
|
|
38
|
+
export type WalletPayHooks = {
|
|
39
|
+
/** Returns the accounts the requesting origin is connected to. */
|
|
40
|
+
getPermittedAccountsForOrigin: () => Promise<string[]>;
|
|
41
|
+
};
|
|
42
|
+
export {};
|
|
43
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,KAAK,EAAE,mDAAmD,EAAE,MAAM,8BAA8B,CAAC;AACxG,OAAO,KAAK,EACV,6BAA6B,EAC7B,uCAAuC,EACxC,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,yCAAyC,EAAE,MAAM,0CAA0C,CAAC;AAC1G,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AASnD,OAAO,KAAK,EACV,yCAAyC,EACzC,iBAAiB,EACjB,eAAe,EAChB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEnE,KAAK,cAAc,GACf,mDAAmD,GACnD,6BAA6B,GAC7B,uCAAuC,GACvC,yCAAyC,GACzC,yCAAyC,CAAC;AAE9C,MAAM,MAAM,iCAAiC,GAAG,SAAS,CACvD,0BAA0B,EAC1B,cAAc,CACf,CAAC;AAKF,eAAO,MAAM,qBAAqB;;;;SAMhC,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAC,CAAC,CAAC,CAAC;AAEtE,oEAAoE;AACpE,MAAM,MAAM,eAAe,GAAG;IAC5B,eAAe,CAAC,EAAE,GAAG,CAAC;CACvB,CAAC;AAEF,oEAAoE;AACpE,MAAM,MAAM,oBAAoB,GAAG;IACjC,OAAO,EAAE,GAAG,CAAC;IACb,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,IAAI,EAAE,eAAe,CAAC;IACtB,sBAAsB,CAAC,EAAE,OAAO,CAAC;CAClC,CAAC;AAEF,+DAA+D;AAC/D,MAAM,MAAM,aAAa,GAAG;IAC1B,yEAAyE;IACzE,SAAS,CAAC,EAAE,CAAC,kBAAkB,EAAE,kBAAkB,KAAK,OAAO,CAAC;IAChE,mBAAmB,CAAC,EAAE,CACpB,SAAS,EAAE,iCAAiC,KACzC,OAAO,CAAC,IAAI,CAAC,CAAC;IACnB,KAAK,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,oBAAoB,CAAC;CAC5D,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAAG,cAAc,CAAC,IAAI,EAAE,CAAC,GAAG;IAC7D,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,kEAAkE;IAClE,6BAA6B,EAAE,MAAM,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;CACxD,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { nonempty, object, optional, pattern, string, tuple, } from '@metamask/superstruct';
|
|
2
|
+
import { HexChecksumAddressStruct } from '@metamask/utils';
|
|
3
|
+
/** A positive decimal string with up to 6 decimals. */
|
|
4
|
+
const AmountStruct = pattern(string(), /^(?!0+(\.0+)?$)\d+(\.\d{1,6})?$/u);
|
|
5
|
+
export const WalletPayParamsStruct = tuple([
|
|
6
|
+
object({
|
|
7
|
+
type: nonempty(string()),
|
|
8
|
+
from: HexChecksumAddressStruct,
|
|
9
|
+
amount: optional(AmountStruct),
|
|
10
|
+
}),
|
|
11
|
+
]);
|
|
12
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,OAAO,EACP,MAAM,EACN,KAAK,GACN,MAAM,uBAAuB,CAAC;AAO/B,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAgB3D,uDAAuD;AACvD,MAAM,YAAY,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,kCAAkC,CAAC,CAAC;AAE3E,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,CAAC;IACzC,MAAM,CAAC;QACL,IAAI,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC;QACxB,IAAI,EAAE,wBAAwB;QAC9B,MAAM,EAAE,QAAQ,CAAC,YAAY,CAAC;KAC/B,CAAC;CACH,CAAC,CAAC","sourcesContent":["import type { Messenger } from '@metamask/messenger';\nimport type { NetworkControllerFindNetworkClientIdByChainIdAction } from '@metamask/network-controller';\nimport type {\n PerpsControllerGetStateAction,\n PerpsControllerRefreshEligibilityAction,\n} from '@metamask/perps-controller';\nimport type { RemoteFeatureFlagControllerGetStateAction } from '@metamask/remote-feature-flag-controller';\nimport type { Infer } from '@metamask/superstruct';\nimport {\n nonempty,\n object,\n optional,\n pattern,\n string,\n tuple,\n} from '@metamask/superstruct';\nimport type {\n TransactionControllerAddTransactionAction,\n TransactionParams,\n TransactionType,\n} from '@metamask/transaction-controller';\nimport type { Hex, Json, JsonRpcRequest } from '@metamask/utils';\nimport { HexChecksumAddressStruct } from '@metamask/utils';\n\nimport type { RemoteFeatureFlags } from './utils/feature-flags.js';\n\ntype AllowedActions =\n | NetworkControllerFindNetworkClientIdByChainIdAction\n | PerpsControllerGetStateAction\n | PerpsControllerRefreshEligibilityAction\n | RemoteFeatureFlagControllerGetStateAction\n | TransactionControllerAddTransactionAction;\n\nexport type TransactionPayMiddlewareMessenger = Messenger<\n 'TransactionPayMiddleware',\n AllowedActions\n>;\n\n/** A positive decimal string with up to 6 decimals. */\nconst AmountStruct = pattern(string(), /^(?!0+(\\.0+)?$)\\d+(\\.\\d{1,6})?$/u);\n\nexport const WalletPayParamsStruct = tuple([\n object({\n type: nonempty(string()),\n from: HexChecksumAddressStruct,\n amount: optional(AmountStruct),\n }),\n]);\n\nexport type WalletPayRequest = Infer<typeof WalletPayParamsStruct>[0];\n\n/** Result of `wallet_pay`, returned once the transfer completes. */\nexport type WalletPayResult = {\n transactionHash?: Hex;\n};\n\n/** A transaction built for a `wallet_pay` request, ready to add. */\nexport type WalletPayTransaction = {\n chainId: Hex;\n transactionParams: TransactionParams;\n type: TransactionType;\n skipInitialGasEstimate?: boolean;\n};\n\n/** How one `wallet_pay` type is enabled, checked and built. */\nexport type WalletPayFlow = {\n /** Flag checks beyond `confirmations_pay_rpc`, if the type needs any. */\n isEnabled?: (remoteFeatureFlags: RemoteFeatureFlags) => boolean;\n assertPreconditions?: (\n messenger: TransactionPayMiddlewareMessenger,\n ) => Promise<void>;\n build: (request: WalletPayRequest) => WalletPayTransaction;\n};\n\nexport type WalletPayJsonRpcRequest = JsonRpcRequest<Json[]> & {\n origin: string;\n};\n\nexport type WalletPayHooks = {\n /** Returns the accounts the requesting origin is connected to. */\n getPermittedAccountsForOrigin: () => Promise<string[]>;\n};\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Json } from '@metamask/utils';
|
|
2
|
+
/**
|
|
3
|
+
* Lists which transaction types each dapp origin may request through
|
|
4
|
+
* `wallet_pay`. A type must also be enabled at the top level (the kill
|
|
5
|
+
* switch).
|
|
6
|
+
*/
|
|
7
|
+
export declare const WALLET_PAY_FEATURE_FLAG = "confirmations_pay_rpc";
|
|
8
|
+
/**
|
|
9
|
+
* Gates in-wallet MetaMask Pay flows per transaction type. A dapp request that
|
|
10
|
+
* reuses an in-wallet flow is gated by it too.
|
|
11
|
+
*/
|
|
12
|
+
export declare const PAY_POST_QUOTE_FEATURE_FLAG = "confirmations_pay_post_quote";
|
|
13
|
+
export type RemoteFeatureFlags = Record<string, Json>;
|
|
14
|
+
/**
|
|
15
|
+
* Whether a dapp may request a transaction type, based on
|
|
16
|
+
* `confirmations_pay_rpc`. Origins match exactly, scheme included, except
|
|
17
|
+
* `http://localhost`, which matches any port. A missing or malformed flag
|
|
18
|
+
* allows nothing.
|
|
19
|
+
*
|
|
20
|
+
* @param remoteFeatureFlags - The remote feature flags.
|
|
21
|
+
* @param origin - The requesting dapp's origin.
|
|
22
|
+
* @param transactionType - The requested transaction type.
|
|
23
|
+
* @returns Whether the request is allowed.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isWalletPayTypeAllowed(remoteFeatureFlags: RemoteFeatureFlags, origin: string, transactionType: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Whether the in-wallet flow for a transaction type is enabled by
|
|
28
|
+
* `confirmations_pay_post_quote`. A type-specific entry (under `overrides`, or
|
|
29
|
+
* directly) wins over `default`.
|
|
30
|
+
*
|
|
31
|
+
* @param remoteFeatureFlags - The remote feature flags.
|
|
32
|
+
* @param transactionType - The transaction type.
|
|
33
|
+
* @returns Whether the flow is enabled.
|
|
34
|
+
*/
|
|
35
|
+
export declare function isPayFlowEnabled(remoteFeatureFlags: RemoteFeatureFlags, transactionType: string): boolean;
|
|
36
|
+
//# sourceMappingURL=feature-flags.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flags.d.ts","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,0BAA0B,CAAC;AAE/D;;;GAGG;AACH,eAAO,MAAM,2BAA2B,iCAAiC,CAAC;AAoB1E,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AAEtD;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,CACpC,kBAAkB,EAAE,kBAAkB,EACtC,MAAM,EAAE,MAAM,EACd,eAAe,EAAE,MAAM,GACtB,OAAO,CAsBT;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,kBAAkB,EAAE,kBAAkB,EACtC,eAAe,EAAE,MAAM,GACtB,OAAO,CAaT"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { array, boolean, is, optional, record, string, type, } from '@metamask/superstruct';
|
|
2
|
+
/**
|
|
3
|
+
* Lists which transaction types each dapp origin may request through
|
|
4
|
+
* `wallet_pay`. A type must also be enabled at the top level (the kill
|
|
5
|
+
* switch).
|
|
6
|
+
*/
|
|
7
|
+
export const WALLET_PAY_FEATURE_FLAG = 'confirmations_pay_rpc';
|
|
8
|
+
/**
|
|
9
|
+
* Gates in-wallet MetaMask Pay flows per transaction type. A dapp request that
|
|
10
|
+
* reuses an in-wallet flow is gated by it too.
|
|
11
|
+
*/
|
|
12
|
+
export const PAY_POST_QUOTE_FEATURE_FLAG = 'confirmations_pay_post_quote';
|
|
13
|
+
// `localhost` is allowed on any port, so local development servers work
|
|
14
|
+
// without listing every port in the flag.
|
|
15
|
+
const LOCALHOST_ORIGIN = 'http://localhost';
|
|
16
|
+
const AllowedTypesStruct = type({ allowedTypes: array(string()) });
|
|
17
|
+
const WalletPayFeatureFlagStruct = type({
|
|
18
|
+
allowedTypes: array(string()),
|
|
19
|
+
dapps: record(string(), AllowedTypesStruct),
|
|
20
|
+
});
|
|
21
|
+
const PayFlowConfigStruct = type({ enabled: optional(boolean()) });
|
|
22
|
+
const PayPostQuoteFeatureFlagStruct = type({
|
|
23
|
+
default: optional(PayFlowConfigStruct),
|
|
24
|
+
overrides: optional(record(string(), PayFlowConfigStruct)),
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* Whether a dapp may request a transaction type, based on
|
|
28
|
+
* `confirmations_pay_rpc`. Origins match exactly, scheme included, except
|
|
29
|
+
* `http://localhost`, which matches any port. A missing or malformed flag
|
|
30
|
+
* allows nothing.
|
|
31
|
+
*
|
|
32
|
+
* @param remoteFeatureFlags - The remote feature flags.
|
|
33
|
+
* @param origin - The requesting dapp's origin.
|
|
34
|
+
* @param transactionType - The requested transaction type.
|
|
35
|
+
* @returns Whether the request is allowed.
|
|
36
|
+
*/
|
|
37
|
+
export function isWalletPayTypeAllowed(remoteFeatureFlags, origin, transactionType) {
|
|
38
|
+
const flag = remoteFeatureFlags[WALLET_PAY_FEATURE_FLAG];
|
|
39
|
+
if (!is(flag, WalletPayFeatureFlagStruct) ||
|
|
40
|
+
!flag.allowedTypes.includes(transactionType)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
const url = parseUrl(origin);
|
|
44
|
+
if (!url) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
const isLocalhost = url.protocol === 'http:' && url.hostname === 'localhost';
|
|
48
|
+
const dappConfig = flag.dapps[url.origin] ??
|
|
49
|
+
(isLocalhost ? flag.dapps[LOCALHOST_ORIGIN] : undefined);
|
|
50
|
+
return dappConfig?.allowedTypes.includes(transactionType) ?? false;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Whether the in-wallet flow for a transaction type is enabled by
|
|
54
|
+
* `confirmations_pay_post_quote`. A type-specific entry (under `overrides`, or
|
|
55
|
+
* directly) wins over `default`.
|
|
56
|
+
*
|
|
57
|
+
* @param remoteFeatureFlags - The remote feature flags.
|
|
58
|
+
* @param transactionType - The transaction type.
|
|
59
|
+
* @returns Whether the flow is enabled.
|
|
60
|
+
*/
|
|
61
|
+
export function isPayFlowEnabled(remoteFeatureFlags, transactionType) {
|
|
62
|
+
const flag = remoteFeatureFlags[PAY_POST_QUOTE_FEATURE_FLAG];
|
|
63
|
+
if (!is(flag, PayPostQuoteFeatureFlagStruct)) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const directConfig = flag[transactionType];
|
|
67
|
+
const typeConfig = flag.overrides?.[transactionType] ??
|
|
68
|
+
(is(directConfig, PayFlowConfigStruct) ? directConfig : undefined);
|
|
69
|
+
return (typeConfig?.enabled ?? flag.default?.enabled) === true;
|
|
70
|
+
}
|
|
71
|
+
function parseUrl(origin) {
|
|
72
|
+
try {
|
|
73
|
+
return new URL(origin);
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=feature-flags.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"feature-flags.js","sourceRoot":"","sources":["../../src/utils/feature-flags.ts"],"names":[],"mappings":"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","sourcesContent":["import {\n array,\n boolean,\n is,\n optional,\n record,\n string,\n type,\n} from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\n\n/**\n * Lists which transaction types each dapp origin may request through\n * `wallet_pay`. A type must also be enabled at the top level (the kill\n * switch).\n */\nexport const WALLET_PAY_FEATURE_FLAG = 'confirmations_pay_rpc';\n\n/**\n * Gates in-wallet MetaMask Pay flows per transaction type. A dapp request that\n * reuses an in-wallet flow is gated by it too.\n */\nexport const PAY_POST_QUOTE_FEATURE_FLAG = 'confirmations_pay_post_quote';\n\n// `localhost` is allowed on any port, so local development servers work\n// without listing every port in the flag.\nconst LOCALHOST_ORIGIN = 'http://localhost';\n\nconst AllowedTypesStruct = type({ allowedTypes: array(string()) });\n\nconst WalletPayFeatureFlagStruct = type({\n allowedTypes: array(string()),\n dapps: record(string(), AllowedTypesStruct),\n});\n\nconst PayFlowConfigStruct = type({ enabled: optional(boolean()) });\n\nconst PayPostQuoteFeatureFlagStruct = type({\n default: optional(PayFlowConfigStruct),\n overrides: optional(record(string(), PayFlowConfigStruct)),\n});\n\nexport type RemoteFeatureFlags = Record<string, Json>;\n\n/**\n * Whether a dapp may request a transaction type, based on\n * `confirmations_pay_rpc`. Origins match exactly, scheme included, except\n * `http://localhost`, which matches any port. A missing or malformed flag\n * allows nothing.\n *\n * @param remoteFeatureFlags - The remote feature flags.\n * @param origin - The requesting dapp's origin.\n * @param transactionType - The requested transaction type.\n * @returns Whether the request is allowed.\n */\nexport function isWalletPayTypeAllowed(\n remoteFeatureFlags: RemoteFeatureFlags,\n origin: string,\n transactionType: string,\n): boolean {\n const flag = remoteFeatureFlags[WALLET_PAY_FEATURE_FLAG];\n\n if (\n !is(flag, WalletPayFeatureFlagStruct) ||\n !flag.allowedTypes.includes(transactionType)\n ) {\n return false;\n }\n\n const url = parseUrl(origin);\n\n if (!url) {\n return false;\n }\n\n const isLocalhost = url.protocol === 'http:' && url.hostname === 'localhost';\n const dappConfig =\n flag.dapps[url.origin] ??\n (isLocalhost ? flag.dapps[LOCALHOST_ORIGIN] : undefined);\n\n return dappConfig?.allowedTypes.includes(transactionType) ?? false;\n}\n\n/**\n * Whether the in-wallet flow for a transaction type is enabled by\n * `confirmations_pay_post_quote`. A type-specific entry (under `overrides`, or\n * directly) wins over `default`.\n *\n * @param remoteFeatureFlags - The remote feature flags.\n * @param transactionType - The transaction type.\n * @returns Whether the flow is enabled.\n */\nexport function isPayFlowEnabled(\n remoteFeatureFlags: RemoteFeatureFlags,\n transactionType: string,\n): boolean {\n const flag = remoteFeatureFlags[PAY_POST_QUOTE_FEATURE_FLAG];\n\n if (!is(flag, PayPostQuoteFeatureFlagStruct)) {\n return false;\n }\n\n const directConfig = (flag as Record<string, unknown>)[transactionType];\n const typeConfig =\n flag.overrides?.[transactionType] ??\n (is(directConfig, PayFlowConfigStruct) ? directConfig : undefined);\n\n return (typeConfig?.enabled ?? flag.default?.enabled) === true;\n}\n\nfunction parseUrl(origin: string): URL | undefined {\n try {\n return new URL(origin);\n } catch {\n return undefined;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,79 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/transaction-pay-middleware",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-preview-55fcf3ef7",
|
|
4
|
+
"description": "Implements the wallet_pay JSON-RPC method, which lets dapps request MetaMask Pay transactions",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"Ethereum",
|
|
7
|
+
"MetaMask"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/MetaMask/core/tree/main/packages/transaction-pay-middleware#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/MetaMask/core/issues"
|
|
12
|
+
},
|
|
13
|
+
"license": "(MIT OR Apache-2.0)",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/MetaMask/core.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist/"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public",
|
|
32
|
+
"registry": "https://registry.npmjs.org/"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsc --project tsconfig.build.json",
|
|
36
|
+
"build:all": "tsc --build tsconfig.build.json --verbose",
|
|
37
|
+
"build:clean": "yarn build:only-clean && yarn build",
|
|
38
|
+
"build:docs": "typedoc",
|
|
39
|
+
"build:only-clean": "rimraf './dist' './tsconfig.build.tsbuildinfo'",
|
|
40
|
+
"changelog:update": "../../scripts/update-changelog.sh @metamask/transaction-pay-middleware",
|
|
41
|
+
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/transaction-pay-middleware",
|
|
42
|
+
"lint:tsconfigs": "node --import ../../scripts/resolver/register.ts --experimental-transform-types ../../scripts/lint-tsconfigs/lint-tsconfigs.ts",
|
|
43
|
+
"lint:tsconfigs:fix": "node --import ../../scripts/resolver/register.ts --experimental-transform-types ../../scripts/lint-tsconfigs/lint-tsconfigs.ts --fix",
|
|
44
|
+
"messenger-action-types:check": "node --import ../../scripts/resolver/register.ts --experimental-transform-types ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
|
|
45
|
+
"messenger-action-types:generate": "node --import ../../scripts/resolver/register.ts --experimental-transform-types ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
|
|
46
|
+
"since-latest-release": "../../scripts/since-latest-release.sh",
|
|
47
|
+
"test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
48
|
+
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
49
|
+
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
50
|
+
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@metamask/controller-utils": "^13.0.0",
|
|
54
|
+
"@metamask/messenger": "^3.0.0",
|
|
55
|
+
"@metamask/network-controller": "^37.1.1",
|
|
56
|
+
"@metamask/perps-controller": "^21.0.0",
|
|
57
|
+
"@metamask/remote-feature-flag-controller": "^7.0.0",
|
|
58
|
+
"@metamask/rpc-errors": "^7.0.3",
|
|
59
|
+
"@metamask/superstruct": "^3.4.1",
|
|
60
|
+
"@metamask/transaction-controller": "^72.2.1",
|
|
61
|
+
"@metamask/utils": "^12.0.0",
|
|
62
|
+
"bignumber.js": "^9.1.2"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"@metamask/auto-changelog": "^6.2.1",
|
|
66
|
+
"@types/jest": "^30.0.0",
|
|
67
|
+
"@typescript/native": "npm:typescript@^7.0.2",
|
|
68
|
+
"deepmerge": "^4.3.1",
|
|
69
|
+
"jest": "^30.5.2",
|
|
70
|
+
"rimraf": "^6.1.3",
|
|
71
|
+
"ts-jest": "^29.4.14",
|
|
72
|
+
"typedoc": "^0.28.20",
|
|
73
|
+
"typedoc-plugin-missing-exports": "^4.1.4",
|
|
74
|
+
"typescript": "npm:@typescript/typescript6@^6.0.2"
|
|
75
|
+
},
|
|
76
|
+
"engines": {
|
|
77
|
+
"node": "^22.14.0 || ^24"
|
|
78
|
+
}
|
|
6
79
|
}
|