From 8d801d92bbed67926645a6738c25156cd08b2973 Mon Sep 17 00:00:00 2001 From: Samuel Siegart Date: Tue, 21 May 2024 12:43:54 -0700 Subject: [PATCH] fix: use unique offer descriptions for swaps (#44) * fix: use unique offer descriptions for swaps * feat: store offerNonce durably --- contract/,tx.json | 0 contract/package.json | 1 + contract/src/swaparoo.contract.js | 16 +++++++++++++++- contract/test/snapshots/test-swap-wallet.js.md | 2 +- .../test/snapshots/test-swap-wallet.js.snap | Bin 898 -> 911 bytes contract/test/test-swap-wallet.js | 13 +++++++++++-- yarn.lock | 2 +- 7 files changed, 29 insertions(+), 5 deletions(-) delete mode 100644 contract/,tx.json diff --git a/contract/,tx.json b/contract/,tx.json deleted file mode 100644 index e69de29b..00000000 diff --git a/contract/package.json b/contract/package.json index 2d5403ce..fe6392cf 100644 --- a/contract/package.json +++ b/contract/package.json @@ -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", diff --git a/contract/src/swaparoo.contract.js b/contract/src/swaparoo.contract.js index cf393b2e..dcccb8aa 100644 --- a/contract/src/swaparoo.contract.js +++ b/contract/src/swaparoo.contract.js @@ -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'; @@ -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 @@ -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 ); diff --git a/contract/test/snapshots/test-swap-wallet.js.md b/contract/test/snapshots/test-swap-wallet.js.md index 9d356e00..2e20c53d 100644 --- a/contract/test/snapshots/test-swap-wallet.js.md +++ b/contract/test/snapshots/test-swap-wallet.js.md @@ -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', }, diff --git a/contract/test/snapshots/test-swap-wallet.js.snap b/contract/test/snapshots/test-swap-wallet.js.snap index b8134522c6d911e26a6c661ae64f7b06ca0a172a..de8f920a640830aad8b3de0c627376d24c41dc09 100644 GIT binary patch literal 911 zcmV;A191F7RzV9NS|~lVXq1(; zqqU8pIt_Z?lfL)8_rCdVbr3}Ex_-Q^Qa=sU+CXaG?dmNV`#O}dz89tX$+qXoRDR@q z$#D$XLv?a41Goxc2;eIK+W_XM)}h+%!J2YYqT<+Yw?QXB0@T7BvDb)$oDMPH8y{z7 z;Ja!e`|&~#)FgTVU>t4w;ijL;)Q`eDv2sm(w-i4ft*)x1pA5AL&C_0}9w$*8X&D&O zeFAP0APIO(z)P}7vq{o*V!sivYs2a`jGtz}8w^-v7WDWu8Ja}PtVGfebt*$wnaF2s zYUO!(69S%!M}xq3mkZ1-xvr9NbZ$egt0h0t=^nuPKut%Z#8m|kHwx24x~btw7FbBn zD?Z+k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.js index 675c84d4..16dbe219 100644 --- a/contract/test/test-swap-wallet.js +++ b/contract/test/test-swap-wallet.js @@ -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 ( @@ -169,6 +170,7 @@ const startJack = async ( wallet, beansAmount, cowsAmount, + offerDescription, jackPays = false, ) => { const instance = wellKnown.instance[contractName]; @@ -188,7 +190,7 @@ const startJack = async ( invitationSpec: { source: 'purse', instance, - description: 'matchOffer', + description: offerDescription, }, proposal, }; @@ -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(); diff --git a/yarn.lock b/yarn.lock index 5c9807b1..266777ea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"