-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmod.rs
1354 lines (1173 loc) · 47.3 KB
/
mod.rs
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// WASM bindings for Dojo client functionality
///
/// # Description
/// Provides interfaces for Starknet operations, cryptographic functions,
/// and Torii client interactions
mod utils;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use cainome::cairo_serde::{self, ByteArray, CairoSerde};
use crypto_bigint::U256;
use dojo_world::contracts::naming::compute_selector_from_tag;
use futures::{FutureExt, StreamExt};
use js_sys::Array;
use serde::{Deserialize, Serialize};
use serde_json::json;
use starknet::accounts::{
Account as _, ConnectedAccount as _, ExecutionEncoding, SingleOwnerAccount,
};
use starknet::core::types::{Felt, FunctionCall};
use starknet::core::utils::get_contract_address;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider as _};
use starknet::signers::{LocalWallet, SigningKey, VerifyingKey};
use starknet_crypto::poseidon_hash_many;
use stream_cancel::{StreamExt as _, Tripwire};
use torii_relay::types::Message;
use torii_typed_data::TypedData;
use tsify_next::Tsify;
use wasm_bindgen::prelude::*;
use crate::constants;
use crate::types::{Account, Provider, Subscription, ToriiClient};
use crate::utils::watch_tx;
mod types;
use types::{
BlockId, Call, Calls, ClientConfig, Controller, Controllers, Entities, Entity, IndexerUpdate,
KeysClause, KeysClauses, Model, Query, Signature, Token, TokenBalance, TokenBalances, Tokens,
};
const JSON_COMPAT_SERIALIZER: serde_wasm_bindgen::Serializer =
serde_wasm_bindgen::Serializer::json_compatible();
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
#[wasm_bindgen(js_namespace = console)]
fn error(s: &str);
}
/// Encodes typed data according to Starknet's typed data specification
///
/// # Parameters
/// * `typed_data` - JSON string containing the typed data
/// * `address` - Address as hex string
///
/// # Returns
/// Result containing encoded data as hex string or error
#[wasm_bindgen(js_name = typedDataEncode)]
pub fn typed_data_encode(typed_data: &str, address: &str) -> Result<String, JsValue> {
let typed_data = serde_json::from_str::<TypedData>(&typed_data)
.map_err(|err| JsValue::from(format!("failed to parse typed data: {err}")))?;
let address = Felt::from_str(&address)
.map_err(|err| JsValue::from(format!("failed to parse address: {err}")))?;
typed_data
.encode(address)
.map(|felt| format!("{:#x}", felt))
.map_err(|err| JsValue::from(err.to_string()))
}
/// Generates a new random signing key
///
/// # Returns
/// Private key as hex string
#[wasm_bindgen(js_name = signingKeyNew)]
pub fn signing_key_new() -> String {
let private_key: SigningKey = SigningKey::from_random();
format!("{:#x}", private_key.secret_scalar())
}
/// Signs a message hash with a private key
///
/// # Parameters
/// * `private_key` - Private key as hex string
/// * `hash` - Message hash as hex string
///
/// # Returns
/// Result containing signature or error
#[wasm_bindgen(js_name = signingKeySign)]
pub fn signing_key_sign(private_key: &str, hash: &str) -> Result<Signature, JsValue> {
let private_key = Felt::from_str(private_key);
if let Err(e) = private_key {
return Err(JsValue::from(format!("failed to parse private key: {e}")));
}
let hash = Felt::from_str(hash);
if let Err(e) = hash {
return Err(JsValue::from(format!("failed to parse hash: {e}")));
}
let private_key = SigningKey::from_secret_scalar(private_key.unwrap());
let sig = private_key.sign(&hash.unwrap());
match sig {
Ok(sig) => Result::Ok(Signature::from(&sig)),
Err(e) => Err(JsValue::from(format!("failed to sign: {e}"))),
}
}
/// Derives a verifying (public) key from a signing (private) key
///
/// # Parameters
/// * `signing_key` - Signing key as hex string
///
/// # Returns
/// Result containing verifying key as hex string or error
#[wasm_bindgen(js_name = verifyingKeyNew)]
pub fn verifying_key_new(signing_key: &str) -> Result<String, JsValue> {
let signing_key = Felt::from_str(signing_key);
if let Err(e) = signing_key {
return Err(JsValue::from(format!("failed to parse signing key: {e}")));
}
let verifying_key = starknet_crypto::get_public_key(&signing_key.unwrap());
Ok(format!("{:#x}", verifying_key))
}
/// Verifies a signature against a message hash using a verifying key
///
/// # Parameters
/// * `verifying_key` - Verifying key as hex string
/// * `hash` - Message hash as hex string
/// * `signature` - Signature to verify
///
/// # Returns
/// Result containing verification success boolean or error
#[wasm_bindgen(js_name = verifyingKeyVerify)]
pub fn verifying_key_verify(
verifying_key: &str,
hash: &str,
signature: Signature,
) -> Result<bool, JsValue> {
let verifying_key = Felt::from_str(verifying_key);
if let Err(e) = verifying_key {
return Err(JsValue::from(format!("failed to parse verifying key: {e}")));
}
let verifying_key = VerifyingKey::from_scalar(verifying_key.unwrap());
let hash = Felt::from_str(hash);
if let Err(e) = hash {
return Err(JsValue::from(format!("failed to parse hash: {e}")));
}
let hash = &hash.unwrap();
let signature = &starknet::core::crypto::Signature::from(&signature);
match verifying_key.verify(hash, signature) {
Ok(result) => Result::Ok(result),
Err(e) => Err(JsValue::from(format!("failed to verify: {e}"))),
}
}
/// Creates a new Starknet provider instance for a given RPC URL
///
/// # Parameters
/// * `rpc_url` - URL of the RPC endpoint
///
/// # Returns
/// Result containing Provider instance or error
#[wasm_bindgen(js_name = createProvider)]
pub unsafe fn create_provider(rpc_url: &str) -> Result<Provider, JsValue> {
let rpc_url = url::Url::parse(rpc_url);
if let Err(e) = rpc_url {
return Err(JsValue::from(format!("failed to parse rpc url: {e}")));
}
let rpc_url = rpc_url.unwrap();
let rpc = JsonRpcClient::new(HttpTransport::new(rpc_url));
Result::Ok(Provider(Arc::new(rpc)))
}
#[wasm_bindgen]
impl Provider {
/// Creates a new account instance with the given private key and address
///
/// # Parameters
/// * `private_key` - Private key as hex string
/// * `address` - Account address as hex string
///
/// # Returns
/// Result containing Account instance or error
#[wasm_bindgen(js_name = createAccount)]
pub async unsafe fn create_account(
&self,
private_key: &str,
address: &str,
) -> Result<Account, JsValue> {
let private_key = Felt::from_str(private_key);
if let Err(e) = private_key {
return Err(JsValue::from(format!("failed to parse private key: {e}")));
}
let private_key = private_key.unwrap();
let address = Felt::from_str(address);
if let Err(e) = address {
return Err(JsValue::from(format!("failed to parse address: {e}")));
}
let address = address.unwrap();
let chain_id = self.0.chain_id().await;
if let Err(e) = chain_id {
return Err(JsValue::from(format!("failed to get chain id: {e}")));
}
let chain_id = chain_id.unwrap();
let signer = LocalWallet::from_signing_key(SigningKey::from_secret_scalar(private_key));
let account = SingleOwnerAccount::new(
self.0.clone(),
signer,
address,
chain_id,
ExecutionEncoding::New,
);
Result::Ok(Account(account))
}
/// Calls a Starknet contract view function
///
/// # Parameters
/// * `call` - Call parameters including contract address and function
/// * `block_id` - Block identifier for the call
///
/// # Returns
/// Result containing array of field elements or error
#[wasm_bindgen(js_name = call)]
pub async unsafe fn call(&self, call: Call, block_id: BlockId) -> Result<Array, JsValue> {
let result = self
.0
.call::<FunctionCall, starknet::core::types::BlockId>(
(&call).into(),
(&block_id).into(),
)
.await;
match result {
Ok(res) => Ok(res.iter().map(|f| JsValue::from(format!("{:#x}", f))).collect()),
Err(e) => Err(JsValue::from_str(&e.to_string())),
}
}
/// Waits for a transaction to be confirmed
///
/// # Parameters
/// * `txn_hash` - Transaction hash as hex string
///
/// # Returns
/// Result containing success boolean or error
#[wasm_bindgen(js_name = waitForTransaction)]
pub async unsafe fn wait_for_transaction(&self, txn_hash: &str) -> Result<bool, JsValue> {
let txn_hash = Felt::from_str(txn_hash)
.map_err(|err| JsValue::from(format!("failed to parse transaction hash: {err}")))?;
let result: Result<(), anyhow::Error> = watch_tx(&self.0, txn_hash).await;
match result {
Ok(_) => Result::Ok(true),
Err(e) => Err(JsValue::from_str(&e.to_string())),
}
}
}
#[wasm_bindgen]
impl Account {
/// Returns the account's address
///
/// # Returns
/// Result containing address as hex string or error
#[wasm_bindgen(js_name = address)]
pub unsafe fn address(&self) -> Result<String, JsValue> {
let address = self.0.address();
Ok(format!("{:#x}", address))
}
/// Returns the account's chain ID
///
/// # Returns
/// Result containing chain ID as hex string or error
#[wasm_bindgen(js_name = chainId)]
pub unsafe fn chain_id(&self) -> Result<String, JsValue> {
let chain_id = self.0.chain_id();
Ok(format!("{:#x}", chain_id))
}
/// Sets the block ID for subsequent operations
///
/// # Parameters
/// * `block_id` - Block ID as hex string
///
/// # Returns
/// Result containing unit or error
#[wasm_bindgen(js_name = setBlockId)]
pub unsafe fn set_block_id(&mut self, block_id: String) -> Result<(), JsValue> {
let block_id = Felt::from_str(&block_id)
.map_err(|err| JsValue::from(format!("failed to parse block id: {err}")))?;
self.0.set_block_id(starknet::core::types::BlockId::Hash(block_id));
Ok(())
}
/// Executes a raw transaction
///
/// # Parameters
/// * `calldata` - Array of contract calls to execute
///
/// # Returns
/// Result containing transaction hash as hex string or error
#[wasm_bindgen(js_name = executeRaw)]
pub async unsafe fn execute_raw(&self, calldata: Calls) -> Result<String, JsValue> {
let calldata = calldata.iter().map(|c| c.into()).collect();
let call = self.0.execute_v1(calldata);
let result = call.send().await;
match result {
Ok(res) => Ok(format!("{:#x}", res.transaction_hash)),
Err(e) => Err(JsValue::from_str(&e.to_string())),
}
}
/// Deploys a burner wallet
///
/// # Parameters
/// * `private_key` - Private key for the burner wallet as hex string
///
/// # Returns
/// Result containing new Account instance or error
#[wasm_bindgen(js_name = deployBurner)]
pub async unsafe fn deploy_burner(&self, private_key: &str) -> Result<Account, JsValue> {
let private_key = match Felt::from_str(private_key) {
Ok(key) => key,
Err(e) => return Err(JsValue::from(format!("failed to parse private key: {e}"))),
};
let signing_key = SigningKey::from_secret_scalar(private_key);
let verifying_key = signing_key.verifying_key();
let address = get_contract_address(
verifying_key.scalar(),
constants::KATANA_ACCOUNT_CLASS_HASH,
&[verifying_key.scalar()],
Felt::ZERO,
);
let signer = LocalWallet::from_signing_key(signing_key);
let chain_id = self.0.chain_id();
let provider = self.0.provider().clone();
let account =
SingleOwnerAccount::new(provider, signer, address, chain_id, ExecutionEncoding::New);
// deploy the burner
let exec = self.0.execute_v1(vec![starknet::core::types::Call {
to: constants::UDC_ADDRESS,
calldata: vec![
constants::KATANA_ACCOUNT_CLASS_HASH, // class_hash
verifying_key.scalar(), // salt
Felt::ZERO, // deployer_address
Felt::ONE, // constructor calldata length (1)
verifying_key.scalar(), // constructor calldata
],
selector: starknet::core::utils::get_selector_from_name("deployContract").unwrap(),
}]);
let result = exec.send().await;
if let Err(e) = result {
return Err(JsValue::from(format!("failed to deploy burner: {e}",)));
}
let result = result.unwrap();
let _ = watch_tx(self.0.provider(), result.transaction_hash).await;
Result::Ok(Account(account))
}
/// Gets the current nonce for the account
///
/// # Returns
/// Result containing nonce as hex string or error
#[wasm_bindgen(js_name = nonce)]
pub async unsafe fn nonce(&self) -> Result<String, JsValue> {
let nonce = self.0.get_nonce().await.map_err(|e| JsValue::from(e.to_string()))?;
Ok(format!("{:#x}", nonce))
}
}
/// Computes a contract address from deployment parameters
///
/// # Parameters
/// * `class_hash` - Contract class hash as hex string
/// * `salt` - Salt value as hex string
/// * `constructor_calldata` - Array of constructor parameters as hex strings
/// * `deployer_address` - Address of deployer as hex string
///
/// # Returns
/// Result containing computed contract address as hex string or error
#[wasm_bindgen(js_name = hashGetContractAddress)]
pub fn hash_get_contract_address(
class_hash: &str,
salt: &str,
constructor_calldata: Vec<String>,
deployer_address: &str,
) -> Result<String, JsValue> {
let class_hash = Felt::from_str(class_hash)
.map_err(|err| JsValue::from(format!("failed to parse class hash: {err}")))?;
let salt = Felt::from_str(salt)
.map_err(|err| JsValue::from(format!("failed to parse salt: {err}")))?;
let deployer_address = Felt::from_str(deployer_address)
.map_err(|err| JsValue::from(format!("failed to parse deployer address: {err}")))?;
let constructor_calldata = constructor_calldata
.into_iter()
.map(|c| {
Felt::from_str(c.as_str()).map_err(|err| {
JsValue::from(format!("failed to parse constructor calldata: {err}"))
})
})
.collect::<Result<Vec<_>, _>>()?;
let address = get_contract_address(salt, class_hash, &constructor_calldata, deployer_address);
Ok(format!("{:#x}", address))
}
/// Computes a selector from a tag string
///
/// # Parameters
/// * `tag` - Tag string to compute selector from
///
/// # Returns
/// Selector as hex string
#[wasm_bindgen(js_name = getSelectorFromTag)]
pub fn get_selector_from_tag(tag: &str) -> String {
let selector = compute_selector_from_tag(tag);
format!("{:#x}", selector)
}
/// Serializes a string into a Cairo byte array
///
/// # Parameters
/// * `str` - String to serialize
///
/// # Returns
/// Result containing array of field elements as hex strings or error
#[wasm_bindgen(js_name = byteArraySerialize)]
pub fn bytearray_serialize(str: &str) -> Result<Vec<String>, JsValue> {
let bytearray = match ByteArray::from_string(str) {
Ok(bytearray) => bytearray,
Err(e) => return Err(JsValue::from(format!("failed to parse bytearray: {e}"))),
};
let felts = cairo_serde::ByteArray::cairo_serialize(&bytearray);
Ok(felts.iter().map(|f| format!("{:#x}", f)).collect())
}
/// Deserializes a Cairo byte array into a string
///
/// # Parameters
/// * `felts` - Array of field elements as hex strings
///
/// # Returns
/// Result containing deserialized string or error
#[wasm_bindgen(js_name = byteArrayDeserialize)]
pub fn bytearray_deserialize(felts: Vec<String>) -> Result<String, JsValue> {
let felts = felts
.into_iter()
.map(|f| Felt::from_str(f.as_str()))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse felts: {e}")))?;
let bytearray = match cairo_serde::ByteArray::cairo_deserialize(&felts, 0) {
Ok(bytearray) => bytearray,
Err(e) => return Err(JsValue::from(format!("failed to deserialize bytearray: {e}"))),
};
match bytearray.to_string() {
Ok(s) => Ok(s),
Err(e) => Err(JsValue::from(format!("failed to serialize bytearray: {e}"))),
}
}
/// Computes a Poseidon hash of the inputs
///
/// # Parameters
/// * `inputs` - Array of field elements as hex strings
///
/// # Returns
/// Result containing hash as hex string or error
#[wasm_bindgen(js_name = poseidonHash)]
pub fn poseidon_hash(inputs: Vec<String>) -> Result<String, JsValue> {
let inputs = inputs
.into_iter()
.map(|i| Felt::from_str(i.as_str()))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse inputs: {e}")))?;
Ok(format!("{:#x}", poseidon_hash_many(&inputs)))
}
/// Gets a selector from a function name
///
/// # Parameters
/// * `name` - Function name to compute selector from
///
/// # Returns
/// Result containing selector as hex string or error
#[wasm_bindgen(js_name = getSelectorFromName)]
pub fn get_selector_from_name(name: &str) -> Result<String, JsValue> {
let selector = starknet::core::utils::get_selector_from_name(name)
.map_err(|e| JsValue::from(e.to_string()))?;
Ok(format!("{:#x}", selector))
}
/// Computes the Starknet variant of Keccak hash
///
/// # Parameters
/// * `inputs` - Byte array to hash
///
/// # Returns
/// Result containing hash as hex string or error
#[wasm_bindgen(js_name = starknetKeccak)]
pub fn starknet_keccak(inputs: js_sys::Uint8Array) -> Result<String, JsValue> {
let inputs = inputs.to_vec();
let hash = starknet::core::utils::starknet_keccak(&inputs);
Ok(format!("{:#x}", hash))
}
/// Converts a short string to a Cairo field element
///
/// # Parameters
/// * `str` - String to convert
///
/// # Returns
/// Result containing field element as hex string or error
#[wasm_bindgen(js_name = cairoShortStringToFelt)]
pub fn cairo_short_string_to_felt(str: &str) -> Result<String, JsValue> {
let felt = starknet::core::utils::cairo_short_string_to_felt(str)
.map_err(|e| JsValue::from(e.to_string()))?;
Ok(format!("{:#x}", felt))
}
/// Parses a Cairo field element into a short string
///
/// # Parameters
/// * `str` - Field element as hex string
///
/// # Returns
/// Result containing parsed string or error
#[wasm_bindgen(js_name = parseCairoShortString)]
pub fn parse_cairo_short_string(str: &str) -> Result<String, JsValue> {
let felt =
Felt::from_str(str).map_err(|e| JsValue::from(format!("failed to parse felt: {e}")))?;
let string = starknet::core::utils::parse_cairo_short_string(&felt)
.map_err(|e| JsValue::from(format!("failed to parse cairo short string: {e}")))?;
Ok(string)
}
#[wasm_bindgen]
impl ToriiClient {
/// Gets controllers along with their usernames for the given contract addresses
///
/// # Parameters
/// * `contract_addresses` - Array of contract addresses as hex strings. If empty, all
/// controllers will be returned.
///
/// # Returns
/// Result containing controllers or error
#[wasm_bindgen(js_name = getControllers)]
pub async fn get_controllers(
&self,
contract_addresses: Vec<String>,
) -> Result<Controllers, JsValue> {
let contract_addresses = contract_addresses
.into_iter()
.map(|c| Felt::from_str(&c))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse contract addresses: {e}")))?;
let controllers = self
.inner
.controllers(contract_addresses)
.await
.map_err(|e| JsValue::from(format!("failed to get controllers: {e}")))?;
Ok(Controllers(controllers.iter().map(|c| c.into()).collect()))
}
/// Gets token information for the given contract addresses
///
/// # Parameters
/// * `contract_addresses` - Array of contract addresses as hex strings
///
/// # Returns
/// Result containing token information or error
#[wasm_bindgen(js_name = getTokens)]
pub async fn get_tokens(
&self,
contract_addresses: Vec<String>,
token_ids: Vec<String>,
) -> Result<Tokens, JsValue> {
let contract_addresses = contract_addresses
.into_iter()
.map(|c| Felt::from_str(&c))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse contract addresses: {e}")))?;
let token_ids = token_ids
.into_iter()
.map(|t| U256::from_be_hex(t.trim_start_matches("0x")))
.collect::<Vec<_>>();
let tokens = self
.inner
.tokens(contract_addresses, token_ids)
.await
.map_err(|e| JsValue::from(format!("failed to get tokens: {e}")))?;
Ok(Tokens(tokens.iter().map(|t| t.into()).collect()))
}
/// Subscribes to token updates
///
/// # Parameters
/// * `contract_addresses` - Array of contract addresses as hex strings
/// * `callback` - JavaScript function to call on updates
///
/// # Returns
/// Result containing subscription handle or error
#[wasm_bindgen(js_name = onTokenUpdated)]
pub fn on_token_updated(
&self,
contract_addresses: Vec<String>,
token_ids: Vec<String>,
callback: js_sys::Function,
) -> Result<Subscription, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();
let contract_addresses = contract_addresses
.into_iter()
.map(|addr| {
Felt::from_str(&addr).map_err(|err| {
JsValue::from(format!("failed to parse contract address: {err}"))
})
})
.collect::<Result<Vec<_>, _>>()?;
let token_ids = token_ids
.into_iter()
.map(|t| U256::from_be_hex(t.trim_start_matches("0x")))
.collect::<Vec<_>>();
let subscription_id = Arc::new(AtomicU64::new(0));
let (trigger, tripwire) = Tripwire::new();
let subscription = Subscription { id: Arc::clone(&subscription_id), trigger };
// Spawn a new task to handle the stream and reconnections
let client = self.inner.clone();
let subscription_id_clone = Arc::clone(&subscription_id);
wasm_bindgen_futures::spawn_local(async move {
let mut backoff = 1000;
let max_backoff = 60000;
loop {
if let Ok(stream) =
client.on_token_updated(contract_addresses.clone(), token_ids.clone()).await
{
backoff = 1000; // Reset backoff on successful connection
let mut stream = stream.take_until_if(tripwire.clone());
while let Some(Ok((id, token))) = stream.next().await {
subscription_id_clone.store(id, Ordering::SeqCst);
let token: Token = (&token).into();
let _ = callback.call1(
&JsValue::null(),
&token.serialize(&JSON_COMPAT_SERIALIZER).unwrap(),
);
}
}
// If we've reached this point, the stream has ended (possibly due to disconnection)
// We'll try to reconnect after a delay, unless the tripwire has been triggered
if tripwire.clone().now_or_never().unwrap_or_default() {
break; // Exit the loop if the subscription has been cancelled
}
gloo_timers::future::TimeoutFuture::new(backoff).await;
backoff = std::cmp::min(backoff * 2, max_backoff);
}
});
Ok(subscription)
}
/// Gets token balances for given accounts and contracts
///
/// # Parameters
/// * `contract_addresses` - Array of contract addresses as hex strings
/// * `account_addresses` - Array of account addresses as hex strings
///
/// # Returns
/// Result containing token balances or error
#[wasm_bindgen(js_name = getTokenBalances)]
pub async fn get_token_balances(
&self,
contract_addresses: Vec<String>,
account_addresses: Vec<String>,
token_ids: Vec<String>,
) -> Result<TokenBalances, JsValue> {
let account_addresses = account_addresses
.into_iter()
.map(|a| Felt::from_str(&a))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse account addresses: {e}")))?;
let contract_addresses = contract_addresses
.into_iter()
.map(|c| Felt::from_str(&c))
.collect::<Result<Vec<_>, _>>()
.map_err(|e| JsValue::from(format!("failed to parse contract addresses: {e}")))?;
let token_ids = token_ids
.into_iter()
.map(|t| U256::from_be_hex(t.trim_start_matches("0x")))
.collect::<Vec<_>>();
let token_balances = self
.inner
.token_balances(account_addresses, contract_addresses, token_ids)
.await
.map_err(|e| JsValue::from(format!("failed to get token balances: {e}")))?;
Ok(TokenBalances(token_balances.iter().map(|t| t.into()).collect()))
}
/// Queries entities based on the provided query parameters
///
/// # Parameters
/// * `query` - Query parameters for filtering entities
///
/// # Returns
/// Result containing matching entities or error
#[wasm_bindgen(js_name = getEntities)]
pub async fn get_entities(&self, query: Query) -> Result<Entities, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();
let results = self.inner.entities((&query).into()).await;
match results {
Ok(entities) => Ok((&entities).into()),
Err(err) => Err(JsValue::from(format!("failed to get entities: {err}"))),
}
}
/// Gets all entities with pagination
///
/// # Parameters
/// * `limit` - Maximum number of entities to return
/// * `offset` - Number of entities to skip
///
/// # Returns
/// Result containing paginated entities or error
#[wasm_bindgen(js_name = getAllEntities)]
pub async fn get_all_entities(&self, limit: u32, offset: u32) -> Result<Entities, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();
let results = self
.inner
.entities(torii_grpc::types::Query {
limit,
offset,
clause: None,
dont_include_hashed_keys: false,
order_by: vec![],
entity_models: vec![],
entity_updated_after: 0,
})
.await;
match results {
Ok(entities) => Ok((&entities).into()),
Err(err) => Err(JsValue::from(format!("failed to get entities: {err}"))),
}
}
/// Gets event messages based on query parameters
///
/// # Parameters
/// * `query` - Query parameters for filtering messages
/// * `historical` - Whether to include historical messages
///
/// # Returns
/// Result containing matching event messages or error
#[wasm_bindgen(js_name = getEventMessages)]
pub async fn get_event_messages(
&self,
query: Query,
historical: bool,
) -> Result<Entities, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();
let results = self.inner.event_messages((&query).into(), historical).await;
match results {
Ok(event_messages) => Ok((&event_messages).into()),
Err(err) => Err(JsValue::from(format!("failed to get event_messages: {err}"))),
}
}
/// Subscribes to entity updates
///
/// # Parameters
/// * `clauses` - Array of key clauses for filtering updates
/// * `callback` - JavaScript function to call on updates
///
/// # Returns
/// Result containing subscription handle or error
#[wasm_bindgen(js_name = onEntityUpdated)]
pub fn on_entity_updated(
&self,
clauses: KeysClauses,
callback: js_sys::Function,
) -> Result<Subscription, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();
let clauses: Vec<_> = clauses.iter().map(|c| c.into()).collect();
let subscription_id = Arc::new(AtomicU64::new(0));
let (trigger, tripwire) = Tripwire::new();
let subscription = Subscription { id: Arc::clone(&subscription_id), trigger };
// Spawn a new task to handle the stream and reconnections
let client = self.inner.clone();
let subscription_id_clone = Arc::clone(&subscription_id);
wasm_bindgen_futures::spawn_local(async move {
let mut backoff = 1000;
let max_backoff = 60000;
loop {
if let Ok(stream) = client.on_entity_updated(clauses.clone()).await {
backoff = 1000; // Reset backoff on successful connection
let mut stream = stream.take_until_if(tripwire.clone());
while let Some(Ok((id, entity))) = stream.next().await {
subscription_id_clone.store(id, Ordering::SeqCst);
let models: Entity = (&entity).into();
let _ = callback.call2(
&JsValue::null(),
&JsValue::from_str(&format!("{:#x}", entity.hashed_keys)),
&models.serialize(&JSON_COMPAT_SERIALIZER).unwrap(),
);
}
}
// If we've reached this point, the stream has ended (possibly due to disconnection)
// We'll try to reconnect after a delay, unless the tripwire has been triggered
if tripwire.clone().now_or_never().unwrap_or_default() {
break; // Exit the loop if the subscription has been cancelled
}
gloo_timers::future::TimeoutFuture::new(backoff).await;
backoff = std::cmp::min(backoff * 2, max_backoff);
}
});
Ok(subscription)
}
/// Updates an existing entity subscription
///
/// # Parameters
/// * `subscription` - Existing subscription to update
/// * `clauses` - New array of key clauses for filtering
///
/// # Returns
/// Result containing unit or error
#[wasm_bindgen(js_name = updateEntitySubscription)]
pub async fn update_entity_subscription(
&self,
subscription: &Subscription,
clauses: KeysClauses,
) -> Result<(), JsValue> {
let clauses = clauses.iter().map(|c| c.into()).collect();
self.inner
.update_entity_subscription(subscription.id.load(Ordering::SeqCst), clauses)
.await
.map_err(|err| JsValue::from(format!("failed to update subscription: {err}")))
}
/// Subscribes to event message updates
///
/// # Parameters
/// * `clauses` - Array of key clauses for filtering updates
/// * `historical` - Whether to include historical messages
/// * `callback` - JavaScript function to call on updates
///
/// # Returns
/// Result containing subscription handle or error
#[wasm_bindgen(js_name = onEventMessageUpdated)]
pub fn on_event_message_updated(
&self,
clauses: KeysClauses,
historical: bool,
callback: js_sys::Function,
) -> Result<Subscription, JsValue> {
#[cfg(feature = "console-error-panic")]
console_error_panic_hook::set_once();
let clauses: Vec<_> = clauses.iter().map(|c| c.into()).collect();
let subscription_id = Arc::new(AtomicU64::new(0));
let (trigger, tripwire) = Tripwire::new();
let subscription = Subscription { id: Arc::clone(&subscription_id), trigger };
// Spawn a new task to handle the stream and reconnections
let client = self.inner.clone();
let subscription_id_clone = Arc::clone(&subscription_id);
wasm_bindgen_futures::spawn_local(async move {
let mut backoff = 1000;
let max_backoff = 60000;
loop {
if let Ok(stream) =
client.on_event_message_updated(clauses.clone(), historical).await
{
backoff = 1000; // Reset backoff on successful connection
let mut stream = stream.take_until_if(tripwire.clone());
while let Some(Ok((id, entity))) = stream.next().await {
subscription_id_clone.store(id, Ordering::SeqCst);
let models: Entity = (&entity).into();
let _ = callback.call2(
&JsValue::null(),
&JsValue::from_str(&format!("{:#x}", entity.hashed_keys)),
&models.serialize(&JSON_COMPAT_SERIALIZER).unwrap(),
);
}
}
// If we've reached this point, the stream has ended (possibly due to disconnection)
// We'll try to reconnect after a delay, unless the tripwire has been triggered
if tripwire.clone().now_or_never().unwrap_or_default() {
break; // Exit the loop if the subscription has been cancelled
}
gloo_timers::future::TimeoutFuture::new(backoff).await;
backoff = std::cmp::min(backoff * 2, max_backoff);
}
});
Ok(subscription)
}
/// Updates an existing event message subscription
///
/// # Parameters
/// * `subscription` - Existing subscription to update
/// * `clauses` - New array of key clauses for filtering
/// * `historical` - Whether to include historical messages
///
/// # Returns