tealrb 0.8.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2480b69400b8916b30ebd58c19b804190a6390780ad335006ac0364ea9d4ad4e
4
- data.tar.gz: 6cf7f7f79217b1938f71bf07348923a7d31d3d83d76a4219f43e58bb1db2e3bc
3
+ metadata.gz: 96c56f8bbc18ac1b7fa42f8ad6b553f72296c30a89973d1e95a663f6776c559c
4
+ data.tar.gz: c7cd2a73fa00da112e0d420224955dc5a9b1a41a0ec23978250f48ac49a2fc59
5
5
  SHA512:
6
- metadata.gz: c8220e4aadbdcbe9a992ada723431281665edda6a19c07771978b5a8b4ae9cf987a208ed69e9c96f4360a6a3ace9762dc930e4bab99574b2114effc0ffd1060e
7
- data.tar.gz: 47790d4ee2171f4c2e95473f7c7cc6674d9634804c8c0dac2eeafff368df073166d9dfd371efc24026e1c94f4c333b1904af9fa4a959992116df7859dc314875
6
+ metadata.gz: b93a01c924cefb65075160e306436b54a560f2ac5a40465a28dac436a589b252f71d2130dfe39f0f4cf127136223392a0fab9f013acc01c5c4161099732b8bfe
7
+ data.tar.gz: 1316ccad828bf8a1682f55987dbffaa9dee942f3e11dfe04d54c31432143732b93de31d6c4bed596509a92e3744a667716acc4cae8b326793efd263d6c1e9354
@@ -4,9 +4,9 @@ module TEALrb
4
4
  class Contract
5
5
  include TEALrb
6
6
  include Opcodes
7
+ include Opcodes::AllOpcodes
7
8
  include ABI
8
9
  include Rewriters
9
- include MaybeOps
10
10
 
11
11
  attr_reader :teal
12
12
 
@@ -90,6 +90,35 @@ module TEALrb
90
90
  end
91
91
  end
92
92
 
93
+ VOID_OPS = %w[assert err return app_global_put b bnz bz store
94
+ stores app_local_put app_global_del app_local_del callsub retsub
95
+ log itxn_submit itxn_next].freeze
96
+
97
+ def teal_source
98
+ teal_lines = []
99
+
100
+ @teal.each_with_index do |line, i|
101
+ ln = line.strip
102
+
103
+ teal_lines << '' if VOID_OPS.include? @teal[i - 1][/\S+/]
104
+
105
+ if (ln[%r{\S+:($| //)}] && !ln[/^if\d+/]) || ln == 'b main' || ln[/^#/]
106
+ teal_lines << ln
107
+ elsif ln == 'retsub'
108
+ teal_lines << " #{ln}"
109
+ teal_lines << ''
110
+ else
111
+ teal_lines << " #{ln}"
112
+ end
113
+ end
114
+
115
+ teal_lines.delete_if.with_index do |t, i|
116
+ t.empty? && teal_lines[i - 1].empty?
117
+ end
118
+
119
+ teal_lines.join("\n")
120
+ end
121
+
93
122
  # return the input without transpiling to TEAL
94
123
  def rb(input)
95
124
  input
@@ -143,6 +172,7 @@ module TEALrb
143
172
  last_line = TEAL.instance.pop
144
173
  TEAL.instance << "#{last_line} //#{content}"
145
174
  else
175
+ TEAL.instance << '' unless TEAL.instance.last[%r{^//}]
146
176
  TEAL.instance << "//#{content}"
147
177
  end
148
178
  end
@@ -178,7 +208,7 @@ module TEALrb
178
208
 
179
209
  def route_abi_methods
180
210
  self.class.abi_description.methods.each do |meth|
181
- signature = "#{meth[:name]}(#{meth[:args].map{ _1[:type]}.join(',')})#{meth[:returns][:type]}"
211
+ signature = "#{meth[:name]}(#{meth[:args].map { _1[:type] }.join(',')})#{meth[:returns][:type]}"
182
212
  selector = OpenSSL::Digest.new('SHA512-256').hexdigest(signature)[..7]
183
213
 
184
214
  IfBlock.new(AppArgs[0] == byte(selector)) do
@@ -223,7 +253,8 @@ module TEALrb
223
253
  puts ''
224
254
  end
225
255
 
226
- [CommentRewriter, ComparisonRewriter, WhileRewriter, IfRewriter, OpRewriter, AssignRewriter].each do |rw|
256
+ [CommentRewriter, ComparisonRewriter, WhileRewriter, InlineIfRewriter, IfRewriter, OpRewriter,
257
+ AssignRewriter].each do |rw|
227
258
  string = rewrite_with_rewriter(string, rw)
228
259
  end
229
260
 
@@ -257,7 +288,7 @@ module TEALrb
257
288
  rescue SyntaxError, StandardError => e
258
289
  @eval_tealrb_rescue_count ||= 0
259
290
 
260
- eval_locations = e.backtrace.select {_1[/\(eval\)/]}
291
+ eval_locations = e.backtrace.select { _1[/\(eval\)/] }
261
292
  error_line = eval_locations[@eval_tealrb_rescue_count].split(':')[1].to_i
262
293
 
263
294
  warn "'#{e}' when evaluating transpiled TEALrb source" if @eval_tealrb_rescue_count.zero?
@@ -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
 
@@ -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,48 +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
- app_params_get field, app_id
607
- end
608
- end
609
-
610
- module Asset
611
- extend Opcodes
612
- extend AssetFields
613
-
614
- def self.opcode(field, asset = nil)
615
- asset_params_get field, asset
616
- end
617
- end
618
-
619
- module Account
620
- extend Opcodes
621
- extend AccountFields
622
-
623
- def self.opcode(field, account = nil)
624
- acct_params_get field, account
625
- end
626
- end
627
-
628
469
  module Txn
629
- extend Opcodes
630
470
  extend TxnFields
631
471
 
632
472
  def self.opcode(field)
633
- txn field
473
+ ExtendedOpcodes.txn field
634
474
  end
635
475
  end
636
476
 
637
477
  module Gtxn
638
478
  extend TxnFields
639
- extend Opcodes
640
479
 
641
480
  def self.opcode(field, index)
642
- gtxn index, field
481
+ ExtendedOpcodes.gtxn index, field
643
482
  end
644
483
 
645
484
  def self.[](index)
@@ -647,67 +486,325 @@ module TEALrb
647
486
  end
648
487
  end
649
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
+
650
501
  class GroupTransaction
651
502
  include TxnFields
652
- include Opcodes
653
503
 
654
504
  def initialize(index)
655
505
  @index = index
656
506
  end
657
507
 
658
508
  def opcode(field)
659
- gtxn @index, field
509
+ ExtendedOpcodes.gtxn @index, field
660
510
  end
661
511
  end
662
512
 
663
- class TxnaField
664
- 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
519
+
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
665
525
 
666
- def initialize(field)
667
- @field = field
526
+ def self.[](index)
527
+ new[index]
668
528
  end
669
529
 
670
530
  def [](index)
671
- 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'
672
770
  end
673
771
  end
674
772
 
675
773
  module Txna
676
774
  def self.application_args
677
- TxnaField.new('ApplicationArgs')
775
+ ApplicationArgs.new
678
776
  end
679
777
 
680
778
  def self.accounts
681
- TxnaField.new('Accounts')
779
+ Accounts.new
682
780
  end
683
781
 
684
782
  def self.assets
685
- TxnaField.new('Assets')
783
+ Assets.new
686
784
  end
687
785
 
688
786
  def self.applications
689
- TxnaField.new('Applications')
787
+ Apps.new
690
788
  end
691
789
 
692
790
  def self.logs
693
- TxnaField.new('Logs')
791
+ Logs.new
694
792
  end
695
793
  end
696
794
 
697
795
  module Global
698
- extend Opcodes
699
796
  extend GlobalFields
700
797
 
701
798
  def self.opcode(field)
702
- global field
799
+ ExtendedOpcodes.global field
703
800
  end
704
801
 
705
802
  def self.[](key)
706
- app_global_get key
803
+ ExtendedOpcodes.app_global_get key
707
804
  end
708
805
 
709
806
  def self.[]=(key, value)
710
- app_global_put key, value
807
+ ExtendedOpcodes.app_global_put key, value
711
808
  end
712
809
  end
713
810
 
@@ -718,8 +815,6 @@ module TEALrb
718
815
  end
719
816
 
720
817
  module MaybeOps
721
- include Opcodes
722
-
723
818
  def app_param_exists?(field, _app_id = nil)
724
819
  app_params_get field
725
820
  swap
@@ -742,6 +837,17 @@ module TEALrb
742
837
  pop
743
838
  end
744
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
+
745
851
  def app_local_ex_exists?(_account = nil, _applicaiton = nil, _key = nil)
746
852
  app_local_get_ex
747
853
  swap
@@ -764,5 +870,14 @@ module TEALrb
764
870
  pop
765
871
  end
766
872
  end
873
+
874
+ module AllOpcodes
875
+ include TEALOpcodes
876
+ include MaybeOps
877
+ end
878
+
879
+ module ExtendedOpcodes
880
+ extend AllOpcodes
881
+ end
767
882
  end
768
883
  end