Skip to content

Commit

Permalink
feat: default new purses to autodeposit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 4, 2021
1 parent fffd37e commit b210b4b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/dapp-svelte-wallet/api/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default async function deployWallet(
issuerToPursePetnameP.set(
issuer,
E(walletAdmin)
.makeEmptyPurse(issuerPetname, pursePetname)
.makeEmptyPurse(issuerPetname, pursePetname, true)
.then(
_ => pursePetname,
_ => pursePetname,
Expand Down
31 changes: 25 additions & 6 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ export function makeWallet({
petnameForBrand = brandMapping.suggestPetname(petnameForBrand, brand);
if (!already && makePurse) {
// eslint-disable-next-line no-use-before-define
p = makeEmptyPurse(petnameForBrand, petnameForBrand);
p = makeEmptyPurse(petnameForBrand, petnameForBrand, true);
} else {
p = Promise.resolve();
}
Expand Down Expand Up @@ -620,7 +620,11 @@ export function makeWallet({
return `instance ${q(petname)} successfully added to wallet`;
};

const makeEmptyPurse = async (brandPetname, petnameForPurse) => {
const makeEmptyPurse = async (
brandPetname,
petnameForPurse,
defaultAutoDeposit = false,
) => {
const brand = brandMapping.petnameToVal.get(brandPetname);
const { issuer } = brandTable.getByBrand(brand);

Expand All @@ -629,6 +633,13 @@ export function makeWallet({
purseToBrand.init(purse, brand);
petnameForPurse = purseMapping.suggestPetname(petnameForPurse, purse);

if (defaultAutoDeposit && !brandToAutoDepositPurse.has(brand)) {
// Try to initialize the autodeposit purse for this brand.
// Don't do state updates, since we'll do that next.
// eslint-disable-next-line no-use-before-define
await doEnableAutoDeposit(petnameForPurse, false);
}

await updatePursesState(petnameForPurse, purse);

// Just notice the balance updates for the purse.
Expand Down Expand Up @@ -1164,15 +1175,18 @@ export function makeWallet({
}

const pendingEnableAutoDeposits = makeStore('brand');
async function enableAutoDeposit(pursePetname) {
async function doEnableAutoDeposit(pursePetname, updateState) {
const purse = purseMapping.petnameToVal.get(pursePetname);
const brand = purseToBrand.get(purse);
if (brandToAutoDepositPurse.has(brand)) {
brandToAutoDepositPurse.set(brand, purse);
} else {
brandToAutoDepositPurse.init(brand, purse);
}
await updateAllPurseState();

if (updateState) {
await updateAllPurseState();
}

const pendingP =
pendingEnableAutoDeposits.has(brand) &&
Expand All @@ -1186,7 +1200,9 @@ export function makeWallet({
const boardId = await boardIdP;
brandToDepositFacetId.init(brand, boardId);

await updateAllPurseState();
if (updateState) {
await updateAllPurseState();
}
return boardIdP;
}

Expand Down Expand Up @@ -1432,7 +1448,10 @@ export function makeWallet({
getOffers,
getSeat: id => idToSeat.get(id),
getSeats: ids => ids.map(wallet.getSeat),
enableAutoDeposit,
enableAutoDeposit(pursePetname) {
// Enable the autodeposit with intermediary state updates.
return doEnableAutoDeposit(pursePetname, true);
},
disableAutoDeposit,
getDepositFacetId,
suggestIssuer,
Expand Down

0 comments on commit b210b4b

Please sign in to comment.