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(web-components): read purses from bank instead of smart wallet #6861

Merged
merged 3 commits into from
Jan 27, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { makeLeader } from '@agoric/casting';
import { makeImportContext } from '@agoric/smart-wallet/src/marshal-contexts.js';
import { getKeplrAddress } from './getKeplrAddress';
import { getChainId } from './getChainId';
import { getChainInfo } from './getChainInfo';
import { watchWallet } from './watchWallet';

// TODO: We need a way to detect the appropriate network-config, and default it
Expand All @@ -13,11 +13,11 @@ export const makeAgoricKeplrConnection = async (
networkConfig = DEFAULT_NETWORK_CONFIG,
context = makeImportContext(),
) => {
const chainId = await getChainId(networkConfig);
const { chainId, rpcs } = await getChainInfo(networkConfig);
const address = await getKeplrAddress(chainId);

const leader = makeLeader(networkConfig);
const walletNotifiers = await watchWallet(leader, address, context);
const walletNotifiers = await watchWallet(leader, address, context, rpcs);

return {
address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// @ts-check
import { Errors } from './errors';

export const getChainId = async networkConfig => {
export const getChainInfo = async networkConfig => {
let chainId;
let rpcs;
try {
// eslint-disable-next-line @jessie.js/no-nested-await
const res = await fetch(networkConfig);
// eslint-disable-next-line @jessie.js/no-nested-await
const { chainName } = await res.json();
const { chainName, rpcAddrs } = await res.json();
chainId = chainName;
rpcs = rpcAddrs;
} catch {
throw new Error(Errors.networkConfig);
}

return chainId;
return { chainId, rpcs };
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { QueryClient, createProtobufRpcClient } from '@cosmjs/stargate';
import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { QueryClientImpl } from 'cosmjs-types/cosmos/bank/v1beta1/query';

/**
* @param {string} address
* @param {string} rpc
*/
export const queryBankBalances = async (address, rpc) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another copy of this? same scaling consideration applies. Filed:

const tendermint = await Tendermint34Client.connect(rpc);
const queryClient = new QueryClient(tendermint);
const rpcClient = createProtobufRpcClient(queryClient);
const bankQueryService = new QueryClientImpl(rpcClient);

const { balances } = await bankQueryService.AllBalances({
Copy link
Member

@dckc dckc Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ambient authority concern applies here too.

address,
});

return balances;
};
78 changes: 59 additions & 19 deletions packages/web-components/src/keplr-connection/watchWallet.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// @ts-check
import { makeFollower } from '@agoric/casting';
import { makeFollower, iterateLatest } from '@agoric/casting';
import { makeNotifierKit } from '@agoric/notifier';

import { fetchCurrent } from './fetchCurrent';
import { followLatest } from './followLatest';
import { AmountMath } from '@agoric/ertp';
import { assertHasData } from '@agoric/smart-wallet/src/utils';
import { Errors } from './errors';
import { queryBankBalances } from './queryBankBalances';

/** @typedef {import('./fetchCurrent').PurseInfo} PurseInfo */

export const watchWallet = async (leader, address, context) => {
const POLL_INTERVAL_MS = 6000;

export const watchWallet = async (leader, address, context, rpcs) => {
const followPublished = path =>
makeFollower(`:published.${path}`, leader, {
unserializer: context.fromMyWallet,
Expand All @@ -30,21 +32,59 @@ export const watchWallet = async (leader, address, context) => {
pursesNotifierKit.updater.updateState(harden(purses));
};

const currentFollower = followPublished(`wallet.${address}.current`);
const { blockHeight, brandToPurse } = await fetchCurrent(
currentFollower,
).catch(() => {
const currentFollower = await followPublished(`wallet.${address}.current`);
try {
// eslint-disable-next-line @jessie.js/no-nested-await
await assertHasData(currentFollower);
} catch {
throw new Error(Errors.noSmartWallet);
});
updatePurses(brandToPurse);

const latestFollower = followPublished(`wallet.${address}`);
followLatest({
startingHeight: blockHeight,
latestFollower,
updatePurses,
brandToPurse,
});
}

const watchChainBalances = () => {
const brandToPurse = new Map();
let vbankAssets;
let bank;

const possiblyUpdateBankPurses = () => {
if (!vbankAssets || !bank) return;

const bankMap = new Map(bank.map(({ denom, amount }) => [denom, amount]));

vbankAssets.forEach(([denom, info]) => {
const amount = bankMap.get(denom) ?? 0n;
const purseInfo = {
brand: info.brand,
currentAmount: AmountMath.make(info.brand, BigInt(amount)),
brandPetname: info.issuerName,
pursePetname: info.issuerName,
displayInfo: info.displayInfo,
};
brandToPurse.set(info.brand, purseInfo);
});

updatePurses(brandToPurse);
};

const watchBank = async () => {
const balances = await queryBankBalances(address, rpcs[0]);
bank = balances;
possiblyUpdateBankPurses();
setTimeout(watchBank, POLL_INTERVAL_MS);
};

const watchVbankAssets = async () => {
const vbankAssetsFollower = followPublished('agoricNames.vbankAsset');
for await (const { value } of iterateLatest(vbankAssetsFollower)) {
vbankAssets = value;
possiblyUpdateBankPurses();
}
};

void watchVbankAssets();
void watchBank();
};

watchChainBalances();

return {
pursesNotifier: pursesNotifierKit.notifier,
Expand Down