class Mobius::Client::App

Interface to user balance in application. rubocop:disable Metrics/ClassLength

Constants

FEE

Public Instance Methods

app_account() click to toggle source

Returns application account @return [Mobius::Client::Blockchain::Account] Application Account

# File lib/mobius/client/app.rb, line 107
def app_account
  @app_account ||= Mobius::Client::Blockchain::Account.new(app_keypair)
end
app_balance() click to toggle source

Returns application balance. @return [Float] Application balance.

# File lib/mobius/client/app.rb, line 37
def app_balance
  app_account.balance
end
app_keypair() click to toggle source

Returns application keypair @return [Stellar::KeyPair] Application KeyPair

# File lib/mobius/client/app.rb, line 95
def app_keypair
  @app_keypair ||= Mobius::Client.to_keypair(seed)
end
authorized?() click to toggle source

Checks if developer is authorized to use an application. @return [Bool] Authorization status.

# File lib/mobius/client/app.rb, line 15
def authorized?
  user_account.authorized?(app_keypair)
end
balance() click to toggle source

@deprecated use {#user_balance} instead

# File lib/mobius/client/app.rb, line 20
  def balance
    warn <<~MSG
      [DEPRECATED] method Mobius::Client::App#balance is deprecated and will be removed,
      use Mobius::Client::App#user_balance instead"
    MSG
    user_balance
  end
charge(amount, target_address: nil) click to toggle source

Charges user's wallet. @param amount [Numeric, String] Payment amount. @param target_address [String] Optional: third party receiver address.

# File lib/mobius/client/app.rb, line 56
def charge(amount, target_address: nil)
  amount = cast_amount(amount)

  raise Mobius::Client::Error::InsufficientFunds if user_balance < amount

  submit_tx do |operations|
    operations << payment_op(amount, dest: app_keypair, src: user_keypair)
    operations << payment_op(amount, dest: target_address, src: app_keypair) if target_address
  end
rescue Faraday::ClientError => err
  handle(err)
end
pay(amount, target_address: nil) click to toggle source

Makes payment. @param amount [Numeric, String] Payment amount. @param target_address [String] Optional: third party receiver address. @deprecated use {#charge} instead

# File lib/mobius/client/app.rb, line 45
  def pay(amount, target_address: nil)
    warn <<~MSG
      [DEPRECATED] method Mobius::Client::App#pay is deprecated and will be removed,
      use Mobius::Client::App#charge instead"
    MSG
    charge(amount, target_address)
  end
payout(amount, target_address: user_keypair.address) click to toggle source

Sends money from application account to user's account or target_address, if given @param amount [Float] Payment amount. @param target_address [String] Optional: third party receiver address.

# File lib/mobius/client/app.rb, line 83
def payout(amount, target_address: user_keypair.address)
  amount = cast_amount(amount)
  raise Mobius::Client::Error::InsufficientFunds if app_balance < amount
  submit_tx do |operations|
    operations << payment_op(amount, dest: target_address, src: app_keypair)
  end
rescue Faraday::ClientError => err
  handle(err)
end
transfer(amount, address) click to toggle source

Sends money from user's account to third party. @param amount [Float] Payment amount. @param address [String] Target address.

# File lib/mobius/client/app.rb, line 72
def transfer(amount, address)
  amount = cast_amount(amount)
  raise Mobius::Client::Error::InsufficientFunds if app_balance < amount
  submit_tx { |operations| operations << payment_op(amount, dest: address, src: user_keypair) }
rescue Faraday::ClientError => err
  handle(err)
end
user_account() click to toggle source

Returns user account @return [Mobius::Client::Blockchain::Account] User Account

# File lib/mobius/client/app.rb, line 113
def user_account
  @user_account ||= Mobius::Client::Blockchain::Account.new(user_keypair)
end
user_balance() click to toggle source

Returns user balance. @return [Float] User balance.

# File lib/mobius/client/app.rb, line 30
def user_balance
  validate!
  user_account.balance
end
user_keypair() click to toggle source

Returns user keypair @return [Stellar::KeyPair] User KeyPair

# File lib/mobius/client/app.rb, line 101
def user_keypair
  @user_keypair ||= Mobius::Client.to_keypair(address)
end

Private Instance Methods

base64(txn) click to toggle source
# File lib/mobius/client/app.rb, line 138
def base64(txn)
  txn.to_envelope(app_keypair).to_xdr(:base64)
end
calc_fee(txn) click to toggle source
# File lib/mobius/client/app.rb, line 134
def calc_fee(txn)
  txn.fee = FEE * txn.operations.size
end
cast_amount(amount) click to toggle source
# File lib/mobius/client/app.rb, line 166
def cast_amount(amount)
  Float(amount).to_d
rescue ArgumentError
  raise Mobius::Client::Error::InvalidAmount, "Invalid amount provided: `#{amount}`"
end
handle(err) click to toggle source
# File lib/mobius/client/app.rb, line 159
def handle(err)
  ops = err.response.dig(:body, "extras", "result_codes", "operations")
  raise Mobius::Client::Error::AccountMissing if ops.include?("op_no_destination")
  raise Mobius::Client::Error::TrustlineMissing if ops.include?("op_no_trust")
  raise err
end
payment_op(amount, dest:, src:) click to toggle source
# File lib/mobius/client/app.rb, line 146
def payment_op(amount, dest:, src:)
  Stellar::Operation.payment(
    source_account: Mobius::Client.to_keypair(src),
    destination: Mobius::Client.to_keypair(dest),
    amount: Stellar::Amount.new(amount, Mobius::Client.stellar_asset).to_payment
  )
end
post_txe(txe) click to toggle source
# File lib/mobius/client/app.rb, line 142
def post_txe(txe)
  Mobius::Client.horizon_client.horizon.transactions._post(tx: txe)
end
submit_tx() { |operations| ... } click to toggle source
# File lib/mobius/client/app.rb, line 119
def submit_tx
  return unless block_given?

  tx = Stellar::Transaction.for_account(
    account: user_keypair,
    sequence: user_account.next_sequence_value
  )

  yield(tx.operations)
  calc_fee(tx)
  txe = base64(tx)

  post_txe(txe).tap { [app_account, user_account].each(&:reload!) }
end
validate!() click to toggle source
# File lib/mobius/client/app.rb, line 154
def validate!
  raise Mobius::Client::Error::AuthorisationMissing unless authorized?
  raise Mobius::Client::Error::TrustlineMissing unless user_account.trustline_exists?
end