Skip to content

Commit

Permalink
Merge pull request #18 from piotr-iohk/ttl_in_tx
Browse files Browse the repository at this point in the history
ttl in transactions
  • Loading branch information
piotr-iohk authored Nov 3, 2020
2 parents b1f6835 + 9836b55 commit 247b23e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/cardano_wallet/shelley.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,18 @@ def list(wid, q = {})
# @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)
# @param ttl [Int] transaction's time-to-live in seconds
#
# @example
# create(wid, passphrase, [{addr1: 1000000}, {addr2: 1000000}], 'self', {"1": "abc"})
def create(wid, passphrase, payments, withdrawal = nil, metadata = nil)
# create(wid, passphrase, [{addr1: 1000000}, {addr2: 1000000}], 'self', {"1": "abc"}, ttl = 10)
def create(wid, passphrase, payments, withdrawal = nil, metadata = nil, ttl = nil)
payments_formatted = Utils.format_payments(payments)
payload = { :payments => payments_formatted,
:passphrase => passphrase
}
payload[:withdrawal] = withdrawal if withdrawal
payload[:metadata] = metadata if metadata
payload[:time_to_live] = { quantity: ttl, unit: "second" } if ttl

self.class.post("/wallets/#{wid}/transactions",
:body => payload.to_json,
Expand All @@ -259,13 +261,14 @@ 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}, {addr2: 1000000}], {"1": "abc"})
def payment_fees(wid, payments, withdrawal = nil, metadata = nil)
# payment_fees(wid, [{addr1: 1000000}, {addr2: 1000000}], {"1": "abc"}, ttl = 10)
def payment_fees(wid, payments, withdrawal = nil, metadata = nil, ttl = nil)
payments_formatted = Utils.format_payments(payments)
payload = { :payments => payments_formatted }

payload[:withdrawal] = withdrawal if withdrawal
payload[:metadata] = metadata if metadata
payload[:time_to_live] = { quantity: ttl, unit: "second" } if ttl

self.class.post("/wallets/#{wid}/payment-fees",
:body => payload.to_json,
Expand Down
58 changes: 57 additions & 1 deletion spec/shelley_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@
@target_id = create_shelley_wallet
@target_id_withdrawal = create_shelley_wallet
@target_id_meta = create_shelley_wallet
@target_id_ttl = create_shelley_wallet
wait_for_shelley_wallet_to_sync(@wid)
wait_for_shelley_wallet_to_sync(@target_id)
wait_for_shelley_wallet_to_sync(@target_id_withdrawal)
wait_for_shelley_wallet_to_sync(@target_id_meta)
wait_for_shelley_wallet_to_sync(@target_id_ttl)

@nighly_wallets = [ @wid,
@target_id,
@target_id_withdrawal,
@target_id_meta,
@target_id_ttl
]

# @wid = "b1fb863243a9ae451bc4e2e662f60ff217b126e2"
# @target_id_meta = "f6168d58ed1b6e6037d535bc59802cf6c7c67523"
Expand All @@ -19,7 +28,9 @@
after(:all) do
settings = CardanoWallet.new.misc.settings
s = settings.update({:pool_metadata_source => "none"})
teardown
@nighly_wallets.each do |wid|
SHELLEY.wallets.delete wid
end
end

describe "Shelley Transactions" do
Expand All @@ -38,6 +49,51 @@
end
end

it "I can send transaction with ttl and funds are received" do
amt = 1000000
ttl_in_s = 120

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

eventually "Funds are on target wallet: #{@target_id}" do
available = SHELLEY.wallets.get(@target_id_ttl)['balance']['available']['quantity']
total = SHELLEY.wallets.get(@target_id_ttl)['balance']['total']['quantity']
(available == amt) && (total == amt)
end
end

it "Transaction with ttl = 0 would expire and I can't forget it (for now)" do
amt = 1000000
ttl_in_s = 0

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

eventually "TX `#{tx_sent['id']}' expires on `#{@wid}'" do
SHELLEY.transactions.get(@wid, tx_sent['id'])['status'] == 'expired'
end

res = SHELLEY.transactions.forget(@wid, tx_sent['id'])
expect(res.code).to eq 403
end

it "I can send transaction using 'withdrawal' flag and funds are received" do
amt = 1000000
address = SHELLEY.addresses.list(@target_id_withdrawal)[0]['id']
Expand Down

0 comments on commit 247b23e

Please sign in to comment.