@@ -4,22 +4,22 @@ import {
4
4
type GetContractReturnType ,
5
5
type PublicClient ,
6
6
type Chain as ViemChain ,
7
- type Account as ViemAccount ,
8
7
type WalletClient ,
9
8
createWalletClient ,
10
9
custom ,
11
10
type TransactionSerializableEIP1559 ,
12
11
} from "viem" ;
13
12
import { getContract , type ThirdwebContract } from "../contract/contract.js" ;
14
- import type { Abi , Address } from "abitype" ;
13
+ import type { Abi } from "abitype" ;
15
14
import type { Chain } from "../chains/types.js" ;
16
15
import type { ThirdwebClient } from "../client/client.js" ;
17
16
import { resolveContractAbi } from "../contract/actions/resolve-abi.js" ;
18
17
import { getRpcUrlForChain } from "../chains/utils.js" ;
19
- import { type Account , getAccountKey } from "../wallets/interfaces/wallet.js" ;
18
+ import type { Account } from "../wallets/interfaces/wallet.js" ;
20
19
import { getRpcClient } from "../rpc/rpc.js" ;
21
- import { secp256k1 } from "@noble/curves/secp256k1" ;
22
- import { toHex } from "../utils/encoding/hex.js" ;
20
+ import { sendTransaction } from "../transaction/actions/send-transaction.js" ;
21
+ import { prepareTransaction } from "../transaction/prepare-transaction.js" ;
22
+ import { estimateGas } from "../transaction/actions/estimate-gas.js" ;
23
23
24
24
export const viemAdapter = {
25
25
contract : {
@@ -170,7 +170,7 @@ type ToViemWalletClientOptions = {
170
170
chain : Chain ;
171
171
} ;
172
172
173
- function toViemWalletClient ( options : ToViemWalletClientOptions ) : WalletClient {
173
+ function toViemWalletClient ( options : ToViemWalletClientOptions ) {
174
174
const { account, chain, client } = options ;
175
175
if ( ! account ) {
176
176
throw new Error ( "Wallet not connected." ) ;
@@ -194,13 +194,25 @@ function toViemWalletClient(options: ToViemWalletClientOptions): WalletClient {
194
194
const transport = custom ( {
195
195
request : async ( request ) => {
196
196
if ( request . method === "eth_sendTransaction" ) {
197
- const result = await account . sendTransaction ( request . params [ 0 ] ) ;
197
+ const result = await sendTransaction ( {
198
+ transaction : prepareTransaction ( {
199
+ ...request . params [ 0 ] ,
200
+ chain,
201
+ client,
202
+ } ) ,
203
+ account : account ,
204
+ } ) ;
198
205
return result . transactionHash ;
199
206
}
200
207
if ( request . method === "eth_estimateGas" ) {
201
- if ( account . estimateGas ) {
202
- return account . estimateGas ( request . params [ 0 ] ) ;
203
- }
208
+ return estimateGas ( {
209
+ transaction : prepareTransaction ( {
210
+ ...request . params [ 0 ] ,
211
+ chain,
212
+ client,
213
+ } ) ,
214
+ account : account ,
215
+ } ) ;
204
216
}
205
217
if ( request . method === "personal_sign" ) {
206
218
return account . signMessage ( {
@@ -217,26 +229,9 @@ function toViemWalletClient(options: ToViemWalletClientOptions): WalletClient {
217
229
} ,
218
230
} ) ;
219
231
220
- // viem defaults to JsonRpcAccounts so we pass the whole account if it's locally generated
221
- let viemAccountOrAddress : ViemAccount | Address ;
222
- const possibleAccountKey = getAccountKey ( account ) ;
223
- if ( possibleAccountKey && account . signTransaction ) {
224
- viemAccountOrAddress = {
225
- ...account ,
226
- signTransaction : account . signTransaction ,
227
- source : "custom" ,
228
- type : "local" as const ,
229
- publicKey : toHex (
230
- secp256k1 . getPublicKey ( possibleAccountKey . slice ( 2 ) , false ) ,
231
- ) ,
232
- } ;
233
- } else {
234
- viemAccountOrAddress = account . address ;
235
- }
236
-
237
232
return createWalletClient ( {
238
233
transport,
239
- account : viemAccountOrAddress ,
234
+ account : account . address ,
240
235
chain : viemChain ,
241
236
key : "thirdweb-wallet" ,
242
237
} ) ;
0 commit comments