fix(cosmos-provider): ensures proper multi-account request handling
This commit is contained in:
parent
cb648ace48
commit
081ae11986
@ -87,7 +87,7 @@ export default function App() {
|
||||
await ping();
|
||||
};
|
||||
|
||||
const testSignDirect: () => Promise<IFormattedRpcResponse> = async () => {
|
||||
const testSignDirect: (account: string) => Promise<IFormattedRpcResponse> = 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<IFormattedRpcResponse> = async () => {
|
||||
const testSignAmino: (account: string) => Promise<IFormattedRpcResponse> = 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<IFormattedRpcResponse>) => 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<IFormattedRpcResponse>) =>
|
||||
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) },
|
||||
|
@ -155,7 +155,7 @@ const Blockchain: FC<PropsWithChildren<BlockchainProps>> = (
|
||||
</Column>
|
||||
</SFullWidthContainer>
|
||||
) : null}
|
||||
{!!actions && actions.length ? (
|
||||
{address && !!actions && actions.length ? (
|
||||
<SFullWidthContainer>
|
||||
<h6>Methods</h6>
|
||||
{actions.map(action => (
|
||||
@ -163,7 +163,7 @@ const Blockchain: FC<PropsWithChildren<BlockchainProps>> = (
|
||||
key={action.method}
|
||||
left
|
||||
rgb={chain.meta.rgb}
|
||||
onClick={() => action.callback(chainId)}
|
||||
onClick={() => action.callback(address)}
|
||||
>
|
||||
{action.method}
|
||||
</SAction>
|
||||
|
@ -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(
|
||||
|
@ -150,7 +150,7 @@ export interface ChainNamespaces {
|
||||
|
||||
export interface AccountAction {
|
||||
method: string;
|
||||
callback: (chainId: string) => Promise<void>;
|
||||
callback: (account: string) => Promise<void>;
|
||||
}
|
||||
|
||||
export interface AccountBalances {
|
||||
|
Loading…
Reference in New Issue
Block a user