-
Notifications
You must be signed in to change notification settings - Fork 212
/
auction.js
144 lines (130 loc) · 4.26 KB
/
auction.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
// @ts-check
// @jessie-check
import { M, mustMatch } from '@endo/patterns';
import { BrandShape, RatioShape } from '@agoric/ertp/src/typeGuards.js';
import { makeBoardClient } from '../lib/boardClient.js';
import { makeVstorageQueryService } from '../lib/vstorage.js';
import { formatTimestamp, makeAssetFormatters } from '../lib/format.js';
// XXX push down into @agoric/ERTP?
const NatAmountShape = harden({ brand: BrandShape, value: M.nat() });
const TimeStampShape = {
timerBrand: M.remotable('timerBrand'),
absValue: M.nat(),
};
const bidData = harden({
timestamp: TimeStampShape,
sequence: M.nat(),
balance: NatAmountShape,
wanted: NatAmountShape,
exitAfterBuy: M.boolean(),
});
const shapeLeaves = harden({
ScaledBidData: {
bidScaling: RatioShape,
...bidData,
},
PricedBidData: {
price: RatioShape,
...bidData,
},
});
const shape = harden({
...shapeLeaves,
BidDataNotification: M.arrayOf(
M.or(shapeLeaves.ScaledBidData, shapeLeaves.PricedBidData),
),
});
/**
*
* @param {unknown} specimen
* @returns { asserts specimen is BidDataNotification }
*
* XXX contract should export this type:
*
* @typedef {{
* balance: Amount<'nat'>,
* exitAfterBuy: boolean,
* sequence: bigint,
* timestamp: import('@agoric/time').Timestamp,
* wanted: Amount<'nat'>,
* } & ({ price: Ratio } | { bidScaling: Ratio})} BidData1
* @typedef {BidData1[]} BidDataNotification
*/
const assertBidDataNotification = specimen => {
mustMatch(specimen, shape.BidDataNotification);
};
/**
*
* @param {ScaledBidData | PricedBidData} bid
* @param {ReturnType<typeof import('../lib/format').makeAssetFormatters>} fmt
*
* @typedef {import('@agoric/inter-protocol/src/auction/auctionBook.js').ScaledBidData} ScaledBidData
* @typedef {import('@agoric/inter-protocol/src/auction/auctionBook.js').PricedBidData} PricedBidData
*/
const fmtBid = (bid, fmt) => {
const { timestamp, sequence, balance, wanted, exitAfterBuy, ...more } = bid;
const p = 'price' in bid ? { price: fmt.price(bid.price) } : {};
const b = 'bidScaling' in bid ? { bidScaling: fmt.rate(bid.bidScaling) } : {};
const exit = exitAfterBuy ? { exitAfterBuy } : {};
// @ts-expect-error XXX how to do this?
const { price: _p, bidScaling: _b, ...rest } = more;
const info = harden({
// assume timerBrand gives values in seconds
timestamp: formatTimestamp(timestamp.absValue),
sequence: Number(sequence),
...p,
...b,
balance: fmt.amount(balance),
wanted: fmt.amount(wanted),
...exit,
...rest,
});
return info;
};
/**
*
* @param {import('commander').Command} interCmd
* @param {object} io
* @param {import('../lib/tui').TUI} io.tui
* @param {() => Promise<ReturnType<import('../lib/vstorage.js').makeBatchQuery>>} io.getBatchQuery
* @param {() => Promise<import('@cosmjs/tendermint-rpc').RpcClient>} io.makeRpcClient
*
* @typedef {import('@cosmjs/tendermint-rpc').RpcClient} RpcClient
*/
export const addBidCommand = (
interCmd,
{ tui, getBatchQuery, makeRpcClient },
) => {
const bidCmd = interCmd
.command('bid')
.description('Add a Bid command/operation');
const makeBoard = () => getBatchQuery().then(makeBoardClient);
bidCmd
.command('list')
.description('List Bids operation')
.option('--book', 'auction book number', Number, 0)
.action(async (/** @type {{book: number}} */ { book }) => {
const bidsPart = 'schedule'; // XXX something goofy is going on in the contract
const [board, queryService] = await Promise.all([
makeBoard(),
makeRpcClient().then(makeVstorageQueryService),
]);
const agoricNames = await board.provideAgoricNames();
const { children: bidKeys } = await queryService.Children({
path: `published.auction.book${book}.${bidsPart}`,
});
const { length: n } = bidKeys;
const more = n > 3 ? '...' : '';
console.warn('fetching', n, 'bids:', bidKeys.slice(0, 3), more);
console.warn('TODO: pagination');
const bids = await board.readBatch(
bidKeys.map(k => `published.auction.book${book}.${bidsPart}.${k}`),
);
assertBidDataNotification(bids);
const fmt = makeAssetFormatters(agoricNames.vbankAsset);
for (const bid of bids) {
tui.show(fmtBid(bid, fmt));
}
});
return bidCmd;
};