fix(cosmos-provider): ensures proper multi-account request handling

This commit is contained in:
Ben Kremer 2022-03-14 16:24:29 +01:00
parent cb648ace48
commit 081ae11986
4 changed files with 59 additions and 46 deletions

View File

@ -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,11 +171,13 @@ export default function App() {
};
const getCosmosActions = (): AccountAction[] => {
const wrapRpcRequest = (rpcRequest: () => Promise<IFormattedRpcResponse>) => async () => {
const wrapRpcRequest =
(rpcRequest: (account: string) => Promise<IFormattedRpcResponse>) =>
async (account: string) => {
openRequestModal();
try {
setIsRpcRequestPending(true);
const result = await rpcRequest();
const result = await rpcRequest(account);
setRpcResult(result);
} catch (error) {
console.error("RPC request failed:", error);

View File

@ -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>

View File

@ -89,7 +89,14 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
await cosmosProvider.disconnect();
}, [cosmosProvider]);
const _subscribeToClientEvents = useCallback(async (_client: Client) => {
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");
}
@ -106,11 +113,18 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
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 {
@ -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(

View File

@ -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 {