From 65d8ef62f257628009ffa8bb964ca1478bcb9d4f Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 14 Feb 2024 17:35:08 -0600 Subject: [PATCH 1/5] docs: explain platform-goals/ --- contract/src/platform-goals/README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 contract/src/platform-goals/README.md diff --git a/contract/src/platform-goals/README.md b/contract/src/platform-goals/README.md new file mode 100644 index 0000000..8ae7a08 --- /dev/null +++ b/contract/src/platform-goals/README.md @@ -0,0 +1,5 @@ +# Platform Goals + +Work on this dapp shows some ways that the platform could be improved. + +The code in this directory shows some possible platform refinements. From fd8d9ecb08ef9bc2ae5dd8d91bc58b8277fe16f4 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 14 Feb 2024 17:35:27 -0600 Subject: [PATCH 2/5] feat: atomicRearrange with named field support --- contract/src/platform-goals/zcfTools.js | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 contract/src/platform-goals/zcfTools.js diff --git a/contract/src/platform-goals/zcfTools.js b/contract/src/platform-goals/zcfTools.js new file mode 100644 index 0000000..f4fe569 --- /dev/null +++ b/contract/src/platform-goals/zcfTools.js @@ -0,0 +1,29 @@ +// @ts-check +import { atomicRearrange as atomicRearrangeTuples } from '@agoric/zoe/src/contractSupport/atomicTransfer.js'; + +/** @typedef {import("@agoric/zoe/src/contractSupport/atomicTransfer").TransferPart} TransferPart */ + +/** + * Refine atomicRearrange to support naming the fields of transfer parts. + * + * @param {ZCF} zcf + * @param { Array} transferParts + * + * @typedef {{ + * fromSeat?: ZCFSeat, + * toSeat?: ZCFSeat, + * fromAmounts?: AmountKeywordRecord, + * toAmounts?: AmountKeywordRecord + * }} TransferPartRecord + */ +export const atomicRearrange = (zcf, transferParts) => { + /** @type {TransferPart[]} */ + const tuples = harden( + transferParts.map(part => + Array.isArray(part) + ? part + : [part.fromSeat, part.toSeat, part.fromAmounts, part.toAmounts], + ), + ); + return atomicRearrangeTuples(zcf, tuples); +}; From 593e65c1bf71fcaab58b75f4a941d826d3349a5d Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 14 Feb 2024 17:38:54 -0600 Subject: [PATCH 3/5] refactor: use named fields in atomicRearrange --- contract/src/offer-up.contract.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/contract/src/offer-up.contract.js b/contract/src/offer-up.contract.js index e24f7d7..983c00a 100644 --- a/contract/src/offer-up.contract.js +++ b/contract/src/offer-up.contract.js @@ -23,7 +23,7 @@ import { Far } from '@endo/far'; import { M, getCopyBagEntries } from '@endo/patterns'; import { AssetKind } from '@agoric/ertp/src/amountMath.js'; import { AmountShape } from '@agoric/ertp/src/typeGuards.js'; -import { atomicRearrange } from '@agoric/zoe/src/contractSupport/atomicTransfer.js'; +import { atomicRearrange } from './platform-goals/zcfTools.js'; import '@agoric/zoe/exported.js'; const { Fail, quote: q } = assert; @@ -107,15 +107,11 @@ export const start = async zcf => { Fail`max ${q(maxItems)} items allowed: ${q(want.Items)}`; const newItems = itemMint.mintGains(want); - atomicRearrange( - zcf, - harden([ - // price from buyer to proceeds - [buyerSeat, proceeds, { Price: tradePrice }], - // new items to buyer - [newItems, buyerSeat, want], - ]), - ); + const charge = { Price: tradePrice }; + atomicRearrange(zcf, [ + { fromSeat: buyerSeat, toSeat: proceeds, fromAmounts: charge }, + { fromSeat: newItems, toSeat: buyerSeat, fromAmounts: want }, + ]); buyerSeat.exit(true); newItems.exit(); From 6df24ef690e135462073b7463e812d32cd0c597e Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 14 Feb 2024 23:06:04 -0600 Subject: [PATCH 4/5] chore: refine names, add mappedTo in TransferPartRecord --- contract/src/offer-up.contract.js | 4 ++-- contract/src/platform-goals/zcfTools.js | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/contract/src/offer-up.contract.js b/contract/src/offer-up.contract.js index 983c00a..4080f80 100644 --- a/contract/src/offer-up.contract.js +++ b/contract/src/offer-up.contract.js @@ -109,8 +109,8 @@ export const start = async zcf => { const newItems = itemMint.mintGains(want); const charge = { Price: tradePrice }; atomicRearrange(zcf, [ - { fromSeat: buyerSeat, toSeat: proceeds, fromAmounts: charge }, - { fromSeat: newItems, toSeat: buyerSeat, fromAmounts: want }, + { from: buyerSeat, to: proceeds, amounts: charge }, + { from: newItems, to: buyerSeat, amounts: want }, ]); buyerSeat.exit(true); diff --git a/contract/src/platform-goals/zcfTools.js b/contract/src/platform-goals/zcfTools.js index f4fe569..f34e4ed 100644 --- a/contract/src/platform-goals/zcfTools.js +++ b/contract/src/platform-goals/zcfTools.js @@ -1,5 +1,6 @@ // @ts-check import { atomicRearrange as atomicRearrangeTuples } from '@agoric/zoe/src/contractSupport/atomicTransfer.js'; +import { mapKeywords } from '@agoric/zoe/src/contractSupport/zoeHelpers.js'; /** @typedef {import("@agoric/zoe/src/contractSupport/atomicTransfer").TransferPart} TransferPart */ @@ -10,20 +11,24 @@ import { atomicRearrange as atomicRearrangeTuples } from '@agoric/zoe/src/contra * @param { Array} transferParts * * @typedef {{ - * fromSeat?: ZCFSeat, - * toSeat?: ZCFSeat, - * fromAmounts?: AmountKeywordRecord, - * toAmounts?: AmountKeywordRecord + * from?: ZCFSeat, + * to?: ZCFSeat, + * amounts?: AmountKeywordRecord, + * mappedTo?: KeywordKeywordRecord * }} TransferPartRecord */ export const atomicRearrange = (zcf, transferParts) => { /** @type {TransferPart[]} */ const tuples = harden( - transferParts.map(part => - Array.isArray(part) - ? part - : [part.fromSeat, part.toSeat, part.fromAmounts, part.toAmounts], - ), + transferParts.map(part => { + if (Array.isArray(part)) { + return part; + } + const toAmounts = part.mappedTo + ? mapKeywords(part.amounts, part.mappedTo) + : undefined; + return [part.from, part.to, part.amounts, toAmounts]; + }), ); return atomicRearrangeTuples(zcf, tuples); }; From 04f7376b9b902e7f617e2a2d6ad286de990e0101 Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 14 Feb 2024 23:25:13 -0600 Subject: [PATCH 5/5] refactor: slightly more concise --- contract/src/platform-goals/zcfTools.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/contract/src/platform-goals/zcfTools.js b/contract/src/platform-goals/zcfTools.js index f34e4ed..0472c67 100644 --- a/contract/src/platform-goals/zcfTools.js +++ b/contract/src/platform-goals/zcfTools.js @@ -24,9 +24,8 @@ export const atomicRearrange = (zcf, transferParts) => { if (Array.isArray(part)) { return part; } - const toAmounts = part.mappedTo - ? mapKeywords(part.amounts, part.mappedTo) - : undefined; + const toAmounts = + part.mappedTo && mapKeywords(part.amounts, part.mappedTo); return [part.from, part.to, part.amounts, toAmounts]; }), );