Skip to content

Commit

Permalink
feat(web-components): surface wallet connection rpc errors
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed Dec 14, 2023
1 parent ebfc261 commit 7289e78
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 17 deletions.
20 changes: 20 additions & 0 deletions packages/web-components/src/wallet-connection/queryBankBalances.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { QueryClient, createProtobufRpcClient } from '@cosmjs/stargate';
import { QueryClientImpl } from 'cosmjs-types/cosmos/bank/v1beta1/query';

/** @typedef {import('@cosmjs/tendermint-rpc').Tendermint34Client} Tendermint34Client */

/**
* @param {string} address
* @param {Tendermint34Client} tendermint
*/
export const queryBankBalances = async (address, tendermint) => {
const queryClient = new QueryClient(tendermint);
const rpcClient = createProtobufRpcClient(queryClient);
const bankQueryService = new QueryClientImpl(rpcClient);

const { balances } = await bankQueryService.AllBalances({
address,
});

return balances;
};
18 changes: 16 additions & 2 deletions packages/web-components/src/wallet-connection/walletConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import { makeInteractiveSigner } from './makeInteractiveSigner.js';
import { watchWallet } from './watchWallet.js';
import { Errors } from '../errors.js';

export const makeAgoricWalletConnection = async (chainStorageWatcher, rpc) => {
/**
* @param {any} chainStorageWatcher
* @param {string} rpc
* @param {((error: unknown) => void)=} onRpcError
*/
export const makeAgoricWalletConnection = async (
chainStorageWatcher,
rpc,
onRpcError = undefined,
) => {
if (!('keplr' in window)) {
throw Error(Errors.noKeplr);
}
Expand All @@ -21,7 +30,12 @@ export const makeAgoricWalletConnection = async (chainStorageWatcher, rpc) => {
SigningStargateClient.connectWithSigner,
);

const walletNotifiers = watchWallet(chainStorageWatcher, address, rpc);
const walletNotifiers = watchWallet(
chainStorageWatcher,
address,
rpc,
onRpcError,
);

const makeOffer = async (
invitationSpec,
Expand Down
58 changes: 43 additions & 15 deletions packages/web-components/src/wallet-connection/watchWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import { makeNotifierKit } from '@agoric/notifier';
import { AmountMath } from '@agoric/ertp';
import { iterateEach, makeFollower, makeLeader } from '@agoric/casting';
import { queryBankBalances } from '../queryBankBalances.js';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { queryBankBalances } from './queryBankBalances.js';

/** @typedef {import('@agoric/smart-wallet/src/types.js').Petname} Petname */

Expand Down Expand Up @@ -30,13 +31,16 @@ import { queryBankBalances } from '../queryBankBalances.js';
*/

const POLL_INTERVAL_MS = 6000;
const RETRY_INTERVAL_MS = 200;
const MAX_ATTEMPTS_TO_WATCH_BANK = 2;

/**
* @param {any} chainStorageWatcher
* @param {string} address
* @param {string} rpc
* @param {((error: unknown) => void)=} onRpcError
*/
export const watchWallet = (chainStorageWatcher, address, rpc) => {
export const watchWallet = (chainStorageWatcher, address, rpc, onRpcError) => {
const pursesNotifierKit = makeNotifierKit(
/** @type {PurseInfo[] | null} */ (null),
);
Expand Down Expand Up @@ -125,9 +129,23 @@ export const watchWallet = (chainStorageWatcher, address, rpc) => {
updatePurses(brandToPurse);
};

const watchBank = async () => {
const balances = await queryBankBalances(address, rpc);
bank = balances;
/** @type {Tendermint34Client} */
let tendermintClient;
const watchBank = async (attempts = 0) => {
await null;

try {
tendermintClient ||= await Tendermint34Client.connect(rpc);
bank = await queryBankBalances(address, tendermintClient);
} catch (e) {
console.error('Error querying bank balances for address', address);
if (attempts >= MAX_ATTEMPTS_TO_WATCH_BANK) {
onRpcError && onRpcError(e);
} else {
setTimeout(() => watchBank(attempts + 1), RETRY_INTERVAL_MS);
return;
}
}
possiblyUpdateBankPurses();
setTimeout(watchBank, POLL_INTERVAL_MS);
};
Expand Down Expand Up @@ -204,6 +222,7 @@ export const watchWallet = (chainStorageWatcher, address, rpc) => {
);
} catch (e) {
console.error('Error getting boardAux for brands', brands, e);
onRpcError && onRpcError(e);
}
}

Expand All @@ -218,17 +237,26 @@ export const watchWallet = (chainStorageWatcher, address, rpc) => {
};

const watchWalletUpdates = async () => {
const leader = makeLeader(rpc);
const follower = makeFollower(`:published.wallet.${address}`, leader, {
proof: 'none',
unserializer: chainStorageWatcher.marshaller,
});

for await (const update of iterateEach(follower)) {
console.debug('wallet update', update);
if ('error' in update) continue;
const watch = async () => {
const leader = makeLeader(rpc);
const follower = makeFollower(`:published.wallet.${address}`, leader, {
proof: 'none',
unserializer: chainStorageWatcher.marshaller,
});

for await (const update of iterateEach(follower)) {
console.debug('wallet update', update);
if ('error' in update) continue;

walletUpdatesNotifierKit.updater.updateState(harden(update.value));
}
};

walletUpdatesNotifierKit.updater.updateState(harden(update.value));
await null;
try {
await watch();
} catch (e) {
onRpcError && onRpcError(e);
}
};

Expand Down

0 comments on commit 7289e78

Please sign in to comment.