forked from NethermindEth/starknet.go
/
mock_test.go
1152 lines (1088 loc) · 35.2 KB
/
mock_test.go
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
package rpc
import (
"context"
"encoding/json"
"fmt"
"math/big"
"os"
"strings"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/starknet.go/utils"
"github.com/pkg/errors"
)
var (
errWrongType = fmt.Errorf("wrong type")
errWrongArgs = fmt.Errorf("wrong number of args")
)
// rpcMock is a mock of the go-ethereum Client that can be used for local tests
// when no integration environment exists.
type rpcMock struct {
closed bool
}
// Close sets the 'closed' field of the rpcMock struct to true.
//
// No parameters.
// No return value.
func (r *rpcMock) Close() {
r.closed = true
}
// CallContext calls the RPC method with the specified parameters and returns an error.
//
// Parameters:
// - ctx: represents the current execution context
// - result: the interface{} to store the result of the RPC call
// - method: the string representing the RPC method to be called
// - args: variadic and can be used to pass additional arguments to the RPC method
// Returns:
// - error: an error if any occurred during the function call
func (r *rpcMock) CallContext(ctx context.Context, result interface{}, method string, args ...interface{}) error {
switch method {
case "starknet_addDeclareTransaction":
return mock_starknet_addDeclareTransaction(result, method, args...)
case "starknet_addInvokeTransaction":
return mock_starknet_addInvokeTransaction(result, method, args...)
case "starknet_addDeployAccountTransaction":
return mock_starknet_addDeployAccountTransaction(result, method, args...)
case "starknet_blockNumber":
return mock_starknet_blockNumber(result, method, args...)
case "starknet_call":
return mock_starknet_call(result, method, args...)
case "starknet_chainId":
return mock_starknet_chainId(result, method, args...)
case "starknet_estimateFee":
return mock_starknet_estimateFee(result, method, args...)
case "starknet_estimateMessageFee":
return mock_starknet_estimateMessageFee(result, method, args...)
case "starknet_getBlockTransactionCount":
return mock_starknet_getBlockTransactionCount(result, method, args...)
case "starknet_getBlockWithTxHashes":
return mock_starknet_getBlockWithTxHashes(result, method, args...)
case "starknet_getBlockWithReceipts":
return mock_starknet_getBlockWithReceipts(result, method, args...)
case "starknet_getClass":
return mock_starknet_getClass(result, method, args...)
case "starknet_getClassAt":
return mock_starknet_getClassAt(result, method, args...)
case "starknet_getClassHashAt":
return mock_starknet_getClassHashAt(result, method, args...)
case "starknet_getEvents":
return mock_starknet_getEvents(result, method, args...)
case "starknet_getNonce":
return mock_starknet_getNonce(result, method, args...)
case "starknet_getStateUpdate":
return mock_starknet_getStateUpdate(result, method, args...)
case "starknet_getStorageAt":
return mock_starknet_getStorageAt(result, method, args...)
case "starknet_getTransactionByBlockIdAndIndex":
return mock_starknet_getTransactionByBlockIdAndIndex(result, method, args...)
case "starknet_getTransactionByHash":
return mock_starknet_getTransactionByHash(result, method, args...)
case "starknet_getTransactionReceipt":
return mock_starknet_getTransactionReceipt(result, method, args...)
case "starknet_syncing":
return mock_starknet_syncing(result, method, args...)
case "starknet_traceBlockTransactions":
return mock_starknet_traceBlockTransactions(result, method, args...)
case "starknet_traceTransaction":
return mock_starknet_traceTransaction(result, method, args...)
default:
return errNotFound
}
}
// mock_starknet_blockNumber is a function that mocks the blockNumber functionality in the StarkNet API.
//
// Parameters:
// - result: The result variable that will hold the block number value
// - method: The method string that specifies the API method being called
// - args: Additional arguments passed to the function
// Returns:
// - error: An error if the result is not of type *big.Int or if the arguments count is not zero
func mock_starknet_blockNumber(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*big.Int)
if !ok || r == nil {
return errWrongType
}
if len(args) != 0 {
return errWrongArgs
}
value1 := big.NewInt(1)
*r = *value1
return nil
}
// mock_starknet_chainId is a function that mocks the behavior of the `starknet_chainId` method.
//
// Parameters:
// - result: an interface{} that holds the result of the function.
// - method: a string that represents the method.
// - args: a variadic parameter of type interface{} that represents the arguments of the function.
// Returns:
// - error: an error if there is a wrong type or wrong number of arguments.
func mock_starknet_chainId(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*string)
if !ok {
return errWrongType
}
if len(args) != 0 {
return errWrongArgs
}
value := "0x534e5f474f45524c49"
*r = value
return nil
}
// mock_starknet_syncing is a function that mocks the behavior of the starknet_syncing function.
//
// Parameters:
// - result: an interface{} that holds the result of the function.
// - method: a string that represents the method.
// - args: a variadic parameter of type interface{} that represents the arguments of the function.
// Return:
// - error: an error if there is a wrong type or wrong number of arguments
func mock_starknet_syncing(result interface{}, method string, args ...interface{}) error {
// Note: Since starknet_syncing returns with bool or SyncStatus, we pass in interface{}
r, ok := result.(*interface{})
if !ok {
return errWrongType
}
if len(args) != 0 {
return errWrongArgs
}
blockDataFeltArr, err := utils.HexArrToFelt([]string{
"0x4b238e99c40d448b85dfc69e4414c2dbeb4d21d5c670b1662b5ad2ad2fcb061",
"0x9cee6f457637180c36532bb0bfc5a091bb410b70f0489bcbbb0f1eca6650be",
})
if err != nil {
return err
}
value := SyncStatus{
StartingBlockHash: blockDataFeltArr[0],
StartingBlockNum: "0x4c602",
CurrentBlockHash: blockDataFeltArr[1],
CurrentBlockNum: "0x4c727",
HighestBlockHash: blockDataFeltArr[1],
HighestBlockNum: "0x4c727",
}
*r = value
return nil
}
// mock_starknet_getTransactionByBlockIdAndIndex is a function that mocks the behavior of getting
// a transaction by block ID and index in the StarkNet API.
//
// Parameters:
// - result: The result of the API call, which will be stored in the provided interface{}. This should be a pointer to a json.RawMessage
// - method: The method of the API call
// - args: The arguments of the API call. This should be a variadic parameter that accepts a variable number of arguments
// Returns:
// - error: An error if the API call fails, otherwise nil
func mock_starknet_getTransactionByBlockIdAndIndex(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
return errWrongType
}
if len(args) != 2 {
return errWrongArgs
}
_, ok = args[1].(uint64)
if !ok {
return errWrongArgs
}
var InvokeTxnV1example = `{
"transaction_hash": "0x705547f8f2f8fdfb10ed533d909f76482bb293c5a32648d476774516a0bebd0",
"type": "INVOKE",
"nonce": "0x0",
"max_fee": "0x53685de02fa5",
"version": "0x1",
"signature": [
"0x4a7849de7b91e52cd0cdaf4f40aa67f54a58e25a15c60e034d2be819c1ecda4",
"0x227fcad2a0007348e64384649365e06d41287b1887999b406389ee73c1d8c4c"
],
"sender_address": "0x315e364b162653e5c7b23efd34f8da27ba9c069b68e3042b7d76ce1df890313",
"calldata": [
"0x1",
"0x13befe6eda920ce4af05a50a67bd808d67eee6ba47bb0892bef2d630eaf1bba"
]
}`
if err := json.Unmarshal([]byte(InvokeTxnV1example), r); err != nil {
return err
}
return nil
}
// mock_starknet_getBlockTransactionCount is a function that mocks the behavior of the
// GetBlockTransactionCount method in the StarkNet API.
//
// Parameters:
// - result: The result of the API call, which will be stored in the provided interface{}. This should be a pointer to a json.RawMessage
// - method: The method of the API call
// - args: The arguments of the API call. This should be a variadic parameter that accepts a variable number of arguments
// Returns:
// - error: An error if the API call fails, otherwise nil
func mock_starknet_getBlockTransactionCount(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
return errWrongType
}
if len(args) != 1 {
return errWrongArgs
}
outputContent, err := json.Marshal(uint64(10))
if err != nil {
return err
}
if err := json.Unmarshal(outputContent, r); err != nil {
return err
}
return nil
}
// mock_starknet_getTransactionByHash is a function that retrieves a transaction by its hash.
//
// Parameters:
// - result: an interface{} that represents the result of the transaction retrieval
// - method: a string that specifies the method used for retrieval
// - args: a variadic parameter that contains the arguments used for retrieval
// Returns:
// - error: an error if there is a failure in retrieving the transaction
func mock_starknet_getTransactionByHash(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
return errWrongType
}
if len(args) != 1 {
return errWrongArgs
}
_, ok = args[0].(*felt.Felt)
if !ok {
return errWrongArgs
}
var InvokeTxnV1example = ` {
"transaction_hash": "0x1779df1c6de5136ad2620f704b645e9cbd554b57d37f08a06ea60142269c5a5",
"version": "0x1",
"max_fee": "0x17970b794f000",
"signature": [
"0xe500c4014c055c3304d8a125cfef44638ffa5b0f6840916049667a4c38aa1c",
"0x45ac538bfce5d8c5741b4421bbdc99f5849451acae75d2048d7cc4bb029ca77"
],
"nonce": "0x2d",
"sender_address": "0x66dd340c03b6b7866fa7bb4bb91cc9e9c2a8eedc321985f334fd55de5e4e071",
"calldata": [
"0x3",
"0x39a04b968d794fb076b0fbb146c12b48a23aa785e3d2e5be1982161f7536218",
"0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354",
"0x0",
"0x3",
"0x3207980cd08767c9310d197c38b1a58b2a9bceb61dd9a99f51b407798702991",
"0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354",
"0x3",
"0x3",
"0x42969068f9e84e9bf1c7bb6eb627455287e58f866ba39e45b123f9656aed5e9",
"0x2f0b3c5710379609eb5495f1ecd348cb28167711b73609fe565a72734550354",
"0x6",
"0x3",
"0x9",
"0x47487560da4c5c5755897e527a5fda37422b5ba02a2aba1ca3ce2b24dfd142e",
"0xde0b6b3a7640000",
"0x0",
"0x47487560da4c5c5755897e527a5fda37422b5ba02a2aba1ca3ce2b24dfd142e",
"0x10f0cf064dd59200000",
"0x0",
"0x47487560da4c5c5755897e527a5fda37422b5ba02a2aba1ca3ce2b24dfd142e",
"0x21e19e0c9bab2400000",
"0x0"
],
"type": "INVOKE"
}`
json.Unmarshal([]byte(InvokeTxnV1example), r)
return nil
}
// mock_starknet_getTransactionReceipt mocks the function that retrieves the transaction receipt information
// from the StarkNet blockchain.
//
// Parameters:
// - result: a pointer to an interface that will store the transaction receipt result
// - method: a string representing the method of the transaction receipt
// - args: a variadic parameter representing the arguments of the transaction receipt
// Returns:
// - error: an error if there is an issue with the type of the result or the number of arguments
func mock_starknet_getTransactionReceipt(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
return errWrongType
}
fmt.Printf("%T, %d", result, len(args))
if len(args) != 1 {
return errWrongArgs
}
arg0Felt := args[0].(*felt.Felt)
testTxnHash, err := utils.HexToFelt("0x4b861c47d0fbc4cc24dacf92cf155ad0a2f7e2a0fd9b057b90cdd64eba7e12e")
if err != nil {
return err
}
if arg0Felt.Equal(testTxnHash) {
var txnRec struct {
Result UnknownTransactionReceipt `json:"result"`
}
read, err := os.ReadFile("tests/receipt/0x4b861c47d0fbc4cc24dacf92cf155ad0a2f7e2a0fd9b057b90cdd64eba7e12e.json")
if err != nil {
return err
}
err = json.Unmarshal(read, &txnRec)
if err != nil {
return err
}
txnReceipt, err := json.Marshal(txnRec.Result.TransactionReceipt)
if err != nil {
return err
}
return json.Unmarshal(txnReceipt, &r)
}
fromAddressFelt, err := utils.HexToFelt("0xdeadbeef")
if err != nil {
return err
}
transaction := InvokeTransactionReceipt(CommonTransactionReceipt{
TransactionHash: arg0Felt,
FinalityStatus: TxnFinalityStatusAcceptedOnL1,
Events: []Event{{
FromAddress: fromAddressFelt,
}},
})
outputContent, err := json.Marshal(transaction)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getClassAt is a function that performs a mock operation to get the class at a given index.
// The function sets the class to a DeprecatedContractClass with a specific program and marshals the class into JSON format.
// Finally, it unmarshals the JSON content into the result.
//
// Parameters:
// - result: An interface{} that represents the result of the operation
// - method: A string that specifies the method to be used
// - args: A variadic parameter that represents the arguments to be passed
// Returns:
// - error: An error if the result is not of type *json.RawMessage or is nil or the number of arguments is not equal to 2
// The function always returns nil.
func mock_starknet_getClassAt(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
fmt.Printf("%T\n", result)
return errWrongType
}
if len(args) != 2 {
return errWrongArgs
}
var class = DeprecatedContractClass{
Program: "H4sIAAAAAAAE/+Vde3PbOJL/Kj5VXW1mVqsC36Sr9g8n0c6mzonnbM",
}
outputContent, err := json.Marshal(class)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getClassHashAt is a function that retrieves the class hash at a specific location in the StarkNet.
//
// Parameters:
// - result: An interface{} that represents the result of the operation
// - method: A string that specifies the method to be used
// - args: A variadic parameter that represents the arguments to be passed
// Returns:
// - error: An error if the result is not of type *json.RawMessage or is nil or the number of arguments is not equal to 2
// The function always returns nil.
func mock_starknet_getClassHashAt(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
fmt.Printf("%T\n", result)
return errWrongType
}
if len(args) != 2 {
return errWrongArgs
}
classHash, err := utils.HexToFelt("0xdeadbeef")
if err != nil {
return err
}
outputContent, err := json.Marshal(classHash)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getClass is a function that retrieves a class from the StarkNet API.
// It takes in a result interface{}, a method string, and variadic args ...interface{}.
// The result interface{} should be a pointer to json.RawMessage.
// The method string specifies the method to be called on the StarkNet API.
// The args ...interface{} are the arguments to be passed to the method.
// The function returns an error if any of the following conditions are met:
// - The result is not of type *json.RawMessage.
// - The args length is not equal to 2.
// - The first argument is not of type BlockID.
// - The second argument is not of type *felt.Felt or does not have a hexadecimal prefix.
// The function assigns a DeprecatedContractClass struct to the variable class.
// The function then marshals the class to JSON and unmarshals it to the result interface{}.
// If successful, the function returns nil.
//
// Parameters:
// - result: The result interface{} that should be a pointer to json.RawMessage
// - method: The method string specifying the method to be called on the StarkNet API
// - args: The variadic args ...interface{} representing the arguments to be passed to the method
// Returns:
// - error: An error if any of the conditions mentioned above are met
func mock_starknet_getClass(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
fmt.Printf("%T\n", result)
return errWrongType
}
if len(args) != 2 {
return errWrongArgs
}
_, ok = args[0].(BlockID)
if !ok {
fmt.Printf("expecting BlockID, instead %T\n", args[1])
return errWrongArgs
}
classHash, ok := args[1].(*felt.Felt)
if !ok || !strings.HasPrefix(classHash.String(), "0x") {
fmt.Printf("%T\n", args[1])
return errWrongArgs
}
var class = DeprecatedContractClass{
Program: "H4sIAAAAAAAA",
}
outputContent, err := json.Marshal(class)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getEvents is a function that retrieves events from the StarkNet blockchain.
//
// Parameters:
// - result: An interface{} that represents the result of the operation
// - method: A string that specifies the method to be used
// - args: A variadic parameter that represents the arguments to be passed
// Returns:
// - error: An error if the result is not of type *json.RawMessage or is nil or the number of arguments is not equal to 1
// The function always returns nil
func mock_starknet_getEvents(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 1 {
return errWrongArgs
}
ei, ok := args[0].(EventsInput)
if !ok {
return errWrongArgs
}
if ei.ChunkSize == 0 {
return fmt.Errorf("-ChuckSize error message-")
}
blockHash, err := utils.HexToFelt("0x59dbe64bf2e2f89f5f2958cff11044dca0c64dea2e37ec6eaad9a5f838793cb")
if err != nil {
return err
}
txHash, err := utils.HexToFelt("0x568147c09d5e5db8dc703ce1da21eae47e9ad9c789bc2f2889c4413a38c579d")
if err != nil {
return err
}
events :=
EventChunk{
Events: []EmittedEvent{
EmittedEvent{
BlockHash: blockHash,
BlockNumber: 1472,
TransactionHash: txHash,
},
},
}
outputContent, err := json.Marshal(events)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_call is a function that mocks a Starknet call.
//
// Parameters:
// - result: The result of the transaction
// - method: The method to be called
// - args: The arguments to be passed to the method
// Returns:
// - error: An error if the transaction fails
func mock_starknet_call(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 2 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
out, err := new(felt.Felt).SetString("0xdeadbeef")
if err != nil {
return err
}
outputContent, err := json.Marshal([]*felt.Felt{out})
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_addDeclareTransaction is a mock function that adds a declare transaction to the StarkNet smart contract.
//
// Parameters:
// - result: The result of the transaction
// - method: The method to be called
// - args: The arguments to be passed to the method
// Return:
// - error: An error if the transaction fails
func mock_starknet_addDeclareTransaction(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
switch args[0].(type) {
case DeclareTxnV2, DeclareTxnV3:
deadbeefFelt, err := utils.HexToFelt("0x41d1f5206ef58a443e7d3d1ca073171ec25fa75313394318fc83a074a6631c3")
if err != nil {
return err
}
output := AddDeclareTransactionOutput{
TransactionHash: deadbeefFelt,
ClassHash: deadbeefFelt,
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
return errors.Wrap(errWrongArgs, fmt.Sprintf("args[0] should be DeclareTxnV2 or DeclareTxnV3, got %T\n", args[0]))
}
// mock_starknet_estimateFee simulates the estimation of a fee in the StarkNet network.
//
// Parameters:
// - result: The result of the transaction
// - method: The method to be called
// - args: The arguments to be passed to the method
// Returns:
// - error: an error if any
func mock_starknet_estimateFee(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 2 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
_, ok = args[0].(FunctionCall)
if !ok {
fmt.Printf("args[0] should be FunctionCall, got %T\n", args[0])
return errWrongArgs
}
_, ok = args[1].(BlockID)
if !ok {
fmt.Printf("args[1] should be *blockID, got %T\n", args[1])
return errWrongArgs
}
gasCons, err := new(felt.Felt).SetString("0x01a4")
if err != nil {
return err
}
gasPrice, err := new(felt.Felt).SetString("0x45")
if err != nil {
return err
}
overallFee, err := new(felt.Felt).SetString("0x7134")
if err != nil {
return err
}
output := FeeEstimate{
GasConsumed: gasCons,
GasPrice: gasPrice,
OverallFee: overallFee,
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_estimateMessageFee is a function that estimates the fee for a StarkNet message.
//
// Parameters:
// - result: The result of the transaction
// - method: The method to be called
// - args: The arguments to be passed to the method
// Returns:
// - error: an error if any
func mock_starknet_estimateMessageFee(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 2 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
_, ok = args[0].(MsgFromL1)
if !ok {
fmt.Printf("args[0] should be MsgFromL1, got %T\n", args[0])
return errWrongArgs
}
_, ok = args[1].(BlockID)
if !ok {
fmt.Printf("args[1] should be *blockID, got %T\n", args[1])
return errWrongArgs
}
output := FeeEstimate{
GasConsumed: new(felt.Felt).SetUint64(1),
GasPrice: new(felt.Felt).SetUint64(2),
OverallFee: new(felt.Felt).SetUint64(3),
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_addInvokeTransaction is a mock function that simulates the behavior of the
// starknet_addInvokeTransaction function.
// Parameters:
// - result: The result of the transaction
// - method: The method to be called
// - args: The arguments to be passed to the method
// Returns:
// - error: an error if any
func mock_starknet_addInvokeTransaction(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 1 {
return errors.Wrap(errWrongArgs, fmt.Sprint("wrong number of args ", len(args)))
}
switch invokeTx := args[0].(type) {
case InvokeTxnV1:
if invokeTx.SenderAddress != nil {
if invokeTx.SenderAddress.Equal(new(felt.Felt).SetUint64(123)) {
unexpErr := *ErrUnexpectedError
unexpErr.Data = "Something crazy happened"
return &unexpErr
}
}
deadbeefFelt, err := utils.HexToFelt("0xdeadbeef")
if err != nil {
return err
}
output := AddInvokeTransactionResponse{
TransactionHash: deadbeefFelt,
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
case InvokeTxnV3:
deadbeefFelt, err := utils.HexToFelt("0x49728601e0bb2f48ce506b0cbd9c0e2a9e50d95858aa41463f46386dca489fd")
if err != nil {
return err
}
output := AddInvokeTransactionResponse{
TransactionHash: deadbeefFelt,
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
default:
return errors.Wrap(errWrongArgs, fmt.Sprintf("args[0] should be InvokeTxnV1 or InvokeTxnV3, got %T\n", args[0]))
}
}
func mock_starknet_addDeployAccountTransaction(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 1 {
return errors.Wrap(errWrongArgs, fmt.Sprint("wrong number of args ", len(args)))
}
switch args[0].(type) {
case DeployAccountTxn, DeployAccountTxnV3:
deadbeefFelt, err := utils.HexToFelt("0x32b272b6d0d584305a460197aa849b5c7a9a85903b66e9d3e1afa2427ef093e")
if err != nil {
return err
}
output := AddDeployAccountTransactionResponse{
TransactionHash: deadbeefFelt,
ContractAddress: new(felt.Felt).SetUint64(0),
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
default:
return errors.Wrap(errWrongArgs, fmt.Sprintf("args[0] should be DeployAccountTxn or DeployAccountTxnV3, got %T\n", args[0]))
}
}
// mock_starknet_getStorageAt mocks the behavior of the StarkNet getStorageAt function.
//
// Parameters:
// - result: The result of the transaction
// - method: The method to be called
// - args: The arguments to be passed to the method
// Returns:
// - error: an error if any
func mock_starknet_getStorageAt(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 3 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
if _, ok := args[0].(*felt.Felt); !ok {
return errWrongArgs
}
if _, ok := args[1].(string); !ok {
return errWrongArgs
}
if _, ok := args[2].(BlockID); !ok {
return errWrongArgs
}
output := "0xdeadbeef"
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getStateUpdate is a function that performs a mock operation to get the state update.
//
// Parameters:
// - result: an interface{} that represents the result of the state update.
// - method: a string that specifies the method used to retrieve the state update.
// - args: a variadic parameter that can accept multiple arguments.
// Returns:
// - error: an error if any
func mock_starknet_getStateUpdate(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 1 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
_, ok = args[0].(BlockID)
if !ok {
fmt.Printf("args[1] should be *blockID, got %T\n", args[0])
return errWrongArgs
}
stateFeltArr, err := utils.HexArrToFelt([]string{
"0x4f1cee281edb6cb31b9ba5a8530694b5527cf05c5ac6502decf3acb1d0cec4",
"0x70677cda9269d47da3ff63bc87cf1c87d0ce167b05da295dc7fc68242b250b",
"0x19aa982a75263d4c4de4cc4c5d75c3dec32e00b95bef7bbb4d17762a0b138af",
"0xe5cc6f2b6d34979184b88334eb64173fe4300cab46ecd3229633fcc45c83d4",
"0x1813aac5f5e7799684c6dc33e51f44d3627fd748c800724a184ed5be09b713e",
"0x1813aac5f5e7799684c6dc33e51f44d3627fd748c800724a184ed5be09b713e",
"0x630b4197"})
if err != nil {
return err
}
output := StateUpdateOutput{
BlockHash: stateFeltArr[0],
NewRoot: stateFeltArr[1],
PendingStateUpdate: PendingStateUpdate{
OldRoot: stateFeltArr[2],
StateDiff: StateDiff{
StorageDiffs: []ContractStorageDiffItem{{
Address: stateFeltArr[3],
StorageEntries: []StorageEntry{
{
Key: stateFeltArr[4],
Value: stateFeltArr[5],
},
},
}},
},
},
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getNonce is a function that retrieves the nonce for a given method and arguments.
//
// Parameters:
// - result: a pointer to the variable where the result will be stored
// - method: the method for which the nonce is being retrieved
// - args: optional arguments for the method
// Returns:
// - error: an error if
// - The result parameter is not of type *json.RawMessage
// - The number of arguments is not equal to 2
// - The first argument is not of type BlockID
// - The second argument is not of type *felt.Felt
func mock_starknet_getNonce(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok {
return errWrongType
}
if len(args) != 2 {
fmt.Printf("args: %d\n", len(args))
return errWrongArgs
}
if _, ok := args[0].(BlockID); !ok {
fmt.Printf("args[0] should be BlockID, got %T\n", args[0])
return errWrongArgs
}
if _, ok := args[1].(*felt.Felt); !ok {
fmt.Printf("args[0] should be *felt.Felt, got %T\n", args[1])
return errWrongArgs
}
output, err := utils.HexToFelt("0x0")
if err != nil {
return err
}
outputContent, err := json.Marshal(output)
if err != nil {
return err
}
json.Unmarshal(outputContent, r)
return nil
}
// mock_starknet_getBlockWithTxHashes mocks the behavior of the starknet_getBlockWithTxHashes function.
// If successful, it populates the result parameter with the json.RawMessage containing the block with the specified transaction hashes.
//
// Parameters:
// - result: the result is expected to be a pointer to json.RawMessage
// - method: the method to be called
// - args: variadic parameter that can contain any number of arguments
// Returns:
// - error: an error if any
func mock_starknet_getBlockWithTxHashes(result interface{}, method string, args ...interface{}) error {
r, ok := result.(*json.RawMessage)
if !ok || r == nil {
return errWrongType
}
if len(args) != 1 {
return errWrongArgs
}
blockId, ok := args[0].(BlockID)
if !ok {
fmt.Printf("args[0] should be BlockID, got %T\n", args[0])
return errWrongArgs
}
txHashes, err := utils.HexArrToFelt([]string{
"0x40c82f79dd2bc1953fc9b347a3e7ab40fe218ed5740bf4e120f74e8a3c9ac99",
"0x28981b14353a28bc46758dff412ac544d16f2ffc8dde31867855592ea054ab1",
})
if err != nil {
return err
}
if blockId.Tag == "pending" {
pBlock, err := json.Marshal(
PendingBlockTxHashes{
PendingBlockHeader{
ParentHash: &felt.Zero,
Timestamp: 123,
SequencerAddress: &felt.Zero},
txHashes,
})
if err != nil {
return err
}
err = json.Unmarshal(pBlock, &r)
if err != nil {
return err
}
} else {
blockHash, err := utils.HexToFelt("0xbeef")
if err != nil {
return err
}
block, err := json.Marshal(
BlockTxHashes{
BlockHeader: BlockHeader{
BlockHash: blockHash,
ParentHash: &felt.Zero,
Timestamp: 124,
SequencerAddress: &felt.Zero,
L1DAMode: L1DAModeBlob,
L1GasPrice: ResourcePrice{
PriceInWei: new(felt.Felt).SetUint64(1),
PriceInFRI: new(felt.Felt).SetUint64(1),
},
L1DataGasPrice: ResourcePrice{
PriceInWei: new(felt.Felt).SetUint64(1),
PriceInFRI: new(felt.Felt).SetUint64(1),
},
},
Status: BlockStatus_AcceptedOnL1,
Transactions: txHashes,
})
if err != nil {