Class: Digitalbits::Operation
- Inherits:
-
Object
- Object
- Digitalbits::Operation
- Extended by:
- DSL
- Defined in:
- lib/digitalbits/compat.rb,
lib/digitalbits/operation.rb
Constant Summary collapse
- MAX_INT64 =
2**63 - 1
- TRUST_LINE_FLAGS_MAPPING =
{ full: Digitalbits::TrustLineFlags., maintain_liabilities: Digitalbits::TrustLineFlags., clawback_enabled: false }.freeze
Class Method Summary collapse
-
.account_merge(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create an account merge operation.
-
.allow_trust(attributes = {}) ⇒ Digitalbits::Operation
deprecated
Deprecated.
Use
set_trustline_flagsoperation - .begin_sponsoring_future_reserves(sponsored:, **attributes) ⇒ Object
- .bump_sequence(attributes = {}) ⇒ Object
-
.change_trust(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid ChangeTrustOp, wrapped in the necessary XDR structs to be included within a transactions
operationsarray. -
.claim_claimable_balance(balance_id:, **attributes) ⇒ Operation
Helper method to create a valid CreateClaimableBalanceOp, ready to be used within a transactions
operationsarray. - .clawback(source_account:, from:, amount:) ⇒ Object
-
.clawback_claimable_balance(source_account:, balance_id:) ⇒ Digitalbits::Operation
Helper method to create clawback claimable balance operation.
- .create_account(attributes = {}) ⇒ Object
-
.create_claimable_balance(asset:, amount:, claimants:, **attributes) ⇒ Operation
Helper method to create a valid CreateClaimableBalanceOp, ready to be used within a transactions
operationsarray. - .create_passive_offer ⇒ Object
- .create_passive_sell_offer(attributes = {}) ⇒ Object
- .end_sponsoring_future_reserves(**attributes) ⇒ Object
-
.inflation(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create an inflation operation.
-
.make(attributes = {}) ⇒ Digitalbits::Operation
Construct a new Digitalbits::Operation from the provided source account and body.
- .manage_buy_offer(attributes = {}) ⇒ Object
-
.manage_data(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create an manage data operation.
- .manage_offer ⇒ Object
- .manage_sell_offer(attributes = {}) ⇒ Object
-
.path_payment(attributes = {}) ⇒ Digitalbits::Operation
deprecated
Deprecated.
Please use Operation.path_payment_strict_receive
-
.path_payment_strict_receive(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid PathPaymentStrictReceiveOp, wrapped in the necessary XDR structs to be included within a transactions
operationsarray. -
.path_payment_strict_send(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid PathPaymentStrictSendOp, wrapped in the necessary XDR structs to be included within a transactions
operationsarray. -
.payment(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid PaymentOp, wrapped in the necessary XDR structs to be included within a transactions
operationsarray. - .revoke_sponsorship(sponsored:, **attributes) ⇒ Object
-
.set_options(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid SetOptionsOp, wrapped in the necessary XDR structs to be included within a transactions
operationsarray. - .set_trust_line_flags(asset:, trustor:, flags: {}, source_account: nil) ⇒ Object
Methods included from DSL
Asset, ClaimPredicate, Claimant, KeyPair, SignerKey
Class Method Details
.account_merge(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create an account merge operation
450 451 452 453 454 455 456 457 458 459 |
# File 'lib/digitalbits/operation.rb', line 450 def account_merge(attributes = {}) destination = attributes[:destination] raise ArgumentError, "Bad :destination" unless destination.is_a?(KeyPair) # TODO: add source_account support make(attributes.merge({ body: [:account_merge, destination.muxed_account] })) end |
.allow_trust(attributes = {}) ⇒ Digitalbits::Operation
Use set_trustline_flags operation
DEPRECATED in favor of set_trustline_flags
Helper method to create a valid AllowTrustOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
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 |
# File 'lib/digitalbits/operation.rb', line 409 def allow_trust(attributes = {}) op = AllowTrustOp.new trustor = attributes[:trustor] # we handle booleans here for the backward compatibility = attributes[:authorize].yield_self { |value| value == true ? :full : value } asset = attributes[:asset] if asset.is_a?(Array) asset = Asset.send(*asset) end raise ArgumentError, "Bad :trustor" unless trustor.is_a?(Digitalbits::KeyPair) allowed_flags = TRUST_LINE_FLAGS_MAPPING.slice(:full, :maintain_liabilities) # we handle booleans here for the backward compatibility op. = if allowed_flags.key?() allowed_flags[].value elsif [:none, false].include?() 0 else raise ArgumentError, "Bad :authorize, supported values: :full, :maintain_liabilities, :none" end raise ArgumentError, "Bad :asset" unless asset.type == Digitalbits::AssetType.asset_type_credit_alphanum4 op.trustor = trustor.account_id op.asset = AssetCode.new(:asset_type_credit_alphanum4, asset.code) make(attributes.merge({ body: [:allow_trust, op] })) end |
.begin_sponsoring_future_reserves(sponsored:, **attributes) ⇒ Object
239 240 241 242 243 244 245 |
# File 'lib/digitalbits/operation.rb', line 239 def begin_sponsoring_future_reserves(sponsored:, **attributes) op = BeginSponsoringFutureReservesOp.new( sponsored_id: KeyPair(sponsored).account_id ) make(attributes.merge(body: [:begin_sponsoring_future_reserves, op])) end |
.bump_sequence(attributes = {}) ⇒ Object
507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/digitalbits/operation.rb', line 507 def bump_sequence(attributes = {}) op = BumpSequenceOp.new bump_to = attributes[:bump_to] raise ArgumentError, ":bump_to too big" unless bump_to <= MAX_INT64 op.bump_to = bump_to make(attributes.merge({ body: [:bump_sequence, op] })) end |
.change_trust(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid ChangeTrustOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/digitalbits/operation.rb', line 189 def change_trust(attributes = {}) line = attributes[:line] unless line.is_a?(Asset) unless Asset::TYPES.include?(line[0]) fail ArgumentError, "must be one of #{Asset::TYPES}" end line = Asset.send(*line) end limit = attributes.key?(:limit) ? interpret_amount(attributes[:limit]) : MAX_INT64 raise ArgumentError, "Bad :limit #{limit}" unless limit.is_a?(Integer) op = ChangeTrustOp.new(line: line, limit: limit) make(attributes.merge({ body: [:change_trust, op] })) end |
.claim_claimable_balance(balance_id:, **attributes) ⇒ Operation
Helper method to create a valid CreateClaimableBalanceOp, ready to be used within a transactions operations array.
233 234 235 236 237 |
# File 'lib/digitalbits/operation.rb', line 233 def claim_claimable_balance(balance_id:, **attributes) op = ClaimClaimableBalanceOp.new(balance_id: balance_id) make(attributes.merge(body: [:claim_claimable_balance, op])) end |
.clawback(source_account:, from:, amount:) ⇒ Object
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/digitalbits/operation.rb', line 521 def clawback(source_account:, from:, amount:) asset, amount = get_asset_amount(amount) if amount == 0 raise ArgumentError, "Amount can not be zero" end if amount < 0 raise ArgumentError, "Negative amount is not allowed" end op = ClawbackOp.new( amount: amount, from: from.muxed_account, asset: asset ) make({ source_account: source_account, body: [:clawback, op] }) end |
.clawback_claimable_balance(source_account:, balance_id:) ⇒ Digitalbits::Operation
Helper method to create clawback claimable balance operation
550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/digitalbits/operation.rb', line 550 def clawback_claimable_balance(source_account:, balance_id:) balance_id = Digitalbits::ClaimableBalanceID.from_xdr(balance_id, :hex) op = ClawbackClaimableBalanceOp.new(balance_id: balance_id) make( source_account: source_account, body: [:clawback_claimable_balance, op] ) rescue XDR::ReadError raise ArgumentError, "Claimable balance id '#{balance_id}' is invalid" end |
.create_account(attributes = {}) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/digitalbits/operation.rb', line 163 def create_account(attributes = {}) destination = attributes[:destination] starting_balance = interpret_amount(attributes[:starting_balance]) raise ArgumentError unless destination.is_a?(KeyPair) op = CreateAccountOp.new op.destination = destination.account_id op.starting_balance = starting_balance make(attributes.merge({ body: [:create_account, op] })) end |
.create_claimable_balance(asset:, amount:, claimants:, **attributes) ⇒ Operation
Helper method to create a valid CreateClaimableBalanceOp, ready to be used within a transactions operations array.
219 220 221 222 223 |
# File 'lib/digitalbits/operation.rb', line 219 def create_claimable_balance(asset:, amount:, claimants:, **attributes) op = CreateClaimableBalanceOp.new(asset: asset, amount: amount, claimants: claimants) make(attributes.merge(body: [:create_claimable_balance, op])) end |
.create_passive_offer ⇒ Object
3 |
# File 'lib/digitalbits/compat.rb', line 3 alias_method :create_passive_offer, :create_passive_sell_offer |
.create_passive_sell_offer(attributes = {}) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/digitalbits/operation.rb', line 317 def create_passive_sell_offer(attributes = {}) = attributes[:buying] if .is_a?(Array) = Asset.send(*) end selling = attributes[:selling] if selling.is_a?(Array) selling = Asset.send(*selling) end amount = interpret_amount(attributes[:amount]) price = interpret_price(attributes[:price]) op = CreatePassiveSellOfferOp.new({ buying: , selling: selling, amount: amount, price: price }) make(attributes.merge({ body: [:create_passive_sell_offer, op] })) end |
.end_sponsoring_future_reserves(**attributes) ⇒ Object
247 248 249 |
# File 'lib/digitalbits/operation.rb', line 247 def end_sponsoring_future_reserves(**attributes) make(attributes.merge(body: [:end_sponsoring_future_reserves])) end |
.inflation(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create an inflation operation
468 469 470 471 472 473 474 475 476 477 |
# File 'lib/digitalbits/operation.rb', line 468 def inflation(attributes = {}) sequence = attributes[:sequence] raise ArgumentError, "Bad :sequence #{sequence}" unless sequence.is_a?(Integer) # TODO: add source_account support make(attributes.merge({ body: [:inflation] })) end |
.make(attributes = {}) ⇒ Digitalbits::Operation
Construct a new Digitalbits::Operation from the provided source account and body
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/digitalbits/operation.rb', line 23 def make(attributes = {}) source_account = attributes[:source_account] if source_account && !source_account.is_a?(Digitalbits::KeyPair) raise ArgumentError, "Bad :source_account" end body = Digitalbits::Operation::Body.new(*attributes[:body]) Digitalbits::Operation.new( body: body, source_account: source_account&.muxed_account ) end |
.manage_buy_offer(attributes = {}) ⇒ Object
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 |
# File 'lib/digitalbits/operation.rb', line 291 def manage_buy_offer(attributes = {}) = attributes[:buying] if .is_a?(Array) = Asset.send(*) end selling = attributes[:selling] if selling.is_a?(Array) selling = Asset.send(*selling) end amount = interpret_amount(attributes[:amount]) offer_id = attributes[:offer_id] || 0 price = interpret_price(attributes[:price]) op = ManageBuyOfferOp.new({ buying: , selling: selling, buy_amount: amount, price: price, offer_id: offer_id }) make(attributes.merge({ body: [:manage_buy_offer, op] })) end |
.manage_data(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create an manage data operation
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/digitalbits/operation.rb', line 486 def manage_data(attributes = {}) op = ManageDataOp.new name = attributes[:name] value = attributes[:value] raise ArgumentError, "Invalid :name" unless name.is_a?(String) raise ArgumentError, ":name too long" unless name.bytesize <= 64 if value.present? raise ArgumentError, ":value too long" unless value.bytesize <= 64 end op.data_name = name op.data_value = value make(attributes.merge({ body: [:manage_data, op] })) end |
.manage_offer ⇒ Object
2 |
# File 'lib/digitalbits/compat.rb', line 2 alias_method :manage_offer, :manage_sell_offer |
.manage_sell_offer(attributes = {}) ⇒ Object
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 |
# File 'lib/digitalbits/operation.rb', line 265 def manage_sell_offer(attributes = {}) = attributes[:buying] if .is_a?(Array) = Asset.send(*) end selling = attributes[:selling] if selling.is_a?(Array) selling = Asset.send(*selling) end amount = interpret_amount(attributes[:amount]) offer_id = attributes[:offer_id] || 0 price = interpret_price(attributes[:price]) op = ManageSellOfferOp.new({ buying: , selling: selling, amount: amount, price: price, offer_id: offer_id }) make(attributes.merge({ body: [:manage_sell_offer, op] })) end |
.path_payment(attributes = {}) ⇒ Digitalbits::Operation
Please use Operation.path_payment_strict_receive
Helper method to create a valid PathPaymentStrictReceiveOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
83 84 85 |
# File 'lib/digitalbits/operation.rb', line 83 def path_payment(attributes = {}) path_payment_strict_receive(attributes) end |
.path_payment_strict_receive(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid PathPaymentStrictReceiveOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/digitalbits/operation.rb', line 102 def path_payment_strict_receive(attributes = {}) destination = attributes[:destination] asset, amount = get_asset_amount(attributes[:amount]) send_asset, send_max = get_asset_amount(attributes[:with]) path = (attributes[:path] || []).map { |p| p.is_a?(Array) ? Digitalbits::Asset.send(*p) : p } raise ArgumentError unless destination.is_a?(KeyPair) op = PathPaymentStrictReceiveOp.new op.send_asset = send_asset op.send_max = send_max op.destination = destination.muxed_account op.dest_asset = asset op.dest_amount = amount op.path = path make(attributes.merge({ body: [:path_payment_strict_receive, op] })) end |
.path_payment_strict_send(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid PathPaymentStrictSendOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/digitalbits/operation.rb', line 140 def path_payment_strict_send(attributes = {}) destination = attributes[:destination] asset, dest_min = get_asset_amount(attributes[:amount]) send_asset, send_amount = get_asset_amount(attributes[:with]) path = (attributes[:path] || []).map { |p| p.is_a?(Array) ? Digitalbits::Asset.send(*p) : p } raise ArgumentError unless destination.is_a?(KeyPair) op = PathPaymentStrictSendOp.new op.send_asset = send_asset op.send_amount = send_amount op.destination = destination.muxed_account op.dest_asset = asset op.dest_min = dest_min op.path = path make(attributes.merge({ body: [:path_payment_strict_send, op] })) end |
.payment(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid PaymentOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/digitalbits/operation.rb', line 50 def payment(attributes = {}) destination = attributes[:destination] asset, amount = get_asset_amount(attributes[:amount]) raise ArgumentError unless destination.is_a?(KeyPair) op = PaymentOp.new op.asset = asset op.amount = amount op.destination = destination.muxed_account make(attributes.merge({ body: [:payment, op] })) end |
.revoke_sponsorship(sponsored:, **attributes) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/digitalbits/operation.rb', line 252 def revoke_sponsorship(sponsored:, **attributes) key_fields = attributes.slice(:offer_id, :data_name, :balance_id, :asset, :signer) raise ArgumentError, "conflicting attributes: #{key_fields.keys.join(", ")}" if key_fields.size > 1 account_id = KeyPair(sponsored).account_id key, value = key_fields.first op = if key == :signer RevokeSponsorshipOp.signer(account_id: account_id, signer_key: SignerKey(value)) else RevokeSponsorshipOp.ledger_key(LedgerKey.from(account_id: account_id, **key_fields)) end make(attributes.merge(body: [:revoke_sponsorship, op])) end |
.set_options(attributes = {}) ⇒ Digitalbits::Operation
Helper method to create a valid SetOptionsOp, wrapped in the necessary XDR structs to be included within a transactions operations array.
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/digitalbits/operation.rb', line 355 def (attributes = {}) op = SetOptionsOp.new op.set_flags = Digitalbits::AccountFlags.make_mask attributes[:set] op.clear_flags = Digitalbits::AccountFlags.make_mask attributes[:clear] op.master_weight = attributes[:master_weight] op.low_threshold = attributes[:low_threshold] op.med_threshold = attributes[:med_threshold] op.high_threshold = attributes[:high_threshold] op.signer = attributes[:signer] op.home_domain = attributes[:home_domain] inflation_dest = attributes[:inflation_dest] if inflation_dest raise ArgumentError, "Bad :inflation_dest" unless inflation_dest.is_a?(Digitalbits::KeyPair) op.inflation_dest = inflation_dest.account_id end make(attributes.merge({ body: [:set_options, op] })) end |
.set_trust_line_flags(asset:, trustor:, flags: {}, source_account: nil) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/digitalbits/operation.rb', line 382 def set_trust_line_flags(asset:, trustor:, flags: {}, source_account: nil) op = Digitalbits::SetTrustLineFlagsOp.new op.trustor = KeyPair(trustor).account_id op.asset = Asset(asset) op.attributes = Digitalbits::TrustLineFlags.set_clear_masks(flags) make( source_account: source_account, body: [:set_trust_line_flags, op] ) end |