Skip to content

Commit ca1004a

Browse files
wip: type fixes post rebase
1 parent 7af016c commit ca1004a

File tree

16 files changed

+198
-442
lines changed

16 files changed

+198
-442
lines changed
Lines changed: 37 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,20 @@
1+
import { getChainId } from '~viem/actions/index.js'
2+
import type { Chain } from '~viem/index.js'
13
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'
27
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'
1812
import type { Client } from '../../../clients/createClient.js'
1913
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'
2915
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'
3616
import { type ChainEIP712, isEip712Transaction } from '../types.js'
3717

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-
7018
/**
7119
* Prepares a transaction request for signing.
7220
*
@@ -110,59 +58,56 @@ export type PrepareTransactionRequestErrorType =
11058
export async function prepareTransactionRequest<
11159
TChain extends ChainEIP712 | undefined,
11260
TAccount extends Account | undefined,
113-
TChainOverride extends ChainEIP712 | undefined = undefined,
61+
TChainOverride extends Chain | undefined = undefined,
11462
>(
11563
client: Client<Transport, TChain, TAccount>,
116-
args: PrepareTransactionRequestParameters<TChain, TAccount, TChainOverride>,
64+
argsIncoming: PrepareTransactionRequestParameters<
65+
TChain,
66+
TAccount,
67+
TChainOverride
68+
>,
11769
): Promise<
11870
PrepareTransactionRequestReturnType<TChain, TAccount, TChainOverride>
11971
> {
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)
12380

124-
const request = { ...args, from: account.address }
81+
const chainId = await getAction(client, getChainId, 'getChainId')({})
82+
const request = { ...args, from: account.address, chainId }
12583

126-
if (typeof nonce === 'undefined') {
84+
if (args.nonce === undefined) {
12785
request.nonce = await getAction(
12886
client,
12987
getTransactionCount,
88+
'getTransactionCount',
13089
)({
13190
address: account.address,
13291
blockTag: 'pending',
13392
})
13493
}
13594

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'
14097

141-
assertRequest(request as AssertRequestParameters)
142-
143-
if (typeof gas === 'undefined') {
98+
if (request.gas === undefined) {
14499
request.gas = await getAction(
145100
client,
146-
estimateGas,
101+
estimateGas<TChain, TAccount>,
102+
'estimateGas',
147103
)({
148104
...request,
149-
account: { address: account.address, type: 'json-rpc' },
150-
} as EstimateGasParameters)
105+
type: 'eip712',
106+
})
151107
}
152108

153-
return request as unknown as PrepareTransactionRequestReturnType<
154-
TChain,
155-
TAccount,
156-
TChainOverride
157-
>
109+
return request
158110
}
159111

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)
168113
}

src/chains/zksync/actions/sendTransaction.test.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,21 @@ import { privateKeyToAccount } from '../../../accounts/privateKeyToAccount.js'
55
import { createWalletClient } from '../../../clients/createWalletClient.js'
66
import { http } from '../../../clients/transports/http.js'
77
import { parseGwei } from '../../../utils/unit/parseGwei.js'
8-
import { zkSyncTestnet } from '../index.js'
9-
import { sendTransaction } from './sendTransaction.js'
8+
import { zkSyncSepoliaTestnet } from '../chains.js'
9+
import { sendTransactionZ } from './sendTransaction.js'
1010

1111
const sourceAccount = accounts[0]
1212
const targetAccount = accounts[1]
1313

1414
describe('zksync on anvil', () => {
1515
const walletClient = createWalletClient({
16-
chain: zkSyncTestnet,
16+
chain: zkSyncSepoliaTestnet,
1717
transport: http(localHttpUrl),
1818
})
1919

2020
test('non-eip712', async () => {
2121
expect(
22-
await sendTransaction(walletClient, {
23-
chain: zkSyncTestnet,
22+
await sendTransactionZ(walletClient, {
2423
account: privateKeyToAccount(sourceAccount.privateKey),
2524
to: targetAccount.address,
2625
maxFeePerGas: parseGwei('25'),
@@ -36,13 +35,12 @@ describe('zksync on anvil', () => {
3635

3736
describe('zksync on zkSyncTestnet', () => {
3837
const walletClient = createWalletClient({
39-
chain: zkSyncTestnet,
40-
transport: http(zkSyncTestnet.rpcUrls.default.http[0]),
38+
chain: zkSyncSepoliaTestnet,
39+
transport: http(zkSyncSepoliaTestnet.rpcUrls.default.http[0]),
4140
})
4241
test('eip712', async () => {
4342
expect(
44-
await sendTransaction(walletClient, {
45-
chain: zkSyncTestnet,
43+
await sendTransactionZ(walletClient, {
4644
account: privateKeyToAccount(sourceAccount.privateKey),
4745
to: targetAccount.address,
4846
maxFeePerGas: parseGwei('25'),

0 commit comments

Comments
 (0)