-
Notifications
You must be signed in to change notification settings - Fork 229
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(smartWallet): defer deposits until purse available #6172
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2bcefc7
feat(smartWallet): defer deposits until purse available
turadg e6f5b6f
feat(smartWallet): payouts deposit eagerly and defer if necessary
turadg 4b7e8c7
fixup! feat(smartWallet): defer deposits until purse available
turadg cf6f0dc
Merge branch 'master' into ta/smartWallet-deposits
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// @ts-check | ||
import { AmountShape, PaymentShape } from '@agoric/ertp'; | ||
import { AmountMath, AmountShape, PaymentShape } from '@agoric/ertp'; | ||
import { isNat } from '@agoric/nat'; | ||
import { | ||
makeStoredPublishKit, | ||
|
@@ -75,6 +75,7 @@ const { details: X, quote: q } = assert; | |
* @typedef {Parameters<initState>[0] & Parameters<initState>[1]} HeldParams | ||
* | ||
* @typedef {Readonly<HeldParams & { | ||
* paymentQueues: MapStore<Brand, Array<import('@endo/far').FarRef<Payment>>>, | ||
* offerToInvitationMakers: MapStore<number, import('./types').RemoteInvitationMakers>, | ||
* brandDescriptors: MapStore<Brand, BrandDescriptor>, | ||
* brandPurses: MapStore<Brand, RemotePurse>, | ||
|
@@ -120,14 +121,19 @@ export const initState = (unique, shared) => { | |
); | ||
|
||
const preciousState = { | ||
// Private purses. This assumes one purse per brand, which will be valid in MN-1 but not always. | ||
brandPurses: makeScalarBigMapStore('brand purses', { durable: true }), | ||
// Payments that couldn't be deposited when received. | ||
// NB: vulnerable to uncapped growth by unpermissioned deposits. | ||
paymentQueues: makeScalarBigMapStore('payments queues', { | ||
durable: true, | ||
}), | ||
// Invitation makers yielded by offer results | ||
offerToInvitationMakers: makeScalarBigMapStore('invitation makers', { | ||
durable: true, | ||
}), | ||
// What purses have reported on construction and by getCurrentAmountNotifier updates. | ||
purseBalances: makeScalarMapStore(), | ||
// Private purses. This assumes one purse per brand, which will be valid in MN-1 but not always. | ||
brandPurses: makeScalarBigMapStore('brand purses', { durable: true }), | ||
}; | ||
|
||
const nonpreciousState = { | ||
|
@@ -202,8 +208,13 @@ const behavior = { | |
/** @type {(desc: Omit<BrandDescriptor, 'displayInfo'>, purse: RemotePurse) => Promise<void>} */ | ||
async addBrand(desc, purseRef) { | ||
/** @type {State} */ | ||
const { address, brandDescriptors, brandPurses, updatePublishKit } = | ||
this.state; | ||
const { | ||
address, | ||
brandDescriptors, | ||
brandPurses, | ||
paymentQueues, | ||
updatePublishKit, | ||
} = this.state; | ||
// assert haven't received this issuer before. | ||
const descriptorsHas = brandDescriptors.has(desc.brand); | ||
const pursesHas = brandPurses.has(desc.brand); | ||
|
@@ -248,27 +259,46 @@ const behavior = { | |
}); | ||
|
||
updatePublishKit.publisher.publish({ updated: 'brand', descriptor }); | ||
|
||
// deposit queued payments | ||
const payments = paymentQueues.has(desc.brand) | ||
? paymentQueues.get(desc.brand) | ||
: []; | ||
const deposits = payments.map(p => | ||
// @ts-expect-error deposit does take a FarRef<Payment> | ||
E(purse).deposit(p), | ||
); | ||
Promise.all(deposits).catch(err => | ||
console.error('ERROR depositing queued payments', err), | ||
); | ||
}, | ||
}, | ||
/** | ||
* Similar to {DepositFacet} but async because it has to look up the purse. | ||
*/ | ||
// TODO(PS0) decide whether to match canonical `DepositFacet'. it would have to take a local Payment. | ||
// TODO(PS0) decide whether to match canonical `DepositFacet'. it would have to take a local Payment | ||
deposit: { | ||
/** | ||
* Put the assets from the payment into the appropriate purse | ||
* Put the assets from the payment into the appropriate purse. | ||
* | ||
* If the purse doesn't exist, we hold the payment until it does. | ||
* | ||
* @param {import('@endo/far').FarRef<Payment>} payment | ||
* @returns {Promise<Amount>} | ||
* @throws if the purse doesn't exist | ||
* NB: the previous smart wallet contract would try again each time there's a new issuer. | ||
* This version does not: 1) for expedience, 2: to avoid resource exhaustion vulnerability. | ||
* @returns {Promise<Amount>} amounts for deferred deposits will be empty | ||
*/ | ||
async receive(payment) { | ||
/** @type {State} */ | ||
const { brandPurses } = this.state; | ||
const { brandPurses, paymentQueues: queues } = this.state; | ||
const brand = await E(payment).getAllegedBrand(); | ||
const purse = brandPurses.get(brand); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This get will throw rather than returning undefined, no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes good catch! I added a unit test for regression and it caught a missing harden too. |
||
if (!purse) { | ||
if (queues.has(brand)) { | ||
queues.get(brand).push(payment); | ||
} else { | ||
queues.init(brand, [payment]); | ||
} | ||
return AmountMath.makeEmpty(brand); | ||
} | ||
|
||
// @ts-expect-error deposit does take a FarRef<Payment> | ||
return E(purse).deposit(payment); | ||
|
@@ -292,7 +322,7 @@ const behavior = { | |
* @throws if any parts of the offer can be determined synchronously to be invalid | ||
*/ | ||
async executeOffer(offerSpec) { | ||
const { state } = this; | ||
const { facets, state } = this; | ||
const { | ||
zoe, | ||
brandPurses, | ||
|
@@ -305,6 +335,7 @@ const behavior = { | |
|
||
const executor = makeOfferExecutor({ | ||
zoe, | ||
depositFacet: facets.deposit, | ||
powers: { | ||
invitationFromSpec: makeInvitationsHelper( | ||
zoe, | ||
|
@@ -388,7 +419,7 @@ const finish = ({ state, facets }) => { | |
/** @type {RemotePurse} */ (invitationPurse), | ||
); | ||
// watch the bank for new issuers to make purses out of | ||
observeIteration(E(bank).getAssetSubscription(), { | ||
void observeIteration(E(bank).getAssetSubscription(), { | ||
async updateState(desc) { | ||
/** @type {RemotePurse} */ | ||
// @ts-expect-error cast to RemotePurse | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 good idea to re-use the depositFacet