class Mobius::Client::Blockchain::Account

Service class used to interact with account on Stellar network.

Public Instance Methods

account() click to toggle source

Returns Stellar::Account instance for given keypair. @return [Stellar::Account] instance

# File lib/mobius/client/blockchain/account.rb, line 37
def account
  @account ||=
    if keypair.sign?
      Stellar::Account.from_seed(keypair.seed)
    else
      Stellar::Account.from_address(keypair.address)
    end
end
authorized?(to_keypair) click to toggle source

Returns true if given keypair is added as cosigner to current account. @param to_keypair [Stellar::Keypair] Keypair in question @return [Boolean] true if cosigner added TODO: Add weight check/return

# 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) click to toggle source

Returns balance for given asset @param asset [Stellar::Asset] Stellar asset to check or :native @return [Float] Balance value.

# 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
info() click to toggle source

Requests and caches Stellar::Account information from network. @return [Stellar::Account] account information.

# File lib/mobius/client/blockchain/account.rb, line 48
def info
  @info ||= Mobius::Client.horizon_client.account_info(account)
end
next_sequence_value() click to toggle source

Invalidates cache and returns next sequence value for given account. @return [Integer] sequence value.

# File lib/mobius/client/blockchain/account.rb, line 59
def next_sequence_value
  reload!
  info.sequence.to_i + 1
end
reload!() click to toggle source

Invalidates account information cache.

# File lib/mobius/client/blockchain/account.rb, line 53
def reload!
  @info = nil
end
trustline_exists?(asset = Mobius::Client.stellar_asset) click to toggle source

Returns true if trustline exists for given asset and limit is positive. @param asset [Stellar::Asset] Stellar asset to check or :native @return [Boolean] true if trustline exists

# 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

Private Instance Methods

balance_matches?(asset, balance) click to toggle source
# File lib/mobius/client/blockchain/account.rb, line 72
def balance_matches?(asset, balance)
  if [:native, Stellar::Asset.native].include?(asset)
    balance["asset_type"] == "native"
  else
    code = balance["asset_code"]
    issuer = balance["asset_issuer"]
    asset_issuer_address = Mobius::Client.to_keypair(asset.issuer).address
    code == asset.code && issuer == asset_issuer_address
  end
end
find_balance(asset) click to toggle source
# File lib/mobius/client/blockchain/account.rb, line 66
def find_balance(asset)
  info.balances.find { |balance| balance_matches?(asset, balance) }
rescue Faraday::ResourceNotFound
  raise Mobius::Client::Error::AccountMissing
end
find_signer(address) click to toggle source

TODO: Think of adding weight check here

# File lib/mobius/client/blockchain/account.rb, line 84
def find_signer(address)
  info.signers.find { |s| s["public_key"] == address || s["key"] == address }
rescue Faraday::ResourceNotFound
  raise Mobius::Client::Error::AccountMissing
end