vega-frontend-monorepo/libs/react-helpers/src/lib/vega-wallet/provider.tsx

118 lines
2.9 KiB
TypeScript
Raw Normal View History

import { LocalStorage } from '@vegaprotocol/storage';
import {
ReactNode,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import { VegaKeyExtended, VegaWalletContextShape } from '.';
import { VegaConnector } from './connectors';
import { VegaWalletContext } from './context';
import { WALLET_KEY } from './storage-keys';
2022-03-10 00:39:59 +00:00
import { OrderSubmissionBody } from '@vegaprotocol/vegawallet-service-api-client';
interface VegaWalletProviderProps {
children: ReactNode;
}
export const VegaWalletProvider = ({ children }: VegaWalletProviderProps) => {
// Current selected publicKey, default with value from local storage
const [publicKey, setPublicKey] = useState<string | null>(() => {
const pk = LocalStorage.getItem(WALLET_KEY);
return pk ? pk : null;
});
// Keypair objects retrieved from the connector
const [keypairs, setKeypairs] = useState<VegaKeyExtended[] | null>(null);
// Reference to the current connector instance
const connector = useRef<VegaConnector | null>(null);
const connect = useCallback(async (c: VegaConnector) => {
connector.current = c;
try {
const res = await connector.current.connect();
if (!res) {
return null;
}
const publicKeysWithName = res.map((pk) => {
const nameMeta = pk.meta?.find((m) => m.key === 'name');
return {
...pk,
name: nameMeta?.value ? nameMeta.value : 'None',
};
});
setKeypairs(publicKeysWithName);
return publicKeysWithName;
} catch (err) {
2022-02-23 05:39:12 +00:00
console.error(err);
return null;
}
}, []);
const disconnect = useCallback(async () => {
try {
await connector.current?.disconnect();
setKeypairs(null);
connector.current = null;
return true;
} catch (err) {
2022-02-23 05:39:12 +00:00
console.error(err);
return false;
}
}, []);
2022-03-10 00:39:59 +00:00
const sendTx = useCallback(async (body: OrderSubmissionBody) => {
if (!connector.current) {
return null;
}
try {
return connector.current.sendTx(body);
} catch (err) {
console.error(err);
return null;
}
}, []);
// Current selected keypair derived from publicKey state
const keypair = useMemo(() => {
const found = keypairs?.find((x) => x.pub === publicKey);
if (found) {
return found;
}
return null;
}, [publicKey, keypairs]);
// Whenever selected public key changes store it
useEffect(() => {
if (publicKey) {
LocalStorage.setItem(WALLET_KEY, publicKey);
}
}, [publicKey]);
const contextValue = useMemo<VegaWalletContextShape>(() => {
return {
keypair,
keypairs,
selectPublicKey: setPublicKey,
connect,
disconnect,
connector: connector.current,
2022-03-10 00:39:59 +00:00
sendTx,
};
2022-03-10 00:39:59 +00:00
}, [keypair, keypairs, setPublicKey, connect, disconnect, connector, sendTx]);
return (
<VegaWalletContext.Provider value={contextValue}>
{children}
</VegaWalletContext.Provider>
);
};