-
Notifications
You must be signed in to change notification settings - Fork 205
/
wallet.js
208 lines (189 loc) · 5.69 KB
/
wallet.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
// @ts-check
/* eslint-disable func-names */
/* global fetch, process */
import {
iterateLatest,
makeCastingSpec,
makeFollower,
makeLeader,
makeLeaderFromRpcAddresses,
} from '@agoric/casting';
import { coalesceWalletState } from '@agoric/smart-wallet/src/utils.js';
import { Command } from 'commander';
import util from 'util';
import { fmtRecordOfLines, summarize } from '../lib/format.js';
import {
boardSlottingMarshaller,
makeRpcUtils,
networkConfig,
} from '../lib/rpc.js';
import { makeLeaderOptions } from '../lib/casting.js';
import {
execSwingsetTransaction,
fetchSwingsetParams,
normalizeAddress,
} from '../lib/chain.js';
import { getCurrent } from '../lib/wallet.js';
const SLEEP_SECONDS = 3;
export const makeWalletCommand = async () => {
const wallet = new Command('wallet').description('wallet commands');
wallet
.command('provision')
.description('provision a Smart Wallet')
.requiredOption(
'--account [address]',
'address literal or name',
normalizeAddress,
)
.option('--spend', 'confirm you want to spend')
.option('--home [dir]', 'agd application home directory')
.option(
'--keyring-backend [os|file|test]',
'keyring\'s backend (os|file|test) (default "os")',
)
.option('--nickname [string]', 'nickname to use', 'my-wallet')
.action(function () {
const {
account,
nickname,
spend,
home,
keyringBackend: backend,
// @ts-expect-error this implicit any
} = this.opts();
const tx = `provision-one ${nickname} ${account} SMART_WALLET`;
if (spend) {
execSwingsetTransaction(tx, networkConfig, account, false, {
home,
backend,
});
} else {
const params = fetchSwingsetParams(networkConfig);
assert(
params.power_flag_fees.length === 1,
'multiple power_flag_fees not supported',
);
const { fee: fees } = params.power_flag_fees[0];
const nf = new Intl.NumberFormat('en-US');
const costs = fees
.map(f => `${nf.format(Number(f.amount))} ${f.denom}`)
.join(' + ');
process.stdout.write(`Provisioning a wallet costs ${costs}\n`);
process.stdout.write(`To really provision, rerun with --spend or...\n`);
execSwingsetTransaction(tx, networkConfig, account, true, {
home,
backend,
});
}
});
wallet
.command('send')
.description('send a prepared offer')
.requiredOption(
'--from [address]',
'address literal or name',
normalizeAddress,
)
.requiredOption('--offer [filename]', 'path to file with prepared offer')
.option('--home [dir]', 'agd application home directory')
.option(
'--keyring-backend [os|file|test]',
'keyring\'s backend (os|file|test) (default "os")',
)
.option('--dry-run', 'spit out the command instead of running it')
.action(function () {
const {
dryRun,
from,
offer,
home,
keyringBackend: backend,
// @ts-expect-error this implicit any
} = this.opts();
execSwingsetTransaction(
`wallet-action --allow-spend "$(cat ${offer})"`,
networkConfig,
from,
dryRun,
{ home, backend },
);
});
wallet
.command('list')
.description('list all wallets in vstorage')
.action(async function () {
const { vstorage } = await makeRpcUtils({ fetch });
const wallets = await vstorage.keys('published.wallet');
process.stdout.write(wallets.join('\n'));
});
wallet
.command('show')
.description('show current state')
.requiredOption(
'--from <address>',
'wallet address literal or name',
normalizeAddress,
)
.action(async function () {
// @ts-expect-error this implicit any
const opts = this.opts();
const { agoricNames, fromBoard, vstorage } = await makeRpcUtils({
fetch,
});
const unserializer = boardSlottingMarshaller(fromBoard.convertSlotToVal);
const leader = makeLeader(networkConfig.rpcAddrs[0]);
const follower = await makeFollower(
`:published.wallet.${opts.from}`,
leader,
{
// @ts-expect-error xxx
unserializer,
},
);
const coalesced = await coalesceWalletState(follower);
const current = await getCurrent(opts.from, fromBoard, {
vstorage,
});
console.warn(
'got coalesced',
util.inspect(coalesced, { depth: 10, colors: true }),
);
try {
const summary = summarize(current, coalesced, agoricNames);
process.stdout.write(fmtRecordOfLines(summary));
} catch (e) {
console.error('CAUGHT HERE', e);
}
});
wallet
.command('watch')
.description('watch for wallet changes')
.requiredOption(
'--from <address>',
'address literal or name',
normalizeAddress,
)
.action(async function () {
// @ts-expect-error this implicit any
const { from } = this.opts();
const spec = `:published.wallet.${from}`;
const leaderOptions = makeLeaderOptions({
sleep: SLEEP_SECONDS,
jitter: 0,
log: () => undefined,
});
const leader = makeLeaderFromRpcAddresses(
networkConfig.rpcAddrs,
leaderOptions,
);
console.warn('Following', spec);
const castingSpec = makeCastingSpec(spec);
const follower = makeFollower(castingSpec, leader);
for await (const { value } of iterateLatest(follower)) {
// xxx relying on console for app output
// worth it to get the nice formatting
console.log(value);
}
});
return wallet;
};