import type { Account } from '../../accounts/types.js' import { type ParseAccountErrorType, parseAccount, } from '../../accounts/utils/parseAccount.js' import type { SignTransactionErrorType } from '../../accounts/utils/signTransaction.js' import type { Client } from '../../clients/createClient.js' import type { Transport } from '../../clients/transports/createTransport.js' import { AccountNotFoundError } from '../../errors/account.js' import type { BaseError } from '../../errors/base.js' import type { ErrorType } from '../../errors/utils.js' import type { GetAccountParameter } from '../../types/account.js' import type { Chain } from '../../types/chain.js' import type { GetChain } from '../../types/chain.js' import type { Hash } from '../../types/misc.js' import type { TransactionRequest, TransactionSerializable, } from '../../types/transaction.js' import type { UnionOmit } from '../../types/utils.js' import type { RequestErrorType } from '../../utils/buildRequest.js' import { type AssertCurrentChainErrorType, assertCurrentChain, } from '../../utils/chain.js' import { type GetTransactionErrorReturnType, getTransactionError, } from '../../utils/errors/getTransactionError.js' import { extract } from '../../utils/formatters/extract.js' import { type FormattedTransactionRequest, formatTransactionRequest, } from '../../utils/formatters/transactionRequest.js' import { type AssertRequestErrorType, type AssertRequestParameters, assertRequest, } from '../../utils/transaction/assertRequest.js' import { type GetChainIdErrorType, getChainId } from '../public/getChainId.js' import { type PrepareTransactionRequestErrorType, prepareTransactionRequest, } from './prepareTransactionRequest.js' import { type SendRawTransactionReturnType, sendRawTransaction, } from './sendRawTransaction.js' export type SendTransactionParameters< TChain extends Chain | undefined = Chain | undefined, TAccount extends Account | undefined = Account | undefined, TChainOverride extends Chain | undefined = Chain | undefined, > = UnionOmit< FormattedTransactionRequest< TChainOverride extends Chain ? TChainOverride : TChain >, 'from' > & GetAccountParameter & GetChain export type SendTransactionReturnType = Hash export type SendTransactionErrorType = | ParseAccountErrorType | GetTransactionErrorReturnType< | AssertCurrentChainErrorType | AssertRequestErrorType | GetChainIdErrorType | PrepareTransactionRequestErrorType | SendRawTransactionReturnType | SignTransactionErrorType | RequestErrorType > | ErrorType /** * Creates, signs, and sends a new transaction to the network. * * - Docs: https://viem.sh/docs/actions/wallet/sendTransaction.html * - Examples: https://stackblitz.com/github/wagmi-dev/viem/tree/main/examples/transactions/sending-transactions * - JSON-RPC Methods: * - JSON-RPC Accounts: [`eth_sendTransaction`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_sendtransaction) * - Local Accounts: [`eth_sendRawTransaction`](https://ethereum.org/en/developers/docs/apis/json-rpc/#eth_sendrawtransaction) * * @param client - Client to use * @param parameters - {@link SendTransactionParameters} * @returns The [Transaction](https://viem.sh/docs/glossary/terms.html#transaction) hash. {@link SendTransactionReturnType} * * @example * import { createWalletClient, custom } from 'viem' * import { mainnet } from 'viem/chains' * import { sendTransaction } from 'viem/wallet' * * const client = createWalletClient({ * chain: mainnet, * transport: custom(window.ethereum), * }) * const hash = await sendTransaction(client, { * account: '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e', * to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', * value: 1000000000000000000n, * }) * * @example * // Account Hoisting * import { createWalletClient, http } from 'viem' * import { privateKeyToAccount } from 'viem/accounts' * import { mainnet } from 'viem/chains' * import { sendTransaction } from 'viem/wallet' * * const client = createWalletClient({ * account: privateKeyToAccount('0x…'), * chain: mainnet, * transport: http(), * }) * const hash = await sendTransaction(client, { * to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', * value: 1000000000000000000n, * }) */ export async function sendTransaction< TChain extends Chain | undefined, TAccount extends Account | undefined, TChainOverride extends Chain | undefined, >( client: Client, args: SendTransactionParameters, ): Promise { const { account: account_ = client.account, chain = client.chain, accessList, data, gas, gasPrice, maxFeePerGas, maxPriorityFeePerGas, nonce, to, value, ...rest } = args if (!account_) throw new AccountNotFoundError({ docsPath: '/docs/actions/wallet/sendTransaction', }) const account = parseAccount(account_) try { assertRequest(args as AssertRequestParameters) let chainId if (chain !== null) { chainId = await getChainId(client) assertCurrentChain({ currentChainId: chainId, chain, }) } if (account.type === 'local') { // Prepare the request for signing (assign appropriate fees, etc.) const request = await prepareTransactionRequest(client, { account, accessList, chain, data, gas, gasPrice, maxFeePerGas, maxPriorityFeePerGas, nonce, to, value, ...rest, } as any) if (!chainId) chainId = await getChainId(client) const serializer = chain?.serializers?.transaction const serializedTransaction = (await account.signTransaction( { ...request, chainId, } as TransactionSerializable, { serializer }, )) as Hash return await sendRawTransaction(client, { serializedTransaction, }) } const format = chain?.formatters?.transactionRequest?.format || formatTransactionRequest const request = format({ // Pick out extra data that might exist on the chain's transaction request type. ...extract(rest, { format }), accessList, data, from: account.address, gas, gasPrice, maxFeePerGas, maxPriorityFeePerGas, nonce, to, value, } as TransactionRequest) return await client.request({ method: 'eth_sendTransaction', params: [request], }) } catch (err) { throw getTransactionError(err as BaseError, { ...args, account, chain: args.chain || undefined, }) } }