diff --git a/dapps/react-dapp-v2-cosmos-provider/src/App.tsx b/dapps/react-dapp-v2-cosmos-provider/src/App.tsx index a52f294..361c309 100644 --- a/dapps/react-dapp-v2-cosmos-provider/src/App.tsx +++ b/dapps/react-dapp-v2-cosmos-provider/src/App.tsx @@ -87,7 +87,7 @@ export default function App() { await ping(); }; - const testSignDirect: () => Promise = async () => { + const testSignDirect: (account: string) => Promise = async account => { if (!cosmosProvider) { throw new Error("cosmosProvider not connected"); } @@ -116,7 +116,7 @@ export default function App() { "cosmoshub-4", ); - const [address] = cosmosProvider.accounts; + const address = account.split(":").pop(); // cosmos_signDirect params const params = { @@ -137,7 +137,7 @@ export default function App() { }; }; - const testSignAmino: () => Promise = async () => { + const testSignAmino: (account: string) => Promise = async account => { if (!cosmosProvider) { throw new Error("cosmosProvider not connected"); } @@ -152,7 +152,7 @@ export default function App() { sequence: "54", }; - const [address] = cosmosProvider.accounts; + const address = account.split(":").pop(); // cosmos_signAmino params const params = { signerAddress: address, signDoc }; @@ -171,19 +171,21 @@ export default function App() { }; const getCosmosActions = (): AccountAction[] => { - const wrapRpcRequest = (rpcRequest: () => Promise) => async () => { - openRequestModal(); - try { - setIsRpcRequestPending(true); - const result = await rpcRequest(); - setRpcResult(result); - } catch (error) { - console.error("RPC request failed:", error); - setRpcResult({ result: error as string }); - } finally { - setIsRpcRequestPending(false); - } - }; + const wrapRpcRequest = + (rpcRequest: (account: string) => Promise) => + async (account: string) => { + openRequestModal(); + try { + setIsRpcRequestPending(true); + const result = await rpcRequest(account); + setRpcResult(result); + } catch (error) { + console.error("RPC request failed:", error); + setRpcResult({ result: error as string }); + } finally { + setIsRpcRequestPending(false); + } + }; return [ { method: "cosmos_signDirect", callback: wrapRpcRequest(testSignDirect) }, diff --git a/dapps/react-dapp-v2-cosmos-provider/src/components/Blockchain.tsx b/dapps/react-dapp-v2-cosmos-provider/src/components/Blockchain.tsx index ac3bb41..0ca366f 100644 --- a/dapps/react-dapp-v2-cosmos-provider/src/components/Blockchain.tsx +++ b/dapps/react-dapp-v2-cosmos-provider/src/components/Blockchain.tsx @@ -155,7 +155,7 @@ const Blockchain: FC> = ( ) : null} - {!!actions && actions.length ? ( + {address && !!actions && actions.length ? (
Methods
{actions.map(action => ( @@ -163,7 +163,7 @@ const Blockchain: FC> = ( key={action.method} left rgb={chain.meta.rgb} - onClick={() => action.callback(chainId)} + onClick={() => action.callback(address)} > {action.method} diff --git a/dapps/react-dapp-v2-cosmos-provider/src/contexts/ClientContext.tsx b/dapps/react-dapp-v2-cosmos-provider/src/contexts/ClientContext.tsx index 8eff908..0a560fe 100644 --- a/dapps/react-dapp-v2-cosmos-provider/src/contexts/ClientContext.tsx +++ b/dapps/react-dapp-v2-cosmos-provider/src/contexts/ClientContext.tsx @@ -89,29 +89,43 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac await cosmosProvider.disconnect(); }, [cosmosProvider]); - const _subscribeToClientEvents = useCallback(async (_client: Client) => { - if (typeof _client === "undefined") { - throw new Error("WalletConnect is not initialized"); - } - - _client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => { - const { uri } = proposal.signal.params; - console.log("EVENT", "QR Code Modal open"); - QRCodeModal.open(uri, () => { - console.log("EVENT", "QR Code Modal closed"); - }); - }); - - _client.on(CLIENT_EVENTS.pairing.created, async () => { - setPairings(_client.pairing.topics); - }); - - _client.on(CLIENT_EVENTS.session.deleted, () => { - console.log("EVENT", "session_deleted"); - resetApp(); - }); + const onSessionConnected = useCallback(async (_session: SessionTypes.Settled) => { + setSession(_session); + setChain(_session.permissions.blockchain.chains[0]); + setAccounts(_session.state.accounts); }, []); + const _subscribeToClientEvents = useCallback( + async (_client: Client) => { + if (typeof _client === "undefined") { + throw new Error("WalletConnect is not initialized"); + } + + _client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => { + const { uri } = proposal.signal.params; + console.log("EVENT", "QR Code Modal open"); + QRCodeModal.open(uri, () => { + console.log("EVENT", "QR Code Modal closed"); + }); + }); + + _client.on(CLIENT_EVENTS.pairing.created, async () => { + setPairings(_client.pairing.topics); + }); + + _client.on(CLIENT_EVENTS.session.updated, (updatedSession: SessionTypes.Settled) => { + console.log("EVENT", "session_updated"); + onSessionConnected(updatedSession); + }); + + _client.on(CLIENT_EVENTS.session.deleted, () => { + console.log("EVENT", "session_deleted"); + resetApp(); + }); + }, + [onSessionConnected], + ); + const createClient = useCallback(async () => { try { setIsInitializing(true); @@ -161,16 +175,13 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac return; } - const _accounts = cosmosProvider.accounts; const _session = await client.session.get(client.session.topics[0]); - setAccounts(_accounts); - setSession(_session); - setChain(caipChainId); + onSessionConnected(_session); QRCodeModal.close(); }, - [client], + [client, onSessionConnected], ); const _checkForPersistedSession = useCallback( diff --git a/dapps/react-dapp-v2-cosmos-provider/src/helpers/types.ts b/dapps/react-dapp-v2-cosmos-provider/src/helpers/types.ts index 71fc62b..ab9ccca 100644 --- a/dapps/react-dapp-v2-cosmos-provider/src/helpers/types.ts +++ b/dapps/react-dapp-v2-cosmos-provider/src/helpers/types.ts @@ -150,7 +150,7 @@ export interface ChainNamespaces { export interface AccountAction { method: string; - callback: (chainId: string) => Promise; + callback: (account: string) => Promise; } export interface AccountBalances {