Class: Gtmtech::Crypto::Data
- Inherits:
-
Object
- Object
- Gtmtech::Crypto::Data
- Defined in:
- lib/gtmtech/crypto/data.rb
Class Method Summary collapse
- .account_exists?(name, currency) ⇒ Boolean
- .add_account(name, currency, type, url) ⇒ Object
- .add_transaction(date, source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount) ⇒ Object
- .delete_transaction(id) ⇒ Object
- .list_accounts ⇒ Object
- .list_transactions ⇒ Object
- .load ⇒ Object
- .reconcile_transactions(conversions) ⇒ Object
- .save ⇒ Object
Class Method Details
.account_exists?(name, currency) ⇒ Boolean
43 44 45 46 47 |
# File 'lib/gtmtech/crypto/data.rb', line 43 def self.account_exists? name, currency return false unless @@document[ "accounts" ].key? name return false unless @@document[ "accounts" ][ name ][ "currencies" ].include? currency true end |
.add_account(name, currency, type, url) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gtmtech/crypto/data.rb', line 22 def self.add_account name, currency, type, url if @@document[ "accounts" ].key? name currencies = @@document[ "accounts" ][ name ][ "currencies" ] currencies << currency @@document[ "accounts" ][ name ][ "currencies" ] = currencies.uniq.sort @@document[ "accounts" ][ name ][ "type" ] = type if type @@document[ "accounts" ][ name ][ "url" ] = url if url else @@document[ "accounts" ][ name ] = { "currencies" => [ currency ], "type" => type, "url" => url } end end |
.add_transaction(date, source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gtmtech/crypto/data.rb', line 49 def self.add_transaction date, source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount if date.downcase == "now" date = Time.now.to_s.split(" ").take(2).join(",") end source_amount = Utils.make_decimal source_amount dest_amount = Utils.make_decimal dest_amount id = SecureRandom.uuid.to_s @@document[ "transactions" ][ id ] = { "date" => date, "source_account" => source_account, "source_currency" => source_currency, "source_amount" => source_amount, "dest_account" => dest_account, "dest_currency" => dest_currency, "dest_amount" => dest_amount, "fees_account" => fees_account, "fees_currency" => fees_currency, "fees_amount" => fees_amount } end |
.delete_transaction(id) ⇒ Object
87 88 89 |
# File 'lib/gtmtech/crypto/data.rb', line 87 def self.delete_transaction id @@document[ "transactions" ].delete( id ) end |
.list_accounts ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/gtmtech/crypto/data.rb', line 34 def self.list_accounts puts "Accounts for profile \"#{ENV['CRYPTO_PROFILE'] || 'main'}\":" printf("%-10s %-10s %-60s\n", "name", "type", "currencies") puts "-" * 80 @@document[ "accounts" ].each do |name, account_info| printf("%-10s %-10s %-60s\n", name, account_info[ "type" ], account_info[ "currencies" ].join(",")) end end |
.list_transactions ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gtmtech/crypto/data.rb', line 70 def self.list_transactions puts "Transactions for profile \"#{ENV['CRYPTO_PROFILE'] || 'main'}\":" printf("%-36s %-20s %-15s %-15s %-15s %-15s %-30s\n", "id", "date", "src account", "src amount", "dest account", "dest amount", "ratio src/dest") puts "-" * 150 @@document[ "transactions" ].each do |id, txn| ratio = "" if txn["source_currency"] != txn["dest_currency"] ratio = Utils.decimal_divide( txn["source_amount"], txn["dest_amount"] ) end printf("%-36s %-20s %-15s %-15s %-15s %-15s %-30s\n", id, txn["date"], "#{txn["source_account"]}.#{txn["source_currency"]}", txn["source_amount"], "#{txn["dest_account"]}.#{txn["dest_currency"]}", txn["dest_amount"], ratio) ratio = "" if txn["fees_account"] printf("%-36s %-20s %-15s %-15s %-15s %-15s %-30s\n", id, txn["date"], "#{txn["fees_account"]}.#{txn["fees_currency"]}", txn["fees_amount"], "fees.#{txn["fees_currency"]}", txn["fees_amount"], ratio) end end end |
.load ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/gtmtech/crypto/data.rb', line 8 def self.load if ENV['CRYPTO_PROFILE'] @@path = "#{ENV['HOME']}/.crypto.#{ENV['CRYPTO_PROFILE']}.yaml" else @@path = "#{ENV['HOME']}/.crypto.yaml" end unless File.exist? @@path File.open(@@path, 'w') do |file| file.write("---\naccounts: {}\ntransactions: {}\n") end end @@document = YAML.load(File.read(@@path)) end |
.reconcile_transactions(conversions) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/gtmtech/crypto/data.rb', line 91 def self.reconcile_transactions conversions self.list_transactions puts "" printf("%-20s %-10s %-20s %-30s\n", "account", "currency", "amount", "GBP equivalent") puts "-" * 120 fees = {} currencies = {} @@document[ "accounts" ].each do | name, account_info | account_info[ "currencies" ].sort.each do | currency | decimal = "0.0" @@document[ "transactions" ].each do |id, txn| # explicit fees if txn[ "fees_account" ] == name and txn[ "fees_currency" ] == currency decimal = Utils.decimal_minus( decimal, txn[ "fees_amount" ] ) unless fees.key? currency fees[ currency ] = "0.0" end fees[ currency ] = Utils.decimal_add( fees[ currency ], txn[ "fees_amount" ]) end # account reconciliation if txn[ "source_account" ] == name and txn[ "source_currency" ] == currency decimal = Utils.decimal_minus( decimal, txn[ "source_amount" ] ) # implicit fees if txn[ "source_currency" ] == txn[ "dest_currency" ] and txn[ "source_amount" ] != txn[ "dest_amount" ] implicit_fees = Utils.decimal_minus( txn[ "source_amount" ], txn[ "dest_amount" ] ) unless fees.key? currency fees[ currency ] = "0.0" end fees[ currency ] = Utils.decimal_add( fees[ currency ], implicit_fees ) end end if txn[ "dest_account" ] == name and txn[ "dest_currency" ] == currency decimal = Utils.decimal_add( decimal, txn[ "dest_amount" ] ) end end unless currencies.key? currency currencies[ currency ] = "0.0" end currencies[ currency ] = Utils.decimal_add( currencies[ currency ], decimal ) gbp_equiv = "?" gbp_equiv = Utils.decimal_multiply( decimal, conversions[ currency ] ) if conversions.key? currency printf( "%-20s %-10s %-20s %-30s\n", name, currency, decimal, gbp_equiv ) end end fees.keys.sort.each do |currency| unless currencies.key? currency currencies[ currency ] = "0.0" end gbp_equiv = "?" gbp_equiv = Utils.decimal_multiply( fees[ currency ], conversions[ currency ] ) if conversions.key? currency printf( "%-20s %-10s %-20s %-30s\n", "**FEES**", currency, fees[ currency ], gbp_equiv) end puts "" printf( "%-10s %-20s\n", "currency", "reconciliation" ) puts "-" * 120 currencies.keys.sort.each do |currency| currency_fees = fees[ currency ] || "0.0" printf( "%-10s %-20s\n", currency, Utils.decimal_add( currencies[ currency ], currency_fees ) ) end gbp_total = "0.0" gbp_known = true puts "" printf( "%-10s %-20s %-20s\n", "currency", "totals", "GBP equivalent" ) puts "-" * 120 currencies.keys.sort.each do |currency| amount = currencies[ currency ] if conversions.key? currency gbp_equiv = Utils.decimal_multiply( amount, conversions[ currency ] ) printf( "%-10s %-20s %-20s\n", currency, currencies[ currency ], gbp_equiv ) gbp_total = Utils.decimal_add( gbp_total, gbp_equiv ) else printf( "%-10s %-20s %-20s\n", currency, currencies[ currency ], "?" ) gbp_known = false end end puts "" if gbp_known puts "GBP profit/loss (equivalent): #{gbp_total}" else puts "GBP profit/loss (equivalent): unknown - specify conversions on command-line" end end |
.save ⇒ Object
180 181 182 183 184 |
# File 'lib/gtmtech/crypto/data.rb', line 180 def self.save File.open(@@path, 'w') do |file| file.write( @@document.to_yaml ) end end |