-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathsendTransaction.ts
More file actions
149 lines (140 loc) · 4.8 KB
/
Copy pathsendTransaction.ts
File metadata and controls
149 lines (140 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import type {
Chain,
Client,
Hash,
SendTransactionParameters,
Transport,
getCode
} from "viem"
import {
type SendUserOperationParameters,
type SmartAccount,
sendUserOperation,
waitForUserOperationReceipt
} from "viem/account-abstraction"
import { getAction, parseAccount } from "viem/utils"
import { AccountNotFoundError } from "../../errors/index.js"
/**
* Creates, signs, and sends a new transaction to the network.
* This function also allows you to sponsor this transaction if sender is a smartAccount
*
* - 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.
*
* @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<
account extends SmartAccount | undefined,
chain extends Chain | undefined,
accountOverride extends SmartAccount | undefined = undefined,
chainOverride extends Chain | undefined = Chain | undefined,
calls extends readonly unknown[] = readonly unknown[]
>(
client: Client<Transport, chain, account>,
args:
| SendTransactionParameters<chain, account, chainOverride>
| SendUserOperationParameters<account, accountOverride, calls>
): Promise<Hash> {
let userOpHash: Hash
if ("to" in args) {
const {
account: account_ = client.account,
data,
maxFeePerGas,
maxPriorityFeePerGas,
to,
value,
nonce
} = args
if (!account_) {
throw new AccountNotFoundError({
docsPath: "/docs/actions/wallet/sendTransaction"
})
}
const account = parseAccount(account_) as SmartAccount
if (!to) throw new Error("Missing to address")
if ("authorization" in args && args.authorization) {
const bytecode = await getCode(client, { address: account.address })
if (bytecode) {
const bc = bytecode.toLowerCase()
// "0x" + "ef0100" + 40 hex (address)
if (bc.startsWith("0xef0100") && bc.length >= 48) {
const attachedAddress = "0x" + bc.slice(8, 48) // <-- exact 20 bytes
const target = (args.authorization as any).address?.toLowerCase?.()
if (target && attachedAddress === target) {
delete (args as any).authorization
}
}
}
}
userOpHash = await getAction(
client,
sendUserOperation,
"sendUserOperation"
)({
...args,
calls: [
{
to,
value: value || BigInt(0),
data: data || "0x"
}
],
account,
maxFeePerGas,
maxPriorityFeePerGas,
nonce: nonce ? BigInt(nonce) : undefined
})
} else {
userOpHash = await getAction(
client,
sendUserOperation,
"sendUserOperation"
)({ ...args } as SendUserOperationParameters<account, accountOverride>)
}
const userOperationReceipt = await getAction(
client,
waitForUserOperationReceipt,
"waitForUserOperationReceipt"
)({
hash: userOpHash
})
return userOperationReceipt?.receipt.transactionHash
}