11import {
22 createPublicClient ,
3- createWalletClient ,
4- custom ,
53 encodeFunctionData ,
64 formatEther ,
75 formatUnits ,
@@ -39,17 +37,24 @@ const publicClient = createPublicClient({
3937 chain : base ,
4038 transport : RPC_URL ? http ( RPC_URL ) : http ( ) ,
4139} ) ;
42- let walletClientCache : any | null = null ;
43-
44- async function getWalletClient ( ) {
45- if ( walletClientCache ) return walletClientCache ;
46- const provider = await window . privyWallet ?. getEmbeddedProvider ?.( ) ;
47- if ( ! provider ) throw new Error ( "Embedded wallet provider unavailable" ) ;
48- walletClientCache = createWalletClient ( {
49- chain : base ,
50- transport : custom ( provider ) ,
51- } ) ;
52- return walletClientCache ;
40+
41+ type SponsoredTransactionInput = {
42+ to ?: `0x${string } `;
43+ data ?: `0x${string } `;
44+ value ?: bigint ;
45+ gas ?: bigint ;
46+ } ;
47+
48+ function getSponsoredSender ( ) : (
49+ tx : SponsoredTransactionInput ,
50+ ) => Promise < `0x${string } `> {
51+ const sender = window . privyWallet ?. sendSponsoredTransaction ;
52+ if ( ! sender ) {
53+ throw new Error (
54+ "Privy gas sponsorship unavailable. Ensure Privy provider initialized." ,
55+ ) ;
56+ }
57+ return sender ;
5358}
5459
5560async function wagmiRead ( params : {
@@ -72,29 +77,38 @@ async function wagmiWrite(params: {
7277 const walletManager = WalletManager . getInstance ( ) ;
7378 const account = walletManager . address as `0x${string } ` | undefined ;
7479 if ( ! account ) throw new Error ( "No connected wallet" ) ;
75- const client = await getWalletClient ( ) ;
7680
77- const { address, abi, functionName, args, value } = params ;
81+ const { address, abi, functionName, args, value, gas } = params ;
7882 const data = encodeFunctionData ( {
7983 abi,
8084 functionName,
8185 args : args ?? [ ] ,
8286 } ) ;
8387
84- const request : any = {
85- account,
86- chain : base ,
88+ const sendSponsored = getSponsoredSender ( ) ;
89+
90+ const txRequest : {
91+ to : `0x${string } `;
92+ data ?: `0x${string } `;
93+ value ?: bigint ;
94+ gas ?: bigint ;
95+ } = {
8796 to : address ,
88- data,
8997 } ;
9098
99+ if ( data && data !== "0x" ) {
100+ txRequest . data = data as `0x${string } `;
101+ }
102+
91103 if ( typeof value !== "undefined" ) {
92- request . value = value ;
104+ txRequest . value = value ;
93105 }
94106
95- return await client . sendTransaction ( request , {
96- sponsor : true ,
97- } ) ;
107+ if ( typeof gas !== "undefined" ) {
108+ txRequest . gas = gas ;
109+ }
110+
111+ return ( await sendSponsored ( txRequest ) ) as Hash ;
98112}
99113
100114function wagmiWatch ( params : {
@@ -265,19 +279,13 @@ async function sendNativeTransaction(to: `0x${string}`, value: bigint) {
265279 const walletManager = WalletManager . getInstance ( ) ;
266280 const account = walletManager . address as `0x${string } ` | undefined ;
267281 if ( ! account ) throw new Error ( "No connected wallet" ) ;
268- const client = await getWalletClient ( ) ;
269282
270- const request : any = {
271- account ,
272- chain : base ,
283+ const sendSponsored = getSponsoredSender ( ) ;
284+
285+ return ( await sendSponsored ( {
273286 to,
274287 value,
275- data : "0x" ,
276- } ;
277-
278- return await client . sendTransaction ( request , {
279- sponsor : true ,
280- } ) ;
288+ } ) ) as Hash ;
281289}
282290
283291export async function withdrawAsset ( params : {
@@ -446,12 +454,16 @@ async function ensureErc20Allowance(params: {
446454 current : currentAllowance . toString ( ) ,
447455 } ) ;
448456
449- await wagmiWrite ( {
457+ const hash = await wagmiWrite ( {
450458 address : token ,
451459 abi : ERC20_ABI ,
452460 functionName : "approve" ,
453461 args : [ CONTRACT_ADDRESS , amount ] ,
454462 } ) ;
463+
464+ await publicClient . waitForTransactionReceipt ( {
465+ hash,
466+ } ) ;
455467}
456468
457469export interface SetAllowlistEnabledParams {
0 commit comments