Skip to content

Commit

Permalink
fixup! feat: durable smart wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jan 31, 2023
1 parent 7487bf9 commit 45ada35
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/inter-protocol/test/smartWallet/contexts.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const makeDefaultTestContext = async (t, makeSpace) => {
{ storageNode, walletBridgeManager },
);

/** @param {string} address */
const simpleProvideWallet = async address => {
// copied from makeClientBanks()
const bank = E(consume.bankManager).getBankForAddress(address);
Expand Down
40 changes: 30 additions & 10 deletions packages/smart-wallet/src/smartWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PurseShape,
} from '@agoric/ertp';
import { observeNotifier, prepareDurablePublishKit } from '@agoric/notifier';
import { StorageNodeShape } from '@agoric/notifier/src/typeGuards.js';
import { M, mustMatch } from '@agoric/store';
import { makeScalarBigMapStore, prepareExoClassKit } from '@agoric/vat-data';
import { makeEphemeralStoredSubscriberProvider } from '@agoric/zoe/src/contractSupport/index.js';
Expand Down Expand Up @@ -89,6 +90,7 @@ const mapToRecord = map => Object.fromEntries(map.entries());
* @typedef {{
* address: string,
* bank: ERef<import('@agoric/vats/src/vat-bank').Bank>,
* currentStorageNode: StorageNode,
* invitationPurse: Purse<'set'>,
* walletStorageNode: StorageNode,
* }} UniqueParams
Expand Down Expand Up @@ -120,7 +122,6 @@ const mapToRecord = map => Object.fromEntries(map.entries());
* brandPurses: MapStore<Brand, RemotePurse>,
* purseBalances: MapStore<RemotePurse, Amount>,
* updatePublishKit: PublishKit<UpdateRecord>,
* currentStorageNode: StorageNode,
* currentPublishKit: PublishKit<CurrentWalletRecord>,
* }>} ImmutableState
*
Expand Down Expand Up @@ -167,7 +168,8 @@ export const prepareSmartWallet = (baggage, shared) => {
address: M.string(),
bank: M.eref(M.remotable()),
invitationPurse: PurseShape,
walletStorageNode: M.eref(M.remotable('StorageNode')),
currentStorageNode: M.eref(StorageNodeShape),
walletStorageNode: M.eref(StorageNodeShape),
}),
);

Expand Down Expand Up @@ -271,7 +273,12 @@ export const prepareSmartWallet = (baggage, shared) => {
}),
};

return prepareExoClassKit(
/**
* Make the durable object to return, but taking some parameters that are awaited by a wrapping function.
* This is necessary because the class kit construction helpers, `initState` and `finish` run synchronously
* and the child storage node must be awaited until we have durable promises.
*/
const makeWalletWithResolvedStorageNodes = prepareExoClassKit(
baggage,
'SmartWallet',
behaviorGuards,
Expand Down Expand Up @@ -552,12 +559,6 @@ export const prepareSmartWallet = (baggage, shared) => {

const { helper } = facets;

const currentStorageNode = await E(
state.walletStorageNode,
).makeChildNode('current');
// @ts-expect-error readonly, after finish()
state.currentStorageNode = currentStorageNode;

// Ensure a purse for each issuer
void helper.addBrand(
{
Expand All @@ -583,7 +584,26 @@ export const prepareSmartWallet = (baggage, shared) => {
},
},
);

/**
* @param {Omit<UniqueParams, 'currentStorageNode' | 'walletStorageNode'> & {walletStorageNode: ERef<StorageNode>}} uniqueWithoutChildNodes
*/
const makeSmartWallet = async uniqueWithoutChildNodes => {
const [walletStorageNode, currentStorageNode] = await Promise.all([
uniqueWithoutChildNodes.walletStorageNode,
E(uniqueWithoutChildNodes.walletStorageNode).makeChildNode('current'),
]);

return makeWalletWithResolvedStorageNodes(
harden({
...uniqueWithoutChildNodes,
currentStorageNode,
walletStorageNode,
}),
).self;
};
return makeSmartWallet;
};
harden(prepareSmartWallet);

/** @typedef {ReturnType<ReturnType<typeof prepareSmartWallet>>['self']} SmartWallet */
/** @typedef {Awaited<ReturnType<ReturnType<typeof prepareSmartWallet>>>} SmartWallet */
6 changes: 3 additions & 3 deletions packages/smart-wallet/src/walletFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ export const start = async (zcf, privateArgs, baggage) => {
/** @type {() => Promise<import('./smartWallet').SmartWallet>} */
const maker = async () => {
const invitationPurse = await E(invitationIssuer).makeEmptyPurse();
const walletStorageNode = await E(storageNode).makeChildNode(address);
const wallet = makeSmartWallet(
const walletStorageNode = E(storageNode).makeChildNode(address);
const wallet = await makeSmartWallet(
harden({ address, walletStorageNode, bank, invitationPurse }),
).self;
);

// An await here would deadlock with invitePSMCommitteeMembers
void publishDepositFacet(address, wallet, namesByAddressAdmin);
Expand Down

0 comments on commit 45ada35

Please sign in to comment.