stellar-base 0.23.1 → 0.27.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +134 -29
  3. data/README.md +7 -7
  4. data/generated/stellar-base-generated.rb +44 -1
  5. data/generated/stellar/account_entry.rb +3 -13
  6. data/generated/stellar/account_entry/ext.rb +2 -16
  7. data/generated/stellar/account_entry_extension_v1.rb +32 -0
  8. data/generated/stellar/account_entry_extension_v1/ext.rb +28 -0
  9. data/generated/stellar/account_entry_extension_v2.rb +34 -0
  10. data/generated/stellar/{account_entry/ext/v1 → account_entry_extension_v2}/ext.rb +8 -12
  11. data/generated/stellar/account_flags.rb +9 -4
  12. data/generated/stellar/account_merge_result.rb +1 -1
  13. data/generated/stellar/account_merge_result_code.rb +3 -1
  14. data/generated/stellar/allow_trust_op.rb +3 -18
  15. data/generated/stellar/asset_code.rb +30 -0
  16. data/generated/stellar/begin_sponsoring_future_reserves_op.rb +18 -0
  17. data/generated/stellar/begin_sponsoring_future_reserves_result.rb +26 -0
  18. data/generated/stellar/begin_sponsoring_future_reserves_result_code.rb +29 -0
  19. data/generated/stellar/claim_claimable_balance_op.rb +18 -0
  20. data/generated/stellar/claim_claimable_balance_result.rb +25 -0
  21. data/generated/stellar/claim_claimable_balance_result_code.rb +31 -0
  22. data/generated/stellar/claim_predicate.rb +43 -0
  23. data/generated/stellar/claim_predicate_type.rb +30 -0
  24. data/generated/stellar/claimable_balance_entry.rb +46 -0
  25. data/generated/stellar/claimable_balance_entry/ext.rb +28 -0
  26. data/generated/stellar/claimable_balance_entry_extension_v1.rb +30 -0
  27. data/generated/stellar/claimable_balance_entry_extension_v1/ext.rb +24 -0
  28. data/generated/stellar/claimable_balance_flags.rb +22 -0
  29. data/generated/stellar/claimable_balance_id.rb +23 -0
  30. data/generated/stellar/claimable_balance_id_type.rb +20 -0
  31. data/generated/stellar/claimant.rb +31 -0
  32. data/generated/stellar/claimant/v0.rb +22 -0
  33. data/generated/stellar/claimant_type.rb +20 -0
  34. data/generated/stellar/clawback_claimable_balance_op.rb +18 -0
  35. data/generated/stellar/clawback_claimable_balance_result.rb +26 -0
  36. data/generated/stellar/clawback_claimable_balance_result_code.rb +29 -0
  37. data/generated/stellar/clawback_op.rb +22 -0
  38. data/generated/stellar/clawback_result.rb +25 -0
  39. data/generated/stellar/clawback_result_code.rb +31 -0
  40. data/generated/stellar/create_claimable_balance_op.rb +22 -0
  41. data/generated/stellar/create_claimable_balance_result.rb +27 -0
  42. data/generated/stellar/create_claimable_balance_result_code.rb +30 -0
  43. data/generated/stellar/create_passive_sell_offer_op.rb +1 -1
  44. data/generated/stellar/end_sponsoring_future_reserves_result.rb +26 -0
  45. data/generated/stellar/end_sponsoring_future_reserves_result_code.rb +25 -0
  46. data/generated/stellar/envelope_type.rb +3 -1
  47. data/generated/stellar/inner_transaction_result.rb +2 -1
  48. data/generated/stellar/inner_transaction_result/result.rb +3 -1
  49. data/generated/stellar/ledger_entry.rb +4 -0
  50. data/generated/stellar/ledger_entry/data.rb +12 -8
  51. data/generated/stellar/ledger_entry/ext.rb +4 -0
  52. data/generated/stellar/ledger_entry_extension_v1.rb +30 -0
  53. data/generated/stellar/ledger_entry_extension_v1/ext.rb +24 -0
  54. data/generated/stellar/ledger_entry_type.rb +7 -5
  55. data/generated/stellar/ledger_key.rb +17 -8
  56. data/generated/stellar/ledger_key/claimable_balance.rb +20 -0
  57. data/generated/stellar/operation.rb +16 -0
  58. data/generated/stellar/operation/body.rb +57 -26
  59. data/generated/stellar/operation_id.rb +32 -0
  60. data/generated/stellar/operation_id/id.rb +24 -0
  61. data/generated/stellar/operation_result.rb +16 -0
  62. data/generated/stellar/operation_result/tr.rb +60 -28
  63. data/generated/stellar/operation_result_code.rb +3 -1
  64. data/generated/stellar/operation_type.rb +31 -15
  65. data/generated/stellar/path_payment_strict_receive_result.rb +2 -1
  66. data/generated/stellar/payment_result_code.rb +1 -1
  67. data/generated/stellar/revoke_sponsorship_op.rb +35 -0
  68. data/generated/stellar/revoke_sponsorship_op/signer.rb +22 -0
  69. data/generated/stellar/revoke_sponsorship_result.rb +25 -0
  70. data/generated/stellar/revoke_sponsorship_result_code.rb +31 -0
  71. data/generated/stellar/revoke_sponsorship_type.rb +22 -0
  72. data/generated/stellar/set_options_result_code.rb +14 -11
  73. data/generated/stellar/set_trust_line_flags_op.rb +25 -0
  74. data/generated/stellar/set_trust_line_flags_result.rb +25 -0
  75. data/generated/stellar/set_trust_line_flags_result_code.rb +31 -0
  76. data/generated/stellar/transaction_result_code.rb +5 -3
  77. data/generated/stellar/trust_line_flags.rb +5 -1
  78. data/lib/stellar-base.rb +18 -5
  79. data/lib/stellar/account_flags.rb +1 -1
  80. data/lib/stellar/asset.rb +10 -0
  81. data/lib/stellar/claim_predicate.rb +198 -0
  82. data/lib/stellar/compat.rb +4 -16
  83. data/lib/stellar/concerns/transaction.rb +5 -4
  84. data/lib/stellar/dsl.rb +93 -0
  85. data/lib/stellar/ext/xdr.rb +50 -0
  86. data/lib/stellar/key_pair.rb +60 -53
  87. data/lib/stellar/ledger_key.rb +32 -0
  88. data/lib/stellar/muxed_account.rb +16 -0
  89. data/lib/stellar/networks.rb +12 -12
  90. data/lib/stellar/operation.rb +144 -17
  91. data/lib/stellar/price.rb +11 -2
  92. data/lib/stellar/transaction.rb +1 -169
  93. data/lib/stellar/transaction_builder.rb +28 -2
  94. data/lib/stellar/transaction_envelope.rb +7 -43
  95. data/lib/stellar/transaction_v0.rb +2 -10
  96. data/lib/stellar/trust_line_flags.rb +53 -0
  97. data/lib/stellar/util/strkey.rb +6 -6
  98. data/lib/stellar/version.rb +1 -3
  99. metadata +177 -30
  100. data/generated/stellar/account_entry/ext/v1.rb +0 -34
  101. data/generated/stellar/allow_trust_op/asset.rb +0 -33
  102. data/lib/stellar/util/continued_fraction.rb +0 -96
@@ -0,0 +1,22 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # struct
9
+ # {
10
+ # AccountID accountID;
11
+ # SignerKey signerKey;
12
+ # }
13
+ #
14
+ # ===========================================================================
15
+ module Stellar
16
+ class RevokeSponsorshipOp
17
+ class Signer < XDR::Struct
18
+ attribute :account_id, AccountID
19
+ attribute :signer_key, SignerKey
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # union RevokeSponsorshipResult switch (RevokeSponsorshipResultCode code)
9
+ # {
10
+ # case REVOKE_SPONSORSHIP_SUCCESS:
11
+ # void;
12
+ # default:
13
+ # void;
14
+ # };
15
+ #
16
+ # ===========================================================================
17
+ module Stellar
18
+ class RevokeSponsorshipResult < XDR::Union
19
+ switch_on RevokeSponsorshipResultCode, :code
20
+
21
+ switch :revoke_sponsorship_success
22
+ switch :default
23
+
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # enum RevokeSponsorshipResultCode
9
+ # {
10
+ # // codes considered as "success" for the operation
11
+ # REVOKE_SPONSORSHIP_SUCCESS = 0,
12
+ #
13
+ # // codes considered as "failure" for the operation
14
+ # REVOKE_SPONSORSHIP_DOES_NOT_EXIST = -1,
15
+ # REVOKE_SPONSORSHIP_NOT_SPONSOR = -2,
16
+ # REVOKE_SPONSORSHIP_LOW_RESERVE = -3,
17
+ # REVOKE_SPONSORSHIP_ONLY_TRANSFERABLE = -4
18
+ # };
19
+ #
20
+ # ===========================================================================
21
+ module Stellar
22
+ class RevokeSponsorshipResultCode < XDR::Enum
23
+ member :revoke_sponsorship_success, 0
24
+ member :revoke_sponsorship_does_not_exist, -1
25
+ member :revoke_sponsorship_not_sponsor, -2
26
+ member :revoke_sponsorship_low_reserve, -3
27
+ member :revoke_sponsorship_only_transferable, -4
28
+
29
+ seal
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # enum RevokeSponsorshipType
9
+ # {
10
+ # REVOKE_SPONSORSHIP_LEDGER_ENTRY = 0,
11
+ # REVOKE_SPONSORSHIP_SIGNER = 1
12
+ # };
13
+ #
14
+ # ===========================================================================
15
+ module Stellar
16
+ class RevokeSponsorshipType < XDR::Enum
17
+ member :revoke_sponsorship_ledger_entry, 0
18
+ member :revoke_sponsorship_signer, 1
19
+
20
+ seal
21
+ end
22
+ end
@@ -18,22 +18,25 @@ require 'xdr'
18
18
  # SET_OPTIONS_UNKNOWN_FLAG = -6, // can't set an unknown flag
19
19
  # SET_OPTIONS_THRESHOLD_OUT_OF_RANGE = -7, // bad value for weight/threshold
20
20
  # SET_OPTIONS_BAD_SIGNER = -8, // signer cannot be masterkey
21
- # SET_OPTIONS_INVALID_HOME_DOMAIN = -9 // malformed home domain
21
+ # SET_OPTIONS_INVALID_HOME_DOMAIN = -9, // malformed home domain
22
+ # SET_OPTIONS_AUTH_REVOCABLE_REQUIRED =
23
+ # -10 // auth revocable is required for clawback
22
24
  # };
23
25
  #
24
26
  # ===========================================================================
25
27
  module Stellar
26
28
  class SetOptionsResultCode < XDR::Enum
27
- member :set_options_success, 0
28
- member :set_options_low_reserve, -1
29
- member :set_options_too_many_signers, -2
30
- member :set_options_bad_flags, -3
31
- member :set_options_invalid_inflation, -4
32
- member :set_options_cant_change, -5
33
- member :set_options_unknown_flag, -6
34
- member :set_options_threshold_out_of_range, -7
35
- member :set_options_bad_signer, -8
36
- member :set_options_invalid_home_domain, -9
29
+ member :set_options_success, 0
30
+ member :set_options_low_reserve, -1
31
+ member :set_options_too_many_signers, -2
32
+ member :set_options_bad_flags, -3
33
+ member :set_options_invalid_inflation, -4
34
+ member :set_options_cant_change, -5
35
+ member :set_options_unknown_flag, -6
36
+ member :set_options_threshold_out_of_range, -7
37
+ member :set_options_bad_signer, -8
38
+ member :set_options_invalid_home_domain, -9
39
+ member :set_options_auth_revocable_required, -10
37
40
 
38
41
  seal
39
42
  end
@@ -0,0 +1,25 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # struct SetTrustLineFlagsOp
9
+ # {
10
+ # AccountID trustor;
11
+ # Asset asset;
12
+ #
13
+ # uint32 clearFlags; // which flags to clear
14
+ # uint32 setFlags; // which flags to set
15
+ # };
16
+ #
17
+ # ===========================================================================
18
+ module Stellar
19
+ class SetTrustLineFlagsOp < XDR::Struct
20
+ attribute :trustor, AccountID
21
+ attribute :asset, Asset
22
+ attribute :clear_flags, Uint32
23
+ attribute :set_flags, Uint32
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # union SetTrustLineFlagsResult switch (SetTrustLineFlagsResultCode code)
9
+ # {
10
+ # case SET_TRUST_LINE_FLAGS_SUCCESS:
11
+ # void;
12
+ # default:
13
+ # void;
14
+ # };
15
+ #
16
+ # ===========================================================================
17
+ module Stellar
18
+ class SetTrustLineFlagsResult < XDR::Union
19
+ switch_on SetTrustLineFlagsResultCode, :code
20
+
21
+ switch :set_trust_line_flags_success
22
+ switch :default
23
+
24
+ end
25
+ end
@@ -0,0 +1,31 @@
1
+ # This code was automatically generated using xdrgen
2
+ # DO NOT EDIT or your changes may be overwritten
3
+
4
+ require 'xdr'
5
+
6
+ # === xdr source ============================================================
7
+ #
8
+ # enum SetTrustLineFlagsResultCode
9
+ # {
10
+ # // codes considered as "success" for the operation
11
+ # SET_TRUST_LINE_FLAGS_SUCCESS = 0,
12
+ #
13
+ # // codes considered as "failure" for the operation
14
+ # SET_TRUST_LINE_FLAGS_MALFORMED = -1,
15
+ # SET_TRUST_LINE_FLAGS_NO_TRUST_LINE = -2,
16
+ # SET_TRUST_LINE_FLAGS_CANT_REVOKE = -3,
17
+ # SET_TRUST_LINE_FLAGS_INVALID_STATE = -4
18
+ # };
19
+ #
20
+ # ===========================================================================
21
+ module Stellar
22
+ class SetTrustLineFlagsResultCode < XDR::Enum
23
+ member :set_trust_line_flags_success, 0
24
+ member :set_trust_line_flags_malformed, -1
25
+ member :set_trust_line_flags_no_trust_line, -2
26
+ member :set_trust_line_flags_cant_revoke, -3
27
+ member :set_trust_line_flags_invalid_state, -4
28
+
29
+ seal
30
+ end
31
+ end
@@ -22,10 +22,11 @@ require 'xdr'
22
22
  # txNO_ACCOUNT = -8, // source account not found
23
23
  # txINSUFFICIENT_FEE = -9, // fee is too small
24
24
  # txBAD_AUTH_EXTRA = -10, // unused signatures attached to transaction
25
- # txINTERNAL_ERROR = -11, // an unknown error occured
25
+ # txINTERNAL_ERROR = -11, // an unknown error occurred
26
26
  #
27
- # txNOT_SUPPORTED = -12, // transaction type not supported
28
- # txFEE_BUMP_INNER_FAILED = -13 // fee bump inner transaction failed
27
+ # txNOT_SUPPORTED = -12, // transaction type not supported
28
+ # txFEE_BUMP_INNER_FAILED = -13, // fee bump inner transaction failed
29
+ # txBAD_SPONSORSHIP = -14 // sponsorship not confirmed
29
30
  # };
30
31
  #
31
32
  # ===========================================================================
@@ -46,6 +47,7 @@ module Stellar
46
47
  member :tx_internal_error, -11
47
48
  member :tx_not_supported, -12
48
49
  member :tx_fee_bump_inner_failed, -13
50
+ member :tx_bad_sponsorship, -14
49
51
 
50
52
  seal
51
53
  end
@@ -11,7 +11,10 @@ require 'xdr'
11
11
  # AUTHORIZED_FLAG = 1,
12
12
  # // issuer has authorized account to maintain and reduce liabilities for its
13
13
  # // credit
14
- # AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG = 2
14
+ # AUTHORIZED_TO_MAINTAIN_LIABILITIES_FLAG = 2,
15
+ # // issuer has specified that it may clawback its credit, and that claimable
16
+ # // balances created with its credit may also be clawed back
17
+ # TRUSTLINE_CLAWBACK_ENABLED_FLAG = 4
15
18
  # };
16
19
  #
17
20
  # ===========================================================================
@@ -19,6 +22,7 @@ module Stellar
19
22
  class TrustLineFlags < XDR::Enum
20
23
  member :authorized_flag, 1
21
24
  member :authorized_to_maintain_liabilities_flag, 2
25
+ member :trustline_clawback_enabled_flag, 4
22
26
 
23
27
  seal
24
28
  end
data/lib/stellar-base.rb CHANGED
@@ -4,6 +4,11 @@ require "digest/sha2"
4
4
  require "active_support/core_ext/object/blank"
5
5
  require "active_support/core_ext/enumerable"
6
6
  require "active_support/core_ext/kernel/reporting"
7
+ require "active_support/core_ext/module/attribute_accessors_per_thread"
8
+ require "active_support/core_ext/module/delegation"
9
+ require "active_support/deprecation"
10
+
11
+ require_relative "stellar/ext/xdr"
7
12
 
8
13
  # See ../generated for code-gen'ed files
9
14
  silence_warnings do
@@ -11,28 +16,36 @@ silence_warnings do
11
16
  end
12
17
  Stellar.load_all!
13
18
 
19
+ require_relative "stellar/version"
20
+
14
21
  Stellar::ONE = 1_0000000
22
+ Stellar::Deprecation = ActiveSupport::Deprecation.new("next release", "stellar-base")
15
23
 
16
24
  # extensions onto the generated files must be loaded manually, below
17
25
 
18
26
  require_relative "./stellar/account_flags"
19
27
  require_relative "./stellar/asset"
28
+ require_relative "./stellar/claim_predicate"
20
29
  require_relative "./stellar/key_pair"
30
+ require_relative "./stellar/ledger_key"
31
+ require_relative "./stellar/networks"
21
32
  require_relative "./stellar/operation"
22
33
  require_relative "./stellar/path_payment_strict_receive_result"
23
34
  require_relative "./stellar/price"
24
35
  require_relative "./stellar/signer_key"
25
36
  require_relative "./stellar/thresholds"
37
+ require_relative "./stellar/trust_line_flags"
38
+
26
39
  require_relative "./stellar/concerns/transaction"
27
- require_relative "./stellar/transaction"
28
- require_relative "./stellar/transaction_v0"
29
40
  require_relative "./stellar/fee_bump_transaction"
41
+ require_relative "./stellar/transaction"
30
42
  require_relative "./stellar/transaction_builder"
31
43
  require_relative "./stellar/transaction_envelope"
44
+ require_relative "./stellar/transaction_v0"
45
+
32
46
  require_relative "./stellar/util/strkey"
33
- require_relative "./stellar/util/continued_fraction"
34
47
  require_relative "./stellar/convert"
35
- require_relative "./stellar/networks"
36
- require_relative "./stellar/version"
48
+
49
+ require_relative "./stellar/dsl"
37
50
 
38
51
  require_relative "./stellar/compat"
@@ -4,7 +4,7 @@ module Stellar
4
4
  # Converts an array of Stellar::AccountFlags members into
5
5
  # an Integer suitable for use in a SetOptionsOp.
6
6
  #
7
- # @param flags=nil [Array<Stellar::AccountFlags>] the flags to combine
7
+ # @param flags [Array<Stellar::AccountFlags>] the flags to combine
8
8
  #
9
9
  # @return [Fixnum] the combined result
10
10
  def self.make_mask(flags = nil)
data/lib/stellar/asset.rb CHANGED
@@ -6,14 +6,24 @@ module Stellar
6
6
  new(:asset_type_native)
7
7
  end
8
8
 
9
+ # @param code [String] asset code
10
+ # @param issuer [#to_keypair] asset issuer
11
+ #
12
+ # @return [Stellar::Asset::AlphaNum4] asset4 representation
9
13
  def self.alphanum4(code, issuer)
14
+ issuer = issuer.to_keypair if issuer.respond_to?(:to_keypair)
10
15
  raise ArgumentError, "Bad :issuer" unless issuer.is_a?(KeyPair)
11
16
  code = normalize_code(code, 4)
12
17
  an = AlphaNum4.new({asset_code: code, issuer: issuer.account_id})
13
18
  new(:asset_type_credit_alphanum4, an)
14
19
  end
15
20
 
21
+ # @param code [String] asset code
22
+ # @param issuer [#to_keypair] asset issuer
23
+ #
24
+ # @return [Stellar::Asset::AlphaNum4] asset4 representation
16
25
  def self.alphanum12(code, issuer)
26
+ issuer = issuer.to_keypair if issuer.respond_to?(:to_keypair)
17
27
  raise ArgumentError, "Bad :issuer" unless issuer.is_a?(KeyPair)
18
28
  code = normalize_code(code, 12)
19
29
  an = AlphaNum12.new({asset_code: code, issuer: issuer.account_id})
@@ -0,0 +1,198 @@
1
+ # frozen_string_literals: true
2
+ require "active_support/core_ext/integer/time"
3
+ require "active_support/core_ext/string/conversions"
4
+
5
+ module Stellar
6
+ # Represents claim predicate on Stellar network.
7
+ #
8
+ # @see https://developers.stellar.org/docs/glossary/claimable-balance/
9
+ class ClaimPredicate
10
+ module FactoryMethods
11
+ # Constructs an `unconditional` claim predicate.
12
+ #
13
+ # This predicate will be always fulfilled.
14
+ #
15
+ # @return [ClaimPredicate] `unconditional` claim predicate
16
+ def unconditional
17
+ ClaimPredicate.new(ClaimPredicateType::UNCONDITIONAL)
18
+ end
19
+
20
+ # Constructs a `before_relative_time` claim predicate.
21
+ #
22
+ # This predicate will be fulfilled if the closing time of the ledger that includes
23
+ # the Stellar::CreateClaimableBalance operation plus this relative time delta (in seconds)
24
+ # is less than the current time.
25
+ #
26
+ # @param seconds [#to_int|#to_i] seconds since `closeTime` of the ledger in which
27
+ # the ClaimableBalanceEntry was created.
28
+ # @return [ClaimPredicate] `before_relative_time` claim predicate
29
+ def before_relative_time(seconds)
30
+ ClaimPredicate.new(ClaimPredicateType::BEFORE_RELATIVE_TIME, Integer(seconds))
31
+ end
32
+
33
+ # Constructs an `before_absolute_time` claim predicate.
34
+ #
35
+ # This predicate will be fulfilled if the closing time of the ledger that includes
36
+ # the Stellar::CreateClaimableBalance operation is less than provided timestamp.
37
+ #
38
+ # @param timestamp [#to_time|#to_int|#to_i] time value or timestamp
39
+ #
40
+ # @return [ClaimPredicate] `before_absolute_time` claim predicate.
41
+ def before_absolute_time(timestamp)
42
+ timestamp = timestamp.to_time if timestamp.respond_to?(:to_time)
43
+
44
+ ClaimPredicate.new(ClaimPredicateType::BEFORE_ABSOLUTE_TIME, Integer(timestamp))
45
+ end
46
+
47
+ # Constructs either relative or absolute time predicate based on the type of the input.
48
+ #
49
+ # If input is an instance of `ActiveSupport::Duration` class it will be handled as a relative time
50
+ # (seconds since close time of the ledger), otherwise it will be treated as an absolute time.
51
+ #
52
+ # It is intended to work with time helpers provided by ActiveSupport, like `1.day` (relative)
53
+ # or `2.weeks.from_now` (absolute).
54
+ #
55
+ # @example relative time
56
+ # ClaimPredicate.before(2.days + 15.seconds)
57
+ #
58
+ # @example absolute time
59
+ # ClaimPredicate.before(5.hours.from_now)
60
+ #
61
+ # @param time [ActiveSupport::Duration|#to_time|#to_i] duration since ledger close time or absolute time value
62
+ #
63
+ # @return [ClaimPredicate] `before_relative_time` or `before_absolute_time` claim predicate.
64
+ def before(time)
65
+ ActiveSupport::Duration === time ? before_relative_time(time.to_i) : before_absolute_time(time)
66
+ end
67
+
68
+ # Constructs a negated predicate from either relative or absolute time based on the type of the input.
69
+ #
70
+ # @see #before
71
+ # @param (see #before)
72
+ # @return (see #before)
73
+ def after(time)
74
+ ~before(time)
75
+ end
76
+
77
+ # Compose a complex predicate by calling DSL methods from the block.
78
+ #
79
+ # @example
80
+ # ClaimPredicate.compose {
81
+ # after(15.minutes) & before(1.day) | after(1.week.from_now) & before(1.week.from_now + 1.day)
82
+ # }
83
+ #
84
+ # @yieldreturn [ClaimPredicate|nil]
85
+ # @return [ClaimPredicate] `not(before_relative_time)` or `not(before_absolute_time)` claim predicate.
86
+ def compose(&block)
87
+ result = instance_eval(&block)
88
+ result.nil? ? unconditional : result
89
+ end
90
+ end
91
+
92
+ extend FactoryMethods
93
+
94
+ # Constructs an `and` claim predicate.
95
+ #
96
+ # This predicate will be fulfilled if both `self` and `other` predicates are fulfilled.
97
+ #
98
+ # @param other [ClaimPredicate] another predicate
99
+ #
100
+ # @return [ClaimPredicate] `and` claim predicate
101
+ def and(other)
102
+ raise TypeError, "no conversion from #{other.class.name} to ClaimPredicate" unless ClaimPredicate === other
103
+ ClaimPredicate.new(ClaimPredicateType::AND, [self, other])
104
+ end
105
+ alias_method :&, :and
106
+
107
+ # Constructs an `or` claim predicate.
108
+ #
109
+ # This predicate will be fulfilled if either of `self` or `other` predicates is fulfilled.
110
+ #
111
+ # @param other [ClaimPredicate] another predicate.
112
+ #
113
+ # @return [ClaimPredicate] `or` claim predicate
114
+ def or(other)
115
+ raise TypeError, "no conversion from #{other.class.name} to ClaimPredicate" unless ClaimPredicate === other
116
+ ClaimPredicate.new(ClaimPredicateType::OR, [self, other])
117
+ end
118
+ alias_method :|, :or
119
+
120
+ # Constructs a `not` claim predicate.
121
+ #
122
+ # This predicate will be fulfilled if `self` is not fulfilled.
123
+ #
124
+ # @return [ClaimPredicate] `not` claim predicate
125
+ def not
126
+ ClaimPredicate.new(ClaimPredicateType::NOT, self)
127
+ end
128
+ alias_method :~@, :not
129
+
130
+ # Evaluates the predicate value for provided inputs.
131
+ #
132
+ # @param created_at [#to_time|#to_int] closing time of the ledger containing CreateClaimableBalance operation
133
+ # @param claiming_at [#to_time|#to_int|ActiveSupport::Duration] time point to evaluate predicate at, either
134
+ # absolute time or duration relative to `created_at`. In reality predicate will be evaluated by stellar-core
135
+ # using the closing time of a ledger containing ClaimClaimableBalance operation, in either successful
136
+ # or failed state.
137
+ #
138
+ # @return [Boolean] `true` if this predicate would allow claiming the balance, `false` otherwise
139
+ def evaluate(created_at, claiming_at)
140
+ created_at = created_at.to_time if created_at.respond_to?(:to_time)
141
+ claiming_at = created_at + claiming_at if claiming_at.is_a?(ActiveSupport::Duration)
142
+ claiming_at = claiming_at.to_time if claiming_at.respond_to?(:to_time)
143
+
144
+ return false if claiming_at < created_at
145
+
146
+ case switch
147
+ when ClaimPredicateType::UNCONDITIONAL
148
+ true
149
+ when ClaimPredicateType::BEFORE_RELATIVE_TIME
150
+ Integer(claiming_at) < Integer(created_at) + value
151
+ when ClaimPredicateType::BEFORE_ABSOLUTE_TIME
152
+ Integer(claiming_at).to_i < value
153
+ when ClaimPredicateType::AND
154
+ value[0].evaluate(created_at, claiming_at) && value[1].evaluate(created_at, claiming_at)
155
+ when ClaimPredicateType::OR
156
+ value[0].evaluate(created_at, claiming_at) || value[1].evaluate(created_at, claiming_at)
157
+ when ClaimPredicateType::NOT
158
+ !value.evaluate(created_at, claiming_at)
159
+ else
160
+ raise ArgumentError, "evaluation is not implemented for #{switch.name} predicate"
161
+ end
162
+ end
163
+
164
+ def describe
165
+ case switch
166
+ when ClaimPredicateType::UNCONDITIONAL
167
+ "always"
168
+ when ClaimPredicateType::BEFORE_RELATIVE_TIME
169
+ dur = ActiveSupport::Duration.build(value)
170
+ "less than #{dur.inspect} since creation"
171
+ when ClaimPredicateType::BEFORE_ABSOLUTE_TIME
172
+ "before #{Time.at(value).to_formatted_s(:db)}"
173
+ when ClaimPredicateType::AND
174
+ value.map(&:describe).join(" and ")
175
+ when ClaimPredicateType::OR
176
+ "(" << value.map(&:describe).join(" or ") << ")"
177
+ when ClaimPredicateType::NOT
178
+ case value.switch
179
+ when ClaimPredicateType::UNCONDITIONAL
180
+ "never"
181
+ when ClaimPredicateType::BEFORE_RELATIVE_TIME
182
+ dur = ActiveSupport::Duration.build(value.value)
183
+ "#{dur.inspect} or more since creation"
184
+ when ClaimPredicateType::BEFORE_ABSOLUTE_TIME
185
+ "after #{Time.at(value.value).to_formatted_s(:db)}"
186
+ else
187
+ "not (#{value.describe})"
188
+ end
189
+ else
190
+ raise ArgumentError, "evaluation is not implemented for #{switch.name} predicate"
191
+ end
192
+ end
193
+
194
+ def inspect
195
+ "#<ClaimPredicate: #{describe}>"
196
+ end
197
+ end
198
+ end