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.



293
294
295
# File 'lib/chain/transaction.rb', line 293

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

Instance Method Details

#actionsArray<Hash>

Returns:

  • (Array<Hash>)


298
299
300
# File 'lib/chain/transaction.rb', line 298

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:



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

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:



304
305
306
307
308
309
310
311
# File 'lib/chain/transaction.rb', line 304

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:



409
410
411
# File 'lib/chain/transaction.rb', line 409

def (params)
  add_action(params.merge(type: :control_account))
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:



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

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:



369
370
371
# File 'lib/chain/transaction.rb', line 369

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:



435
436
437
# File 'lib/chain/transaction.rb', line 435

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.

Returns:



393
394
395
# File 'lib/chain/transaction.rb', line 393

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:



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

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

#to_hHash

Returns:

  • (Hash)


320
321
322
323
324
325
326
327
328
329
330
# File 'lib/chain/transaction.rb', line 320

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)


333
334
335
# File 'lib/chain/transaction.rb', line 333

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:



354
355
356
357
358
359
# File 'lib/chain/transaction.rb', line 354

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

#ttl(ttl) ⇒ Builder

Returns:



314
315
316
317
# File 'lib/chain/transaction.rb', line 314

def ttl(ttl)
  @ttl = ttl
  self
end