@@ -2,14 +2,15 @@ import "./styles/App.css";
22
33import { Porto } from "porto" ;
44import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
5- import { type Address , type Chain , createWalletClient , custom } from "viem" ;
6- import { getAddresses , requestAddresses , waitForTransactionReceipt } from "viem/actions" ;
75import {
8- applyChainId ,
9- ensureChainSelected ,
10- readPendingChainId ,
11- getChainById ,
12- } from "./utils/helpers.ts" ;
6+ type Address ,
7+ type Chain ,
8+ createWalletClient ,
9+ custom ,
10+ type TransactionReceipt ,
11+ } from "viem" ;
12+ import { getAddresses , requestAddresses , waitForTransactionReceipt } from "viem/actions" ;
13+ import { applyChainId , api , isOk , renderJSON } from "./utils/helpers.ts" ;
1314import type {
1415 ApiErr ,
1516 ApiOk ,
@@ -18,7 +19,6 @@ import type {
1819 EIP6963ProviderInfo ,
1920 PendingAny ,
2021} from "./utils/types.ts" ;
21- import { api , pick , readAddr , renderJSON } from "./utils/api.ts" ;
2222
2323declare global {
2424 interface Window {
@@ -43,7 +43,7 @@ export function App() {
4343 const [ account , setAccount ] = useState < Address > ( ) ;
4444 const [ chainId , setChainId ] = useState < number > ( ) ;
4545 const [ chain , setChain ] = useState < Chain > ( ) ;
46- const [ lastTxReceipt , setLastTxReceipt ] = useState < any | null > ( null ) ;
46+ const [ lastTxReceipt , setLastTxReceipt ] = useState < TransactionReceipt | null > ( null ) ;
4747 const [ lastTxHash , setLastTxHash ] = useState < string | null > ( null ) ;
4848
4949 const pollRef = useRef < number | null > ( null ) ;
@@ -62,12 +62,12 @@ export function App() {
6262 const resp = await api <
6363 ApiOk < { connected : boolean ; account ?: string ; chainId ?: number } > | ApiErr
6464 > ( "/api/connection" ) ;
65- const ok = resp && ( resp as ApiOk < any > ) . status === "ok" ;
66- const data = ok ? ( resp as ApiOk < any > ) . data : null ;
6765
68- const serverConnected = ! ! data ?. connected ;
69- const serverAccount = ( data ?. account as string | undefined ) ?. toLowerCase ( ) ;
70- const serverChainId = data ?. chainId as number | undefined ;
66+ if ( ! isOk ( resp ) ) return ;
67+
68+ const serverConnected = ! ! resp . data ?. connected ;
69+ const serverAccount = ( resp . data ?. account as string | undefined ) ?. toLowerCase ( ) ;
70+ const serverChainId = resp . data ?. chainId as number | undefined ;
7171
7272 if ( ! account || chainId == null ) {
7373 if ( serverConnected ) {
@@ -134,67 +134,36 @@ export function App() {
134134 const tx = pending ;
135135
136136 try {
137- const targetChainId = readPendingChainId ( tx ) ?? chainId ;
138- await ensureChainSelected ( selected . provider , targetChainId , chainId ) ;
139- const desiredChain =
140- chain ?? ( targetChainId != null ? getChainById ( targetChainId ) : undefined ) ;
141- if ( ! desiredChain ) throw new Error ( "No chain metadata available" ) ;
142-
143- try {
144- const raw = await selected . provider . request < string > ( { method : "eth_chainId" } ) ;
145- applyChainId ( raw , setChainId , setChain ) ;
146- } catch { }
137+ const hash = ( await selected . provider . request ( {
138+ method : "eth_sendTransaction" ,
139+ params : [ tx . request ] ,
140+ } ) ) as `0x${string } `;
141+ setLastTxHash ( hash ) ;
147142
148- const { readHex } = await import ( "./utils/api.ts" ) ;
149- const from =
150- ( account as `0x${string } ` | undefined ) ??
151- ( readAddr ( tx , "from" , "sender" ) as `0x${string } ` | undefined ) ;
152- if ( ! from ) throw new Error ( "No sender account available" ) ;
143+ const receipt = await waitForTransactionReceipt ( walletClient , { hash } ) ;
144+ setLastTxReceipt ( receipt ) ;
153145
154- const serverFrom = readAddr ( tx , "from" , "sender" ) ;
155- if ( serverFrom && account && serverFrom . toLowerCase ( ) !== account . toLowerCase ( ) ) {
156- throw new Error ( `Server 'from' (${ serverFrom } ) != connected (${ account } )` ) ;
157- }
158-
159- const to = readAddr ( tx , "to" ) ;
160- const value = pick ( readHex ( tx , "value" , "amount" ) ) ;
161- const data = pick ( readHex ( tx , "data" , "input" , "calldata" ) ) ;
162- const gas = pick ( readHex ( tx , "gas" , "gasLimit" ) ) ;
163- const gasPrice = pick ( readHex ( tx , "gasPrice" ) ) ;
164- const maxFeePerGas = pick ( readHex ( tx , "maxFeePerGas" ) ) ;
165- const maxPriorityFeePerGas = pick ( readHex ( tx , "maxPriorityFeePerGas" ) ) ;
166- const nonce = tx . nonce as number | undefined ;
167-
168- const params : any = { account : from , to, value, data, gas, nonce } ;
169- if ( gasPrice ) params . gasPrice = gasPrice ;
170- else if ( maxFeePerGas || maxPriorityFeePerGas ) {
171- params . maxFeePerGas = maxFeePerGas ;
172- params . maxPriorityFeePerGas = maxPriorityFeePerGas ;
173- }
174-
175- const lastHash = await walletClient . sendTransaction ( {
176- ...params ,
177- chain : desiredChain ,
178- } ) ;
179- console . log ( "tx sent:" , { id : tx . id , hash : lastHash } ) ;
180- setLastTxHash ( lastHash ) ;
181-
182- const lastReceipt = await waitForTransactionReceipt ( walletClient , { hash : lastHash } ) ;
183- console . log ( "tx receipt:" , lastReceipt ) ;
184- setLastTxReceipt ( lastReceipt ) ;
185-
186- await api ( "/api/transaction/response" , "POST" , { id : tx . id , hash : lastHash , error : null } ) ;
146+ await api ( "/api/transaction/response" , "POST" , { id : tx . id , hash, error : null } ) ;
187147 await pollTick ( ) ;
188- } catch ( e : any ) {
189- console . log ( "send failed:" , String ( e ?. message ?? e ) ) ;
148+ } catch ( e : unknown ) {
149+ const msg =
150+ typeof e === "object" &&
151+ e &&
152+ "message" in e &&
153+ typeof ( e as { message ?: unknown } ) . message === "string"
154+ ? ( e as { message : string } ) . message
155+ : String ( e ) ;
156+
157+ console . log ( "send failed:" , msg ) ;
190158
191159 try {
192160 await api ( "/api/transaction/response" , "POST" , {
193- id : tx ! . id ,
161+ id : ( pending as { id ?: string } ) . id ,
194162 hash : null ,
195- error : String ( e ?. message ?? e ) ,
163+ error : msg ,
196164 } ) ;
197165 } catch { }
166+
198167 await pollTick ( ) ;
199168 }
200169 } ;
0 commit comments