Skip to content

Commit

Permalink
fix: use unique offer descriptions for swaps (#44)
Browse files Browse the repository at this point in the history
* fix: use unique offer descriptions for swaps

* feat: store offerNonce durably
  • Loading branch information
samsiegart authored May 21, 2024
1 parent dfda549 commit 8d801d9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
Empty file removed contract/,tx.json
Empty file.
1 change: 1 addition & 0 deletions contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@agoric/governance": "^0.10.4-u14.0",
"@agoric/inter-protocol": "0.16.2-u14.1",
"@agoric/vats": "0.15.2-u14.0",
"@agoric/vat-data": "0.5.3-u14.0",
"@agoric/zoe": "^0.26.3-u14.0",
"@endo/bundle-source": "^2.8.0",
"@endo/far": "^0.2.22",
Expand Down
16 changes: 15 additions & 1 deletion contract/src/swaparoo.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { ParamTypes } from '@agoric/governance/src/constants.js';
import { CONTRACT_ELECTORATE } from '@agoric/governance/src/contractGovernance/governParam.js';
import { handleParamGovernance } from '@agoric/governance/src/contractHelper.js';
import { provide } from '@agoric/vat-data';
import { makeCollectFeesInvitation } from './collectFees.js';
import { fixHub } from './fixHub.js';

Expand Down Expand Up @@ -121,6 +122,18 @@ export const start = async (zcf, privateArgs, baggage) => {
// TODO: update with Fee param
const feeShape = makeNatAmountShape(feeBrand, params.getFee().value);

const generateOfferNonce = (() => {
// Provide the nonce durably so it can stay unique if contract upgrades.
// See: https://docs.agoric.com/guides/zoe/contract-upgrade.html#durability
let offerNonce = provide(baggage, 'offerNonce', () => -1);

return () => {
offerNonce += 1;
baggage.set('offerNonce', offerNonce);
return offerNonce;
};
})();

/**
* @param { ZCFSeat } firstSeat
* @param {{ addr: string }} offerArgs
Expand Down Expand Up @@ -158,9 +171,10 @@ export const start = async (zcf, privateArgs, baggage) => {
return swapWithFee(zcf, firstSeat, secondSeat, feeSeat, params.getFee());
};

const description = `matchOffer-${generateOfferNonce()}`;
const secondSeatInvitation = await zcf.makeInvitation(
secondSeatOfferHandler,
'matchOffer',
description,
{ give: give1, want: want1 }, // "give" and "want" are from the proposer's perspective
);

Expand Down
2 changes: 1 addition & 1 deletion contract/test/snapshots/test-swap-wallet.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Generated by [AVA](https://avajs.dev).
{
id: 'jack-123',
invitationSpec: {
description: 'matchOffer',
description: 'matchOffer-0',
instance: Object @Alleged: InstanceHandle {},
source: 'purse',
},
Expand Down
Binary file modified contract/test/snapshots/test-swap-wallet.js.snap
Binary file not shown.
13 changes: 11 additions & 2 deletions contract/test/test-swap-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const startAlice = async (
* @param {MockWallet} wallet
* @param {Amount} beansAmount
* @param {Amount} cowsAmount
* @param {string} offerDescription
* @param {boolean} [jackPays]
*/
const startJack = async (
Expand All @@ -169,6 +170,7 @@ const startJack = async (
wallet,
beansAmount,
cowsAmount,
offerDescription,
jackPays = false,
) => {
const instance = wellKnown.instance[contractName];
Expand All @@ -188,7 +190,7 @@ const startJack = async (
invitationSpec: {
source: 'purse',
instance,
description: 'matchOffer',
description: offerDescription,
},
proposal,
};
Expand Down Expand Up @@ -278,7 +280,14 @@ test.serial('basic swap', async t => {
await E(E.get(bldIssuerKit).mint).mintPayment(cowAmount),
);
const jackSeat = seatLike(
await startJack(t, wellKnown, wallet.jack, fiveBeans, cowAmount),
await startJack(
t,
wellKnown,
wallet.jack,
fiveBeans,
cowAmount,
'matchOffer-0',
),
);

const jackPayouts = await jackSeat.getPayoutAmounts();
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8453,7 +8453,7 @@ eslint@^8.47.0, eslint@^8.56.0:
strip-ansi "^6.0.1"
text-table "^0.2.0"

esm@agoric-labs/esm#Agoric-built, "esm@github:agoric-labs/esm#Agoric-built":
esm@agoric-labs/esm#Agoric-built:
version "3.2.25"
resolved "https://codeload.github.com/agoric-labs/esm/tar.gz/3603726ad4636b2f865f463188fcaade6375638e"

Expand Down

0 comments on commit 8d801d9

Please sign in to comment.