Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use batch client for wallet connection #140

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"postinstall": "patch-package"
},
"dependencies": {
"@agoric/ertp": "^0.15.4-dev-1adb7a2.0",
"@agoric/inter-protocol": "^0.13.2-dev-95a7fa5.0",
"@agoric/smart-wallet": "^0.4.3-dev-8cbf697.0",
"@agoric/ui-components": "^0.3.4-dev-1adb7a2.0",
"@agoric/wallet": "^0.17.1-dev-1adb7a2.0",
"@agoric/web-components": "^0.5.1-dev-1adb7a2.0",
"@agoric/zoe": "^0.25.4-dev-1adb7a2.0",
"@agoric/ertp": "^0.16.3-dev-f581a7e.0",
"@agoric/inter-protocol": "^0.16.2-dev-f581a7e.0",
"@agoric/smart-wallet": "^0.5.4-dev-f581a7e.0",
"@agoric/ui-components": "^0.3.9-dev-39b1586.0",
"@agoric/wallet": "^0.18.4-dev-f581a7e.0",
"@agoric/web-components": "0.6.4-dev-933b31c.0",
"@agoric/zoe": "^0.26.3-dev-f581a7e.0",
"@endo/eventual-send": "^0.17.2",
"@endo/init": "^0.5.56",
"@endo/marshal": "^0.8.5",
Expand Down
8 changes: 5 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { secondsSinceEpoch } from 'utils/date';
import { vaultStoreAtom } from 'store/vaults';
import AppVersionDialog from 'components/AppVersionDialog';
import { currentlyVisitedHash, ipfsHashLength } from 'utils/ipfs';
import { fetchRPCAddr } from 'utils/rpc';
import { fetchChainInfo } from 'utils/rpc';
import { makeAgoricChainStorageWatcher } from 'rpc';

import 'react-toastify/dist/ReactToastify.css';
Expand Down Expand Up @@ -93,18 +93,20 @@ const App = () => {
if (chainStorageWatcher) return;
const startWatching = async () => {
try {
const rpcAddr = await fetchRPCAddr(netConfig.url);
const { rpc, chainName } = await fetchChainInfo(netConfig.url);
if (isCancelled) return;
setChainStorageWatcher(
makeAgoricChainStorageWatcher(
rpcAddr,
rpc,
chainName,
importContext.fromBoard.unserialize,
e => {
setError(e);
throw e;
},
),
);

watchVbank();
} catch (e) {
if (isCancelled) return;
Expand Down
8 changes: 6 additions & 2 deletions src/components/ConnectWalletButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Oval } from 'react-loader-spinner';
import {
ChainConnection,
chainConnectionAtom,
chainStorageWatcherAtom,
isWalletConnectionInProgressAtom,
localStorageStore,
networkConfigAtom,
Expand All @@ -18,6 +19,7 @@ const truncatedAddress = (chainConnection: ChainConnection) =>

const ConnectWalletButton = () => {
const walletService = useAtomValue(walletServiceAtom);
const chainStorageWatcher = useAtomValue(chainStorageWatcherAtom);
const isConnectionInProgress = useAtomValue(isWalletConnectionInProgressAtom);
const chainConnection = useAtomValue(chainConnectionAtom);
const { url } = useAtomValue(networkConfigAtom);
Expand All @@ -26,17 +28,19 @@ const ConnectWalletButton = () => {
// Automatically connect if the user has previously connected.
useEffect(() => {
if (
chainStorageWatcher &&
hasWalletPreviouslyConnected &&
!(isConnectionInProgress || chainConnection)
) {
walletService.connect(url);
walletService.connect();
}
}, [
chainConnection,
hasWalletPreviouslyConnected,
isConnectionInProgress,
url,
walletService,
chainStorageWatcher,
]);

const status = (() => {
Expand All @@ -57,7 +61,7 @@ const ConnectWalletButton = () => {
!chainConnection &&
'hover:bg-black hover:bg-opacity-5',
)}
onClick={() => walletService.connect(url)}
onClick={() => walletService.connect()}
>
<>
{status}
Expand Down
4 changes: 1 addition & 3 deletions src/components/DisclaimerDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
isDisclaimerDialogShowingAtom,
latestDisclaimerIndex,
localStorageStore,
networkConfigAtom,
walletServiceAtom,
} from 'store/app';
import { useAtom, useAtomValue } from 'jotai';
Expand All @@ -13,7 +12,6 @@ import { disclaimerHref } from 'config';

const DisclaimerDialog = () => {
const walletService = useAtomValue(walletServiceAtom);
const { url } = useAtomValue(networkConfigAtom);
const { setlatestDisclaimerAgreedIndex } = useStore(localStorageStore);
const [isDisclaimerDialogShowing, setIsDisclaimerDialogShowing] = useAtom(
isDisclaimerDialogShowingAtom,
Expand All @@ -23,7 +21,7 @@ const DisclaimerDialog = () => {
const proceed = () => {
setlatestDisclaimerAgreedIndex(latestDisclaimerIndex);
setIsDisclaimerDialogShowing(false);
walletService.connect(url, false);
walletService.connect(false);
};

const body = (
Expand Down
6 changes: 4 additions & 2 deletions src/components/OfferSignerBridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
bridgeHrefAtom,
walletUiHrefAtom,
setIsDappApprovedAtom,
chainStorageWatcherAtom,
} from 'store/app';
import type { OfferConfig } from 'store/app';
import { signerTarget } from 'config';
Expand Down Expand Up @@ -52,6 +53,7 @@ const WalletBridge = () => {
const toastId = useRef<Id | null>(null);
const bridgeHref = useAtomValue(bridgeHrefAtom);
const walletUiHref = useAtomValue(walletUiHrefAtom);
const watcher = useAtomValue(chainStorageWatcherAtom);

const clearCurrentToast = () =>
toastId.current && toast.dismiss(toastId.current);
Expand Down Expand Up @@ -142,14 +144,14 @@ const WalletBridge = () => {

return (
<div className="hidden">
{chainConnection && (
{chainConnection && watcher && (
<DappWalletBridge
bridgeHref={bridgeHref}
onBridgeMessage={onBridgeMessage}
onBridgeReady={onBridgeReady}
onError={onError}
address={chainConnection.address}
chainId={chainConnection.chainId}
chainId={watcher.chainId}
/>
)}
</div>
Expand Down
4 changes: 4 additions & 0 deletions src/rpc/src/chainStorageWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ const makePathSubscriber = <T>(
* requests for efficiency.
*
* @param rpcAddr RPC server URL
* @param chainId the chain id to use
* @param unserialize CapData unserializer to use
* @returns
*/
export const makeAgoricChainStorageWatcher = (
rpcAddr: string,
chainId: string,
unserialize: FromCapData<string>,
onError?: (e: Error) => void,
newPathQueryDelayMs = defaults.newPathQueryDelayMs,
Expand Down Expand Up @@ -189,5 +191,7 @@ export const makeAgoricChainStorageWatcher = (

return {
watchLatest,
chainId,
rpcAddr,
};
};
7 changes: 6 additions & 1 deletion src/rpc/test/chainStorageWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ global.fetch = fetch;
global.harden = val => val;

const fakeRpcAddr = 'https://agoric-rpc.vitest-nodes.com:443';
const fakeChainId = 'agoric-unit-test-1';
const serialize = (val: unknown) => val;
const unserialize = (val: unknown) => val;

let watcher: ReturnType<typeof makeAgoricChainStorageWatcher>;

describe('makeAgoricChainStorageWatcher', () => {
beforeEach(() => {
watcher = makeAgoricChainStorageWatcher(fakeRpcAddr, unserialize);
watcher = makeAgoricChainStorageWatcher(
fakeRpcAddr,
fakeChainId,
unserialize,
);
vi.useFakeTimers();
});

Expand Down
19 changes: 8 additions & 11 deletions src/service/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { toast } from 'react-toastify';
import SmartWalletNotFoundToast from 'components/SmartWalletNotFoundToast';
import {
makeAgoricKeplrConnection,
makeAgoricWalletConnection,
AgoricKeplrConnectionErrors as Errors,
} from '@agoric/web-components';
import type { Id as ToastId, ToastContent, ToastOptions } from 'react-toastify';
Expand Down Expand Up @@ -85,12 +85,12 @@ export const makeWalletService = () => {
toastId = toast.error(content, options);
};

const connect = async (
networkConfigUrl: string,
shouldCheckDisclaimer = true,
) => {
const { isWalletConnectionInProgress, chainConnection, importContext } =
appStore.getState();
const connect = async (shouldCheckDisclaimer = true) => {
const {
isWalletConnectionInProgress,
chainConnection,
chainStorageWatcher,
} = appStore.getState();

if (isWalletConnectionInProgress || chainConnection) return;

Expand All @@ -105,10 +105,7 @@ export const makeWalletService = () => {

appStore.setState({ isWalletConnectionInProgress: true });
try {
const connection = await makeAgoricKeplrConnection(
networkConfigUrl,
importContext,
);
const connection = await makeAgoricWalletConnection(chainStorageWatcher);
appStore.setState({ chainConnection: connection });
stopWatchingPurses = watchPurses(connection);
stopWatchingPublicSubscribers = watchPublicSubscribers(connection);
Expand Down
9 changes: 6 additions & 3 deletions src/utils/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export const fetchRPCAddr = async (netconfigURL: string) => {
export const fetchChainInfo = async (netconfigURL: string) => {
const response = await fetch(netconfigURL, {
headers: { accept: 'application/json' },
});
const { rpcAddrs } = await response.json();
const { rpcAddrs, chainName } = await response.json();

return rpcAddrs[Math.floor(Math.random() * rpcAddrs.length)];
return {
rpc: rpcAddrs[Math.floor(Math.random() * rpcAddrs.length)],
chainName,
};
};
2 changes: 1 addition & 1 deletion src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ declare module '@agoric/ui-components' {

// UNTIL https://github.com/Agoric/agoric-sdk/issues/6591
declare module '@agoric/web-components' {
export const makeAgoricKeplrConnection;
export const makeAgoricWalletConnection;
export const AgoricKeplrConnectionErrors;
export const BridgeProtocol;
}
Expand Down
Loading