|
1 | | -import { Fail } from '@endo/errors'; |
| 1 | +import { Fail, b } from '@endo/errors'; |
2 | 2 | import { E } from '@endo/eventual-send'; |
3 | 3 | import { makePromiseKit } from '@endo/promise-kit'; |
4 | | -import { mustMatch, keyEQ } from '@agoric/store'; |
| 4 | +import { mustMatch, keyEQ, isKey, isPattern } from '@endo/patterns'; |
5 | 5 | import { AssetKind } from '@agoric/ertp'; |
6 | 6 | import { fromUniqueEntries } from '@agoric/internal'; |
7 | 7 | import { satisfiesWant } from '../contractFacet/offerSafety.js'; |
8 | 8 | import { atomicTransfer, fromOnly, toOnly } from './atomicTransfer.js'; |
9 | 9 |
|
10 | 10 | /** |
11 | 11 | * @import {Pattern} from '@endo/patterns'; |
12 | | - * @import {ContractMeta, Invitation, Proposal, ZCF, ZCFSeat} from '@agoric/zoe'; |
| 12 | + * @import {Invitation, Proposal, ZCF, ZCFSeat, AmountBoundKeywordRecord} from '@agoric/zoe'; |
13 | 13 | */ |
14 | 14 |
|
15 | 15 | export const defaultAcceptanceMsg = `The offer has been accepted. Once the contract has been completed, please check your payout`; |
@@ -73,16 +73,42 @@ export const swap = (zcf, leftSeat, rightSeat) => { |
73 | 73 | return defaultAcceptanceMsg; |
74 | 74 | }; |
75 | 75 |
|
| 76 | +/** |
| 77 | + * @param {AmountBoundKeywordRecord} want |
| 78 | + * @param {string} complaint |
| 79 | + * @returns {AmountKeywordRecord} |
| 80 | + */ |
| 81 | +export const mustBeKey = (want, complaint) => { |
| 82 | + if (isKey(want)) { |
| 83 | + return want; |
| 84 | + } |
| 85 | + if (isPattern(want)) { |
| 86 | + throw Fail`${b(complaint)}: ${want}`; |
| 87 | + } |
| 88 | + throw Fail`Must be key: ${want}`; |
| 89 | +}; |
| 90 | +harden(mustBeKey); |
| 91 | + |
76 | 92 | /** @type {SwapExact} */ |
77 | 93 | export const swapExact = (zcf, leftSeat, rightSeat) => { |
78 | 94 | try { |
| 95 | + const { give: rightGive, want: rightWantBound } = rightSeat.getProposal(); |
| 96 | + const { give: leftGive, want: leftWantBound } = leftSeat.getProposal(); |
| 97 | + const rightWant = mustBeKey( |
| 98 | + rightWantBound, |
| 99 | + 'TODO: swapExact does not yet support want patterns', |
| 100 | + ); |
| 101 | + const leftWant = mustBeKey( |
| 102 | + leftWantBound, |
| 103 | + 'TODO: swapExact does not yet support want patterns', |
| 104 | + ); |
79 | 105 | zcf.atomicRearrange( |
80 | 106 | harden([ |
81 | | - fromOnly(rightSeat, rightSeat.getProposal().give), |
82 | | - fromOnly(leftSeat, leftSeat.getProposal().give), |
| 107 | + fromOnly(rightSeat, rightGive), |
| 108 | + fromOnly(leftSeat, leftGive), |
83 | 109 |
|
84 | | - toOnly(leftSeat, leftSeat.getProposal().want), |
85 | | - toOnly(rightSeat, rightSeat.getProposal().want), |
| 110 | + toOnly(leftSeat, leftWant), |
| 111 | + toOnly(rightSeat, rightWant), |
86 | 112 | ]), |
87 | 113 | ); |
88 | 114 | } catch (err) { |
|
0 commit comments