stellar_core_commander 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92445ff35302b2b1dd07d27a73691fd1350e9d1b
4
- data.tar.gz: 00d2f4a74b10243d7ca7c0dbc35da9df143b488a
3
+ metadata.gz: 69125ec58eb849d896c5fc814cc969c81a1cb652
4
+ data.tar.gz: b60d9215848229eb69ead17594c59a84147bc8f6
5
5
  SHA512:
6
- metadata.gz: 5685616a9700bb4fa092d391be34fdd0915a7ddc37950eb99083ac485545769ca5d53aba1f5f80975caafb8be6a9054d15f30b1694ad6fadcaf4cee254c46e81
7
- data.tar.gz: b60e6f66d0904aedc4c725484e8dd32518e5a55f2fbc19521fdc70a1d888da4e15dd3e9303009cb7b26149f1e10a03c8a9f632c4bda56f7a96cd97591bf85405
6
+ metadata.gz: c14d87026ec3a2d740a92467f20825f748ac288b29d9c40c9f9b6d26f41c61991da9e46c92cd72e1772f4ba82baca6f644201dc98ce3bea1e7f88b52998f7c21
7
+ data.tar.gz: ff2c49f39722e4e717be0567aa29d2d01d6402942f7cb3b28dc039b702465658cf5878a204c70d88f34be30c5a6fa97b059b1fa8e0a57c0713cbab3afd4988bd
data/.gitignore CHANGED
@@ -13,3 +13,4 @@
13
13
  *.a
14
14
  mkmf.log
15
15
  out.sql
16
+ .DS_Store
@@ -0,0 +1,24 @@
1
+ account :usd_gateway
2
+ account :scott
3
+ account :andrew
4
+
5
+ payment :master, :usd_gateway, [:native, 1000_000000]
6
+ payment :master, :scott, [:native, 1000_000000]
7
+ payment :master, :andrew, [:native, 1000_000000]
8
+
9
+ close_ledger
10
+
11
+ require_trust_auth :usd_gateway
12
+
13
+ close_ledger
14
+
15
+ trust :scott, :usd_gateway, "USD"
16
+ trust :andrew, :usd_gateway, "USD"
17
+
18
+ close_ledger
19
+
20
+ allow_trust :usd_gateway, :scott, "USD"
21
+
22
+ # close_ledger
23
+
24
+ # payment :usd_gateway, :scott, ["USD", :usd_gateway, 1000_000000]
@@ -27,8 +27,8 @@ payment :eur_gateway, :bartek, ["EUR", :eur_gateway, 1000_000000]
27
27
 
28
28
  close_ledger
29
29
 
30
- offer :offer_1, :andrew, ["EUR", :eur_gateway], ["USD", :usd_gateway], 200_000000, 1.0
30
+ offer :andrew, {buy:["USD", :usd_gateway], with:["EUR", :eur_gateway]}, 200_000000, 1.0
31
31
 
32
32
  close_ledger
33
33
 
34
- payment :scott, :bartek, ["EUR", :eur_gateway, 10], path: [["USD", :usd_gateway], ["EUR", :eur_gateway]]
34
+ payment :scott, :bartek, ["EUR", :eur_gateway, 10], path: [["USD", :usd_gateway]]
data/examples/trade.rb ADDED
@@ -0,0 +1,31 @@
1
+ account :usd_gateway
2
+ account :eur_gateway
3
+ account :scott
4
+ account :bartek
5
+
6
+ payment :master, :usd_gateway, [:native, 1000_000000]
7
+ payment :master, :eur_gateway, [:native, 1000_000000]
8
+
9
+ payment :master, :scott, [:native, 1000_000000]
10
+ payment :master, :bartek, [:native, 1000_000000]
11
+
12
+ close_ledger
13
+
14
+ trust :scott, :usd_gateway, "USD"
15
+ trust :bartek, :usd_gateway, "USD"
16
+ trust :scott, :eur_gateway, "EUR"
17
+ trust :bartek, :eur_gateway, "EUR"
18
+
19
+ close_ledger
20
+
21
+ payment :usd_gateway, :scott, ["USD", :usd_gateway, 1000_000000]
22
+ payment :eur_gateway, :bartek, ["EUR", :eur_gateway, 1000_000000]
23
+
24
+ close_ledger
25
+
26
+ offer :bartek, {buy:["USD", :usd_gateway], with:["EUR", :eur_gateway]}, 1000_000000, 1.0
27
+
28
+ close_ledger
29
+
30
+ offer :scott, {sell:["USD", :usd_gateway], for:["EUR", :eur_gateway]}, 500_000000, 1.0
31
+
@@ -15,6 +15,7 @@ module StellarCoreCommander
15
15
  autoload :Commander
16
16
  autoload :Process
17
17
  autoload :Transactor
18
+ autoload :OperationBuilder
18
19
 
19
20
  autoload :Convert
20
21
  end
@@ -0,0 +1,138 @@
1
+ module StellarCoreCommander
2
+
3
+ class OperationBuilder
4
+ include Contracts
5
+
6
+ Currency = [String, Symbol]
7
+ Amount = Any #TODO
8
+
9
+ OfferCurrencies = Or[
10
+ {sell:Currency, for: Currency},
11
+ {buy:Currency, with: Currency},
12
+ ]
13
+
14
+ Contract Transactor => Any
15
+ def initialize(transactor)
16
+ @transactor = transactor
17
+ end
18
+
19
+ Contract Symbol, Symbol, Amount, Or[{}, {path: Any}] => Any
20
+ def payment(from, to, amount, options={})
21
+ from = get_account from
22
+ to = get_account to
23
+
24
+ if amount.first != :native
25
+ amount = [:iso4217] + amount
26
+ amount[2] = get_account(amount[2])
27
+ amount[1] = amount[1].ljust(4, "\x00")
28
+ end
29
+
30
+ attrs = {
31
+ account: from,
32
+ destination: to,
33
+ sequence: next_sequence(from),
34
+ amount: amount,
35
+ }
36
+
37
+ if options[:path]
38
+ attrs[:path] = options[:path].map{|p| make_currency p}
39
+ end
40
+
41
+ Stellar::Transaction.payment(attrs).to_envelope(from)
42
+ end
43
+
44
+ Contract Symbol, Symbol, String => Any
45
+ def trust(account, issuer, code)
46
+ change_trust account, issuer, code, (2**63)-1
47
+ end
48
+
49
+ Contract Symbol, Symbol, String, Num => Any
50
+ def change_trust(account, issuer, code, limit)
51
+ account = get_account account
52
+
53
+ Stellar::Transaction.change_trust({
54
+ account: account,
55
+ sequence: next_sequence(account),
56
+ line: make_currency([code, issuer]),
57
+ limit: limit
58
+ }).to_envelope(account)
59
+ end
60
+
61
+ Contract Symbol, Symbol, String, Num => Any
62
+ def allow_trust(account, trustor, code)
63
+ currency = make_currency([code, account])
64
+ account = get_account account
65
+ trustor = get_account trustor
66
+
67
+
68
+ Stellar::Transaction.allow_trust({
69
+ account: account,
70
+ sequence: next_sequence(account),
71
+ currency: currency,
72
+ trustor: trustor,
73
+ authorize: true,
74
+ }).to_envelope(account)
75
+ end
76
+
77
+ Contract Symbol, OfferCurrencies, Num, Num => Any
78
+ def offer(account, currencies, amount, price)
79
+ account = get_account account
80
+
81
+ if currencies.has_key?(:sell)
82
+ taker_pays = make_currency currencies[:for]
83
+ taker_gets = make_currency currencies[:sell]
84
+ else
85
+ taker_pays = make_currency currencies[:buy]
86
+ taker_gets = make_currency currencies[:with]
87
+ end
88
+
89
+ Stellar::Transaction.create_offer({
90
+ account: account,
91
+ sequence: next_sequence(account),
92
+ taker_gets: taker_gets,
93
+ taker_pays: taker_pays,
94
+ amount: amount,
95
+ price: price,
96
+ }).to_envelope(account)
97
+ end
98
+
99
+
100
+ Contract Symbol => Any
101
+ def require_trust_auth(account)
102
+ set_flags account, [:auth_required_flag]
103
+ end
104
+
105
+ Contract Symbol, ArrayOf[Symbol] => Any
106
+ def set_flags(account, flags)
107
+ account = get_account account
108
+
109
+ tx = Stellar::Transaction.set_options({
110
+ account: account,
111
+ sequence: next_sequence(account),
112
+ set: make_account_flags(flags),
113
+ })
114
+
115
+ tx.to_envelope(account)
116
+ end
117
+
118
+ private
119
+
120
+ delegate :get_account, to: :@transactor
121
+ delegate :next_sequence, to: :@transactor
122
+
123
+ Contract Currency => [Symbol, String, Stellar::KeyPair]
124
+ def make_currency(input)
125
+ code, issuer = *input
126
+ code = code.ljust(4, "\x00")
127
+ issuer = get_account issuer
128
+
129
+ [:iso4217, code, issuer]
130
+ end
131
+
132
+ def make_account_flags(flags=nil)
133
+ flags ||= []
134
+ flags.map{|f| Stellar::AccountFlags.send(f)}
135
+ end
136
+
137
+ end
138
+ end
@@ -148,7 +148,7 @@ module StellarCoreCommander
148
148
  body = ActiveSupport::JSON.decode(response.body)
149
149
 
150
150
  unless body["wasReceived"] == true
151
- raise "transaction failed: #{body["result"]}"
151
+ raise "transaction failed: #{body.inspect}"
152
152
  end
153
153
 
154
154
  hex_tr = body["result"]
@@ -11,19 +11,16 @@ module StellarCoreCommander
11
11
 
12
12
  class FailedTransaction < StandardError ; end
13
13
 
14
- Currency = [String, Symbol]
15
- Amount = Any #TODO
16
-
17
14
  Contract Process => Any
18
15
  def initialize(process)
19
16
  @process = process
20
17
  @named = {}.with_indifferent_access
21
18
  @unverified = []
19
+ @operation_builder = OperationBuilder.new(self)
22
20
  account :master, Stellar::KeyPair.from_raw_seed("allmylifemyhearthasbeensearching")
23
21
  end
24
22
 
25
23
  Contract String => Any
26
-
27
24
  #
28
25
  # Runs the provided recipe against the process identified by @process
29
26
  #
@@ -53,85 +50,59 @@ module StellarCoreCommander
53
50
  add_named name, keypair
54
51
  end
55
52
 
56
- Contract Symbol, Symbol, Amount, Or[{}, {path: Any}] => Any
57
- def payment(from, to, amount, options={})
58
- from = get_account from
59
- to = get_account to
60
-
61
- if amount.first != :native
62
- amount = [:iso4217] + amount
63
- amount[2] = get_account(amount[2])
64
- amount[1] = amount[1].ljust(4, "\x00")
65
- end
66
-
67
- attrs = {
68
- account: from,
69
- destination: to,
70
- sequence: next_sequence(from),
71
- amount: amount,
72
- }
73
53
 
74
- if options[:path]
75
- attrs[:path] = options[:path].map{|p| make_currency p}
76
- end
77
- envelope = Stellar::Transaction.payment(attrs).to_envelope(from)
54
+ #
55
+ # @see StellarCoreCommander::OperationBuilder#payment
56
+ def payment(*args)
57
+ envelope = @operation_builder.payment(*args)
78
58
 
79
59
  submit_transaction envelope do |result|
80
60
  payment_result = result.result.results!.first.tr!.payment_result!
81
-
82
61
  raise FailedTransaction unless payment_result.code.value >= 0
83
62
  end
84
63
  end
85
64
 
86
- Contract Symbol, Symbol, String => Any
87
- def trust(account, issuer, code)
88
- change_trust account, issuer, code, (2**63)-1
89
- end
90
-
91
- Contract Symbol, Symbol, String, Num => Any
92
- def change_trust(account, issuer, code, limit)
93
- account = get_account account
94
-
95
- tx = Stellar::Transaction.change_trust({
96
- account: account,
97
- sequence: next_sequence(account),
98
- line: make_currency([code, issuer]),
99
- limit: limit
100
- })
101
-
102
- envelope = tx.to_envelope(account)
65
+ #
66
+ # @see StellarCoreCommander::OperationBuilder#trust
67
+ def trust(*args)
68
+ envelope = @operation_builder.trust(*args)
69
+ submit_transaction envelope
70
+ end
103
71
 
72
+ #
73
+ # @see StellarCoreCommander::OperationBuilder#change_trust
74
+ def change_trust(*args)
75
+ envelope = @operation_builder.change_trust(*args)
104
76
  submit_transaction envelope
105
77
  end
106
78
 
107
- Contract Symbol, Symbol, Currency, Currency, Num, Num => Any
108
- def offer(name, account, taker_gets, taker_pays, amount, price)
109
- account = get_account account
110
- taker_gets = make_currency taker_gets
111
- taker_pays = make_currency taker_pays
112
-
113
- tx = Stellar::Transaction.create_offer({
114
- account: account,
115
- sequence: next_sequence(account),
116
- taker_gets: taker_gets,
117
- taker_pays: taker_pays,
118
- amount: amount,
119
- price: price,
120
- })
121
-
122
- envelope = tx.to_envelope(account)
79
+ #
80
+ # @see StellarCoreCommander::OperationBuilder#offer
81
+ def offer(*args)
82
+ envelope = @operation_builder.offer(*args)
83
+ submit_transaction envelope
84
+ end
123
85
 
124
- submit_transaction envelope do |result|
125
- offer = begin
126
- co_result = result.result.results!.first.tr!.create_offer_result!
127
- co_result.success!.offer.offer!
128
- rescue
129
- raise FailedTransaction, "Could not extract offer from result:#{result.to_xdr(:base64)}"
130
- end
86
+ #
87
+ # @see StellarCoreCommander::OperationBuilder#require_trust_auth
88
+ def require_trust_auth(*args)
89
+ envelope = @operation_builder.require_trust_auth(*args)
90
+ submit_transaction envelope
91
+ end
131
92
 
132
- add_named name, offer
133
- end
93
+ #
94
+ # @see StellarCoreCommander::OperationBuilder#set_flags
95
+ def set_flags(*args)
96
+ envelope = @operation_builder.set_flags(*args)
97
+ submit_transaction envelope
134
98
  end
99
+
100
+ #
101
+ # @see StellarCoreCommander::OperationBuilder#allow_trust
102
+ def allow_trust(*args)
103
+ envelope = @operation_builder.allow_trust(*args)
104
+ submit_transaction envelope
105
+ end
135
106
 
136
107
  Contract None => Any
137
108
  #
@@ -147,8 +118,8 @@ module StellarCoreCommander
147
118
  result = validate_transaction envelope
148
119
  after_confirmation.call(result) if after_confirmation
149
120
  rescue FailedTransaction
150
- require 'pry'; binding.pry
151
121
  $stderr.puts "Failed to validate tx: #{Convert.to_hex envelope.tx.hash}"
122
+ $stderr.puts "failed result: #{result.to_xdr(:hex)}"
152
123
  exit 1
153
124
  end
154
125
  end
@@ -157,25 +128,6 @@ module StellarCoreCommander
157
128
  @unverified.clear
158
129
  end
159
130
 
160
- private
161
- Contract Symbol, Any => Any
162
- def add_named(name, object)
163
- if @named.has_key?(name)
164
- raise ArgumentError, "#{name} is already registered"
165
- end
166
-
167
- @named[name] = object
168
- end
169
-
170
- Contract Stellar::TransactionEnvelope, Or[nil, Proc] => Any
171
- def submit_transaction(envelope, &after_confirmation)
172
- hex = envelope.to_xdr(:hex)
173
- @process.submit_transaction hex
174
-
175
- # submit to process
176
- @unverified << [envelope, after_confirmation]
177
- end
178
-
179
131
  Contract Symbol => Stellar::KeyPair
180
132
  def get_account(name)
181
133
  @named[name].tap do |found|
@@ -193,13 +145,23 @@ module StellarCoreCommander
193
145
  base_sequence + inflight_count + 1
194
146
  end
195
147
 
196
- Contract Currency => [Symbol, String, Stellar::KeyPair]
197
- def make_currency(input)
198
- code, issuer = *input
199
- code = code.ljust(4, "\x00")
200
- issuer = get_account issuer
148
+ private
149
+ Contract Symbol, Any => Any
150
+ def add_named(name, object)
151
+ if @named.has_key?(name)
152
+ raise ArgumentError, "#{name} is already registered"
153
+ end
154
+
155
+ @named[name] = object
156
+ end
157
+
158
+ Contract Stellar::TransactionEnvelope, Or[nil, Proc] => Any
159
+ def submit_transaction(envelope, &after_confirmation)
160
+ hex = envelope.to_xdr(:hex)
161
+ @process.submit_transaction hex
201
162
 
202
- [:iso4217, code, issuer]
163
+ # submit to process
164
+ @unverified << [envelope, after_confirmation]
203
165
  end
204
166
 
205
167
  Contract Stellar::TransactionEnvelope => Stellar::TransactionResult
@@ -209,12 +171,19 @@ module StellarCoreCommander
209
171
 
210
172
  base64_result = @process.transaction_result(hex_hash)
211
173
 
212
- raise "couldn't fine result for #{hex_hash}" if base64_result.blank?
174
+ raise "couldn't find result for #{hex_hash}" if base64_result.blank?
213
175
 
214
176
  raw_result = Convert.from_base64(base64_result)
215
177
 
216
178
  pair = Stellar::TransactionResultPair.from_xdr(raw_result)
217
- pair.result
179
+ result = pair.result
180
+
181
+ # ensure success for every operation
182
+ expected = Stellar::TransactionResultCode.tx_success
183
+ actual = result.result.code
184
+ raise "transaction failed" unless expected == actual
185
+
186
+ result
218
187
  end
219
188
 
220
189
  end
@@ -1,3 +1,3 @@
1
1
  module StellarCoreCommander
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_dependency "stellar-base", "~> 0.0.1"
20
+ spec.add_dependency "stellar-base", "= 0.0.5"
21
21
  spec.add_dependency "slop", "~> 3.6.0"
22
22
  spec.add_dependency "faraday", "~> 0.9.1"
23
23
  spec.add_dependency "faraday_middleware", "~> 0.9.1"
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stellar_core_commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Fleckenstein
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-27 00:00:00.000000000 Z
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stellar-base
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.0.1
19
+ version: 0.0.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.0.1
26
+ version: 0.0.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: slop
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -178,12 +178,15 @@ files:
178
178
  - README.md
179
179
  - Rakefile
180
180
  - bin/scc
181
+ - examples/allow_trust.rb
181
182
  - examples/non_native_payment.rb
182
183
  - examples/pathed_payment.rb
183
184
  - examples/simple_payment.rb
185
+ - examples/trade.rb
184
186
  - lib/stellar_core_commander.rb
185
187
  - lib/stellar_core_commander/commander.rb
186
188
  - lib/stellar_core_commander/convert.rb
189
+ - lib/stellar_core_commander/operation_builder.rb
187
190
  - lib/stellar_core_commander/process.rb
188
191
  - lib/stellar_core_commander/transactor.rb
189
192
  - lib/stellar_core_commander/version.rb