Skip to content

Commit

Permalink
fix(app): set acc from keplr (#1165)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimakorzhovnik authored May 27, 2024
1 parent 9f2d88c commit 5c1317f
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/contexts/signerClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import React, {
useMemo,
useState,
} from 'react';
import _ from 'lodash';
import { SigningCyberClient } from '@cybercongress/cyber-js';
import configKeplr, { getKeplr } from 'src/utils/keplrUtils';
import { OfflineSigner } from '@cybercongress/cyber-js/build/signingcyberclient';
import { Option } from 'src/types';
import { useAppSelector } from 'src/redux/hooks';
import { useAppDispatch, useAppSelector } from 'src/redux/hooks';
import { Keplr } from '@keplr-wallet/types';
import { addAddressPocket, setDefaultAccount } from 'src/redux/features/pocket';
import { accountsKeplr } from 'src/utils/utils';
Expand Down Expand Up @@ -53,11 +54,37 @@ export function useSigningClient() {
}

function SigningClientProvider({ children }: { children: React.ReactNode }) {
const { defaultAccount } = useAppSelector((state) => state.pocket);
const { defaultAccount, accounts } = useAppSelector((state) => state.pocket);
const dispatch = useAppDispatch();
const [signer, setSigner] = useState<SignerClientContextType['signer']>();
const [signerReady, setSignerReady] = useState(false);
const [signingClient, setSigningClient] =
useState<SignerClientContextType['signingClient']>();
const prevAccounts = usePrevious(accounts);

const selectAddress = useCallback(
async (keplr: Keplr) => {
if (!accounts || _.isEqual(prevAccounts, accounts)) {
return;
}
const keyInfo = await keplr.getKey(CHAIN_ID);

const findAccount = Object.keys(accounts).find((key) => {
if (accounts[key].cyber.bech32 === keyInfo.bech32Address) {
return key;
}

return undefined;
});

if (findAccount) {
dispatch(setDefaultAccount({ name: findAccount }));
} else {
dispatch(addAddressPocket(accountsKeplr(keyInfo)));
}
},
[accounts, prevAccounts, dispatch]
);

useEffect(() => {
(async () => {
Expand All @@ -76,6 +103,8 @@ function SigningClientProvider({ children }: { children: React.ReactNode }) {
const initSigner = useCallback(async () => {
const windowKeplr = await getKeplr();
if (windowKeplr && windowKeplr.experimentalSuggestChain) {
selectAddress(windowKeplr);

windowKeplr.defaultOptions = {
sign: {
preferNoSetFee: true,
Expand All @@ -90,7 +119,7 @@ function SigningClientProvider({ children }: { children: React.ReactNode }) {
setSigner(offlineSigner);
setSigningClient(clientJs);
}
}, []);
}, [selectAddress]);

useEffect(() => {
(async () => {
Expand Down

0 comments on commit 5c1317f

Please sign in to comment.