-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8af66c9
commit 45439ab
Showing
6 changed files
with
135 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// @ts-check | ||
|
||
import { Far } from '@agoric/marshal'; | ||
|
||
/** | ||
* | ||
* @param {Issuer} feeIssuer | ||
* @returns {{ | ||
* makeFeePurse: MakeFeePurse | ||
* isFeePurse: (feePurse: Purse) => boolean | ||
* }} | ||
*/ | ||
const setupMakeFeePurse = feeIssuer => { | ||
const feePurses = new WeakSet(); | ||
|
||
/** @type {MakeFeePurse} */ | ||
const makeFeePurse = () => { | ||
const purse = feeIssuer.makeEmptyPurse(); | ||
/** @type {FeePurse} */ | ||
const feePurse = Far('feePurse', { | ||
...purse, | ||
}); | ||
feePurses.add(feePurse); | ||
|
||
// After keeping the purse methods, we throw away the purse | ||
return feePurse; | ||
}; | ||
|
||
/** | ||
* @param {Purse} feePurse | ||
*/ | ||
const isFeePurse = feePurse => feePurses.has(feePurse); | ||
|
||
return { | ||
makeFeePurse, | ||
isFeePurse, | ||
}; | ||
}; | ||
|
||
harden(setupMakeFeePurse); | ||
export { setupMakeFeePurse }; |
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
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// @ts-check | ||
|
||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js'; | ||
|
||
import { makeIssuerKit, AssetKind, AmountMath } from '@agoric/ertp'; | ||
|
||
import { setupMakeFeePurse } from '../../../src/zoeService/feePurse.js'; | ||
|
||
const setup = () => { | ||
const runIssuerKit = makeIssuerKit('RUN', AssetKind.NAT, { | ||
decimalPlaces: 6, | ||
}); | ||
const { makeFeePurse, isFeePurse } = setupMakeFeePurse(runIssuerKit.issuer); | ||
return { makeFeePurse, isFeePurse, runIssuerKit }; | ||
}; | ||
|
||
test('feePurse starts empty', async t => { | ||
const { makeFeePurse } = setup(); | ||
const feePurse = makeFeePurse(); | ||
|
||
t.true(AmountMath.isEmpty(feePurse.getCurrentAmount())); | ||
}); | ||
|
||
test('depositing into and withdrawing from feePurse', async t => { | ||
const { makeFeePurse, runIssuerKit } = setup(); | ||
const feePurse = makeFeePurse(); | ||
|
||
const run1000 = AmountMath.make(runIssuerKit.brand, 1000n); | ||
const payment = runIssuerKit.mint.mintPayment(run1000); | ||
feePurse.deposit(payment); | ||
|
||
t.true(AmountMath.isEqual(feePurse.getCurrentAmount(), run1000)); | ||
|
||
feePurse.withdraw(run1000); | ||
|
||
t.true(AmountMath.isEmpty(feePurse.getCurrentAmount())); | ||
}); | ||
|
||
test('isFeePurse', async t => { | ||
const { makeFeePurse, isFeePurse } = setup(); | ||
const feePurse = makeFeePurse(); | ||
|
||
t.true(isFeePurse(feePurse)); | ||
}); |