Skip to content
On this page

sendTransaction

Creates, signs, and sends a new transaction to the network.

Usage

ts
import { getAccount } from 'viem'
import { walletClient } from './client'
 
const account = getAccount('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266')
 
const hash = await walletClient.sendTransaction({ 
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n
})
// '0x...'
ts
import { createWalletClient, custom } from 'viem'

export const walletClient = createWalletClient({
  transport: custom(window.ethereum)
})

Returns

Hash

The Transaction hash.

Parameters

account

  • Type: Account

The Account sender. Read more.

ts
const hash = await walletClient.sendTransaction({
  account: getAccount('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'), 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n
})

to

  • Type: number

The transaction recipient or contract address.

ts
const hash = await walletClient.sendTransaction({
  account, 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n,
  nonce: 69
})

accessList (optional)

The access list.

ts
const data = await publicClient.sendTransaction({
  accessList: [ 
    {
      address: '0x1',
      storageKeys: ['0x1'],
    },
  ],
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})

assertChain (optional)

  • Type: boolean
  • Default: true

Throws an error if chain does not match the current wallet chain.

Defaults to true, but you can turn this off if your dapp is primarily multi-chain.

ts
import { optimism } from 'viem/chains' 

const hash = await walletClient.sendTransaction({
  assertChain: false, 
  chain: optimism, 
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n
})

chain (optional)

  • Type: Chain
  • Default: walletClient.chain

The target chain. If there is a mismatch between the wallet's current chain & the target chain, an error will be thrown if assertChain is truthy.

The chain is also used to infer its request type (e.g. the Celo chain has a gatewayFee that you can pass through to sendTransaction).

ts
import { optimism } from 'viem/chains' 

const hash = await walletClient.sendTransaction({
  chain: optimism, 
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n
})

data (optional)

  • Type: 0x${string}

A contract hashed method call with encoded args.

ts
const hash = await walletClient.sendTransaction({
  data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n
})

gasPrice (optional)

  • Type: bigint

The price (in wei) to pay per gas. Only applies to Legacy Transactions.

ts
const hash = await walletClient.sendTransaction({
  account,
  gasPrice: parseGwei('20'), 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1') 
})

maxFeePerGas (optional)

  • Type: bigint

Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions

ts
const hash = await walletClient.sendTransaction({
  account,
  maxFeePerGas: parseGwei('20'),  
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})

maxPriorityFeePerGas (optional)

  • Type: bigint

Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions

ts
const hash = await walletClient.sendTransaction({
  account,
  maxFeePerGas: parseGwei('20'),
  maxPriorityFeePerGas: parseGwei('2'), 
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1')
})

nonce (optional)

  • Type: number

Unique number identifying this transaction.

ts
const hash = await walletClient.sendTransaction({
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: 1000000000000000000n,
  nonce: 69 
})

value (optional)

  • Type: number

Value in wei sent with this transaction.

ts
const hash = await walletClient.sendTransaction({
  account,
  to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
  value: parseEther('1'), 
  nonce: 69
})

Tips

  • For dapps: When using this action, it is assumed that the user has connected to their wallet (e.g. given permission for the dapp to access their accounts via requestAddresses). You can also check if the user has granted access to their accounts via getAddresses

Live Example

Check out the usage of sendTransaction in the live Sending Transactions Example below.

JSON-RPC Methods

Released under the MIT License.