-
Notifications
You must be signed in to change notification settings - Fork 212
/
pegasus.js
500 lines (447 loc) · 15.2 KB
/
pegasus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// @ts-check
import { assert, X, Fail, makeError } from '@endo/errors';
import { makeLegacyWeakMap, makeLegacyMap } from '@agoric/store';
import { E, Far } from '@endo/far';
import {
assertProposalShape,
atomicTransfer,
} from '@agoric/zoe/src/contractSupport/index.js';
import { makeSubscriptionKit } from '@agoric/notifier';
import { IBCSourceTraceDenomTransformer } from './ibc-trace.js';
import { ICS20TransferProtocol } from './ics20.js';
import { makeCourierMaker, getCourierPK } from './courier.js';
/**
* @import {CloseReason, Connection} from '@agoric/network';
* @import {Remote} from '@agoric/vow';
*/
const DEFAULT_DENOM_TRANSFORMER = IBCSourceTraceDenomTransformer;
const DEFAULT_TRANSFER_PROTOCOL = ICS20TransferProtocol;
const TRANSFER_PROPOSAL_SHAPE = {
give: {
Transfer: null,
},
};
/**
* Make a Pegasus public API.
*
* @param {object} powers
* @param {ZCF} powers.zcf the Zoe Contract Facet
* @param {Remote<BoardDepositFacet>} powers.board where to find depositFacets by boardID
* @param {Remote<NameHub>} powers.namesByAddress where to find depositFacets by bech32
* @param {ReturnType<import('@agoric/vow').prepareVowTools>['when']} powers.when
*
* @import {NameHub} from '@agoric/vats'
*/
export const makePegasus = ({ zcf, board, namesByAddress, when }) => {
/**
* @typedef {object} LocalDenomState
* @property {string} localAddr
* @property {string} remoteAddr
* @property {LegacyMap<Denom, PromiseRecord<Courier>>} receiveDenomToCourierPK
* @property {IterationObserver<Denom>} receiveDenomPublication
* @property {Subscription<Denom>} remoteDenomSubscription
* @property {bigint} lastDenomNonce Distinguish Pegasus-created denom names
* that are sent and received from a remote connection
* @property {(reason: CloseReason) => void} abort
*/
let lastLocalIssuerNonce = 0n;
/**
* Create a new issuer keyword (based on Local + nonce)
*
* @returns {string}
*/
const createLocalIssuerKeyword = () => {
lastLocalIssuerNonce += 1n;
return `Local${lastLocalIssuerNonce}`;
};
/**
* @type {LegacyWeakMap<Remote<Peg>, LocalDenomState>}
*/
// Legacy because Mappings mix functions and data
const pegToDenomState = makeLegacyWeakMap('Peg');
/**
* Create a fresh Peg associated with a descriptor.
*
* @typedef {object} PegasusDescriptor
* @property {Brand} localBrand
* @property {Denom} receiveDenom
* @property {Denom} sendDenom
* @property {string} allegedName
*
* @param {LocalDenomState} state
* @param {PegasusDescriptor} desc
* @returns {Peg}
*/
const makePeg = (state, desc) => {
/** @type {Peg} */
const peg = Far(`${desc.allegedName} peg`, {
getAllegedName() {
return desc.allegedName;
},
getLocalBrand() {
return desc.localBrand;
},
getReceiveDenom() {
return desc.receiveDenom;
},
getSendDenom() {
return desc.sendDenom;
},
});
pegToDenomState.init(peg, state);
return peg;
};
/**
* @param {object} param0
* @param {ReturnType<typeof makeCourierMaker>} param0.makeCourier
* @param {LocalDenomState} param0.localDenomState
* @param {Remote<TransferProtocol>} param0.transferProtocol
* @param {Remote<DenomTransformer>} param0.denomTransformer
* @returns {PegasusConnectionActions}
*/
const makePegasusConnectionActions = ({
makeCourier,
localDenomState,
transferProtocol,
denomTransformer,
}) => {
let checkAbort = () => {};
/** @type {Set<Peg>} */
const pegs = new Set();
/** @type {PegasusConnectionActions} */
const pegasusConnectionActions = Far('pegasusConnectionActions', {
async rejectTransfersWaitingForPegRemote(remoteDenom) {
checkAbort();
const { receiveDenomToCourierPK } = localDenomState;
const { receiveDenom } = await E(
denomTransformer,
).getDenomsForRemotePeg(
remoteDenom,
localDenomState.localAddr,
localDenomState.remoteAddr,
);
checkAbort();
const { reject, promise } = receiveDenomToCourierPK.get(receiveDenom);
// Only continue if the promise isn't already resolved.
const raceWinner = await Promise.race([promise, null]);
checkAbort();
!raceWinner || Fail`${receiveDenom} is already resolved`;
// If rejected, the rejection is returned to our caller, so we have
// handled it correctly and that flow doesn't need to trigger an
// additional UnhandledRejectionWarning in our vat.
promise.catch(() => {});
reject(makeError(X`${receiveDenom} is temporarily unavailable`));
// Allow new transfers to be initiated after this rejection.
receiveDenomToCourierPK.delete(receiveDenom);
},
async pegRemote(
allegedName,
remoteDenom,
assetKind = undefined,
displayInfo = undefined,
) {
checkAbort();
const { receiveDenomToCourierPK } = localDenomState;
// Create the issuer for the local erights corresponding to the remote values.
const localKeyword = createLocalIssuerKeyword();
const zcfMint = await zcf.makeZCFMint(
localKeyword,
assetKind,
displayInfo,
);
checkAbort();
const { brand: localBrand } = zcfMint.getIssuerRecord();
const { sendDenom, receiveDenom } = await E(
denomTransformer,
).getDenomsForRemotePeg(
remoteDenom,
localDenomState.localAddr,
localDenomState.remoteAddr,
);
checkAbort();
// Describe how to retain/redeem pegged shadow erights.
const courier = makeCourier({
zcf,
localBrand,
board,
namesByAddress,
sendDenom,
retain: (zcfSeat, amounts) =>
zcfMint.burnLosses(harden(amounts), zcfSeat),
redeem: (zcfSeat, amounts) => {
zcfMint.mintGains(harden(amounts), zcfSeat);
},
transferProtocol,
when,
});
const courierPK = getCourierPK(receiveDenom, receiveDenomToCourierPK);
courierPK.resolve(courier);
checkAbort();
const peg = makePeg(localDenomState, {
localBrand,
sendDenom,
receiveDenom,
allegedName,
});
pegs.add(peg);
return peg;
},
async pegLocal(allegedName, localIssuer) {
checkAbort();
localDenomState.lastDenomNonce += 1n;
const remoteDenom = `pegasus${localDenomState.lastDenomNonce}`;
// Create a seat in which to keep our escrowed ERTP assets of this denomination.
const { zcfSeat: poolSeat } = zcf.makeEmptySeatKit();
// Ensure the issuer can be used in Zoe offers.
const localKeyword = createLocalIssuerKeyword();
const { brand: localBrand } = await zcf.saveIssuer(
localIssuer,
localKeyword,
);
checkAbort();
const { sendDenom, receiveDenom } = await E(
denomTransformer,
).getDenomsForLocalPeg(
remoteDenom,
localDenomState.localAddr,
localDenomState.remoteAddr,
);
checkAbort();
/**
* Transfer amount (of localBrand) from loser to winner seats.
*
* @param {Amount} amount amount to transfer
* @param {Keyword} loserKeyword the keyword to take from the loser
* @param {ZCFSeat} loser seat to transfer from
* @param {Keyword} winnerKeyword the keyword to give to the winner
* @param {ZCFSeat} winner seat to transfer to
*/
const transferAmountFrom = (
amount,
loserKeyword,
loser,
winnerKeyword,
winner,
) => {
// Transfer the amount to our backing seat.
atomicTransfer(
zcf,
loser,
winner,
{ [loserKeyword]: amount },
{ [winnerKeyword]: amount },
);
};
// Describe how to retain/redeem real local erights.
const courier = makeCourier({
zcf,
board,
namesByAddress,
sendDenom,
localBrand,
retain: (transferSeat, amounts) =>
transferAmountFrom(
amounts.Transfer,
'Transfer',
transferSeat,
'Pool',
poolSeat,
),
redeem: (transferSeat, amounts) =>
transferAmountFrom(
amounts.Transfer,
'Pool',
poolSeat,
'Transfer',
transferSeat,
),
transferProtocol,
when,
});
const { receiveDenomToCourierPK } = localDenomState;
const courierPK = getCourierPK(receiveDenom, receiveDenomToCourierPK);
courierPK.resolve(courier);
const peg = makePeg(localDenomState, {
localBrand,
sendDenom,
receiveDenom,
allegedName,
});
pegs.add(peg);
return peg;
},
abort: reason => {
checkAbort();
checkAbort = () => {
throw reason;
};
for (const peg of pegs) {
pegToDenomState.delete(peg);
}
},
});
return pegasusConnectionActions;
};
return Far('pegasus', {
/**
* Return a handler that can be used with the Network API.
*
* @param {Remote<TransferProtocol>} [transferProtocol]
* @param {Remote<DenomTransformer>} [denomTransformer]
* @returns {PegasusConnectionKit}
*/
makePegasusConnectionKit(
transferProtocol = DEFAULT_TRANSFER_PROTOCOL,
denomTransformer = DEFAULT_DENOM_TRANSFORMER,
) {
/**
* @type {LegacyWeakMap<Remote<Connection>, LocalDenomState>}
*/
// Legacy because the value contains a JS Set
const connectionToLocalDenomState = makeLegacyWeakMap('Connection');
/**
* @type {SubscriptionRecord<PegasusConnection>}
*/
const {
subscription: connectionSubscription,
publication: connectionPublication,
} = makeSubscriptionKit();
/** @type {ConnectionHandler} */
const handler = {
async onOpen(c, localAddr, remoteAddr) {
// Register `c` with the table of Peg receivers.
const {
subscription: remoteDenomSubscription,
publication: receiveDenomPublication,
} = makeSubscriptionKit();
const receiveDenomToCourierPK = makeLegacyMap('Denomination');
/** @type {LocalDenomState} */
const localDenomState = {
localAddr,
remoteAddr,
receiveDenomToCourierPK,
lastDenomNonce: 0n,
receiveDenomPublication,
remoteDenomSubscription,
abort: reason => {
actions.abort(reason);
},
};
// The courier is the only thing that we use to send messages to `c`.
const makeCourier = makeCourierMaker(c);
const actions = makePegasusConnectionActions({
localDenomState,
makeCourier,
transferProtocol,
denomTransformer,
});
connectionToLocalDenomState.init(c, localDenomState);
/** @type {PegasusConnection} */
const state = harden({
localAddr,
remoteAddr,
actions,
remoteDenomSubscription,
});
connectionPublication.updateState(state);
},
async onReceive(c, packetBytes) {
const doReceive = async () => {
// Dispatch the packet to the appropriate Peg for this connection.
const parts =
await E(transferProtocol).parseTransferPacket(packetBytes);
const { remoteDenom: receiveDenom } = parts;
assert.typeof(receiveDenom, 'string');
const { receiveDenomToCourierPK, receiveDenomPublication } =
connectionToLocalDenomState.get(c);
if (!receiveDenomToCourierPK.has(receiveDenom)) {
// This is the first time we've heard of this denomination.
receiveDenomPublication.updateState(receiveDenom);
}
// Wait for the courier to be instantiated.
const courierPK = getCourierPK(
receiveDenom,
receiveDenomToCourierPK,
);
const { receive } = await courierPK.promise;
return receive(parts);
};
return doReceive().catch(error =>
E(transferProtocol).makeTransferPacketAck(false, error),
);
},
async onClose(c) {
// Unregister `c`. Pending transfers will be rejected by the Network
// API.
const {
receiveDenomPublication,
receiveDenomToCourierPK,
localAddr,
remoteAddr,
abort,
} = connectionToLocalDenomState.get(c);
connectionToLocalDenomState.delete(c);
const err = makeError(X`pegasusConnectionHandler closed`);
receiveDenomPublication.fail(err);
/** @type {PegasusConnection} */
const state = harden({
localAddr,
remoteAddr,
});
connectionPublication.updateState(state);
for (const courierPK of receiveDenomToCourierPK.values()) {
try {
courierPK.reject(err);
} catch (e) {
// Already resolved/rejected, so ignore.
}
}
abort(err);
},
};
return harden({
handler: Far('pegasusConnectionHandler', handler),
subscription: connectionSubscription,
});
},
getLocalIssuer(localBrand) {
return zcf.getIssuerForBrand(localBrand);
},
/**
* Create a Zoe invitation to transfer assets over network to a deposit address.
*
* @param {Remote<Peg>} pegP the peg over which to transfer
* @param {DepositAddress} depositAddress the remote receiver's address
* @param {string} [memo] the memo to attach to ics transfer packet
* @param {SenderOptions} [opts] additional sender options
* @returns {Promise<Invitation>} to transfer, make an offer of { give: { Transfer: pegAmount } } to this invitation
*/
async makeInvitationToTransfer(pegP, depositAddress, memo = '', opts = {}) {
// Verify the peg.
const peg = await pegP;
const denomState = pegToDenomState.get(peg);
// Get details from the peg.
const [receiveDenom, sendDenom] = await Promise.all([
E(peg).getReceiveDenom(),
E(peg).getSendDenom(),
]);
const { receiveDenomToCourierPK } = denomState;
const courierPK = getCourierPK(receiveDenom, receiveDenomToCourierPK);
const { send } = await courierPK.promise;
/**
* Attempt the transfer, returning a refund if failed.
*
* @type {OfferHandler}
*/
const offerHandler = zcfSeat => {
assertProposalShape(zcfSeat, TRANSFER_PROPOSAL_SHAPE);
send(zcfSeat, depositAddress, memo, opts);
};
return zcf.makeInvitation(offerHandler, `pegasus ${sendDenom} transfer`);
},
});
};
harden(makePegasus);
/**
* @typedef {ReturnType<typeof makePegasus>} Pegasus
*/