-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
799 lines (685 loc) · 29.6 KB
/
index.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
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
import { DeployStrParams, PaymentStrParams, getTimestamp, SDK, SessionStrParams, publicKeyFromSecretKey, Bytes, Deploy, EventParseResult, Subscription, Transaction, TransactionStrParams, TransactionBuilderParams, AddressableEntityHash } from 'casper-sdk';
const fs = require('fs').promises;
const http = require('http');
const rpc_address = 'http://localhost:11101';
const node_address = 'localhost:28101';
const sdk = new SDK(rpc_address, node_address);
// const server = http.createServer(async (req, res) => {
// res.writeHead(200, { 'Content-Type': 'text/plain' });
// let peers_object = await sdk.get_peers();
// console.log(peers_object.peers);
// const peers_as_json = peers_object.toJson();
// console.log(peers_as_json);
// res.end(JSON.stringify(peers_as_json));
// });
//const PORT = process.env.PORT || 3000;
// example0
// const example0 = server.listen(PORT, () => {
// console.log(`Server is running on port ${PORT}`);
// });
// get_transaction
const example1 = async () => {
const transaction_hash_as_string =
'27d81df41801602f47cdb4618a814407daf38d0c39be32c7f6c109d7e39a3f4b';
const finalized_approvals = true;
const get_transaction_options = sdk.get_transaction_options({
transaction_hash_as_string,
finalized_approvals,
});
const transaction_result = await sdk.get_transaction(get_transaction_options);
const transaction = transaction_result.transaction;
const timestamp = transaction.timestamp;
const hash = transaction.hash.toString();
console.log(timestamp, hash);
};
// get_auction_info
const example2 = async () => {
const get_auction_info = await sdk.get_auction_info();
const auction_state = get_auction_info.auction_state;
const state_root_hash = auction_state.state_root_hash.toString();
const block_height = auction_state.block_height.toString();
console.log(state_root_hash, block_height);
};
// get_peers
const example3 = async () => {
const get_peers = await sdk.get_peers();
const peers = get_peers.peers;
peers.forEach((peer) => {
console.log(peer);
});
};
// get_peers binary
const example3_binary = async () => {
const peers = await sdk.get_binary_peers();
peers.forEach((peer) => {
console.log(peer);
});
};
// get_block
const example4 = async () => {
const get_block = await sdk.get_block();
let block = get_block.block;
let block_hash = block.hash;
console.log(block_hash);
};
// make_transfer_transaction
const example5 = async () => {
const chain_name = 'casper-net-1';
const public_key =
'01aff5c18a954604dd27d139d8e0cfc533ac3d53784d76c7a7ac5ff4039510fdf6';
const secret_key = undefined;
const timestamp = getTimestamp(); // or Date.now().toString(); // or undefined
const ttl = '1h'; // or undefined
const payment_amount = '100000000';
const transfer_amount = '2500000000';
const target_account =
'01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const transaction_params = new TransactionStrParams(
chain_name,
public_key,
secret_key,
timestamp,
ttl
);
transaction_params.payment_amount = payment_amount;
const make_transfer_transaction = sdk.make_transfer_transaction(
undefined, // Optional maybe_source
target_account,
transfer_amount,
transaction_params,
);
const make_transfer_transaction_as_json = make_transfer_transaction.toJson();
console.log(make_transfer_transaction_as_json);
};
// transfer_transaction
const example6 = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const timestamp = getTimestamp(); // or Date.now().toString(); // or undefined
const ttl = '1h'; // or undefined
const payment_amount = '100000000';
const transfer_amount = '2500000000';
const target_account =
'01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const transaction_params = new TransactionStrParams(
chain_name,
public_key,
secret_key,
timestamp,
ttl
);
transaction_params.payment_amount = payment_amount;
const transfer_transaction_result = await sdk.transfer_transaction(
undefined, // Optional maybe_source
target_account,
transfer_amount,
transaction_params,
);
const transfer_transaction_result_as_json = transfer_transaction_result.toJson();
console.log(transfer_transaction_result_as_json);
const transaction_hash = transfer_transaction_result.transaction_hash.toString();
console.log(transaction_hash);
};
// make_transaction
const example7 = async () => {
const chain_name = 'integration-test';
const public_key =
'01aff5c18a954604dd27d139d8e0cfc533ac3d53784d76c7a7ac5ff4039510fdf6';
const payment_amount = '5000000000';
const entity_hash_hex_string =
'5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const entry_point = 'set_variables';
const transaction_params = new TransactionStrParams(chain_name, public_key);
transaction_params.payment_amount = payment_amount;
let entity_hash = new AddressableEntityHash(entity_hash_hex_string);
let builder_params = TransactionBuilderParams.newInvocableEntity(entity_hash, entry_point);
const transaction = sdk.make_transaction(builder_params, transaction_params);
const transaction_as_json = transaction.toJson();
console.log(transaction_as_json);
};
// transaction
const example8 = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const payment_amount = '5000000000';
const entity_hash_hex_string =
'5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const entry_point = 'set_variables';
const transaction_params = new TransactionStrParams(chain_name, public_key, secret_key);
transaction_params.payment_amount = payment_amount;
let entity_hash = new AddressableEntityHash(entity_hash_hex_string);
let builder_params = TransactionBuilderParams.newInvocableEntity(entity_hash, entry_point);
const transaction_result = await sdk.transaction(builder_params, transaction_params);
const transaction_result_as_json = transaction_result.toJson();
console.log(transaction_result_as_json);
};
// put_transaction
const example9 = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const payment_amount = '5000000000';
const entity_hash_formatted_string =
'addressable-entity-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const entry_point = 'set_variables';
const transaction_params = new TransactionStrParams(chain_name, public_key, secret_key);
transaction_params.payment_amount = payment_amount;
let entity_hash = AddressableEntityHash.fromFormattedStr(entity_hash_formatted_string);
let builder_params = TransactionBuilderParams.newInvocableEntity(entity_hash, entry_point);
const transaction = Transaction.newSession(
builder_params,
transaction_params
);
const put_transaction_result = await sdk.put_transaction(transaction);
const put_transaction_result_as_json = put_transaction_result.toJson();
console.log(put_transaction_result_as_json);
};
// put_transaction transfer_transaction
const example10 = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const payment_amount = '100000000';
const transfer_amount = '2500000000';
const target_account =
'01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const transfer_params = new TransactionStrParams(chain_name, public_key, secret_key);
transfer_params.payment_amount = payment_amount;
const transfer_transaction = Transaction.newTransfer(
undefined, // optional maybe_source
target_account,
transfer_amount,
transfer_params,
undefined, // optional transfer_id
);
const put_transaction_result = await sdk.put_transaction(transfer_transaction);
const put_transaction_result_as_json = put_transaction_result.toJson();
console.log(put_transaction_result_as_json);
};
// install
const example11 = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const events_address = 'http://127.0.0.1:18101/events';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const initiator_addr = publicKeyFromSecretKey(secret_key);
const transaction_params = new TransactionStrParams(chain_name, initiator_addr, secret_key);
transaction_params.session_args_json = JSON.stringify([
{ "name": "collection_name", "type": "String", "value": "enhanced-nft-1" },
{ "name": "collection_symbol", "type": "String", "value": "ENFT-1" },
{ "name": "total_token_supply", "type": "U64", "value": 100 },
{ "name": "ownership_mode", "type": "U8", "value": 0 },
{ "name": "nft_kind", "type": "U8", "value": 1 },
{ "name": "allow_minting", "type": "Bool", "value": true },
{ "name": "owner_reverse_lookup_mode", "type": "U8", "value": 0 },
{ "name": "nft_metadata_kind", "type": "U8", "value": 2 },
{ "name": "identifier_mode", "type": "U8", "value": 0 },
{ "name": "metadata_mutability", "type": "U8", "value": 0 },
{ "name": "events_mode", "type": "U8", "value": 1 }
]);
transaction_params.payment_amount = '500000000000';
async function loadFile() {
try {
const fileBuffer = await fs.readFile(__dirname + '/../../../tests/wasm/cep78.wasm');
return fileBuffer.buffer; // Returns an ArrayBuffer
} catch (error) {
throw new Error('Error reading file: ' + error.message);
}
}
const buffer = await loadFile();
const wasm = buffer && new Uint8Array(buffer);
const wasmBuffer = wasm?.buffer;
if (!wasmBuffer) {
console.error('Failed to read wasm file.');
return;
}
const install_result = await sdk.install(
transaction_params,
Bytes.fromUint8Array(wasm)
);
const install_result_as_json = install_result.toJson();
console.log(install_result_as_json.transaction_hash);
const eventParseResult: EventParseResult = await sdk.waitTransaction(events_address, install_result_as_json.transaction_hash);
const cost = eventParseResult.body?.transaction_processed?.execution_result.Success?.cost;
// console.log(eventParseResult.body.transaction_processed);
console.log(`install cost ${cost}`);
};
// call_entrypoint
const example12 = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const events_address = 'http://127.0.0.1:18101/events';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const initiator_addr = publicKeyFromSecretKey(secret_key);
const entity_hash_formatted_string =
'addressable-entity-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const entry_point = 'mint';
const token_owner =
'account-hash-878985c8c07064e09e67cc349dd21219b8e41942a0adc4bfa378cf0eace32611';
const payment_amount = '5000000000';
const transaction_params = new TransactionStrParams(chain_name, initiator_addr, secret_key);
transaction_params.session_args_simple = ["token_meta_data:String='test_meta_data'", `token_owner:Key='${token_owner}'`];
transaction_params.payment_amount = payment_amount;
let entity_hash = AddressableEntityHash.fromFormattedStr(entity_hash_formatted_string);
let builder_params = TransactionBuilderParams.newInvocableEntity(entity_hash, entry_point);
const call_entrypoint_result = await sdk.call_entrypoint(
builder_params,
transaction_params
);
const call_entrypoint_result_as_json = call_entrypoint_result.toJson();
// watch transaction_hash to trigger callback
const transaction_hash_results = [call_entrypoint_result_as_json.transaction_hash];
const watcher = sdk.watchTransaction(events_address);
const Subscriptions: Subscription[] = [];
const getEventHandlerFn = (transactionHash: string) => {
const eventHandlerFn = (eventParseResult: EventParseResult) => {
console.log(`callback for ${transactionHash}`);
if (eventParseResult.err) {
return false;
}
else if (eventParseResult.body?.transaction_processed?.execution_result.Success) {
console.log(eventParseResult.body?.transaction_processed?.execution_result.Success);
return true;
}
else {
console.error(eventParseResult.body?.transaction_processed?.execution_result.Failure);
return false;
};
};
return eventHandlerFn;
};
transaction_hash_results.map(async (transaction_hash) => {
const eventHandlerFn = getEventHandlerFn(transaction_hash);
console.log(transaction_hash);
const subscription: Subscription = new Subscription(transaction_hash, eventHandlerFn);
Subscriptions.push(subscription);
});
watcher.subscribe(Subscriptions);
const results = await watcher.start();
watcher.stop();
console.log(results);
};
// sign transaction
const example13 = async () => {
const chain_name = 'integration-test';
const payment_amount = '5000000000';
const entity_hash_formatted_string =
'addressable-entity-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const initiator_addr = publicKeyFromSecretKey(secret_key);
const transaction_params = new TransactionStrParams(chain_name, initiator_addr);
transaction_params.payment_amount = payment_amount;
let entity_hash = AddressableEntityHash.fromFormattedStr(entity_hash_formatted_string);
let builder_params = TransactionBuilderParams.newInvocableEntity(entity_hash, 'set_variables');
const transaction = sdk.make_transaction(builder_params, transaction_params);
const signed_transaction = transaction.sign(secret_key);
console.log(signed_transaction.approvals());
};
// add signature to transaction
const example14 = async () => {
const chain_name = 'integration-test';
const payment_amount = '5000000000';
const entity_hash_hex_string =
'5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
let public_key_kms = '01aff5c18a954604dd27d139d8e0cfc533ac3d53784d76c7a7ac5ff4039510fdf6';
const transaction_params = new TransactionStrParams(chain_name, public_key_kms);
transaction_params.payment_amount = payment_amount;
let entity_hash = new AddressableEntityHash(entity_hash_hex_string);
let builder_params = TransactionBuilderParams.newInvocableEntity(entity_hash, 'set_variables');
const transaction = sdk.make_transaction(builder_params, transaction_params);
const signature_kms = '012dbd52d47f982e870476ab6c123f3f29848199b08f5997f757f63986ef656480e27f8e12698c39f14281d2a62c1e8896cc9f272ae3312a68228c5863f849980b';
let signed_transaction = transaction.addSignature(public_key_kms, signature_kms);
const public_key_kms_2 = '01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const signature_kms_2 = '012dbd52d47f982e870476ab6c123f3f29848199b08f5997f757f63986ef656480e27f8e12698c39f14281d2a62c1e8896cc9f272ae3312a68228c5863f849980c';
signed_transaction = signed_transaction.addSignature(public_key_kms_2, signature_kms_2);
console.log(signed_transaction.approvals);
};
// get_deploy
const example1_legacy = async () => {
const deploy_hash_as_string =
'4ea826ecae4a3b02dc8627c36c7115539c8aac9b73551fffab3f98fc47fd0499';
const finalized_approvals = true;
const get_deploy_options = sdk.get_deploy_options({
deploy_hash_as_string,
finalized_approvals,
});
const deploy_result = await sdk.get_deploy(get_deploy_options);
const deploy: Deploy = deploy_result.deploy;
const timestamp = deploy.timestamp();
const header = deploy.toJson().header; // DeployHeader type not being exposed right now by the SDK you can convert every type to JSON
console.log(timestamp, header);
};
// make_transfer
const example5_legacy = async () => {
const chain_name = 'casper-net-1';
const public_key =
'01aff5c18a954604dd27d139d8e0cfc533ac3d53784d76c7a7ac5ff4039510fdf6';
const secret_key = undefined;
const timestamp = getTimestamp(); // or Date.now().toString(); // or undefined
const ttl = '1h'; // or undefined
const payment_amount = '100000000';
const transfer_amount = '2500000000';
const target_account =
'01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const deploy_params = new DeployStrParams(
chain_name,
public_key,
secret_key,
timestamp,
ttl
);
const payment_params = new PaymentStrParams(payment_amount);
const transfer_deploy = sdk.make_transfer(
transfer_amount,
target_account,
undefined, // transfer_id
deploy_params,
payment_params
);
const transfer_deploy_as_json = transfer_deploy.toJson();
console.log(transfer_deploy_as_json);
};
// transfer
const example6_legacy = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const timestamp = getTimestamp(); // or Date.now().toString(); // or undefined
const ttl = '1h'; // or undefined
const payment_amount = '100000000';
const transfer_amount = '2500000000';
const target_account =
'01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const deploy_params = new DeployStrParams(
chain_name,
public_key,
secret_key,
timestamp,
ttl
);
const payment_params = new PaymentStrParams(payment_amount);
const transfer_result = await sdk.transfer(
transfer_amount,
target_account,
undefined, // transfer_id
deploy_params,
payment_params
);
const transfer_result_as_json = transfer_result.toJson();
console.log(transfer_result_as_json);
console.log(transfer_result.deploy_hash.toString());
};
// make_deploy
const example7_legacy = async () => {
const chain_name = 'integration-test';
const public_key =
'01aff5c18a954604dd27d139d8e0cfc533ac3d53784d76c7a7ac5ff4039510fdf6';
const payment_amount = '5000000000';
const contract_hash =
'hash-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const deploy_params = new DeployStrParams(chain_name, public_key);
const session_params = new SessionStrParams();
session_params.session_hash = contract_hash;
session_params.session_entry_point = 'set_variables';
const payment_params = new PaymentStrParams(payment_amount);
const deploy = sdk.make_deploy(deploy_params, session_params, payment_params);
const deploy_as_json = deploy.toJson();
console.log(deploy_as_json);
};
// deploy
const example8_legacy = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const payment_amount = '5000000000';
const contract_hash =
'hash-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const deploy_params = new DeployStrParams(chain_name, public_key, secret_key);
const session_params = new SessionStrParams();
session_params.session_hash = contract_hash;
session_params.session_entry_point = 'set_variables';
const payment_params = new PaymentStrParams(payment_amount);
const deploy_result = await sdk.deploy(deploy_params, session_params, payment_params);
const deploy_result_as_json = deploy_result.toJson();
console.log(deploy_result_as_json);
};
// put_deploy
const example9_legacy = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const payment_amount = '5000000000';
const contract_hash =
'hash-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const entry_point = 'set_variables';
const deploy_params = new DeployStrParams(chain_name, public_key, secret_key);
const session_params = new SessionStrParams();
session_params.session_hash = contract_hash;
session_params.session_entry_point = entry_point;
const payment_params = new PaymentStrParams(payment_amount);
const deploy = Deploy.withPaymentAndSession(
deploy_params,
session_params,
payment_params
);
const put_deploy_result = await sdk.put_deploy(deploy);
const put_deploy_result_as_json = put_deploy_result.toJson();
console.log(put_deploy_result_as_json);
};
// put_deploy transfer
const example10_legacy = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const payment_amount = '100000000';
const transfer_amount = '2500000000';
const target_account =
'01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const deploy_params = new DeployStrParams(chain_name, public_key, secret_key);
const payment_params = new PaymentStrParams(payment_amount);
const transfer_deploy = Deploy.withTransfer(
transfer_amount,
target_account,
undefined, // transfer_id
deploy_params,
payment_params
);
const put_deploy_result = await sdk.put_deploy(transfer_deploy);
const put_deploy_result_as_json = put_deploy_result.toJson();
console.log(put_deploy_result_as_json);
};
// install_deploy
const example11_legacy = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const events_address = 'http://127.0.0.1:18101/events';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const deploy_params = new DeployStrParams(chain_name, public_key, secret_key);
async function loadFile() {
try {
const fileBuffer = await fs.readFile(__dirname + '/../../../tests/wasm/cep78.wasm');
return fileBuffer.buffer; // Returns an ArrayBuffer
} catch (error) {
throw new Error('Error reading file: ' + error.message);
}
}
const session_params = new SessionStrParams();
session_params.session_args_json = JSON.stringify([
{ "name": "collection_name", "type": "String", "value": "enhanced-nft-1" },
{ "name": "collection_symbol", "type": "String", "value": "ENFT-1" },
{ "name": "total_token_supply", "type": "U64", "value": 100 },
{ "name": "ownership_mode", "type": "U8", "value": 0 },
{ "name": "nft_kind", "type": "U8", "value": 1 },
{ "name": "allow_minting", "type": "Bool", "value": true },
{ "name": "owner_reverse_lookup_mode", "type": "U8", "value": 0 },
{ "name": "nft_metadata_kind", "type": "U8", "value": 2 },
{ "name": "identifier_mode", "type": "U8", "value": 0 },
{ "name": "metadata_mutability", "type": "U8", "value": 0 },
{ "name": "events_mode", "type": "U8", "value": 1 }
]);
const payment_amount = '500000000000';
const buffer = await loadFile();
const wasm = buffer && new Uint8Array(buffer);
const wasmBuffer = wasm?.buffer;
if (!wasmBuffer) {
console.error('Failed to read wasm file.');
return;
}
session_params.session_bytes = Bytes.fromUint8Array(wasm);
const install_result = await sdk.install_deploy(
deploy_params,
session_params,
payment_amount
);
const install_result_as_json = install_result.toJson();
console.log(install_result_as_json.deploy_hash);
const eventParseResult: EventParseResult = await sdk.waitDeploy(events_address, install_result_as_json.deploy_hash);
const cost = eventParseResult.body?.transaction_processed?.execution_result.Success?.cost;
// console.log(eventParseResult.body.transaction_processed);
console.log(`install cost ${cost}`);
};
// call_entrypoint_deploy
const example12_legacy = async () => {
const rpc_address = 'http://127.0.0.1:11101';
const events_address = 'http://127.0.0.1:18101/events';
const sdk = new SDK(rpc_address);
const chain_name = 'casper-net-1';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const contract_hash =
'hash-7705c58f20c445c605ba1bf5adab66686a8f891879d6012e07fe24c8bf3af3f2';
const entry_point = 'mint';
const token_owner =
'account-hash-878985c8c07064e09e67cc349dd21219b8e41942a0adc4bfa378cf0eace32611';
const payment_amount = '5000000000';
const deploy_params = new DeployStrParams(chain_name, public_key, secret_key);
const session_params = new SessionStrParams();
session_params.session_hash = contract_hash;
session_params.session_entry_point = entry_point;
session_params.session_args_simple = ["token_meta_data:String='test_meta_data'", `token_owner:Key='${token_owner}'`];
const call_entrypoint_deploy_result = await sdk.call_entrypoint_deploy(
deploy_params,
session_params,
payment_amount
);
const call_entrypoint_deploy_result_as_json = call_entrypoint_deploy_result.toJson();
// watch deploy_hash to trigger callback
const deploy_hash_results = [call_entrypoint_deploy_result_as_json.deploy_hash];
const watcher = sdk.watchDeploy(events_address);
const subscriptions: Subscription[] = [];
const getEventHandlerFn = (deployHash: string) => {
const eventHandlerFn = (eventParseResult: EventParseResult) => {
console.log(`callback for ${deployHash}`);
if (eventParseResult.err) {
return false;
}
else if (eventParseResult.body?.transaction_processed?.execution_result.Success) {
console.log(eventParseResult.body?.transaction_processed?.execution_result.Success);
return true;
}
else {
console.error(eventParseResult.body?.transaction_processed?.execution_result.Failure);
return false;
};
};
return eventHandlerFn;
};
deploy_hash_results.map(async (deploy_hash) => {
const eventHandlerFn = getEventHandlerFn(deploy_hash);
console.log(deploy_hash);
const subscription: Subscription = new Subscription(deploy_hash, eventHandlerFn);
subscriptions.push(subscription);
});
watcher.subscribe(subscriptions);
const results = await watcher.start();
watcher.stop();
console.log(results);
};
// sign deploy
const example13_legacy = async () => {
const chain_name = 'integration-test';
const payment_amount = '5000000000';
const contract_hash =
'hash-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
const secret_key = `-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEII8ULlk1CJ12ZQ+bScjBt/IxMAZNggClWqK56D1/7CbI
-----END PRIVATE KEY-----`;
const public_key = publicKeyFromSecretKey(secret_key);
const deploy_params = new DeployStrParams(chain_name, public_key);
const session_params = new SessionStrParams();
session_params.session_hash = contract_hash;
session_params.session_entry_point = 'set_variables';
const payment_params = new PaymentStrParams(payment_amount);
const deploy = sdk.make_deploy(deploy_params, session_params, payment_params);
const deploy_signed = deploy.sign(secret_key);
console.log(deploy_signed.toJson());
};
// add signature to deploy
const example14_legacy = async () => {
const chain_name = 'integration-test';
const payment_amount = '5000000000';
const contract_hash =
'hash-5be5b0ef09a7016e11292848d77f539e55791cb07a7012fbc336b1f92a4fe743';
let public_key_kms = '01aff5c18a954604dd27d139d8e0cfc533ac3d53784d76c7a7ac5ff4039510fdf6';
const deploy_params = new DeployStrParams(chain_name, public_key_kms);
const session_params = new SessionStrParams();
session_params.session_hash = contract_hash;
session_params.session_entry_point = 'set_variables';
const payment_params = new PaymentStrParams(payment_amount);
const deploy = sdk.make_deploy(deploy_params, session_params, payment_params);
const signature_kms = '012dbd52d47f982e870476ab6c123f3f29848199b08f5997f757f63986ef656480e27f8e12698c39f14281d2a62c1e8896cc9f272ae3312a68228c5863f849980b';
let deploy_signed = deploy.addSignature(public_key_kms, signature_kms);
const public_key_kms_2 = '01868e06026ba9c8695f6f3bb10d44782004dbc144ff65017cf484436f9cf7b0f6';
const signature_kms_2 = '012dbd52d47f982e870476ab6c123f3f29848199b08f5997f757f63986ef656480e27f8e12698c39f14281d2a62c1e8896cc9f272ae3312a68228c5863f849980c';
deploy_signed = deploy_signed.addSignature(public_key_kms_2, signature_kms_2);
console.log(deploy_signed.toJson());
};
example3_binary();