forked from orao-network/solana-vrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
425 lines (393 loc) · 14.7 KB
/
cli.ts
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
import * as anchor from "@coral-xyz/anchor";
import { Program, web3, AnchorProvider } from "@coral-xyz/anchor";
import IDL from "./target/idl/example_client.json";
import { ExampleClient } from "./target/types/example_client";
import { Command, InvalidArgumentError, Option } from "commander";
import { readFile } from "node:fs/promises";
import {
PROGRAM_ID as VrfProgramId,
OraoCb,
RegisterBuilder,
requestAccountAddress,
clientAddress,
Client,
RequestAccount,
} from "@orao-network/solana-vrf-cb";
import { NetworkState } from "@orao-network/solana-vrf-cb";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
const program = new Command();
program.addOption(
new Option("-c, --cluster <name>", "Solana cluster to connect to")
.choices(["localnet", "devnet", "mainnet"])
.default("devnet")
);
program.requiredOption("-k, --key <path>", "Wallet secret key path.");
program
.command("init")
.description("Initializes the deployed ExampleClient program")
.action(async (_options, command) => {
let cluster = command.parent.opts().cluster as Cluster;
let exampleClient = await openProgram(
cluster,
command.parent.opts().key
);
let tx = await exampleClient.methods.initialize().rpc();
console.log("Initialized in", tx);
});
program
.command("register")
.description("Registers ExampleClient program as a VRF client")
.action(async (_options, command) => {
let cluster = command.parent.opts().cluster as Cluster;
let exampleClient = await openProgram(
cluster,
command.parent.opts().key
);
let vrf = new OraoCb(exampleClient.provider);
let [clientStateAddr, clientStateBump] =
web3.PublicKey.findProgramAddressSync(
[Buffer.from("CLIENT_STATE")],
exampleClient.programId
);
let builder = await new RegisterBuilder(
vrf,
exampleClient.programId,
clientStateAddr,
[Buffer.from("CLIENT_STATE"), Buffer.from([clientStateBump])]
).build();
let tx = await builder.rpc();
console.log("Registered in", tx);
});
program
.command("deposit")
.requiredOption(
"--amount <amount>",
"how many lamports to deposit",
myParseInt
)
.action(async (options, command) => {
let cluster = command.parent.opts().cluster as Cluster;
let exampleClient = await openProgram(
cluster,
command.parent.opts().key
);
let [clientStateAddr, clientStateBump] =
web3.PublicKey.findProgramAddressSync(
[Buffer.from("CLIENT_STATE")],
exampleClient.programId
);
const [clientAddr, clientBump] = clientAddress(
exampleClient.programId,
clientStateAddr
);
let transfer = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: exampleClient.provider.publicKey,
toPubkey: clientAddr,
lamports: options.amount,
})
);
let tx = await exampleClient.provider.sendAndConfirm(transfer);
console.log("Deposited in", tx);
});
program
.command("request")
.addOption(
new Option("--amount <num>", "Number of requests to make")
.default(1)
.argParser(myParseInt)
)
.option(
"--callback-override <param>",
"Overrides the callback with or without additional account",
myParseInt
)
.option(
"--with-additional-account",
"send additional account to overridden callback (ignored if --callback-override not given)",
false
)
.description("Sends new randomness request and waits for result")
.action(async (options, command) => {
console.log("Making", options.amount, "request(s)");
let cluster = command.parent.opts().cluster as Cluster;
let exampleClient = await openProgram(
cluster,
command.parent.opts().key
);
let vrf = new OraoCb(exampleClient.provider);
const [additionalAccountAddress, _additionalAccountBump] =
web3.PublicKey.findProgramAddressSync(
[Buffer.from("ADDITIONAL_ACCOUNT")],
exampleClient.programId
);
let [clientStateAddr, _clientStateBump] =
web3.PublicKey.findProgramAddressSync(
[Buffer.from("CLIENT_STATE")],
exampleClient.programId
);
const [clientAddr, _clientBump] = clientAddress(
exampleClient.programId,
clientStateAddr
);
console.log("=== Client Account (before request)===");
let client = await vrf.getClient(clientAddr);
let balance = await vrf.provider.connection.getBalance(clientAddr);
describeClient(clientAddr, client, balance);
console.log("");
console.log("=== Client State Account (before request) ===");
let stateBefore = await exampleClient.account.clientState.fetch(
clientStateAddr
);
describeState(clientStateAddr, stateBefore);
console.log("");
console.log("=== Additional Account (before request) ===");
let additionalAccount =
await exampleClient.account.additionalAccount.fetch(
additionalAccountAddress
);
describeAdditionalAccount(additionalAccountAddress, additionalAccount);
console.log("");
let seeds = [...Buffer.alloc(options.amount)].map(
(_x) => web3.Keypair.generate().publicKey
);
let requestAddrs = seeds.map(
(seed) => requestAccountAddress(clientAddr, seed.toBuffer())[0]
);
let howToOverride = null;
if (options.callbackOverride !== undefined) {
howToOverride = {
parameter: options.callbackOverride,
sendAdditionalAccount: options.withAdditionalAccount,
};
}
let networkState = await vrf.getNetworkState();
let builder = exampleClient.methods
.request([...seeds[0].toBytes()], howToOverride)
.accountsPartial({
vrf: VrfProgramId,
clientState: clientStateAddr,
client: clientAddr,
networkState: NetworkState.createAddress(networkState.bump)[0],
treasury: networkState.config.treasury,
request: requestAddrs[0],
});
let instructions = [];
for (let i = 1; i < options.amount; i++) {
let instruction = await exampleClient.methods
.request([...seeds[i].toBytes()], howToOverride)
.accountsPartial({
vrf: VrfProgramId,
clientState: clientStateAddr,
client: clientAddr,
networkState: NetworkState.createAddress(
networkState.bump
)[0],
treasury: networkState.config.treasury,
request: requestAddrs[i],
})
.instruction();
instructions.push(instruction);
}
builder.postInstructions(instructions);
let tx = await builder.rpc();
console.log("Requested in", tx);
console.log("");
for (let i = 0; i < options.amount; i++) {
for (let j = 0; j < 10; j++) {
try {
let request = await vrf.getRequestAccount(requestAddrs[i]);
console.log(
`=== Request Account for ${seeds[
i
].toString()} (new) ===`
);
describeRequest(requestAddrs[i], request);
console.log("");
break;
} catch (e) {
// pass
}
}
}
let fulfilledAccounts = [];
for (let i = 0; i < options.amount; i++) {
let fulfilled = await vrf.waitFulfilled(
clientAddr,
seeds[i].toBuffer()
);
fulfilledAccounts.push(fulfilled);
let txs = await vrf.provider.connection.getSignaturesForAddress(
requestAddrs[i],
undefined,
"confirmed"
);
let slot = txs.reduce((acc, cur) => Math.max(acc, cur.slot), 0);
let milliseconds = (slot - fulfilled.slot.toNumber()) * 400;
console.log(
`${seeds[i].toString()} fulfilled ~ in ${(
milliseconds / 1000
).toFixed(3)} seconds`
);
}
console.log("");
for (let i = 0; i < options.amount; i++) {
console.log(
`=== Request Account for ${seeds[i].toString()} (fulfilled) ===`
);
describeRequest(
requestAddrs[i],
fulfilledAccounts[i] as RequestAccount
);
console.log("");
}
console.log("=== Client Account (after requests) ===");
client = await vrf.account.client.fetch(clientAddr);
balance = await vrf.provider.connection.getBalance(clientAddr);
describeClient(clientAddr, client, balance);
console.log("");
console.log("=== Client State Account (after requests fulfilled) ===");
let stateAfter = await exampleClient.account.clientState.fetch(
clientStateAddr
);
describeState(clientStateAddr, stateAfter);
console.log("");
console.log("=== Additional Account (after requests fulfilled) ===");
additionalAccount = await exampleClient.account.additionalAccount.fetch(
additionalAccountAddress
);
describeAdditionalAccount(additionalAccountAddress, additionalAccount);
console.log("");
});
program.parse();
type Cluster = "localnet" | "devnet" | "mainnet";
async function getProvider(
cluster: Cluster,
key: string
): Promise<AnchorProvider> {
let secretKey: Array<number> = JSON.parse(
await readFile(key, { encoding: "utf-8" })
);
let payer = web3.Keypair.fromSecretKey(Buffer.from(secretKey));
let url =
cluster === "devnet"
? web3.clusterApiUrl("devnet")
: cluster === "mainnet"
? web3.clusterApiUrl("mainnet-beta")
: "http://127.0.0.1:8899";
let connection = new web3.Connection(url, "confirmed");
let wallet = new anchor.Wallet(payer);
return new AnchorProvider(connection, wallet);
}
async function openProgram(
cluster: Cluster,
key: string
): Promise<Program<ExampleClient>> {
let provider = await getProvider(cluster, key);
return new Program(IDL as ExampleClient, provider);
}
function myParseInt(value, dummyPrevious) {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
throw new InvalidArgumentError("Not a number.");
}
return parsedValue;
}
function describeClient(
clientAddr: web3.PublicKey,
client: Client,
balance: number
) {
console.log("Client:", clientAddr.toString());
console.log(" Balance:", balance);
console.log(" Number of Requests:", client.numRequests.toString());
if (client.callback) {
console.log(" Callback:");
console.log(" Data:", bs58.encode(client.callback.data));
if (client.callback.remainingAccounts.length > 0) {
console.log(" Remaining Accounts:");
for (let account of client.callback.remainingAccounts) {
if (account.isWritable) {
console.log(` - (rw) ${account.pubkey}`);
} else {
console.log(` - (ro) ${account.pubkey}`);
}
}
} else {
console.log(" Remaining Accounts: []");
}
} else {
console.log(" Callback: None");
}
}
function describeState(
stateAddr: anchor.web3.PublicKey,
state: anchor.IdlAccounts<ExampleClient>["clientState"]
) {
console.log("Client State:", stateAddr.toString());
console.log(" Latest client-level callback call:");
console.log(" Parameter:", state.clientLevelCallbackParam);
console.log(
" Randomness:",
bs58.encode(state.clientLevelCallbackRandomness)
);
console.log(" Latest request-level callback call:");
console.log(" Parameter:", state.requestLevelCallbackParam);
console.log(
" Randomness:",
bs58.encode(state.requestLevelCallbackRandomness)
);
}
function describeAdditionalAccount(
address: anchor.web3.PublicKey,
account: anchor.IdlAccounts<ExampleClient>["additionalAccount"]
) {
console.log("Additional Account:", address.toString());
console.log(" Latest data:");
console.log(" Parameter:", account.param);
console.log(" Randomness:", bs58.encode(account.randomness));
}
function describeRequest(requestAddr: web3.PublicKey, request: RequestAccount) {
console.log("Request Account:", requestAddr.toString());
console.log(" Client:", request.client.toString());
console.log(" Seed:", bs58.encode(request.seed));
let pending = request.getPending();
let fulfilled = request.getFulfilled();
if (pending) {
console.log(" Pending:");
if (pending.responses.length > 0) {
console.log(" Responses:");
for (let response of pending.responses) {
console.log(" - From:", response.pubkey);
console.log(
" Contribution:",
bs58.encode(response.randomness)
);
}
} else {
console.log(" Responses: []");
}
if (pending.callbackOverride) {
console.log(" Callback: OVERRIDDEN");
console.log(" Data:", bs58.encode(pending.callback.data));
if (pending.callback.remainingAccounts.length > 0) {
console.log(" Remaining Accounts:");
for (let account of pending.callback.remainingAccounts) {
if (account.isWritable) {
console.log(` - (rw) ${account.pubkey}`);
} else {
console.log(` - (ro) ${account.pubkey}`);
}
}
} else {
console.log(" Remaining Accounts: []");
}
} else {
console.log(" Callback: NOT OVERRIDDEN");
}
} else if (fulfilled) {
console.log(" Fulfilled:");
console.log(" Randomness:", bs58.encode(fulfilled.randomness));
}
}