|
| 1 | +import { getChainId } from '~viem/actions/index.js' |
| 2 | +import type { Chain } from '~viem/index.js' |
1 | 3 | import type { Account } from '../../../accounts/types.js' |
| 4 | +import { parseAccount } from '../../../accounts/utils/parseAccount.js' |
| 5 | +import { estimateGas } from '../../../actions/public/estimateGas.js' |
| 6 | +import { getTransactionCount } from '../../../actions/public/getTransactionCount.js' |
2 | 7 | import { |
3 | | - type ParseAccountErrorType, |
4 | | - parseAccount, |
5 | | -} from '../../../accounts/utils/parseAccount.js' |
6 | | -import { type EstimateFeesPerGasErrorType } from '../../../actions/public/estimateFeesPerGas.js' |
7 | | -import { |
8 | | - type EstimateGasErrorType, |
9 | | - type EstimateGasParameters, |
10 | | - estimateGas, |
11 | | -} from '../../../actions/public/estimateGas.js' |
12 | | -import { type GetBlockErrorType } from '../../../actions/public/getBlock.js' |
13 | | -import { |
14 | | - type GetTransactionCountErrorType, |
15 | | - getTransactionCount, |
16 | | -} from '../../../actions/public/getTransactionCount.js' |
17 | | -import { prepareTransactionRequest as originalPrepareTransactionRequest } from '../../../actions/wallet/prepareTransactionRequest.js' |
| 8 | + type PrepareTransactionRequestParameters, |
| 9 | + type PrepareTransactionRequestReturnType, |
| 10 | + prepareTransactionRequest as originalPrepareTransactionRequest, |
| 11 | +} from '../../../actions/wallet/prepareTransactionRequest.js' |
18 | 12 | import type { Client } from '../../../clients/createClient.js' |
19 | 13 | import type { Transport } from '../../../clients/transports/createTransport.js' |
20 | | -import { |
21 | | - AccountNotFoundError, |
22 | | - type AccountNotFoundErrorType, |
23 | | -} from '../../../errors/account.js' |
24 | | -import type { GetAccountParameter } from '../../../types/account.js' |
25 | | -import type { GetChain } from '../../../types/chain.js' |
26 | | -import { type TransactionSerializable } from '../../../types/transaction.js' |
27 | | -import type { UnionOmit } from '../../../types/utils.js' |
28 | | -import type { FormattedTransactionRequest } from '../../../utils/formatters/transactionRequest.js' |
| 14 | +import { AccountNotFoundError } from '../../../errors/account.js' |
29 | 15 | import { getAction } from '../../../utils/getAction.js' |
30 | | -import type { |
31 | | - AssertRequestErrorType, |
32 | | - AssertRequestParameters, |
33 | | -} from '../../../utils/transaction/assertRequest.js' |
34 | | -import { assertRequest } from '../../../utils/transaction/assertRequest.js' |
35 | | -import { type GetTransactionType } from '../../../utils/transaction/getTransactionType.js' |
36 | 16 | import { type ChainEIP712, isEip712Transaction } from '../types.js' |
37 | 17 |
|
38 | | -export type PrepareTransactionRequestParameters< |
39 | | - TChain extends ChainEIP712 | undefined = ChainEIP712 | undefined, |
40 | | - TAccount extends Account | undefined = Account | undefined, |
41 | | - TChainOverride extends ChainEIP712 | undefined = ChainEIP712 | undefined, |
42 | | -> = UnionOmit< |
43 | | - FormattedTransactionRequest< |
44 | | - TChainOverride extends ChainEIP712 ? TChainOverride : TChain |
45 | | - >, |
46 | | - 'from' |
47 | | -> & |
48 | | - GetAccountParameter<TAccount> & |
49 | | - GetChain<TChain, TChainOverride> |
50 | | - |
51 | | -export type PrepareTransactionRequestReturnType< |
52 | | - TChain extends ChainEIP712 | undefined = ChainEIP712 | undefined, |
53 | | - TAccount extends Account | undefined = Account | undefined, |
54 | | - TChainOverride extends ChainEIP712 | undefined = ChainEIP712 | undefined, |
55 | | -> = FormattedTransactionRequest< |
56 | | - TChainOverride extends ChainEIP712 ? TChainOverride : TChain |
57 | | -> & |
58 | | - GetAccountParameter<TAccount> & |
59 | | - GetChain<TChain, TChainOverride> |
60 | | - |
61 | | -export type PrepareTransactionRequestErrorType = |
62 | | - | AccountNotFoundErrorType |
63 | | - | AssertRequestErrorType |
64 | | - | ParseAccountErrorType |
65 | | - | GetBlockErrorType |
66 | | - | GetTransactionCountErrorType |
67 | | - | EstimateGasErrorType |
68 | | - | EstimateFeesPerGasErrorType |
69 | | - |
70 | 18 | /** |
71 | 19 | * Prepares a transaction request for signing. |
72 | 20 | * |
@@ -110,59 +58,56 @@ export type PrepareTransactionRequestErrorType = |
110 | 58 | export async function prepareTransactionRequest< |
111 | 59 | TChain extends ChainEIP712 | undefined, |
112 | 60 | TAccount extends Account | undefined, |
113 | | - TChainOverride extends ChainEIP712 | undefined = undefined, |
| 61 | + TChainOverride extends Chain | undefined = undefined, |
114 | 62 | >( |
115 | 63 | client: Client<Transport, TChain, TAccount>, |
116 | | - args: PrepareTransactionRequestParameters<TChain, TAccount, TChainOverride>, |
| 64 | + argsIncoming: PrepareTransactionRequestParameters< |
| 65 | + TChain, |
| 66 | + TAccount, |
| 67 | + TChainOverride |
| 68 | + >, |
117 | 69 | ): Promise< |
118 | 70 | PrepareTransactionRequestReturnType<TChain, TAccount, TChainOverride> |
119 | 71 | > { |
120 | | - const { account: account_ = client.account, nonce, gas } = args |
121 | | - if (!account_) throw new AccountNotFoundError() |
122 | | - const account = parseAccount(account_) |
| 72 | + const args = { |
| 73 | + ...argsIncoming, |
| 74 | + account: argsIncoming.account || client.account, |
| 75 | + chain: argsIncoming.chain || client.chain, |
| 76 | + } |
| 77 | + |
| 78 | + if (!args.account) throw new AccountNotFoundError() |
| 79 | + const account = parseAccount(args.account) |
123 | 80 |
|
124 | | - const request = { ...args, from: account.address } |
| 81 | + const chainId = await getAction(client, getChainId, 'getChainId')({}) |
| 82 | + const request = { ...args, from: account.address, chainId } |
125 | 83 |
|
126 | | - if (typeof nonce === 'undefined') { |
| 84 | + if (args.nonce === undefined) { |
127 | 85 | request.nonce = await getAction( |
128 | 86 | client, |
129 | 87 | getTransactionCount, |
| 88 | + 'getTransactionCount', |
130 | 89 | )({ |
131 | 90 | address: account.address, |
132 | 91 | blockTag: 'pending', |
133 | 92 | }) |
134 | 93 | } |
135 | 94 |
|
136 | | - if (isEip712Transaction(request as TransactionSerializable)) { |
137 | | - request.type = |
138 | | - 'eip712' as GetTransactionType<TransactionSerializable> as any |
139 | | - // Do nothing... |
| 95 | + if (isEip712Transaction({ ...request })) { |
| 96 | + request.type = 'eip712' |
140 | 97 |
|
141 | | - assertRequest(request as AssertRequestParameters) |
142 | | - |
143 | | - if (typeof gas === 'undefined') { |
| 98 | + if (request.gas === undefined) { |
144 | 99 | request.gas = await getAction( |
145 | 100 | client, |
146 | | - estimateGas, |
| 101 | + estimateGas<TChain, TAccount>, |
| 102 | + 'estimateGas', |
147 | 103 | )({ |
148 | 104 | ...request, |
149 | | - account: { address: account.address, type: 'json-rpc' }, |
150 | | - } as EstimateGasParameters) |
| 105 | + type: 'eip712', |
| 106 | + }) |
151 | 107 | } |
152 | 108 |
|
153 | | - return request as unknown as PrepareTransactionRequestReturnType< |
154 | | - TChain, |
155 | | - TAccount, |
156 | | - TChainOverride |
157 | | - > |
| 109 | + return request |
158 | 110 | } |
159 | 111 |
|
160 | | - return originalPrepareTransactionRequest( |
161 | | - client, |
162 | | - args as unknown as PrepareTransactionRequestParameters, |
163 | | - ) as unknown as PrepareTransactionRequestReturnType< |
164 | | - TChain, |
165 | | - TAccount, |
166 | | - TChainOverride |
167 | | - > |
| 112 | + return originalPrepareTransactionRequest(client, args) |
168 | 113 | } |
0 commit comments