Skip to content

Commit

Permalink
Fix payments param so it is possible to send multi-tx to the same add…
Browse files Browse the repository at this point in the history
…ress
  • Loading branch information
Piotr Stachyra committed Oct 15, 2020
1 parent b0c2ed8 commit 128b289
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
8 changes: 4 additions & 4 deletions lib/cardano_wallet/byron.rb
Expand Up @@ -181,7 +181,7 @@ def initialize opt
# @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/byronSelectCoins
#
# @example
# random(wid, {address1: 123, address2: 456})
# random(wid, [{addr1: 1000000}, {addr2: 1000000}])
def random(wid, payments)
payments_formatted = Utils.format_payments(payments)
self.class.post("/byron-wallets/#{wid}/coin-selections/random",
Expand Down Expand Up @@ -217,10 +217,10 @@ def list(wid, q = {})
# @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postByronTransaction
# @param wid [String] source wallet id
# @param passphrase [String] source wallet's passphrase
# @param payments [Hash] addres, amount pair
# @param payments [Array of Hashes] addres, amount pair
#
# @example
# create(wid, passphrase, {addr1: 1000000})
# create(wid, passphrase, [{addr1: 1000000}, {addr2: 1000000}])
def create(wid, passphrase, payments)
payments_formatted = Utils.format_payments(payments)
self.class.post("/byron-wallets/#{wid}/transactions",
Expand All @@ -234,7 +234,7 @@ def create(wid, passphrase, payments)
# @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postTransactionFee
#
# @example
# payment_fees(wid, {addr1: 1000000})
# payment_fees(wid, [{addr1: 1000000}, {addr2: 1000000}])
def payment_fees(wid, payments)
payments_formatted = Utils.format_payments(payments)
self.class.post("/byron-wallets/#{wid}/payment-fees",
Expand Down
8 changes: 4 additions & 4 deletions lib/cardano_wallet/shelley.rb
Expand Up @@ -158,7 +158,7 @@ def initialize opt
# @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/selectCoins
#
# @example
# random(wid, {address1: 123, address2: 456})
# random(wid, [{addr1: 1000000}, {addr2: 1000000}])
def random(wid, payments)
payments_formatted = Utils.format_payments(payments)
self.class.post("/wallets/#{wid}/coin-selections/random",
Expand Down Expand Up @@ -194,12 +194,12 @@ def list(wid, q = {})
# @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postTransaction
# @param wid [String] source wallet id
# @param passphrase [String] source wallet's passphrase
# @param payments [Hash] addres, amount pair
# @param payments [Array of Hashes] addres, amount pair
# @param withdrawal [String or Array] 'self' or mnemonic sentence
# @param metadata [Hash] special metadata JSON subset format (cf: https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postTransaction)
#
# @example
# create(wid, passphrase, {addr1: 1000000}, 'self', {"1": "abc"})
# create(wid, passphrase, [{addr1: 1000000}, {addr2: 1000000}], 'self', {"1": "abc"})
def create(wid, passphrase, payments, withdrawal = nil, metadata = nil)
payments_formatted = Utils.format_payments(payments)
payload = { :payments => payments_formatted,
Expand All @@ -217,7 +217,7 @@ def create(wid, passphrase, payments, withdrawal = nil, metadata = nil)
# @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postTransactionFee
#
# @example
# payment_fees(wid, {addr1: 1000000}, {"1": "abc"})
# payment_fees(wid, [{addr1: 1000000}, {addr2: 1000000}], {"1": "abc"})
def payment_fees(wid, payments, withdrawal = nil, metadata = nil)
payments_formatted = Utils.format_payments(payments)
payload = { :payments => payments_formatted }
Expand Down
23 changes: 17 additions & 6 deletions lib/cardano_wallet/utils.rb
Expand Up @@ -5,13 +5,24 @@ def self.verify_param_is_hash!(param)
raise ArgumentError, "argument should be Hash" unless param.is_a?(Hash)
end

def self.verify_param_is_array!(param)
raise ArgumentError, "argument should be Array" unless param.is_a?(Array)
end

##
# @param payments [Array of Hashes] - [{addr1: 1}, {addr2: 2}]
# @return [Array of Hashes] - [{:address=>"addr1", :amount=>{:quantity=>1, :unit=>"lovelace"}}, {:address=>"addr2", :amount=>{:quantity=>2, :unit=>"lovelace"}}]
def self.format_payments(payments)
verify_param_is_hash!(payments)
payments.collect do |addr, amt|
{:address => addr.to_s,
:amount => {:quantity => amt.to_i,
:unit => "lovelace"}
}
verify_param_is_array!(payments)
raise ArgumentError, "argument should be Array of single Hashes" unless payments.all?{|p| p.size == 1 && p.is_a?(Hash)}
payments.map do |p|
a = p.collect do |addr, amt|
{:address => addr.to_s,
:amount => {:quantity => amt.to_i,
:unit => "lovelace"}
}
end.flatten
Hash[*a]
end
end

Expand Down
28 changes: 16 additions & 12 deletions spec/byron_spec.rb
Expand Up @@ -23,7 +23,7 @@ def test_byron_tx(source_wid, target_wid)
amt = 1000000
address = SHELLEY.addresses.list(target_wid)[0]['id']

tx_sent = BYRON.transactions.create(source_wid, PASS, {address => amt})
tx_sent = BYRON.transactions.create(source_wid, PASS, [{address => amt}])
expect(tx_sent['status']).to eq "pending"
expect(tx_sent.code).to eq 202

Expand Down Expand Up @@ -217,10 +217,10 @@ def test_byron_tx(source_wid, target_wid)
it "I could trigger random coin selection - if had money" do
wid = create_byron_wallet "icarus"
addresses = BYRON.addresses.list(wid)
addr_amount =
{addresses[0]['id'] => 123,
addresses[1]['id'] => 456
}
addr_amount = [
{ addresses[0]['id'] => 123 },
{ addresses[1]['id'] => 456 }
]

rnd = BYRON.coin_selections.random wid, addr_amount
expect(rnd).to include "not_enough_money"
Expand All @@ -229,12 +229,16 @@ def test_byron_tx(source_wid, target_wid)

it "ArgumentError on bad argument address_amount" do
wid = create_byron_wallet
payments =[[{addr1: 1, addr2: 2}], "addr:123", 123]
p =[[{addr1: 1, addr2: 2}], "addr:123", 123]
cs = BYRON.coin_selections
payments.each do |p|
expect{ cs.random(wid, p) }.to raise_error ArgumentError,
"argument should be Hash"
end
expect{ cs.random(wid, p[0]) }.to raise_error ArgumentError,
"argument should be Array of single Hashes"

expect{ cs.random(wid, p[1]) }.to raise_error ArgumentError,
"argument should be Array"

expect{ cs.random(wid, p[2]) }.to raise_error ArgumentError,
"argument should be Array"
end
end

Expand Down Expand Up @@ -271,7 +275,7 @@ def test_byron_tx(source_wid, target_wid)
target_id = create_byron_wallet "icarus"
target_addr = BYRON.addresses.list(target_id)[0]['id']

tx_sent = BYRON.transactions.create(id, PASS, {target_addr => 1000000})
tx_sent = BYRON.transactions.create(id, PASS, [{target_addr => 1000000}])
expect(tx_sent.code).to eq 403
expect(tx_sent).to include "not_enough_money"
end
Expand All @@ -281,7 +285,7 @@ def test_byron_tx(source_wid, target_wid)
target_id = create_byron_wallet "icarus"
target_addr = BYRON.addresses.list(target_id)[0]['id']

fees = BYRON.transactions.payment_fees(id, {target_addr => 1000000})
fees = BYRON.transactions.payment_fees(id, [{target_addr => 1000000}])
expect(fees.code).to eq 403
expect(fees).to include "not_enough_money"
end
Expand Down
43 changes: 25 additions & 18 deletions spec/shelley_spec.rb
Expand Up @@ -26,7 +26,7 @@
amt = 1000000

address = SHELLEY.addresses.list(@target_id)[0]['id']
tx_sent = SHELLEY.transactions.create(@wid, PASS, {address => amt})
tx_sent = SHELLEY.transactions.create(@wid, PASS, [{address => amt}])
expect(tx_sent['status']).to eq "pending"
expect(tx_sent.code).to eq 202

Expand All @@ -41,7 +41,7 @@
amt = 1000000
address = SHELLEY.addresses.list(@target_id_withdrawal)[0]['id']

tx_sent = SHELLEY.transactions.create(@wid, PASS, {address => amt}, 'self')
tx_sent = SHELLEY.transactions.create(@wid, PASS, [{address => amt}], 'self')
expect(tx_sent['status']).to eq "pending"
expect(tx_sent.code).to eq 202

Expand All @@ -64,7 +64,7 @@
address = SHELLEY.addresses.list(@target_id_meta)[0]['id']
tx_sent = SHELLEY.transactions.create(@wid,
PASS,
{address => amt},
[{address => amt}],
nil,
metadata
)
Expand Down Expand Up @@ -335,24 +335,28 @@
it "I could trigger random coin selection - if had money" do
wid = create_shelley_wallet
addresses = SHELLEY.addresses.list(wid)
addr_amount =
{addresses[0]['id'] => 123,
addresses[1]['id'] => 456
}
addr_amount = [
{ addresses[0]['id'] => 123 },
{ addresses[1]['id'] => 456 }
]

rnd = SHELLEY.coin_selections.random wid, addr_amount
expect(rnd.code).to eq 403
expect(rnd).to include "not_enough_money"
expect(rnd.code).to eq 403
end

it "ArgumentError on bad argument address_amount" do
wid = create_shelley_wallet
payments =[[{addr1: 1, addr2: 2}], "addr:123", 123]
p =[[{addr1: 1, addr2: 2}], "addr:123", 123]
cs = SHELLEY.coin_selections
payments.each do |p|
expect{ cs.random(wid, p) }.to raise_error ArgumentError,
"argument should be Hash"
end
expect{ cs.random(wid, p[0]) }.to raise_error ArgumentError,
"argument should be Array of single Hashes"

expect{ cs.random(wid, p[1]) }.to raise_error ArgumentError,
"argument should be Array"

expect{ cs.random(wid, p[2]) }.to raise_error ArgumentError,
"argument should be Array"
end
end

Expand Down Expand Up @@ -388,8 +392,9 @@
target_id = create_shelley_wallet
address = SHELLEY.addresses.list(target_id)[0]['id']
txs = SHELLEY.transactions
amt = [{address => 1000000}]

tx_sent = txs.create(id, PASS, {address => 1000000})
tx_sent = txs.create(id, PASS, amt)
expect(tx_sent).to include "not_enough_money"
expect(tx_sent.code).to eq 403
end
Expand All @@ -399,8 +404,9 @@
target_id = create_shelley_wallet
address = SHELLEY.addresses.list(target_id)[0]['id']
txs = SHELLEY.transactions
amt = [{address => 1000000}]

tx_sent = txs.create(id, PASS, {address => 1000000}, 'self')
tx_sent = txs.create(id, PASS, amt, 'self')
expect(tx_sent).to include "not_enough_money"
expect(tx_sent.code).to eq 403
end
Expand All @@ -409,14 +415,15 @@
id = create_shelley_wallet
target_id = create_shelley_wallet
address = SHELLEY.addresses.list(target_id)[0]['id']
amt = [{address => 1000000}]

txs = SHELLEY.transactions

fees = txs.payment_fees(id, {address => 1000000})
fees = txs.payment_fees(id, amt)
expect(fees).to include "not_enough_money"
expect(fees.code).to eq 403

fees = txs.payment_fees(id, {address => 1000000}, 'self')
fees = txs.payment_fees(id, amt, 'self')
expect(fees).to include "not_enough_money"
expect(fees.code).to eq 403

Expand All @@ -427,7 +434,7 @@
"4"=>{ "map"=>[ { "k"=>{ "string"=>"key" }, "v"=>{ "string"=>"value" } },
{ "k"=>{ "int"=>14 }, "v"=>{ "int"=>42 } } ] } }

fees = txs.payment_fees(id, {address => 1000000}, 'self', metadata)
fees = txs.payment_fees(id, amt, 'self', metadata)
expect(fees).to include "not_enough_money"
expect(fees.code).to eq 403
end
Expand Down

0 comments on commit 128b289

Please sign in to comment.