Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit 241ebcd

Browse files
authored
Allow asset objects to be passed instead of list of parameters (#59)
1 parent 7862800 commit 241ebcd

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

lib/stellar/operation.rb

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def self.make(attributes={})
4343
# Stellar::PaymentOp body
4444
def self.payment(attributes={})
4545
destination = attributes[:destination]
46-
asset, amount = extract_amount(attributes[:amount])
46+
asset, amount = get_asset_amount(attributes[:amount])
4747

4848
raise ArgumentError unless destination.is_a?(KeyPair)
4949

@@ -96,9 +96,11 @@ def self.path_payment(attributes={})
9696
#
9797
def self.path_payment_strict_receive(attributes={})
9898
destination = attributes[:destination]
99-
asset, amount = extract_amount(attributes[:amount])
100-
send_asset, send_max = extract_amount(attributes[:with])
101-
path = (attributes[:path] || []).map{|p| Stellar::Asset.send(*p)}
99+
asset, amount = get_asset_amount(attributes[:amount])
100+
send_asset, send_max = get_asset_amount(attributes[:with])
101+
path = (attributes[:path] || []).map{
102+
|p| p.is_a?(Array) ? Stellar::Asset.send(*p) : p
103+
}
102104

103105
raise ArgumentError unless destination.is_a?(KeyPair)
104106

@@ -132,9 +134,11 @@ def self.path_payment_strict_receive(attributes={})
132134
#
133135
def self.path_payment_strict_send(attributes={})
134136
destination = attributes[:destination]
135-
asset, dest_min = extract_amount(attributes[:amount])
136-
send_asset, send_amount = extract_amount(attributes[:with])
137-
path = (attributes[:path] || []).map{|p| Stellar::Asset.send(*p)}
137+
asset, dest_min = get_asset_amount(attributes[:amount])
138+
send_asset, send_amount = get_asset_amount(attributes[:with])
139+
path = (attributes[:path] || []).map{
140+
|p| p.is_a?(Array) ? Stellar::Asset.send(*p) : p
141+
}
138142

139143
raise ArgumentError unless destination.is_a?(KeyPair)
140144

@@ -199,11 +203,17 @@ def self.change_trust(attributes={})
199203
end
200204

201205
def self.manage_sell_offer(attributes={})
202-
buying = Asset.send(*attributes[:buying])
203-
selling = Asset.send(*attributes[:selling])
204-
amount = interpret_amount(attributes[:amount])
205-
offer_id = attributes[:offer_id] || 0
206-
price = interpret_price(attributes[:price])
206+
buying = attributes[:buying]
207+
if buying.is_a?(Array)
208+
buying = Asset.send(*buying)
209+
end
210+
selling = attributes[:selling]
211+
if selling.is_a?(Array)
212+
selling = Asset.send(*selling)
213+
end
214+
amount = interpret_amount(attributes[:amount])
215+
offer_id = attributes[:offer_id] || 0
216+
price = interpret_price(attributes[:price])
207217

208218
op = ManageSellOfferOp.new({
209219
buying: buying,
@@ -219,11 +229,17 @@ def self.manage_sell_offer(attributes={})
219229
end
220230

221231
def self.manage_buy_offer(attributes={})
222-
buying = Asset.send(*attributes[:buying])
223-
selling = Asset.send(*attributes[:selling])
224-
amount = interpret_amount(attributes[:amount])
225-
offer_id = attributes[:offer_id] || 0
226-
price = interpret_price(attributes[:price])
232+
buying = attributes[:buying]
233+
if buying.is_a?(Array)
234+
buying = Asset.send(*buying)
235+
end
236+
selling = attributes[:selling]
237+
if selling.is_a?(Array)
238+
selling = Asset.send(*selling)
239+
end
240+
amount = interpret_amount(attributes[:amount])
241+
offer_id = attributes[:offer_id] || 0
242+
price = interpret_price(attributes[:price])
227243

228244
op = ManageBuyOfferOp.new({
229245
buying: buying,
@@ -239,10 +255,16 @@ def self.manage_buy_offer(attributes={})
239255
end
240256

241257
def self.create_passive_sell_offer(attributes={})
242-
buying = Asset.send(*attributes[:buying])
243-
selling = Asset.send(*attributes[:selling])
244-
amount = interpret_amount(attributes[:amount])
245-
price = interpret_price(attributes[:price])
258+
buying = attributes[:buying]
259+
if buying.is_a?(Array)
260+
buying = Asset.send(*buying)
261+
end
262+
selling = attributes[:selling]
263+
if selling.is_a?(Array)
264+
selling = Asset.send(*selling)
265+
end
266+
amount = interpret_amount(attributes[:amount])
267+
price = interpret_price(attributes[:price])
246268

247269
op = CreatePassiveSellOfferOp.new({
248270
buying: buying,
@@ -309,9 +331,12 @@ def self.set_options(attributes={})
309331
def self.allow_trust(attributes={})
310332
op = AllowTrustOp.new()
311333

312-
trustor = attributes[:trustor]
334+
trustor = attributes[:trustor]
313335
authorize = attributes[:authorize]
314-
asset = Asset.send(*attributes[:asset])
336+
asset = attributes[:asset]
337+
if asset.is_a?(Array)
338+
asset = Asset.send(*asset)
339+
end
315340

316341
raise ArgumentError, "Bad :trustor" unless trustor.is_a?(Stellar::KeyPair)
317342
raise ArgumentError, "Bad :authorize" unless authorize == !!authorize # check boolean
@@ -407,9 +432,13 @@ def self.bump_sequence(attributes={})
407432
end
408433

409434
private
410-
def self.extract_amount(a)
411-
amount = interpret_amount(a.last)
412-
asset = Stellar::Asset.send(*a[0...-1])
435+
def self.get_asset_amount(values)
436+
amount = interpret_amount(values.last)
437+
if values[0].is_a?(Stellar::Asset)
438+
asset = values.first
439+
else
440+
asset = Stellar::Asset.send(*values[0...-1])
441+
end
413442

414443
return asset, amount
415444
end

spec/lib/stellar/operation_spec.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
require 'spec_helper'
22

33
describe Stellar::Operation, ".payment" do
4-
5-
64
it "correctly translates the provided amount to the native representation" do
75
op = Stellar::Operation.payment(destination: Stellar::KeyPair.random, amount: [:native, 20])
86
expect(op.body.value.amount).to eql(20_0000000)
97
op = Stellar::Operation.payment(destination: Stellar::KeyPair.random, amount: [:native, "20"])
108
expect(op.body.value.amount).to eql(20_0000000)
119
end
12-
1310
end
1411

1512
def pk_to_address(pk)
@@ -22,13 +19,14 @@ def pk_to_address(pk)
2219
let(:send_asset){ Stellar::Asset.alphanum4("USD", send_asset_issuer) }
2320
let(:dest_asset_issuer){ Stellar::KeyPair.master }
2421
let(:dest_asset){ Stellar::Asset.alphanum4("EUR", dest_asset_issuer) }
25-
let(:amount){ [:alphanum4, dest_asset.code, dest_asset_issuer, 9.2] }
26-
let(:with){ [:alphanum4, send_asset.code, send_asset_issuer, 10] }
22+
let(:amount){ [Stellar::Asset.alphanum4(dest_asset.code, dest_asset_issuer), 9.2] }
23+
let(:with){ [Stellar::Asset.alphanum4(send_asset.code, send_asset_issuer), 10] }
2724

2825
describe Stellar::Operation, ".path_payment" do
2926
it "works" do
3027
destination = Stellar::KeyPair.random
31-
amount = [:alphanum4, "USD", Stellar::KeyPair.master, 10]
28+
# test both forms of arrays
29+
amount = [Stellar::Asset.alphanum4("USD", Stellar::KeyPair.master), 10]
3230
with = [:alphanum4, "EUR", Stellar::KeyPair.master, 9.2]
3331

3432
op = Stellar::Operation.path_payment(
@@ -98,7 +96,7 @@ def pk_to_address(pk)
9896
let(:asset) { Stellar::Asset.alphanum4("USD", issuer) }
9997

10098
it "creates a ChangeTrustOp" do
101-
op = Stellar::Operation.change_trust(line: [:alphanum4, "USD", issuer])
99+
op = Stellar::Operation.change_trust(line: Stellar::Asset.alphanum4("USD", issuer))
102100
expect(op.body.value).to be_an_instance_of(Stellar::ChangeTrustOp)
103101
expect(op.body.value.line).to eq(Stellar::Asset.alphanum4("USD", issuer))
104102
expect(op.body.value.limit).to eq(9223372036854775807)
@@ -119,15 +117,15 @@ def pk_to_address(pk)
119117
end
120118

121119
it "creates a ChangeTrustOp with limit" do
122-
op = Stellar::Operation.change_trust(line: [:alphanum4, "USD", issuer], limit: 1234.75)
120+
op = Stellar::Operation.change_trust(line: Stellar::Asset.alphanum4("USD", issuer), limit: 1234.75)
123121
expect(op.body.value).to be_an_instance_of(Stellar::ChangeTrustOp)
124122
expect(op.body.value.line).to eq(Stellar::Asset.alphanum4("USD", issuer))
125123
expect(op.body.value.limit).to eq(12347500000)
126124
end
127125

128126
it "throws ArgumentError for incorrect limit argument" do
129127
expect {
130-
Stellar::Operation.change_trust(line: [:alphanum4, "USD", issuer], limit: true)
128+
Stellar::Operation.change_trust(line: Stellar::Asset.alphanum4("USD", issuer), limit: true)
131129
}.to raise_error(ArgumentError)
132130
end
133131
end

0 commit comments

Comments
 (0)