Class: Chain::Transaction::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/chain/transaction.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



322
323
324
# File 'lib/chain/transaction.rb', line 322

def initialize(&block)
  block.call(self) if block
end

Instance Method Details

#actionsArray<Hash>

Returns:

  • (Array<Hash>)


327
328
329
# File 'lib/chain/transaction.rb', line 327

def actions
  @actions ||= []
end

#add_action(params) ⇒ Builder

Add an action to the transaction builder

Parameters:

  • params (Hash)

    Action parameters containing a type field and the required parameters for that type

Returns:



370
371
372
373
374
375
376
# File 'lib/chain/transaction.rb', line 370

def add_action(params)
  # Some actions require an idempotency token, so we'll add it here as a
  # generic parameter.
  params = {client_token: SecureRandom.uuid}.merge(params)
  actions << params
  self
end

#base_transaction(template_or_raw_tx) ⇒ Builder

Parameters:

Returns:



333
334
335
336
337
338
339
340
# File 'lib/chain/transaction.rb', line 333

def base_transaction(template_or_raw_tx)
  if template_or_raw_tx.is_a?(Transaction::Template)
    @base_transaction = template_or_raw_tx.raw_transaction
  else
    @base_transaction = template_or_raw_tx
  end
  self
end

#control_with_account(params) ⇒ Builder

Add a control action taken on a particular account.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :asset_id (String)

    Asset ID specifying the asset to be controlled. You must specify either an ID or an alias.

  • :asset_alias (String)

    Asset alias specifying the asset to be controlled. You must specify either an ID or an alias.

  • :account_id (String)

    Account ID specifying the account controlling the asset. You must specify either an ID or an alias.

  • :account_alias (String)

    Account alias specifying the account controlling the asset. You must specify either an ID or an alias.

  • :amount (Integer)

    amount of the asset to be controlled.

Returns:



440
441
442
# File 'lib/chain/transaction.rb', line 440

def (params)
  add_action(params.merge(type: :control_account))
end

#control_with_program(params) ⇒ Builder

Deprecated.

(as of version 1.1) Use #control_with_receiver instead.

Add a control action taken on a control program.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :asset_id (String)

    Asset ID specifying the asset to be controlled. You must specify either an ID or an alias.

  • :asset_alias (String)

    Asset alias specifying the asset to be controlled. You must specify either an ID or an alias.

  • :control_program (String)

    The control program to be used

  • :amount (Integer)

    amount of the asset to be controlled.

Returns:



468
469
470
# File 'lib/chain/transaction.rb', line 468

def control_with_program(params)
  add_action(params.merge(type: :control_program))
end

#control_with_receiver(params) ⇒ Builder

Sends assets to the specified receiver.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :control_program (Receiver)

    the receiver object.

  • :asset_id (String)

    Asset ID specifying the asset to be controlled. You must specify either an ID or an alias.

  • :asset_alias (String)

    Asset alias specifying the asset to be controlled. You must specify either an ID or an alias.

  • :amount (Integer)

    amount of the asset to be controlled.

Returns:



454
455
456
# File 'lib/chain/transaction.rb', line 454

def control_with_receiver(params)
  add_action(params.merge(type: :control_receiver))
end

#issue(params) ⇒ Builder

Add an issuance action.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :asset_id (String)

    Asset ID specifying the asset to be issued. You must specify either an ID or an alias.

  • :asset_alias (String)

    Asset alias specifying the asset to be issued. You must specify either an ID or an alias.

  • :amount (Integer)

    amount of the asset to be issued

Returns:



398
399
400
# File 'lib/chain/transaction.rb', line 398

def issue(params)
  add_action(params.merge(type: :issue))
end

#retire(params) ⇒ Builder

Add a retire action.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :asset_id (String)

    Asset ID specifying the asset to be retired. You must specify either an ID or an alias.

  • :asset_alias (String)

    Asset alias specifying the asset to be retired. You must specify either an ID or an alias.

  • :amount (Integer)

    Amount of the asset to be retired.

Returns:



480
481
482
# File 'lib/chain/transaction.rb', line 480

def retire(params)
  add_action(params.merge(type: :retire))
end

#spend_account_unspent_output(params) ⇒ Builder

Add a spend action taken on a particular unspent output.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :output_id (String)

    Output ID specifying the transaction output to spend.

  • :transaction_id (String)

    DEPRECATED (as of version 1.1) Transaction ID specifying the transaction to select an output from.

  • :position (Integer)

    DEPRECATED (as of version 1.1) Position of the output within the transaction to be spent.

Returns:



424
425
426
# File 'lib/chain/transaction.rb', line 424

def (params)
  add_action(params.merge(type: :spend_account_unspent_output))
end

#spend_from_account(params) ⇒ Builder

Add a spend action taken on a particular account.

Parameters:

  • params (Hash)

    Action parameters

Options Hash (params):

  • :asset_id (String)

    Asset ID specifying the asset to be spent. You must specify either an ID or an alias.

  • :asset_alias (String)

    Asset alias specifying the asset to be spent. You must specify either an ID or an alias.

  • :account_id (String)

    Account ID specifying the account spending the asset. You must specify either an ID or an alias.

  • :account_alias (String)

    Account alias specifying the account spending the asset. You must specify either an ID or an alias.

  • :amount (Integer)

    amount of the asset to be spent.

Returns:



414
415
416
# File 'lib/chain/transaction.rb', line 414

def (params)
  add_action(params.merge(type: :spend_account))
end

#to_hHash

Returns:

  • (Hash)


349
350
351
352
353
354
355
356
357
358
359
# File 'lib/chain/transaction.rb', line 349

def to_h
  {
    actions: actions,
    base_transaction: @base_transaction,
    ttl: @ttl,
  }.select do |k,v|
    # TODO: Patches an issue in Chain Core 1.0 where nil values are rejected
    # Remove in 1.1.0 or later
    v != nil
  end
end

#to_json(opts = nil) ⇒ String

Returns:

  • (String)


362
363
364
# File 'lib/chain/transaction.rb', line 362

def to_json(opts = nil)
  to_h.to_json(opts)
end

#transaction_reference_data(reference_data) ⇒ Builder

Sets the transaction-level reference data. May only be used once per transaction.

Parameters:

  • reference_data (Hash)

    User specified, unstructured data to be embedded in a transaction

Returns:



383
384
385
386
387
388
# File 'lib/chain/transaction.rb', line 383

def transaction_reference_data(reference_data)
  add_action(
    type: :set_transaction_reference_data,
    reference_data: reference_data,
  )
end

#ttl(ttl) ⇒ Builder

Returns:



343
344
345
346
# File 'lib/chain/transaction.rb', line 343

def ttl(ttl)
  @ttl = ttl
  self
end