clawchain/viem/site/pages/tempo/transactions.mdx

328 lines
9.7 KiB
Plaintext

# Tempo Transactions
Tempo Transactions are a Tempo-native EIP-2718 transaction type designed for payments. They support configurable fee tokens, fee sponsorship, batched calls, concurrent nonces, access keys, and scheduled execution. They are recommended over Ethereum transactions on Tempo.
[See the full specification](https://docs.tempo.xyz/protocol/transactions#tempo-transactions)
## Setup
In order to use Tempo Transactions, ensure that you have set up your Viem client
with the `tempoModerato` chain.
```ts twoslash [viem.config.ts]
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains' // [!code focus]
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato, // [!code focus]
transport: http(),
})
```
Now that you have set up your Viem client, you can now start to leverage Tempo Transactions.
Check out the features below on what is enabled with Tempo Transactions.
## Features
### Pay Fees with Stablecoins
Use the `feeToken` field to pay fees in any USD-denominated TIP-20 token. Tempo's Fee AMM automatically swaps to the validator's preferred token.
:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'
const alphaUsd = '0x20c0000000000000000000000000000000000001'
const receipt = await client.sendTransactionSync({
data: '0xdeadbeef',
feeToken: alphaUsd,
to: '0xcafebabecafebabecafebabecafebabecafebabe',
})
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts" filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
:::tip
You can also set a default `feeToken` globally by extending the chain.
```ts twoslash [example.ts]
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato.extend({ // [!code focus]
feeToken: '0x20c0000000000000000000000000000000000001' // [!code focus]
}), // [!code focus]
transport: http(),
})
```
:::
:::info
See a full guide on [paying fees in any stablecoin](https://docs.tempo.xyz/guide/payments/pay-fees-in-any-stablecoin).
:::
### Fee Sponsorship
A fee payer can cover gas fees for the sender via the `feePayer` field. You can use a local account or a remote relay service.
:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'
import { privateKeyToAccount } from 'viem/accounts'
const feePayer = privateKeyToAccount('0x...')
const receipt = await client.sendTransactionSync({
data: '0xdeadbeef',
feePayer,
to: '0xcafebabecafebabecafebabecafebabecafebabe',
})
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
:::tip
It is also possible to use a remote [Fee Payer Relay](https://docs.tempo.xyz/guide/payments/sponsor-user-fees#fee-payer-relay) instead of a local account.
:::
:::info
See a full guide on [sponsoring fees](https://docs.tempo.xyz/guide/payments/sponsor-user-fees).
:::
### Batch Calls
Bundle multiple operations atomically with `calls`.
:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'
const receipt = await client.sendTransactionSync({
calls: [
{
to: '0xcafebabecafebabecafebabecafebabecafebabe',
data: '0xdeadbeef0000000000000000000000000000000001',
},
{
to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
data: '0xcafebabe0000000000000000000000000000000001',
},
],
})
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
### Access Keys
Access keys enable you to delegate signing authority from a primary account to a secondary key,
such as device-bound non-extractable [WebCrypto key](https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair). The primary account signs a key authorization that grants the access key permission
to sign transactions on its behalf.
This authorization is then attached to the next transaction (that can be signed by either the primary or the access key), then all
transactions thereafter can be signed by the access key.
:::code-group
```tsx twoslash [example.ts]
// @noErrors
import { Account, WebCryptoP256 } from 'viem/tempo'
import { client } from './viem.config'
// 1. Instantiate account.
const account = Account.fromSecp256k1('0x...')
// 2. Generate a non-extractable WebCrypto key pair & instantiate access key.
const keyPair = await WebCryptoP256.createKeyPair()
const accessKey = Account.fromWebCryptoP256(keyPair, {
access: account,
})
// 3. Sign over key authorization with account.
const keyAuthorization = await account.signKeyAuthorization(accessKey)
// 4. Attach key authorization to (next) transaction.
const receipt = await client.sendTransactionSync({
account: accessKey, // sign transaction with access key // [!code hl]
data: '0xdeadbeef0000000000000000000000000000000001',
keyAuthorization, // [!code hl]
to: '0xcafebabecafebabecafebabecafebabecafebabe',
})
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
### Concurrent Transactions
Concurrent transactions enable higher throughput by allowing multiple transactions from the same account to be sent
in parallel without waiting for sequential nonce confirmation.
By using different nonce keys, you can submit multiple transactions simultaneously that don't conflict with each other,
enabling parallel execution and significantly improved transaction throughput for high-activity accounts.
You can leverage concurrent transactions in Viem by using `Promise.all`.
:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'
const [receipt1, receipt2, receipt3] = await Promise.all([
client.sendTransactionSync({
data: '0xdeadbeef0000000000000000000000000000000001',
to: '0xcafebabecafebabecafebabecafebabecafebabe',
}),
client.sendTransactionSync({
data: '0xcafebabe0000000000000000000000000000000001',
to: '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef',
}),
client.sendTransactionSync({
data: '0xdeadbeef0000000000000000000000000000000001',
to: '0xcafebabecafebabecafebabecafebabecafebabe',
}),
])
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
You can also have control over the `nonceKey`s
:::code-group
```tsx twoslash [example.ts]
// @noErrors
import { client } from './viem.config'
const hash1 = await client.sendTransaction({
data: '0xdeadbeef0000000000000000000000000000000001',
nonceKey: 567n, // [!code hl]
to: '0xcafebabecafebabecafebabecafebabecafebabe',
})
const hash2 = await client.sendTransaction({
data: '0xdeadbeef0000000000000000000000000000000001',
nonceKey: 789n, // [!code hl]
to: '0xcafebabecafebabecafebabecafebabecafebabe',
})
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
### Scheduled Transactions
Scheduled transactions allow you to sign a transaction in advance and specify a time window for when it can be
executed onchain. By setting `validAfter` and `validBefore` timestamps, you define the earliest and latest times
the transaction can be included in a block.
:::code-group
```ts twoslash [example.ts]
import { client } from './viem.config'
const receipt = await client.sendTransactionSync({
data: '0xdeadbeef0000000000000000000000000000000001',
to: '0xcafebabecafebabecafebabecafebabecafebabe',
validAfter: Math.floor(Number(new Date('2026-01-01')) / 1000), // [!code hl]
validBefore: Math.floor(Number(new Date('2026-01-02')) / 1000), // [!code hl]
})
```
```tsx twoslash [viem.config.ts] filename="viem.config.ts"
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { tempoModerato } from 'viem/chains'
export const client = createWalletClient({
account: privateKeyToAccount('0x...'),
chain: tempoModerato,
transport: http(),
})
```
:::
## More Resources
- Tempo protocol docs: [Tempo Transactions](https://docs.tempo.xyz/protocol/transactions#tempo-transactions)
- Tempo Viem setup: [Getting Started](https://docs.tempo.xyz/tempo)
- Tempo transports: [`withFeePayer`](https://docs.tempo.xyz/tempo/transports/withFeePayer)