Class: Excoin::Account::Wallet

Inherits:
Object
  • Object
show all
Defined in:
lib/account/wallet.rb

Defined Under Namespace

Classes: Deposit, Withdrawal

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active, wallet_data) ⇒ Wallet

Returns a new instance of Wallet.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/account/wallet.rb', line 7

def initialize(active, wallet_data)
  begin
    if active
      @status = "active"
      @currency = wallet_data['currency']
      @deposits = Hash.new
      @withdrawals = Hash.new
      @deposit_address = wallet_data['address']
      @confirmed_balance = BigDecimal.new(wallet_data['confirmed_balance'])
      @available_balance = BigDecimal.new(wallet_data['available_balance'])
      @order_balance = BigDecimal.new(wallet_data['order_balance'])
      @pending_deposit_balance = BigDecimal.new(wallet_data['pending_deposit_balance'])
      @pending_withdrawal_balance = BigDecimal.new(wallet_data['pending_withdrawal_balance'])
    else
      @status = "inactive"
      @currency = wallet_data['currency']
      @deposit_address = wallet_data['address']
    end
  rescue
    puts "Error in Excoin::Account::Wallet.initialize"
    puts wallet_data
  end
end

Instance Attribute Details

#available_balanceObject (readonly)

Returns the value of attribute available_balance.



2
3
4
# File 'lib/account/wallet.rb', line 2

def available_balance
  @available_balance
end

#confirmed_balanceObject (readonly)

Returns the value of attribute confirmed_balance.



2
3
4
# File 'lib/account/wallet.rb', line 2

def confirmed_balance
  @confirmed_balance
end

#currencyObject (readonly)

Returns the value of attribute currency.



2
3
4
# File 'lib/account/wallet.rb', line 2

def currency
  @currency
end

#deposit_addressObject (readonly)

Returns the value of attribute deposit_address.



2
3
4
# File 'lib/account/wallet.rb', line 2

def deposit_address
  @deposit_address
end

#depositsObject (readonly)

Returns the value of attribute deposits.



2
3
4
# File 'lib/account/wallet.rb', line 2

def deposits
  @deposits
end

#order_balanceObject (readonly)

Returns the value of attribute order_balance.



2
3
4
# File 'lib/account/wallet.rb', line 2

def order_balance
  @order_balance
end

#pending_deposit_balanceObject (readonly)

Returns the value of attribute pending_deposit_balance.



2
3
4
# File 'lib/account/wallet.rb', line 2

def pending_deposit_balance
  @pending_deposit_balance
end

#pending_withdrawal_balanceObject (readonly)

Returns the value of attribute pending_withdrawal_balance.



2
3
4
# File 'lib/account/wallet.rb', line 2

def pending_withdrawal_balance
  @pending_withdrawal_balance
end

#statusObject (readonly)

Returns the value of attribute status.



2
3
4
# File 'lib/account/wallet.rb', line 2

def status
  @status
end

#withdrawalsObject (readonly)

Returns the value of attribute withdrawals.



2
3
4
# File 'lib/account/wallet.rb', line 2

def withdrawals
  @withdrawals
end

Instance Method Details

#add_deposit(deposit_data) ⇒ Object



55
56
57
# File 'lib/account/wallet.rb', line 55

def add_deposit(deposit_data)
  @deposits.merge!({deposit_data['txid'] => Deposit.new(deposit_data)})
end

#add_withdrawal(withdrawal_data) ⇒ Object



59
60
61
# File 'lib/account/wallet.rb', line 59

def add_withdrawal(withdrawal_data)
  @withdrawals.merge!({withdrawal_data['id'] => Withdrawal.new(withdrawal_data)})
end

#unconfirmed_depositsObject



47
48
49
# File 'lib/account/wallet.rb', line 47

def unconfirmed_deposits
  return @deposits.select{|id, deposit_object| deposit_object.confirmed == false}
end

#unconfirmed_withdrawalsObject



51
52
53
# File 'lib/account/wallet.rb', line 51

def unconfirmed_withdrawals
  return @withdrawals.select{|id, withdrawal_object| withdrawal_object.confirmed == false}
end

#update(wallet_data) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/account/wallet.rb', line 31

def update(wallet_data)
  begin
    if wallet_data['address']
      @deposit_address = wallet_data['address']
    end
    @confirmed_balance = BigDecimal.new(wallet_data['confirmed_balance'])
    @available_balance = BigDecimal.new(wallet_data['available_balance'])
    @order_balance = BigDecimal.new(wallet_data['order_balance'])
    @pending_deposit_balance = BigDecimal.new(wallet_data['pending_deposit_balance'])
    @pending_withdrawal_balance = BigDecimal.new(wallet_data['pending_withdrawal_balance'])
  rescue
    puts "Error in Excoin::Account::Wallet.update"
    puts wallet_data
  end
end

#withdraw(address, amount) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/account/wallet.rb', line 63

def withdraw(address, amount)
  if BigDecimal.new(amount) <= self.available_balance
    withdrawal_data = Excoin.api.(self.currency, address, amount)
    Excoin..add_withdrawal(withdrawal_data)
  else
    puts "Insufficient funds for withdrawal"
  end
end