Skip to content

Commit 221ec64

Browse files
committed
refactor: enhance Starknet wallet integration and improve account handling
- Replaced the use of `useAccount` with a custom `useWallet` hook to streamline wallet provider access. - Updated the adapter's client creation methods to utilize the active wallet's Starknet account metadata for improved account management. - Simplified the login configuration logic to ensure it correctly references the Starknet account, enhancing overall clarity and functionality.
1 parent fcacb9b commit 221ec64

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

‎apps/app/components/WalletBridges/StarknetWalletBridge.tsx‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import {
33
useRegisterWallet,
44
chainNamespace,
55
type TrainWalletAdapter,
6-
type Caip2Id,
76
} from '@train-protocol/react'
87
import type { TrainSDK } from '@train-protocol/sdk'
9-
import { useAccount } from '@starknet-react/core'
108
import { useSettingsState } from '@/context/settings'
119
import { useRpcConfigStore } from '@/stores/rpcConfigStore'
10+
import useWallet from '@/hooks/useWallet'
1211

1312
export function StarknetWalletBridge() {
14-
const { account, address } = useAccount()
1513
const { networks } = useSettingsState()
1614
const getEffectiveRpcUrls = useRpcConfigStore(s => s.getEffectiveRpcUrls)
15+
const { providers } = useWallet()
16+
const starknetWalletProvider = providers.find(p => p.id == 'starknet')
1717

1818
const adapter = useMemo<TrainWalletAdapter>(() => {
1919
function getRpcUrl(): string {
@@ -24,29 +24,33 @@ export function StarknetWalletBridge() {
2424
return {
2525
chainNamespace: chainNamespace('starknet'),
2626

27-
createClient(sdk: TrainSDK, networkId: Caip2Id) {
27+
createClient(sdk: TrainSDK) {
2828
return sdk.createHTLCClient('starknet', { rpcUrl: getRpcUrl() })
2929
},
3030

31-
createWriteClient(sdk: TrainSDK, networkId: Caip2Id, _address?: string) {
31+
createWriteClient(sdk: TrainSDK, _address?: string) {
32+
const starknetAccount = starknetWalletProvider?.activeWallet?.metadata?.starknetAccount
33+
3234
return sdk.createHTLCClient('starknet', {
3335
rpcUrl: getRpcUrl(),
34-
signer: address && account ? { address, account } : undefined,
36+
signer: starknetAccount ? { address: starknetAccount.address, account: starknetAccount } : undefined,
3537
})
3638
},
3739

3840
getLoginConfig: (_address?: string) => {
39-
if (!account || !address) return null
41+
const starknetAccount = starknetWalletProvider?.activeWallet?.metadata?.starknetAccount
42+
43+
if (!starknetAccount) return null
4044
const isSandbox = process.env.NEXT_PUBLIC_API_VERSION === 'sandbox'
4145
return {
42-
provider: account,
43-
address,
46+
provider: starknetAccount,
47+
address: starknetAccount.address,
4448
options: { chainId: isSandbox ? 'SN_SEPOLIA' : 'SN_MAIN' },
4549
}
4650
},
4751
}
48-
}, [account, address, networks, getEffectiveRpcUrls])
52+
}, [starknetWalletProvider, networks, getEffectiveRpcUrls])
4953

5054
useRegisterWallet(adapter)
5155
return null
52-
}
56+
}

0 commit comments

Comments
 (0)