-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support switch network for tron
- Loading branch information
Showing
8 changed files
with
75 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@node-real/walletkit': patch | ||
--- | ||
|
||
Support switch network for tron |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,39 @@ | ||
import { useWalletKit } from '@/core/index'; | ||
import { useWallet } from '@tronweb3/tronwallet-adapter-react-hooks'; | ||
import { useCallback } from 'react'; | ||
import { useCallback, useState } from 'react'; | ||
|
||
export function useTronConnect() { | ||
const { tronConfig } = useWalletKit(); | ||
const { select, wallets: adapters } = useWallet(); | ||
const { select, wallets: adapters, connected, disconnect } = useWallet(); | ||
|
||
const { log } = useWalletKit(); | ||
const [isConnected, setIsConnected] = useState(connected); | ||
|
||
const connect = useCallback( | ||
async ({ adapterName }: { adapterName: string }) => { | ||
async ({ adapterName, chainId }: { adapterName: string; chainId?: string | number }) => { | ||
select(adapterName as any); | ||
if (!tronConfig?.autoConnect) { | ||
const adapter = adapters.find((item) => item.adapter.name === adapterName)?.adapter; | ||
if (adapter) { | ||
|
||
const finalChainId = typeof chainId === 'number' ? `0x${chainId.toString(16)}` : chainId; | ||
const adapter = adapters.find((item) => item.adapter.name === adapterName)?.adapter; | ||
|
||
if (adapter) { | ||
try { | ||
await adapter.connect(); | ||
if (finalChainId) { | ||
await adapter?.switchChain(finalChainId); | ||
} | ||
setIsConnected(true); | ||
} catch (err) { | ||
setIsConnected(false); | ||
disconnect(); | ||
log(err); | ||
} | ||
} | ||
}, | ||
[adapters, select, tronConfig?.autoConnect], | ||
[adapters, disconnect, log, select], | ||
); | ||
|
||
return { | ||
connect, | ||
isConnected, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters