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

Query bank balances #51

Merged
merged 8 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
2 changes: 1 addition & 1 deletion src/components/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { useState } from 'react';

import { makeStyles, useTheme } from '@mui/styles';
import HelpIcon from '@mui/icons-material/HelpOutline';
import SettingsIcon from '@mui/icons-material/SettingsOutlined';
Expand Down
5 changes: 4 additions & 1 deletion src/components/SmartWalletConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const useProvisionPoolMetrics = (unserializer, leader) => {
break;
}
console.log('provisionPoolData', value);
setData(value);
setData(value as ProvisionPoolMetrics);
}
};
fetchData().catch(e =>
Expand Down Expand Up @@ -172,6 +172,7 @@ const SmartWalletConnection = ({
makeFollower(`:published.${path}`, leader, {
unserializer: context.fromMyWallet,
});

const bridge = makeWalletBridgeFromFollowers(
{
chainId: keplrConnection.chainId,
Expand All @@ -184,6 +185,8 @@ const SmartWalletConnection = ({
makeFollower(`:beansOwing.${publicAddress}`, leader, {
unserializer: { unserialize: data => data },
}),
followPublished('agoricNames.vbankAsset'),
followPublished('agoricNames.brand'),
keplrConnection,
// @ts-expect-error xxx
backendError,
Expand Down
93 changes: 53 additions & 40 deletions src/service/Offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
makeAsyncIterableFromNotifier,
} from '@agoric/notifier';
import { E } from '@endo/eventual-send';

import {
loadOffers as load,
removeOffer as remove,
Expand All @@ -17,11 +16,11 @@ import {

import type { SmartWalletKey } from '../store/Dapps';
import type { OfferSpec, OfferStatus } from '@agoric/smart-wallet/src/offers';
import { Marshal } from '@endo/marshal';

import type { Marshal } from '@endo/marshal';
import type { Notifier } from '@agoric/notifier/src/types';
import { Petname } from '@agoric/smart-wallet/src/types';
import { Brand } from '@agoric/ertp/src/types';
import type { Petname } from '@agoric/smart-wallet/src/types';
import type { Brand } from '@agoric/ertp/src/types';
import { AmountMath } from '@agoric/ertp';

export const getOfferService = (
smartWalletKey: SmartWalletKey,
Expand All @@ -30,7 +29,7 @@ export const getOfferService = (
boardIdMarshaller: Marshal<string>,
) => {
const offers = new Map<number, Offer>();
const { notifier, updater } = makeNotifierKit<OfferStatus>();
const { notifier, updater } = makeNotifierKit<Offer[]>();
const broadcastUpdates = () => updater.updateState([...offers.values()]);

const addSpendActionAndInstancePetname = async (
Expand All @@ -41,33 +40,38 @@ export const getOfferService = (
id,
instanceHandle,
publicInvitationMaker,
proposalTemplate: { give, want },
proposalTemplate: { give: giveTemplate, want: wantTemplate },
} = offer;

const mapPursePetnamesToBrands = paymentProposals =>
Object.fromEntries(
const convertProposals = async paymentProposals => {
const entries = await Promise.all(
Copy link
Member

Choose a reason for hiding this comment

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

this is a little hard to follow. consider filtering and naming the value transformer.

const convertProposal = ({ pursePetname, value, amount: serializedAmount }) =>
  serializedAmount
              ? await E(boardIdMarshaller).unserialize(serializedAmount)
              : AmountMath.make(
                  pursePetnameToBrand.get(pursePetname),
                  BigInt(value);

Object.entries(paymentProposals).
filter(([_kw, { pursePetname, amount}]) => amount && pursePetnameToBrand.get(pursePetname))
.map(([kw, proposal]) => convertProposal);

Then it's a small step to using objectMap and deeplyFulfilled;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I gave this a shot but I got a little confused with how to use those utility functions and the code wasn't looking much simpler, I'd prefer to leave this as is.

Object.entries(paymentProposals).map(
// @ts-expect-error
([kw, { pursePetname, value }]) => {
const brand = pursePetnameToBrand.get(pursePetname);
if (!brand) {
async ([kw, { pursePetname, value, amount: serializedAmount }]) => {
if (!serializedAmount && !pursePetnameToBrand.get(pursePetname)) {
return [];
}
return [
kw,
{
brand,
value: BigInt(value),
},
];

/// TODO: test e2e with dapp inter once feasible.
const amount = serializedAmount
? await E(boardIdMarshaller).unserialize(serializedAmount)
: AmountMath.make(
pursePetnameToBrand.get(pursePetname),
BigInt(value),
);

return [kw, amount];
},
),
);
return Object.fromEntries(entries);
};

const instance = await E(boardIdMarshaller).unserialize(instanceHandle);
const {
slots: [instanceBoardId],
} = await E(boardIdMarshaller).serialize(instance);
const [instance, give, want] = await Promise.all([
E(boardIdMarshaller).unserialize(instanceHandle),
convertProposals(giveTemplate),
convertProposals(wantTemplate),
]);

const offerForAction: OfferSpec = {
id,
Expand All @@ -77,17 +81,25 @@ export const getOfferService = (
publicInvitationMaker,
},
proposal: {
give: mapPursePetnamesToBrands(give),
want: mapPursePetnamesToBrands(want),
give,
want,
},
};

const spendAction = await E(boardIdMarshaller).serialize(
harden({
method: 'executeOffer',
offer: offerForAction,
}),
);
const [
{
slots: [instanceBoardId],
},
spendAction,
] = await Promise.all([
E(boardIdMarshaller).serialize(instance),
E(boardIdMarshaller).serialize(
harden({
method: 'executeOffer',
offer: offerForAction,
}),
),
]);

return {
...offer,
Expand Down Expand Up @@ -125,29 +137,30 @@ export const getOfferService = (
chainOffersNotifier,
)) {
console.log('offerStatus', { status, offers });
const oldOffer = offers.get(status?.id);
const id = status && Number(status?.id);
const oldOffer = offers.get(id);
if (!oldOffer) {
console.warn('Update for unknown offer, doing nothing.');
} else {
if (status.error !== undefined) {
offers.set(status.id, {
offers.set(id, {
...oldOffer,
id: status.id,
id,
status: OfferUIStatus.rejected,
error: `${status.error}`,
});
remove(smartWalletKey, status.id);
remove(smartWalletKey, id);
} else if (status.numWantsSatisfied !== undefined) {
offers.set(status.id, {
offers.set(id, {
...oldOffer,
id: status.id,
id,
status: OfferUIStatus.accepted,
});
remove(smartWalletKey, status.id);
remove(smartWalletKey, id);
} else if (status.numWantsSatisfied === undefined) {
offers.set(status.id, {
offers.set(id, {
...oldOffer,
id: status.id,
id,
status: OfferUIStatus.pending,
});
upsertOffer({ ...oldOffer, status: OfferUIStatus.pending });
Expand Down
1 change: 1 addition & 0 deletions src/store/Offers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Offer = {
status: OfferUIStatus;
instancePetname?: string;
spendAction?: string;
error?: unknown;
} & OfferConfig;

export const loadOffers = ({ chainId, address }: SmartWalletKey) =>
Expand Down
Loading