tealrb 0.6.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,24 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TEALrb
4
- module ExtendedOpcodes
5
- extend Opcodes
6
- end
7
-
8
4
  module Opcodes
9
5
  class AccountLocal
10
- include TEALrb::Opcodes
11
-
12
6
  def initialize(account)
13
7
  @account = account
14
8
  end
15
9
 
16
10
  def [](key)
17
- app_local_get @account, key
11
+ ExtendedOpcodes.app_local_get @account, key
18
12
  end
19
13
 
20
14
  def []=(key, value)
21
- app_local_put @account, key, value
15
+ ExtendedOpcodes.app_local_put @account, key, value
22
16
  end
23
17
  end
24
18
 
@@ -33,7 +27,7 @@ module TEALrb
33
27
  private
34
28
 
35
29
  def txn_type_int(type)
36
- TEALrb::TEAL.current[Thread.current] << "int #{type}"
30
+ TEAL.instance << "int #{type}"
37
31
  end
38
32
  end
39
33
 
@@ -393,132 +387,6 @@ module TEALrb
393
387
  end
394
388
  end
395
389
 
396
- module AppFields
397
- # @return [[]byte] Bytecode of Approval Program
398
- def app_approval_program(*args)
399
- opcode('AppApprovalProgram', *args)
400
- end
401
-
402
- # @return [[]byte] Bytecode of Clear State Program
403
- def app_clear_state_program(*args)
404
- opcode('AppClearStateProgram', *args)
405
- end
406
-
407
- # @return [uint64] Number of uint64 values allowed in Global State
408
- def app_global_num_uint(*args)
409
- opcode('AppGlobalNumUint', *args)
410
- end
411
-
412
- # @return [uint64] Number of byte array values allowed in Global State
413
- def app_global_num_byte_slice(*args)
414
- opcode('AppGlobalNumByteSlice', *args)
415
- end
416
-
417
- # @return [uint64] Number of uint64 values allowed in Local State
418
- def app_local_num_uint(*args)
419
- opcode('AppLocalNumUint', *args)
420
- end
421
-
422
- # @return [uint64] Number of byte array values allowed in Local State
423
- def app_local_num_byte_slice(*args)
424
- opcode('AppLocalNumByteSlice', *args)
425
- end
426
-
427
- # @return [uint64] Number of Extra Program Pages of code space
428
- def app_extra_program_pages(*args)
429
- opcode('AppExtraProgramPages', *args)
430
- end
431
-
432
- # @return [[]byte] Creator address
433
- def app_creator(*args)
434
- opcode('AppCreator', *args)
435
- end
436
-
437
- # @return [[]byte] Address for which this application has authority
438
- def app_address(*args)
439
- opcode('AppAddress', *args)
440
- end
441
- end
442
-
443
- module AssetFields
444
- # @return [uint64] Total number of units of this asset (v1)
445
- def asset_total(*args)
446
- opcode('AssetTotal', *args)
447
- end
448
-
449
- # @return [uint64] See AssetParams.Decimals (v1)
450
- def asset_decimals(*args)
451
- opcode('AssetDecimals', *args)
452
- end
453
-
454
- # @return [uint64] Frozen by default or not (v1)
455
- def asset_default_frozen(*args)
456
- opcode('AssetDefaultFrozen', *args)
457
- end
458
-
459
- # @return [[]byte] Asset unit name (v1)
460
- def asset_unit_name(*args)
461
- opcode('AssetUnitName', *args)
462
- end
463
-
464
- # @return [[]byte] Asset name (v1)
465
- def asset_name(*args)
466
- opcode('AssetName', *args)
467
- end
468
-
469
- # @return [[]byte] URL with additional info about the asset (v1)
470
- def asset_url(*args)
471
- opcode('AssetURL', *args)
472
- end
473
-
474
- # @return [[]byte] Arbitrary commitment (v1)
475
- def asset_metadata_hash(*args)
476
- opcode('AssetMetadataHash', *args)
477
- end
478
-
479
- # @return [[]byte] Manager commitment (v1)
480
- def asset_manager(*args)
481
- opcode('AssetManager', *args)
482
- end
483
-
484
- # @return [[]byte] Reserve address (v1)
485
- def asset_reserve(*args)
486
- opcode('AssetReserve', *args)
487
- end
488
-
489
- # @return [[]byte] Freeze address (v1)
490
- def asset_freeze(*args)
491
- opcode('AssetFreeze', *args)
492
- end
493
-
494
- # @return [[]byte] Clawback address (v1)
495
- def asset_clawback(*args)
496
- opcode('AssetClawback', *args)
497
- end
498
-
499
- # @return [[]byte] Creator address (v5)
500
- def asset_creator(*args)
501
- opcode('AssetCreator', *args)
502
- end
503
- end
504
-
505
- module AccountFields
506
- # @return [uint64] Account balance in microalgos
507
- def acct_balance(*args)
508
- opcode('AcctBalance', *args)
509
- end
510
-
511
- # @return [uint64] in microalgos (Minimum required blance for account)
512
- def acct_min_balance(*args)
513
- opcode('AcctMinBalance', *args)
514
- end
515
-
516
- # @return [[]byte] Address the account is rekeyed to.
517
- def acct_auth_addr(*args)
518
- opcode('AcctAuthAddr', *args)
519
- end
520
- end
521
-
522
390
  module GlobalFields
523
391
  # @return [uint64] microalgos (v1)
524
392
  def min_txn_fee(*args)
@@ -598,52 +466,19 @@ module TEALrb
598
466
  end
599
467
  end
600
468
 
601
- module App
602
- extend Opcodes
603
- extend AppFields
604
-
605
- def self.opcode(field, app_id = nil)
606
- @teal = TEALrb::TEAL.current[Thread.current]
607
- app_params_get field, app_id
608
- end
609
- end
610
-
611
- module Asset
612
- extend Opcodes
613
- extend AssetFields
614
-
615
- def self.opcode(field, asset = nil)
616
- @teal = TEALrb::TEAL.current[Thread.current]
617
- asset_params_get field, asset
618
- end
619
- end
620
-
621
- module Account
622
- extend Opcodes
623
- extend AccountFields
624
-
625
- def self.opcode(field, account = nil)
626
- @teal = TEALrb::TEAL.current[Thread.current]
627
- acct_params_get field, account
628
- end
629
- end
630
-
631
469
  module Txn
632
- extend Opcodes
633
470
  extend TxnFields
634
471
 
635
472
  def self.opcode(field)
636
- txn field
473
+ ExtendedOpcodes.txn field
637
474
  end
638
475
  end
639
476
 
640
477
  module Gtxn
641
478
  extend TxnFields
642
- extend Opcodes
643
479
 
644
480
  def self.opcode(field, index)
645
- @teal = TEALrb::TEAL.current[Thread.current]
646
- gtxn index, field
481
+ ExtendedOpcodes.gtxn index, field
647
482
  end
648
483
 
649
484
  def self.[](index)
@@ -651,69 +486,325 @@ module TEALrb
651
486
  end
652
487
  end
653
488
 
489
+ module Gtxns
490
+ extend TxnFields
491
+
492
+ def self.opcode(field)
493
+ ExtendedOpcodes.gtxns field
494
+ end
495
+
496
+ def self.[](_index)
497
+ self
498
+ end
499
+ end
500
+
654
501
  class GroupTransaction
655
502
  include TxnFields
656
- include Opcodes
657
503
 
658
504
  def initialize(index)
659
505
  @index = index
660
506
  end
661
507
 
662
508
  def opcode(field)
663
- gtxn @index, field
509
+ ExtendedOpcodes.gtxn @index, field
664
510
  end
665
511
  end
666
512
 
667
- class TxnaField
668
- include Opcodes
513
+ class TxnArray
514
+ TEALrb::Opcodes::BINARY_OPCODE_METHOD_MAPPING.each do |meth, opcode|
515
+ define_method(meth) do |other|
516
+ ExtendedOpcodes.send(opcode, self, other)
517
+ end
518
+ end
669
519
 
670
- def initialize(field)
671
- @field = field
520
+ TEALrb::Opcodes::UNARY_OPCODE_METHOD_MAPPING.each do |meth, opcode|
521
+ define_method(meth) do
522
+ ExtendedOpcodes.send(opcode, self)
523
+ end
524
+ end
525
+
526
+ def self.[](index)
527
+ new[index]
672
528
  end
673
529
 
674
530
  def [](index)
675
- @teal = TEALrb::TEAL.current[Thread.current]
676
- txna @field, index
531
+ ExtendedOpcodes.txna @field, index
532
+ self
533
+ end
534
+ end
535
+
536
+ class ApplicationArgs < TxnArray
537
+ def initialize
538
+ super
539
+ @field = 'ApplicationArgs'
540
+ end
541
+ end
542
+
543
+ class Logs < TxnArray
544
+ def initialize
545
+ super
546
+ @field = 'Logs'
547
+ end
548
+ end
549
+
550
+ class Accounts < TxnArray
551
+ def initialize
552
+ super
553
+ @field = 'Accounts'
554
+ end
555
+
556
+ def min_balance
557
+ ExtendedOpcodes.acct_param_value 'AcctMinBalance'
558
+ end
559
+
560
+ def balance
561
+ ExtendedOpcodes.acct_param_value 'AcctBalance'
562
+ end
563
+
564
+ def auth_addr
565
+ ExtendedOpcodes.acct_param_value 'AcctAuthAddr'
566
+ end
567
+
568
+ def balance?
569
+ ExtendedOpcodes.acct_has_balance?
570
+ end
571
+ end
572
+
573
+ class Apps < TxnArray
574
+ def initialize
575
+ super
576
+ @field = 'Applications'
577
+ end
578
+
579
+ # @return [[]byte] Bytecode of Approval Program
580
+ def approval_program(*_args)
581
+ ExtendedOpcodes.app_param_value 'AppApprovalProgram'
582
+ end
583
+
584
+ # @return [[]byte] Bytecode of Clear State Program
585
+ def clear_state_program(*_args)
586
+ ExtendedOpcodes.app_param_value 'AppClearStateProgram'
587
+ end
588
+
589
+ # @return [uint64] Number of uint64 values allowed in Global State
590
+ def global_num_uint(*_args)
591
+ ExtendedOpcodes.app_param_value 'AppGlobalNumUint'
592
+ end
593
+
594
+ # @return [uint64] Number of byte array values allowed in Global State
595
+ def global_num_byte_slice(*_args)
596
+ ExtendedOpcodes.app_param_value 'AppGlobalNumByteSlice'
597
+ end
598
+
599
+ # @return [uint64] Number of uint64 values allowed in Local State
600
+ def local_num_uint(*_args)
601
+ ExtendedOpcodes.app_param_value 'AppLocalNumUint'
602
+ end
603
+
604
+ # @return [uint64] Number of byte array values allowed in Local State
605
+ def local_num_byte_slice(*_args)
606
+ ExtendedOpcodes.app_param_value 'AppLocalNumByteSlice'
607
+ end
608
+
609
+ # @return [uint64] Number of Extra Program Pages of code space
610
+ def extra_program_pages(*_args)
611
+ ExtendedOpcodes.app_param_value 'AppExtraProgramPages'
612
+ end
613
+
614
+ # @return [[]byte] Creator address
615
+ def creator(*_args)
616
+ ExtendedOpcodes.app_param_value 'AppCreator'
617
+ end
618
+
619
+ # @return [[]byte] Address for which this application has authority
620
+ def address(*_args)
621
+ ExtendedOpcodes.app_param_value 'AppAddress'
622
+ end
623
+
624
+ # @return [[]byte] Bytecode of Approval Program
625
+ def approval_program?(*_args)
626
+ ExtendedOpcodes.app_param_exists? 'AppApprovalProgram'
627
+ end
628
+
629
+ # @return [[]byte] Bytecode of Clear State Program
630
+ def clear_state_program?(*_args)
631
+ ExtendedOpcodes.app_param_exists? 'AppClearStateProgram'
632
+ end
633
+
634
+ # @return [uint64] Number of uint64 values allowed in Global State
635
+ def global_num_uint?(*_args)
636
+ ExtendedOpcodes.app_param_exists? 'AppGlobalNumUint'
637
+ end
638
+
639
+ # @return [uint64] Number of byte array values allowed in Global State
640
+ def global_num_byte_slice?(*_args)
641
+ ExtendedOpcodes.app_param_exists? 'AppGlobalNumByteSlice'
642
+ end
643
+
644
+ # @return [uint64] Number of uint64 values allowed in Local State
645
+ def local_num_uint?(*_args)
646
+ ExtendedOpcodes.app_param_exists? 'AppLocalNumUint'
647
+ end
648
+
649
+ # @return [uint64] Number of byte array values allowed in Local State
650
+ def local_num_byte_slice?(*_args)
651
+ ExtendedOpcodes.app_param_exists? 'AppLocalNumByteSlice'
652
+ end
653
+
654
+ # @return [uint64] Number of Extra Program Pages of code space
655
+ def extra_program_pages?(*_args)
656
+ ExtendedOpcodes.app_param_exists? 'AppExtraProgramPages'
657
+ end
658
+
659
+ # @return [[]byte] Creator address
660
+ def creator?(*_args)
661
+ ExtendedOpcodes.app_param_exists? 'AppCreator'
662
+ end
663
+
664
+ # @return [[]byte] Address for which this application has authority
665
+ def address?(*_args)
666
+ ExtendedOpcodes.app_param_exists? 'AppAddress'
667
+ end
668
+ end
669
+
670
+ class Assets < TxnArray
671
+ def initialize
672
+ super
673
+ @field = 'Assets'
674
+ end
675
+
676
+ def total
677
+ ExtendedOpcodes.asset_param_value 'AssetTotal'
678
+ end
679
+
680
+ def decimals
681
+ ExtendedOpcodes.asset_param_value 'AssetDecimals'
682
+ end
683
+
684
+ def default_frozen
685
+ ExtendedOpcodes.asset_param_value 'AssetDefaultFrozen'
686
+ end
687
+
688
+ def name
689
+ ExtendedOpcodes.asset_param_value 'AssetName'
690
+ end
691
+
692
+ def unit_name
693
+ ExtendedOpcodes.asset_param_value 'AssetUnitName'
694
+ end
695
+
696
+ def url
697
+ ExtendedOpcodes.asset_param_value 'AssetURL'
698
+ end
699
+
700
+ def metadata_hash
701
+ ExtendedOpcodes.asset_param_value 'AssetMetadataHash'
702
+ end
703
+
704
+ def manager
705
+ ExtendedOpcodes.asset_param_value 'AssetManager'
706
+ end
707
+
708
+ def reserve
709
+ ExtendedOpcodes.asset_param_value 'AssetReserve'
710
+ end
711
+
712
+ def freeze
713
+ ExtendedOpcodes.asset_param_value 'AssetFreeze'
714
+ end
715
+
716
+ def clawback
717
+ ExtendedOpcodes.asset_param_value 'AssetClawback'
718
+ end
719
+
720
+ def creator
721
+ ExtendedOpcodes.asset_param_value 'AssetCreator'
722
+ end
723
+
724
+ def total?
725
+ ExtendedOpcodes.asset_param_exists? 'AssetTotal'
726
+ end
727
+
728
+ def decimals?
729
+ ExtendedOpcodes.asset_param_exists? 'AssetDecimals'
730
+ end
731
+
732
+ def default_frozen?
733
+ ExtendedOpcodes.asset_param_exists? 'AssetDefaultFrozen'
734
+ end
735
+
736
+ def name?
737
+ ExtendedOpcodes.asset_param_exists? 'AssetName'
738
+ end
739
+
740
+ def unit_name?
741
+ ExtendedOpcodes.asset_param_exists? 'AssetUnitName'
742
+ end
743
+
744
+ def url?
745
+ ExtendedOpcodes.asset_param_exists? 'AssetURL'
746
+ end
747
+
748
+ def metadata_hash?
749
+ ExtendedOpcodes.asset_param_exists? 'AssetMetadataHash'
750
+ end
751
+
752
+ def manager?
753
+ ExtendedOpcodes.asset_param_exists? 'AssetManager'
754
+ end
755
+
756
+ def reserve?
757
+ ExtendedOpcodes.asset_param_exists? 'AssetReserve'
758
+ end
759
+
760
+ def freeze?
761
+ ExtendedOpcodes.asset_param_exists? 'AssetFreeze'
762
+ end
763
+
764
+ def clawback?
765
+ ExtendedOpcodes.asset_param_exists? 'AssetClawback'
766
+ end
767
+
768
+ def creator?
769
+ ExtendedOpcodes.asset_param_exists? 'AssetCreator'
677
770
  end
678
771
  end
679
772
 
680
773
  module Txna
681
774
  def self.application_args
682
- TxnaField.new('ApplicationArgs')
775
+ ApplicationArgs.new
683
776
  end
684
777
 
685
778
  def self.accounts
686
- TxnaField.new('Accounts')
779
+ Accounts.new
687
780
  end
688
781
 
689
782
  def self.assets
690
- TxnaField.new('Assets')
783
+ Assets.new
691
784
  end
692
785
 
693
786
  def self.applications
694
- TxnaField.new('Applications')
787
+ Apps.new
695
788
  end
696
789
 
697
790
  def self.logs
698
- TxnaField.new('Logs')
791
+ Logs.new
699
792
  end
700
793
  end
701
794
 
702
795
  module Global
703
- extend Opcodes
704
796
  extend GlobalFields
705
797
 
706
798
  def self.opcode(field)
707
- @teal = TEALrb::TEAL.current[Thread.current]
708
- global field
799
+ ExtendedOpcodes.global field
709
800
  end
710
801
 
711
802
  def self.[](key)
712
- app_global_get key
803
+ ExtendedOpcodes.app_global_get key
713
804
  end
714
805
 
715
806
  def self.[]=(key, value)
716
- app_global_put key, value
807
+ ExtendedOpcodes.app_global_put key, value
717
808
  end
718
809
  end
719
810
 
@@ -722,5 +813,71 @@ module TEALrb
722
813
  Txna.application_args[index]
723
814
  end
724
815
  end
816
+
817
+ module MaybeOps
818
+ def app_param_exists?(field, _app_id = nil)
819
+ app_params_get field
820
+ swap
821
+ pop
822
+ end
823
+
824
+ def app_param_value(field, _app_id = nil)
825
+ app_params_get field
826
+ pop
827
+ end
828
+
829
+ def asset_param_exists?(field, _asset_id = nil)
830
+ asset_params_get field
831
+ swap
832
+ pop
833
+ end
834
+
835
+ def asset_param_value(field, _asset_id = nil)
836
+ asset_params_get field
837
+ pop
838
+ end
839
+
840
+ def acct_has_balance?(_asset_id = nil)
841
+ acct_params_get 'AcctBalance'
842
+ swap
843
+ pop
844
+ end
845
+
846
+ def acct_param_value(field, _asset_id = nil)
847
+ acct_params_get field
848
+ pop
849
+ end
850
+
851
+ def app_local_ex_exists?(_account = nil, _applicaiton = nil, _key = nil)
852
+ app_local_get_ex
853
+ swap
854
+ pop
855
+ end
856
+
857
+ def app_local_ex_value(_account = nil, _applicaiton = nil, _key = nil)
858
+ app_local_get_ex
859
+ pop
860
+ end
861
+
862
+ def app_global_ex_exists?(_account = nil, _applicaiton = nil, _key = nil)
863
+ app_global_get_ex
864
+ swap
865
+ pop
866
+ end
867
+
868
+ def app_global_ex_value(_account = nil, _applicaiton = nil, _key = nil)
869
+ app_global_get_ex
870
+ pop
871
+ end
872
+ end
873
+
874
+ module AllOpcodes
875
+ include TEALOpcodes
876
+ include MaybeOps
877
+ end
878
+
879
+ module ExtendedOpcodes
880
+ extend AllOpcodes
881
+ end
725
882
  end
726
883
  end