Skip to content

Commit

Permalink
feat(wallet): subscribe to bank assets and fetch purses
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 1, 2021
1 parent 3148b83 commit 92a0a44
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/dapp-svelte-wallet/api/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default async function deployWallet(
const {
agoric: {
agoricNames,
bank,
namesByAddress,
myAddressNameAdmin,
board,
Expand Down Expand Up @@ -75,7 +76,7 @@ export default async function deployWallet(
// Claim the payments.
const issuerToPetname = new Map();
const issuerToPursePetnameP = new Map();
const wallet = E(walletVat).getWallet();
const wallet = E(walletVat).getWallet(bank);
const walletAdmin = E(wallet).getAdminFacet();
await Promise.all(
paymentInfo.map(async ({ issuerPetname, issuer }) => {
Expand Down
36 changes: 33 additions & 3 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { amountMath } from '@agoric/ertp';
import { E } from '@agoric/eventual-send';

import { makeMarshal, passStyleOf, Far } from '@agoric/marshal';
import { makeNotifierKit, observeNotifier } from '@agoric/notifier';
import {
makeNotifierKit,
observeIteration,
observeNotifier,
} from '@agoric/notifier';
import { makePromiseKit } from '@agoric/promise-kit';

import { makeIssuerTable } from './issuerTable';
Expand Down Expand Up @@ -637,11 +641,13 @@ export function makeWallet({
brandPetname,
petnameForPurse,
defaultAutoDeposit = false,
purseMaker = issuer => E(issuer).makeEmptyPurse(),
) => {
const brand = brandMapping.petnameToVal.get(brandPetname);
const { issuer } = brandTable.getByBrand(brand);

const purse = await E(issuer).makeEmptyPurse();
/** @type {Purse} */
const purse = await purseMaker(issuer);

purseToBrand.init(purse, brand);
petnameForPurse = purseMapping.suggestPetname(petnameForPurse, purse);
Expand Down Expand Up @@ -1519,5 +1525,29 @@ export function makeWallet({
.then(addInviteDepositFacet);
zoeInvitePurse = wallet.getPurse(ZOE_INVITE_PURSE_PETNAME);
};
return { admin: wallet, initialized: initialize() };

const importBankAssets = async bank => {
observeIteration(E(bank).getAssetSubscription(), {
async updateState({ proposedName, issuerName, issuer, brand }) {
try {
await addIssuer(issuerName, issuer);
const purse = await E(bank).getPurse(brand);
await wallet.makeEmptyPurse(
issuerName,
proposedName,
true,
() => purse,
);
} catch (e) {
console.error('/// could not add bank asset purse', e, {
issuerName,
proposedName,
issuer,
brand,
});
}
},
}).finally(() => console.error('/// This is the end of the bank assets'));
};
return { admin: wallet, initialized: initialize(), importBankAssets };
}
10 changes: 9 additions & 1 deletion packages/dapp-svelte-wallet/api/src/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import { bigintStringify } from './bigintStringify';
import './internal-types';

export function buildRootObject(_vatPowers) {
let walletRoot;
/** @type {WalletAdminFacet} */
let walletAdmin;
/** @type {Array<PursesJSONState>} */
let pursesState = [];
/** @type {Array<OfferState>} */
let inboxState = [];
let http;

const bridgeHandles = new Set();
const offerSubscriptions = new Map();

Expand Down Expand Up @@ -95,6 +97,7 @@ export function buildRootObject(_vatPowers) {
await w.initialized;
console.error('wallet initialized');
walletAdmin = w.admin;
walletRoot = w;
}

/**
Expand Down Expand Up @@ -265,7 +268,12 @@ export function buildRootObject(_vatPowers) {
};
harden(preapprovedBridge);

async function getWallet() {
async function getWallet(bank) {
console.error('/// importing bank assets', bank);
if (bank) {
walletRoot.importBankAssets(bank);
}

/**
* This is the complete wallet, including the means to get the
* WalletAdminFacet (which is not yet standardized, but necessary for the
Expand Down

0 comments on commit 92a0a44

Please sign in to comment.