vega-frontend-monorepo/libs/wallet/src/provider.tsx

116 lines
3.1 KiB
TypeScript
Raw Normal View History

import { LocalStorage, t } from '@vegaprotocol/react-helpers';
import type { ReactNode } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import type { VegaKeyExtended, VegaWalletContextShape } from '.';
Feat/471 cancel order (#610) * chore: [#471] update @vegaprotocol/vegawallet-service-api-client to 0.4.12 * fix: [#471] set up storybook in order-list lib and add tailwind * fix: [#471] organize order list components * chore: [471] pull theme switcher changes * feat: [#471] add cancel order button * feat: [#471] initial impl of use order cancel hook * fix: [#471] fix format of the price in order list * fix: #471 fix static assets issue when merging * fix: #471 refactor order dialog to vega tx dialog * fix: #471 move use cancel order hook in wallet lib * fix: [#471] cancel order dialog and hook refactor * fix: [#471] remove commented code from storybook preview and fix test * fix: [#471] update order-list.tsx * fix: [#471] fix update subscription - show order is cancelled * fix: [#471] fix eslint error * chore: [#471] refactoring and add tests for dialogs and cancel hook * fix: #471 add ref to order list table * fix: #471 add field for cancel fix test * fix: #471 rename vega-order-transaction-dialog, error handiling, open dialog on finalized order * fix: #471 sendTx body mandatory * fix: #471 use BusEventType.Order to check the typename * fix: #471 revert using BusEventType.Order to check the typename * Update libs/wallet/src/order-hooks/use-order-cancel.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #471 fix order-list refactoring and fixes * fix: #471 generate orders added as a mock in order-list * fix: #471 reset transaction after order updated * fix: #471 remove unused import useEffect * fix: #471 generate mock orders * fix: #471 revert generate mock orders * fix: #471 order list price set to display all decimals * fix: #471 generate orders updates * fix: #471 remove unused import * fix: #471 remove __typename from mock orders genOrder * Update libs/wallet/src/order-hooks/order-event-query.ts Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #471 update order event sub and pull master changes Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-06-29 09:03:20 +00:00
import type { VegaConnector } from './connectors/vega-connector';
import { VegaWalletContext } from './context';
import { WALLET_KEY } from './storage-keys';
Chore/657 refactor wallet and orders libs (#709) * feat: 470 edit orders hook and @vegaprotocol/vegawallet-service-api-client@0.4.14 * fix: 470 add methods for dialog intent and title * fix: #657 rename order-list lib to orders * chore: #657 move hooks to orders lib * chore: #657 vega tx dialog used for order cancellation and order submission * chore: #657 use client subscribe and unsubscribe on reset, refactor vegatxdialog * fix: #657 revert script src=./env-config.js ending * fix: #657 format project.json * Update project.json * fix: #657 cancel all subs and async tasks in useffect cleanup function * feat: #657 styling updates on vega order dialog * fix: #657 rename set dialog open and awaiting confirmation dialog update * fix: #657 updates on cancel order id check * fix: #657 fix vega tx dialog test * fix: #657 fix cypress trading-deal-tciket test * fix: #657 fix data-testid market test * Update libs/orders/README.md Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * Update libs/wallet/src/vega-order-transaction-dialog/vega-order-transaction-dialog.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * Update libs/wallet/src/vega-transaction-dialog/vega-transaction-dialog.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * Update libs/wallet/src/vega-order-transaction-dialog/vega-order-transaction-dialog.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * Update libs/wallet/src/vega-order-transaction-dialog/vega-order-transaction-dialog.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #657 remove the magic string and use the ordertype enum from types package * fix: #657 guarantee that order.id is present at this point or we need to determine the id of the order * fix: #657 fix translation in dialog * fix: #657 rename wallet types, delete ticket query, set finalized order null in submit * fix: #657 fix deal ticket steps test * fix: #657 remove settings.json * fix: #657 use order submit in orders lib * fix: #463 final modal links to block explorer * fix: #745 long/short instead of buy/sell * fix: #657 use only one vega tx dialog * fix: #657 keep ref of subscription and unsubscribe * fix: #657 hide cancelled orders * fix: #657 sub only when id set * fix: WIP: trying to unsub when order updated * fix: #745 long/short instead of buy/sell * fix: ensure observable defined * fix: #657 remove redundant test * Update libs/orders/src/lib/order-hooks/use-order-submit.ts * fix: failing test due to resizeobserver loop limit exceeded * fix: lint * fix: #657 fix test resize observer loop limit exceeded Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> Co-authored-by: Matthew Russell <mattrussell36@gmail.com> Co-authored-by: Joe <joe@vega.xyz>
2022-07-13 14:23:46 +00:00
import type { TransactionSubmission } from './wallet-types';
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 : t('None'),
};
});
setKeypairs(publicKeysWithName);
if (publicKey === null) {
setPublicKey(publicKeysWithName[0].pub);
}
return publicKeysWithName;
} catch (err) {
return null;
}
},
[publicKey]
);
const disconnect = useCallback(async () => {
try {
await connector.current?.disconnect();
setKeypairs(null);
setPublicKey(null);
connector.current = null;
LocalStorage.removeItem(WALLET_KEY);
return true;
} catch (err) {
2022-02-23 05:39:12 +00:00
console.error(err);
return false;
}
}, []);
const sendTx = useCallback((body: TransactionSubmission) => {
if (!connector.current) {
return null;
}
2022-03-10 00:39:59 +00:00
return connector.current.sendTx(body);
}, []);
2022-03-10 00:39:59 +00:00
// 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,
Feat/471 cancel order (#610) * chore: [#471] update @vegaprotocol/vegawallet-service-api-client to 0.4.12 * fix: [#471] set up storybook in order-list lib and add tailwind * fix: [#471] organize order list components * chore: [471] pull theme switcher changes * feat: [#471] add cancel order button * feat: [#471] initial impl of use order cancel hook * fix: [#471] fix format of the price in order list * fix: #471 fix static assets issue when merging * fix: #471 refactor order dialog to vega tx dialog * fix: #471 move use cancel order hook in wallet lib * fix: [#471] cancel order dialog and hook refactor * fix: [#471] remove commented code from storybook preview and fix test * fix: [#471] update order-list.tsx * fix: [#471] fix update subscription - show order is cancelled * fix: [#471] fix eslint error * chore: [#471] refactoring and add tests for dialogs and cancel hook * fix: #471 add ref to order list table * fix: #471 add field for cancel fix test * fix: #471 rename vega-order-transaction-dialog, error handiling, open dialog on finalized order * fix: #471 sendTx body mandatory * fix: #471 use BusEventType.Order to check the typename * fix: #471 revert using BusEventType.Order to check the typename * Update libs/wallet/src/order-hooks/use-order-cancel.tsx Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #471 fix order-list refactoring and fixes * fix: #471 generate orders added as a mock in order-list * fix: #471 reset transaction after order updated * fix: #471 remove unused import useEffect * fix: #471 generate mock orders * fix: #471 revert generate mock orders * fix: #471 order list price set to display all decimals * fix: #471 generate orders updates * fix: #471 remove unused import * fix: #471 remove __typename from mock orders genOrder * Update libs/wallet/src/order-hooks/order-event-query.ts Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com> * fix: #471 update order event sub and pull master changes Co-authored-by: Dexter Edwards <dexter.edwards93@gmail.com>
2022-06-29 09:03:20 +00:00
} as VegaWalletContextShape;
2022-03-10 00:39:59 +00:00
}, [keypair, keypairs, setPublicKey, connect, disconnect, connector, sendTx]);
return (
<VegaWalletContext.Provider value={contextValue}>
{children}
</VegaWalletContext.Provider>
);
};