Skip to content

Commit

Permalink
refactor: fix most of the typecheck errors
Browse files Browse the repository at this point in the history
  • Loading branch information
roberts-pumpurs committed Dec 27, 2023
1 parent 43b710b commit be7a86d
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 539 deletions.
170 changes: 103 additions & 67 deletions src/chains/zksync/actions/prepareTransactionRequest.ts
Original file line number Diff line number Diff line change
@@ -1,87 +1,107 @@
import { getChainId } from '~viem/actions/index.js'
import type { Chain } from '~viem/index.js'
import type { Address } from 'abitype'
import type { Account } from '../../../accounts/types.js'
import { parseAccount } from '../../../accounts/utils/parseAccount.js'
import { estimateGas } from '../../../actions/public/estimateGas.js'
import { getTransactionCount } from '../../../actions/public/getTransactionCount.js'
import {
type PrepareTransactionRequestParameters,
type PrepareTransactionRequestReturnType,
prepareTransactionRequest as originalPrepareTransactionRequest,
} from '../../../actions/wallet/prepareTransactionRequest.js'
import type { Client } from '../../../clients/createClient.js'
import type { Transport } from '../../../clients/transports/createTransport.js'
import { AccountNotFoundError } from '../../../errors/account.js'

import type {
PrepareTransactionRequestParameterType,
PrepareTransactionRequestReturnType,
} from '../../../index.js'
import type { GetAccountParameter } from '../../../types/account.js'
import type { Chain, DeriveChain } from '../../../types/chain.js'
import type { GetChainParameter } from '../../../types/chain.js'
import type { UnionOmit } from '../../../types/utils.js'
import type { FormattedTransactionRequest } from '../../../utils/formatters/transactionRequest.js'
import { getAction } from '../../../utils/getAction.js'
import { type ChainEIP712, isEip712Transaction } from '../types.js'

/**
* Prepares a transaction request for signing.
*
* - Docs: https://viem.sh/docs/zksync/actions/prepareEip712TransactionRequest.html
*
* @param args - {@link PrepareTransactionRequestParameters}
* @returns The transaction request. {@link PrepareTransactionRequestReturnType}
*
* @example
* import { createWalletClient, custom } from 'viem'
* import { zkSync } from 'viem/chains'
* import { prepareTransactionRequest } from 'viem/chains/zksync'
*
* const client = createWalletClient({
* chain: zkSync,
* transport: custom(window.ethereum),
* })
* const request = await prepareTransactionRequest(client, {
* account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
* to: '0x0000000000000000000000000000000000000000',
* value: 1n,
* })
*
* @example
* // Account Hoisting
* import { createWalletClient, http } from 'viem'
* import { privateKeyToAccount } from 'viem/accounts'
* import { zkSync } from 'viem/chains'
* import { prepareEip712TransactionRequest } from 'viem/chains/zksync'
*
* const client = createWalletClient({
* account: privateKeyToAccount('0x…'),
* chain: zkSync,
* transport: custom(window.ethereum),
* })
* const request = await prepareEip712TransactionRequest(client, {
* to: '0x0000000000000000000000000000000000000000',
* value: 1n,
* })
*/
import { getChainId } from '~viem/actions/index.js'
import { prepareTransactionRequest as originalPrepareTransactionRequest } from '../../../actions/wallet/prepareTransactionRequest.js'

type Eip712PrepareTransactionRequestParameterType =
| PrepareTransactionRequestParameterType
| 'customSignature'
| 'paymaster'
| 'paymasterInput'
| 'gasPerPubdata'
| 'factoryDeps'

export type PrepareTransactionRequestParameters<
TChain extends Chain | undefined = Chain | undefined,
TAccount extends Account | undefined = Account | undefined,
TChainOverride extends Chain | undefined = Chain | undefined,
TAccountOverride extends Account | Address | undefined =
| Account
| Address
| undefined,
TParameterType extends
Eip712PrepareTransactionRequestParameterType = Eip712PrepareTransactionRequestParameterType,
///
derivedChain extends Chain | undefined = DeriveChain<TChain, TChainOverride>,
> = UnionOmit<FormattedTransactionRequest<derivedChain>, 'from'> &
GetAccountParameter<TAccount, TAccountOverride, false> &
GetChainParameter<TChain, TChainOverride> & {
parameters?: TParameterType[]
}

export async function prepareTransactionRequest<
TChain extends ChainEIP712 | undefined,
TAccount extends Account | undefined,
TChainOverride extends Chain | undefined = undefined,
TParameterType extends PrepareTransactionRequestParameterType,
TAccountOverride extends Account | Address | undefined,
TChainOverride extends Chain | undefined = ChainEIP712 | undefined,
>(
client: Client<Transport, TChain, TAccount>,
argsIncoming: PrepareTransactionRequestParameters<
args: PrepareTransactionRequestParameters<
TChain,
TAccount,
TChainOverride
TChainOverride,
TAccountOverride,
TParameterType
>,
): Promise<
PrepareTransactionRequestReturnType<TChain, TAccount, TChainOverride>
PrepareTransactionRequestReturnType<
TChain,
TAccount,
TChainOverride,
TAccountOverride,
TParameterType
>
> {
const args = {
...argsIncoming,
account: argsIncoming.account || client.account,
chain: argsIncoming.chain || client.chain,
}
const {
account: account_ = client.account,
nonce,
parameters = [
'fees',
'gas',
'nonce',
'type',
'customSignature',
'paymaster',
'paymasterInput',
'gasPerPubdata',
'factoryDeps',
],
} = args
const account = account_ ? parseAccount(account_) : undefined

if (!args.account) throw new AccountNotFoundError()
const account = parseAccount(args.account)
let chainId = 0
if (args.chain?.id) {
chainId = args.chain.id
} else {
chainId = await getAction(client, getChainId, 'getChainId')({})
}

const chainId = await getAction(client, getChainId, 'getChainId')({})
const request = { ...args, from: account.address, chainId }
const request = {
...args,
chainId,
...(account ? { from: account?.address } : {}),
}

if (args.nonce === undefined) {
if (parameters.includes('nonce') && nonce === undefined && account)
request.nonce = await getAction(
client,
getTransactionCount,
Expand All @@ -90,7 +110,6 @@ export async function prepareTransactionRequest<
address: account.address,
blockTag: 'pending',
})
}

if (isEip712Transaction({ ...request })) {
request.type = 'eip712'
Expand All @@ -102,12 +121,29 @@ export async function prepareTransactionRequest<
'estimateGas',
)({
...request,
type: 'eip712',
})
account: account
? { address: account.address, type: 'json-rpc' }
: undefined,
} as any)
}

return request
return request as unknown as PrepareTransactionRequestReturnType<
TChain,
TAccount,
TChainOverride,
TAccountOverride,
TParameterType
>
}

return originalPrepareTransactionRequest(client, args)
return (await originalPrepareTransactionRequest(
client,
args,
)) as unknown as PrepareTransactionRequestReturnType<
TChain,
TAccount,
TChainOverride,
TAccountOverride,
TParameterType
>
}
6 changes: 3 additions & 3 deletions src/chains/zksync/actions/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createWalletClient } from '../../../clients/createWalletClient.js'
import { http } from '../../../clients/transports/http.js'
import { parseGwei } from '../../../utils/unit/parseGwei.js'
import { zkSyncSepoliaTestnet } from '../chains.js'
import { sendTransactionZ } from './sendTransaction.js'
import { sendTransaction } from './sendTransaction.js'

const sourceAccount = accounts[0]
const targetAccount = accounts[1]
Expand All @@ -19,7 +19,7 @@ describe('zksync on anvil', () => {

test('non-eip712', async () => {
expect(
await sendTransactionZ(walletClient, {
await sendTransaction(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
to: targetAccount.address,
maxFeePerGas: parseGwei('25'),
Expand All @@ -40,7 +40,7 @@ describe('zksync on zkSyncTestnet', () => {
})
test('eip712', async () => {
expect(
await sendTransactionZ(walletClient, {
await sendTransaction(walletClient, {
account: privateKeyToAccount(sourceAccount.privateKey),
to: targetAccount.address,
maxFeePerGas: parseGwei('25'),
Expand Down
12 changes: 6 additions & 6 deletions src/chains/zksync/actions/sendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { getAction } from '../../../utils/getAction.js'
import { assertRequest } from '../../../utils/transaction/assertRequest.js'
import { type ChainEIP712, isEip712Transaction } from '../types.js'
import { prepareTransactionRequest } from './prepareTransactionRequest.js'
import { signEip712Transaction } from './signTransaction.js'
import { signTransaction } from './signTransaction.js'

/**
* Creates, signs, and sends a new transaction to the network.
Expand Down Expand Up @@ -72,7 +72,7 @@ import { signEip712Transaction } from './signTransaction.js'
export async function sendTransaction<
TChain extends ChainEIP712 | undefined,
TAccount extends Account | undefined,
TChainOverride extends Chain | undefined = undefined,
TChainOverride extends Chain | undefined = ChainEIP712 | undefined,
>(
client: Client<Transport, TChain, TAccount>,
argsIncoming: SendTransactionParameters<TChain, TAccount, TChainOverride>,
Expand Down Expand Up @@ -114,16 +114,16 @@ export async function sendTransaction<
client,
prepareTransactionRequest,
'prepareTransactionRequest',
)({ ...args })
)({ ...args } as any)

if (!chainId)
chainId = await getAction(client, getChainId, 'getChainId')({})

// EIP712 sign will be done inside the sign transaction
const serializedTransaction = (await signEip712Transaction(client, {
const serializedTransaction = (await signTransaction(client, {
...request,
chainId,
})) as Hash
} as any)) as Hash

return await getAction(
client,
Expand All @@ -140,7 +140,7 @@ export async function sendTransaction<
client,
prepareTransactionRequest,
'prepareTransactionRequest',
)({ ...argsIncoming, chainId })
)({ ...argsIncoming, chainId } as any)

if (!chainId)
chainId = await getAction(client, getChainId, 'getChainId')({})
Expand Down
2 changes: 1 addition & 1 deletion src/chains/zksync/actions/signTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createWalletClient } from '../../../clients/createWalletClient.js'
import { http } from '../../../clients/transports/http.js'
import { parseGwei } from '../../../utils/unit/parseGwei.js'
import { zkSyncSepoliaTestnet } from '../../index.js'
import { signEip712Transaction as signTransaction } from './signTransaction.js'
import { signTransaction } from './signTransaction.js'

const sourceAccount = accounts[0]

Expand Down
22 changes: 10 additions & 12 deletions src/chains/zksync/actions/signTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {
Chain,
SignTransactionParameters,
SignTransactionReturnType,
} from '~viem/index.js'
Expand Down Expand Up @@ -60,10 +59,10 @@ import { type ChainEIP712, isEip712Transaction } from '../types.js'
* value: 1n,
* })
*/
export async function signEip712Transaction<
export async function signTransaction<
TChain extends ChainEIP712 | undefined,
TAccount extends Account | undefined,
TChainOverride extends Chain | undefined = undefined,
TChainOverride extends ChainEIP712 | undefined,
>(
client: Client<Transport, TChain, TAccount>,
argsIncoming: SignTransactionParameters<TChain, TAccount, TChainOverride>,
Expand All @@ -89,10 +88,7 @@ export async function signEip712Transaction<
chain: args.chain,
})

const formatters = args.chain?.formatters || client.chain?.formatters
const format =
formatters?.transactionRequest?.format || formatTransactionRequest

// For EIP712 we don't need to ask MetaMask to sign it,
if (
client.chain?.custom.eip712domain?.eip712domain &&
client.chain?.serializers?.transaction &&
Expand Down Expand Up @@ -132,24 +128,26 @@ export async function signEip712Transaction<
{
...args,
chainId,
// TODO: I am suspicious that we actually don't want to override the type here.
type: 'eip712',
// TODO: Get rid of this `as any` when we fix the `type` issue.
type: args.type as any,
from: account.address,
gasPrice: undefined,
},
{ serializer: client.chain?.serializers?.transaction },
)
}

// For EIP712 we don't need to ask MetaMask to sign it,
const formatters = args.chain?.formatters || client.chain?.formatters
const format =
formatters?.transactionRequest?.format || formatTransactionRequest
return await client.request({
method: 'eth_signTransaction',
params: [
{
...format(args),
...format(args as any),
chainId: numberToHex(chainId),
from: account.address,
},
} as any,
],
})
}
32 changes: 0 additions & 32 deletions src/chains/zksync/actions/writeContract.test.ts

This file was deleted.

Loading

0 comments on commit be7a86d

Please sign in to comment.