Class: Gtmtech::Crypto::Subcommands::Txn
- Inherits:
-
Gtmtech::Crypto::Subcommand
- Object
- Gtmtech::Crypto::Subcommand
- Gtmtech::Crypto::Subcommands::Txn
- Defined in:
- lib/gtmtech/crypto/subcommands/txn.rb
Class Method Summary collapse
- .create ⇒ Object
- .delete ⇒ Object
- .description ⇒ Object
- .execute ⇒ Object
- .list ⇒ Object
- .options ⇒ Object
- .usage ⇒ Object
Methods inherited from Gtmtech::Crypto::Subcommand
all_options, error, find, hidden?, parse, prettyname, validate
Class Method Details
.create ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 68 def self.create self.error "--date required" unless @@options[:date_given] self.error "--date must be in format \"now\" or YYYY-MM-DD,hh:mm:ss" unless (@@options[:date] == "now" or @@options[:date] =~ /\d\d\d\d\-\d\d\-\d\d,\d\d:\d\d:\d\d/) self.error "--from required" unless @@options[:from_given] self.error "--from must be in format <name>.<currency>=<amount>" unless @@options[:from] =~ /^\w+\.\w+=[\d\.]+$/ self.error "--to required" unless @@options[:to_given] self.error "--to must be in format <name>.<currency>=<amount>" unless @@options[:to] =~ /^\w+\.\w+=[\d\.]+$/ if @@options[:fees_given] self.error "--fees must be in format <name>.<currency>=<amount>" unless @@options[:fees] =~ /^\w+\.\w+=[\d\.]+$/ end source_account = @@options[:from].split(".")[0] source_currency = @@options[:from].split(".")[1].split("=")[0] source_amount = @@options[:from].split("=")[1] self.error "--from: txn amount must be a decimal or integer" unless source_amount =~ /^\d*\.\d+$/ or source_amount =~ /^\d+$/ dest_account = @@options[:to].split(".")[0] dest_currency = @@options[:to].split(".")[1].split("=")[0] dest_amount = @@options[:to].split("=")[1] self.error "--to: txn amount must be a decimal or integer" unless dest_amount =~ /^\d*\.\d+$/ or dest_amount =~ /^\d+$/ self.error "cannot create a self-referential transaction" if source_account == dest_account and source_currency == dest_currency fees_account = nil fees_currency = nil fees_amount = nil if @@options[:fees_given] fees_account = @@options[:fees].split(".")[0] fees_currency = @@options[:fees].split(".")[1].split("=")[0] fees_amount = @@options[:fees].split("=")[1] end Data.load self.error "The account #{source_account}.#{source_currency} does not yet exist" unless Data.account_exists? source_account, source_currency self.error "The account #{dest_account}.#{dest_currency} does not yet exist" unless Data.account_exists? dest_account, dest_currency Data.add_transaction @@options[:date], source_account, source_currency, source_amount, dest_account, dest_currency, dest_amount, fees_account, fees_currency, fees_amount, @@options[ :references ], @@options[ :percentage ] Data.save end |
.delete ⇒ Object
115 116 117 118 119 120 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 115 def self.delete self.error "--id required" unless @@options[:id_given] Data.load Data.delete_transaction @@options[:id] Data.save end |
.description ⇒ Object
9 10 11 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 9 def self.description "manage accounts" end |
.execute ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 122 def self.execute verb = ARGV.shift case verb.downcase when "new", "create", "add" self.create when "list" self.list when "delete" self.delete else self.error "transaction takes an action [new, list, delete] . See --help for more info" end end |
.list ⇒ Object
110 111 112 113 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 110 def self.list Data.load Data.list_transactions end |
.options ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 34 def self. [{:name => :date, :description => "Date of transaction (in format 2017-06-24,11:23:45 or \"now\")", :short => 'd', :type => :string}, {:name => :from, :description => "Source of the transaction in the form <name>.<currency>=<sent amount> e.g. barclays.GBP=20.50", :short => 'n', :type => :string}, {:name => :to, :description => "Destination of the transaction in the form <name>.<currency>=<received amount> e.g. kraken.BTC=0.40503", :short => 't', :type => :string}, {:name => :fees, :description => "Any fees EXTERNAL to the transaction, in the form <name>.<currency>=<amount> e.g. kraken.BTC=0.00001", :short => 'f', :type => :string}, {:name => :id, :description => "Specify the id you wish to amend/delete in a delete operation", :short => 'i', :type => :string}, {:name => :references, :description => "Specify any thirdparty / blockchain reference IDs associated with the transaction (Optional)", :short => 'r', :type => :string, :default => ""}, {:name => :percentage, :description => "Percentage of the transaction which actually applies to this profile (for the case of a transaction which spans multiple profiles and should only be counted in part) (Optional)", :short => 'p', :type => :string, :default => "100.0"} ] end |
.usage ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gtmtech/crypto/subcommands/txn.rb', line 13 def self.usage <<-EOS Usage (crypto #{self.prettyname}) crypto #{self.prettyname} new --date=<s> --from=<s> --to=<s> [--fees=<s>] [--references=<s>] [--percentage=<s>] - create a new transaction crypto #{self.prettyname} delete --id=<s> - delete a transaction crypto #{self.prettyname} reconcile - reconcile all transactions crypto #{self.prettyname} list - list all transactions Options: EOS end |