Class: Mobius::Client::Blockchain::Account

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/mobius/client/blockchain/account.rb

Overview

Service class used to interact with account on Stellar network.

Instance Method Summary collapse

Constructor Details

#initialize(keypair) ⇒ Object

Parameters:

  • keypair (Stellar::Keypair)

    account keypair



8
# File 'lib/mobius/client/blockchain/account.rb', line 8

param :keypair, Mobius::Client.method(:to_keypair)

Instance Method Details

#accountStellar::Account

Returns Stellar::Account instance for given keypair.

Returns:

  • (Stellar::Account)

    instance



37
38
39
40
41
42
43
44
# File 'lib/mobius/client/blockchain/account.rb', line 37

def 
  @account ||=
    if keypair.sign?
      Stellar::Account.from_seed(keypair.seed)
    else
      Stellar::Account.from_address(keypair.address)
    end
end

#authorized?(to_keypair) ⇒ Boolean

Returns true if given keypair is added as cosigner to current account. TODO: Add weight check/return

Parameters:

  • to_keypair (Stellar::Keypair)

    Keypair in question

Returns:

  • (Boolean)

    true if cosigner added



31
32
33
# File 'lib/mobius/client/blockchain/account.rb', line 31

def authorized?(to_keypair)
  !find_signer(to_keypair.address).nil?
end

#balance(asset = Mobius::Client.stellar_asset) ⇒ Float

Returns balance for given asset

Parameters:

  • asset (Stellar::Asset) (defaults to: Mobius::Client.stellar_asset)

    Stellar asset to check or :native

Returns:

  • (Float)

    Balance value.



21
22
23
24
25
# File 'lib/mobius/client/blockchain/account.rb', line 21

def balance(asset = Mobius::Client.stellar_asset)
  balance = find_balance(asset)
  balance = balance&.dig("balance")
  return balance.to_d if balance
end

#infoStellar::Account

Requests and caches Stellar::Account information from network.

Returns:

  • (Stellar::Account)

    account information.



48
49
50
# File 'lib/mobius/client/blockchain/account.rb', line 48

def info
  @info ||= Mobius::Client.horizon_client.()
end

#next_sequence_valueInteger

Invalidates cache and returns next sequence value for given account.

Returns:

  • (Integer)

    sequence value.



59
60
61
62
# File 'lib/mobius/client/blockchain/account.rb', line 59

def next_sequence_value
  reload!
  info.sequence.to_i + 1
end

#reload!Object

Invalidates account information cache.



53
54
55
# File 'lib/mobius/client/blockchain/account.rb', line 53

def reload!
  @info = nil
end

#trustline_exists?(asset = Mobius::Client.stellar_asset) ⇒ Boolean

Returns true if trustline exists for given asset and limit is positive.

Parameters:

  • asset (Stellar::Asset) (defaults to: Mobius::Client.stellar_asset)

    Stellar asset to check or :native

Returns:

  • (Boolean)

    true if trustline exists



13
14
15
16
# File 'lib/mobius/client/blockchain/account.rb', line 13

def trustline_exists?(asset = Mobius::Client.stellar_asset)
  balance = find_balance(asset)
  (balance && !balance.dig("limit").to_d.zero?) || false
end