@tetherto/wdk-wallet-evm 1.0.0-beta.20 → 1.0.0-beta.21

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.
@@ -1,202 +0,0 @@
1
- // Copyright 2024 Tether Operations Limited
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
- 'use strict'
15
-
16
- import { ValueError } from '@tetherto/wdk-wallet'
17
-
18
- import { Signature, toQuantity } from 'ethers'
19
-
20
- /** @typedef {import('ethers').Provider} Provider */
21
- /** @typedef {import('ethers').AuthorizationLike} AuthorizationLike */
22
-
23
- /**
24
- * A fully or partially specified EVM transaction, prior to signing.
25
- *
26
- * @typedef {Object} UnsignedEvmTransaction
27
- * @property {number} chainId - The id of the chain the transaction targets.
28
- * @property {number} nonce - The sender's transaction count, used to order transactions.
29
- * @property {string} from - The sender's address.
30
- * @property {string|null} to - The recipient's address, or null for contract creation.
31
- * @property {string} data - The transaction's calldata as a hex string.
32
- * @property {number|bigint} value - The amount of native currency (in wei) to transfer.
33
- * @property {number} type - The EIP-2718 transaction type (0/1 legacy, 2 EIP-1559, 3 EIP-4844, 4 EIP-7702).
34
- * @property {number|bigint} gasLimit - The maximum amount of gas the transaction may consume.
35
- * @property {number|bigint} [gasPrice] - The gas price (in wei) for legacy (type 0/1) transactions.
36
- * @property {number|bigint} [maxFeePerGas] - The maximum total fee (in wei) per gas for EIP-1559 transactions.
37
- * @property {number|bigint} [maxPriorityFeePerGas] - The maximum priority fee (in wei) per gas for EIP-1559 transactions.
38
- * @property {any[]} [accessList] - The EIP-2930 access list of addresses and storage keys.
39
- * @property {number|bigint} [maxFeePerBlobGas] - The maximum fee (in wei) per blob gas for EIP-4844 transactions.
40
- * @property {any[]} [blobs] - The blobs to include in an EIP-4844 transaction.
41
- * @property {string[]} [blobVersionedHashes] - The versioned hashes of the EIP-4844 blobs.
42
- * @property {AuthorizationLike[]} [authorizationList] - The EIP-7702 authorization tuples.
43
- */
44
-
45
- /**
46
- * Build a fully populated unsigned transaction ready for signing.
47
- *
48
- * Resolves chain ID, nonce, gas limit and fee fields from the provider when not
49
- * explicitly supplied in `tx`. Supports legacy (type 0/1), EIP-1559 (type 2),
50
- * EIP-4844 (type 3) and EIP-7702 (type 4) transaction styles.
51
- *
52
- * @param {Provider} provider - An ethers-compatible JSON-RPC provider.
53
- * @param {string} from - The sender address.
54
- * @param {UnsignedEvmTransaction} tx - The partial transaction to populate.
55
- * @returns {Promise<UnsignedEvmTransaction>} The fully populated unsigned transaction.
56
- * @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
57
- */
58
- export async function populateTransactionEvm (provider, from, tx) {
59
- const net = await provider.getNetwork()
60
- const chainId = Number(net.chainId)
61
-
62
- const has1559 = (tx.maxFeePerGas != null || tx.maxPriorityFeePerGas != null)
63
- const hasLegacy = (tx.gasPrice != null)
64
- const hasAccessList = (tx.accessList != null && Array.isArray(tx.accessList))
65
- const hasBlobs = (tx.blobs != null || tx.blobVersionedHashes != null || tx.maxFeePerBlobGas != null)
66
- const hasAuthList = (tx.authorizationList != null && Array.isArray(tx.authorizationList))
67
-
68
- const explicitType = (tx.type != null) ? Number(tx.type) : null
69
-
70
- if ((explicitType === 2 || (explicitType == null && has1559)) && hasLegacy) {
71
- throw new ValueError('eip-1559 transaction does not support gasPrice')
72
- }
73
- if ((explicitType === 0 || explicitType === 1) && has1559) {
74
- throw new ValueError('pre-eip-1559 transaction does not support maxFeePerGas/maxPriorityFeePerGas')
75
- }
76
- if ((explicitType === 3 || hasBlobs) && hasLegacy) {
77
- throw new ValueError('blob transaction does not support gasPrice')
78
- }
79
- if ((explicitType === 4 || (explicitType == null && hasAuthList)) && hasLegacy) {
80
- throw new ValueError('eip-7702 transaction does not support gasPrice')
81
- }
82
-
83
- const feeData = await provider.getFeeData()
84
-
85
- let type = explicitType
86
- if (type == null) {
87
- if (hasAuthList) {
88
- type = 4
89
- } else if (hasBlobs) {
90
- type = 3
91
- } else if (feeData.maxFeePerGas != null && feeData.maxPriorityFeePerGas != null) {
92
- type = 2
93
- } else {
94
- type = 0
95
- }
96
- }
97
-
98
- let gasLimit
99
- if (tx.gasLimit != null) {
100
- gasLimit = tx.gasLimit
101
- } else if (hasAuthList) {
102
- gasLimit = await _estimateGasWithAuthList(provider, { from, ...tx })
103
- } else {
104
- gasLimit = await provider.estimateGas({ from, to: tx.to ?? null, data: tx.data ?? '0x', value: tx.value ?? 0 })
105
- }
106
-
107
- const populated = {
108
- from,
109
- to: tx.to ?? null,
110
- data: tx.data ?? '0x',
111
- value: tx.value ?? 0,
112
- chainId,
113
- nonce: (tx.nonce != null) ? Number(tx.nonce) : Number(await provider.getTransactionCount(from, 'pending')),
114
- gasLimit
115
- }
116
-
117
- if (type === 0 || type === 1) {
118
- populated.type = type
119
- populated.gasPrice = tx.gasPrice ?? feeData.gasPrice ?? feeData.maxFeePerGas
120
- if (type === 1 && hasAccessList) populated.accessList = tx.accessList
121
- return populated
122
- }
123
-
124
- if (type === 2) {
125
- populated.type = 2
126
- if (tx.gasPrice != null) {
127
- populated.maxFeePerGas = tx.gasPrice
128
- populated.maxPriorityFeePerGas = tx.gasPrice
129
- } else {
130
- populated.maxFeePerGas = tx.maxFeePerGas ?? feeData.maxFeePerGas
131
- populated.maxPriorityFeePerGas = tx.maxPriorityFeePerGas ?? feeData.maxPriorityFeePerGas
132
- }
133
- if (hasAccessList) populated.accessList = tx.accessList
134
- return populated
135
- }
136
-
137
- if (type === 3) {
138
- populated.type = 3
139
- populated.maxFeePerGas = tx.maxFeePerGas ?? feeData.maxFeePerGas
140
- populated.maxPriorityFeePerGas = tx.maxPriorityFeePerGas ?? feeData.maxPriorityFeePerGas
141
- if (tx.maxFeePerBlobGas == null) throw new ValueError('maxFeePerBlobGas is required for type 3 transactions')
142
- populated.maxFeePerBlobGas = tx.maxFeePerBlobGas
143
- if (tx.blobs != null) populated.blobs = tx.blobs
144
- if (tx.blobVersionedHashes != null) populated.blobVersionedHashes = tx.blobVersionedHashes
145
- if (hasAccessList) populated.accessList = tx.accessList
146
- return populated
147
- }
148
-
149
- if (type === 4) {
150
- populated.type = 4
151
- populated.maxFeePerGas = tx.maxFeePerGas ?? feeData.maxFeePerGas
152
- populated.maxPriorityFeePerGas = tx.maxPriorityFeePerGas ?? feeData.maxPriorityFeePerGas
153
- if (hasAccessList) populated.accessList = tx.accessList
154
- if (tx.authorizationList != null) populated.authorizationList = tx.authorizationList
155
- return populated
156
- }
157
-
158
- // Future types; pass-through
159
- populated.type = type
160
- if (hasAccessList) populated.accessList = tx.accessList
161
- if (hasLegacy) {
162
- populated.gasPrice = tx.gasPrice
163
- } else {
164
- populated.maxFeePerGas = tx.maxFeePerGas ?? feeData.maxFeePerGas
165
- populated.maxPriorityFeePerGas = tx.maxPriorityFeePerGas ?? feeData.maxPriorityFeePerGas
166
- }
167
- if (hasBlobs) {
168
- populated.maxFeePerBlobGas = tx.maxFeePerBlobGas
169
- if (tx.blobs != null) populated.blobs = tx.blobs
170
- if (tx.blobVersionedHashes != null) populated.blobVersionedHashes = tx.blobVersionedHashes
171
- }
172
- if (tx.authorizationList != null) populated.authorizationList = tx.authorizationList
173
-
174
- return populated
175
- }
176
-
177
- async function _estimateGasWithAuthList (provider, { from, to, value, data, authorizationList }) {
178
- const formatAuth = (auth) => {
179
- const { address, nonce, chainId } = auth
180
- const signature = auth.signature instanceof Signature
181
- ? auth.signature
182
- : Signature.from(auth.signature)
183
- return {
184
- address,
185
- nonce: toQuantity(nonce),
186
- chainId: toQuantity(chainId),
187
- r: toQuantity(signature.r),
188
- s: toQuantity(signature.s),
189
- yParity: toQuantity(signature.yParity)
190
- }
191
- }
192
- const rpcTx = {
193
- from,
194
- to,
195
- value: toQuantity(value ?? 0),
196
- data: data ?? '0x',
197
- type: '0x04',
198
- authorizationList: authorizationList.map(formatAuth)
199
- }
200
- const result = await provider.send('eth_estimateGas', [rpcTx])
201
- return BigInt(result)
202
- }
@@ -1,85 +0,0 @@
1
- /**
2
- * A fully or partially specified EVM transaction, prior to signing.
3
- */
4
- export type UnsignedEvmTransaction = {
5
- /**
6
- * - The id of the chain the transaction targets.
7
- */
8
- chainId: number;
9
- /**
10
- * - The sender's transaction count, used to order transactions.
11
- */
12
- nonce: number;
13
- /**
14
- * - The sender's address.
15
- */
16
- from: string;
17
- /**
18
- * - The recipient's address, or null for contract creation.
19
- */
20
- to: string | null;
21
- /**
22
- * - The transaction's calldata as a hex string.
23
- */
24
- data: string;
25
- /**
26
- * - The amount of native currency (in wei) to transfer.
27
- */
28
- value: number | bigint;
29
- /**
30
- * - The EIP-2718 transaction type (0/1 legacy, 2 EIP-1559, 3 EIP-4844, 4 EIP-7702).
31
- */
32
- type: number;
33
- /**
34
- * - The maximum amount of gas the transaction may consume.
35
- */
36
- gasLimit: number | bigint;
37
- /**
38
- * - The gas price (in wei) for legacy (type 0/1) transactions.
39
- */
40
- gasPrice?: number | bigint;
41
- /**
42
- * - The maximum total fee (in wei) per gas for EIP-1559 transactions.
43
- */
44
- maxFeePerGas?: number | bigint;
45
- /**
46
- * - The maximum priority fee (in wei) per gas for EIP-1559 transactions.
47
- */
48
- maxPriorityFeePerGas?: number | bigint;
49
- /**
50
- * - The EIP-2930 access list of addresses and storage keys.
51
- */
52
- accessList?: any[];
53
- /**
54
- * - The maximum fee (in wei) per blob gas for EIP-4844 transactions.
55
- */
56
- maxFeePerBlobGas?: number | bigint;
57
- /**
58
- * - The blobs to include in an EIP-4844 transaction.
59
- */
60
- blobs?: any[];
61
- /**
62
- * - The versioned hashes of the EIP-4844 blobs.
63
- */
64
- blobVersionedHashes?: string[];
65
- /**
66
- * - The EIP-7702 authorization tuples.
67
- */
68
- authorizationList?: AuthorizationLike[];
69
- };
70
- export type Provider = import("ethers").Provider;
71
- export type AuthorizationLike = import("ethers").AuthorizationLike;
72
- /**
73
- * Build a fully populated unsigned transaction ready for signing.
74
- *
75
- * Resolves chain ID, nonce, gas limit and fee fields from the provider when not
76
- * explicitly supplied in `tx`. Supports legacy (type 0/1), EIP-1559 (type 2),
77
- * EIP-4844 (type 3) and EIP-7702 (type 4) transaction styles.
78
- *
79
- * @param {Provider} provider - An ethers-compatible JSON-RPC provider.
80
- * @param {string} from - The sender address.
81
- * @param {UnsignedEvmTransaction} tx - The partial transaction to populate.
82
- * @returns {Promise<UnsignedEvmTransaction>} The fully populated unsigned transaction.
83
- * @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
84
- */
85
- export function populateTransactionEvm(provider: Provider, from: string, tx: UnsignedEvmTransaction): Promise<UnsignedEvmTransaction>;