lib/cardano_wallet/byron.rb in cardano_wallet-0.3.2 vs lib/cardano_wallet/byron.rb in cardano_wallet-0.3.3

- old
+ new

@@ -1,17 +1,18 @@ +# frozen_string_literal: true + module CardanoWallet + ## + # Byron APIs module Byron - def self.new(opt) Init.new opt end + ## + # Init class for Byron APIs class Init < Base - def initialize opt - super - end - # Get API for Byron wallets # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#tag/Byron-Wallets def wallets Wallets.new @opt end @@ -45,38 +46,31 @@ def assets Assets.new @opt end end + ## + # Init for Byron assets APIs class Assets < Base - def initialize opt - super - end - # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/listByronAssets # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/getByronAsset # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/getByronAssetDefault def get(wid, policy_id = nil, asset_name = nil) ep = "/byron-wallets/#{wid}/assets" ep += "/#{policy_id}" if policy_id ep += "/#{asset_name}" if asset_name self.class.get(ep) end - end # Byron wallets # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#tag/Byron-Wallets class Wallets < Base - def initialize opt - super - end - # List Byron wallets # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/listByronWallets def list - self.class.get("/byron-wallets") + self.class.get('/byron-wallets') end # Get Byron wallet details # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/getByronWallet def get(wid) @@ -88,18 +82,17 @@ # # @example # create({style: "random", # name: "Random Wallet from mnemonic_sentence", # passphrase: "Secure Passphrase", - # mnemonic_sentence: %w[arctic decade pink easy jar index base bright vast ocean hard pizza], + # mnemonic_sentence: %w[arctic decade pizza ...], # }) def create(params) Utils.verify_param_is_hash!(params) - self.class.post( "/byron-wallets", - :body => params.to_json, - :headers => { 'Content-Type' => 'application/json' } - ) + self.class.post('/byron-wallets', + body: params.to_json, + headers: { 'Content-Type' => 'application/json' }) end # Delete Byron wallet # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/deleteByronWallet def delete(wid) @@ -112,13 +105,12 @@ # @example # update_metadata(wid, {name: "New wallet name"}) def update_metadata(wid, params) Utils.verify_param_is_hash!(params) self.class.put("/byron-wallets/#{wid}", - :body => params.to_json, - :headers => { 'Content-Type' => 'application/json' } - ) + body: params.to_json, + headers: { 'Content-Type' => 'application/json' }) end # See Byron wallet's utxo distribution # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/getByronUTxOsStatistics def utxo(wid) @@ -131,31 +123,26 @@ # @example # update_passphrase(wid, {old_passphrase: "Secure Passphrase", new_passphrase: "Securer Passphrase"}) def update_passphrase(wid, params) Utils.verify_param_is_hash!(params) self.class.put("/byron-wallets/#{wid}/passphrase", - :body => params.to_json, - :headers => { 'Content-Type' => 'application/json' } - ) + body: params.to_json, + headers: { 'Content-Type' => 'application/json' }) end end # Byron addresses # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#tag/Byron-Addresses class Addresses < Base - def initialize opt - super - end - # List Byron addresses. # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/listByronAddresses # # @example # list(wid, {state: "used"}) - def list(wid, q = {}) - q.empty? ? query = '' : query = Utils.to_query(q) - self.class.get("/byron-wallets/#{wid}/addresses#{query}") + def list(wid, query = {}) + query_formatted = query.empty? ? '' : Utils.to_query(query) + self.class.get("/byron-wallets/#{wid}/addresses#{query_formatted}") end # Create address for Byron random wallet. # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/createAddress # @param wid [String] wallet id @@ -165,14 +152,13 @@ # create(wid, {passphrase: "Secure Passphrase", address_index: 2147483648}) # @example Create address with random index. # create(wid, {passphrase: "Secure Passphrase"}) def create(wid, params) Utils.verify_param_is_hash!(params) - self.class.post( "/byron-wallets/#{wid}/addresses", - :body => params.to_json, - :headers => { 'Content-Type' => 'application/json' } - ) + self.class.post("/byron-wallets/#{wid}/addresses", + body: params.to_json, + headers: { 'Content-Type' => 'application/json' }) end # Import address to Byron wallet. # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/importAddress # @param wid [String] wallet id @@ -185,49 +171,42 @@ # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/importAddresses # @param wid [String] wallet id # @param addresses [Array] array of addresses def bulk_import(wid, addresses) self.class.put("/byron-wallets/#{wid}/addresses", - :body => { :addresses => addresses - }.to_json, - :headers => { 'Content-Type' => 'application/json' } ) + body: { addresses: addresses }.to_json, + headers: { 'Content-Type' => 'application/json' }) end end # API for CoinSelections # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#tag/Byron-Coin-Selections class CoinSelections < Base - def initialize opt - super - end - # Show random coin selection for particular payment # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/byronSelectCoins # # @example # random(wid, [{addr1: 1000000}, {addr2: 1000000}]) - # random(wid, [{ "address": "addr1..", "amount": { "quantity": 42000000, "unit": "lovelace" }, "assets": [{"policy_id": "pid", "asset_name": "name", "quantity": 0 } ] } ]) + # random(wid, [{ "address": "addr1..", + # "amount": { "quantity": 42000000, "unit": "lovelace" }, + # "assets": [{"policy_id": "pid", "asset_name": "name", "quantity": 0 } ] } ]) def random(wid, payments) Utils.verify_param_is_array!(payments) - if payments.any?{|p| p.has_key?("address".to_sym) || p.has_key?("address")} - payments_formatted = payments - else - payments_formatted = Utils.format_payments(payments) - end + payments_formatted = if payments.any? { |p| p.key?(:address) || p.key?('address') } + payments + else + Utils.format_payments(payments) + end self.class.post("/byron-wallets/#{wid}/coin-selections/random", - :body => {:payments => payments_formatted}.to_json, - :headers => { 'Content-Type' => 'application/json' }) + body: { payments: payments_formatted }.to_json, + headers: { 'Content-Type' => 'application/json' }) end end # Byron transactions # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postByronTransactionFee class Transactions < Base - def initialize opt - super - end - # Get tx by id # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/getByronTransaction def get(wid, tx_id) self.class.get("/byron-wallets/#{wid}/transactions/#{tx_id}") end @@ -235,55 +214,58 @@ # List all Byron wallet's transactions. # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/listByronTransactions # # @example # list(wid, {start: "2012-09-25T10:15:00Z", order: "descending"}) - def list(wid, q = {}) - q.empty? ? query = '' : query = Utils.to_query(q) - self.class.get("/byron-wallets/#{wid}/transactions#{query}") + def list(wid, query = {}) + query_formatted = query.empty? ? '' : Utils.to_query(query) + self.class.get("/byron-wallets/#{wid}/transactions#{query_formatted}") end # Create a transaction from the Byron wallet. # @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 [Array of Hashes] addres, amount pair # # @example # create(wid, passphrase, [{addr1: 1000000}, {addr2: 1000000}]) - # create(wid, passphrase, [{ "address": "addr1..", "amount": { "quantity": 42000000, "unit": "lovelace" }, "assets": [{"policy_id": "pid", "asset_name": "name", "quantity": 0 } ] } ]) + # create(wid, passphrase, [{ "address": "addr1..", + # "amount": { "quantity": 42000000, "unit": "lovelace" }, + # "assets": [{"policy_id": "pid", "asset_name": "name", "quantity": 0 } ] } ]) def create(wid, passphrase, payments) Utils.verify_param_is_array!(payments) - if payments.any?{|p| p.has_key?("address".to_sym) || p.has_key?("address")} - payments_formatted = payments - else - payments_formatted = Utils.format_payments(payments) - end + payments_formatted = if payments.any? { |p| p.key?(:address) || p.key?('address') } + payments + else + Utils.format_payments(payments) + end self.class.post("/byron-wallets/#{wid}/transactions", - :body => { :payments => payments_formatted, - :passphrase => passphrase - }.to_json, - :headers => { 'Content-Type' => 'application/json' } ) + body: { payments: payments_formatted, + passphrase: passphrase }.to_json, + headers: { 'Content-Type' => 'application/json' }) end # Estimate fees for transaction # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/postTransactionFee # # @example # payment_fees(wid, [{addr1: 1000000}, {addr2: 1000000}]) - # payment_fees(wid, [{ "address": "addr1..", "amount": { "quantity": 42000000, "unit": "lovelace" }, "assets": [{"policy_id": "pid", "asset_name": "name", "quantity": 0 } ] } ]) + # payment_fees(wid, [{ "address": "addr1..", + # "amount": { "quantity": 42000000, "unit": "lovelace" }, + # "assets": [{"policy_id": "pid", "asset_name": "name", "quantity": 0 } ] } ]) def payment_fees(wid, payments) Utils.verify_param_is_array!(payments) - if payments.any?{|p| p.has_key?("address".to_sym) || p.has_key?("address")} - payments_formatted = payments - else - payments_formatted = Utils.format_payments(payments) - end + payments_formatted = if payments.any? { |p| p.key?(:address) || p.key?('address') } + payments + else + Utils.format_payments(payments) + end self.class.post("/byron-wallets/#{wid}/payment-fees", - :body => { :payments => payments_formatted }.to_json, - :headers => { 'Content-Type' => 'application/json' } ) + body: { payments: payments_formatted }.to_json, + headers: { 'Content-Type' => 'application/json' }) end # Forget a transaction. # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/deleteByronTransaction def forget(wid, txid) @@ -292,14 +274,10 @@ end # Byron migrations # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#tag/Byron-Migrations class Migrations < Base - def initialize opt - super - end - # Calculate migration cost # @see https://input-output-hk.github.io/cardano-wallet/api/edge/#operation/getByronWalletMigrationInfo def cost(wid) self.class.get("/byron-wallets/#{wid}/migrations") end @@ -309,14 +287,12 @@ # @param wid [String] wallet id # @param passphrase [String] wallet's passphrase # @param [Array] array of addresses def migrate(wid, passphrase, addresses) self.class.post("/byron-wallets/#{wid}/migrations", - :body => { :addresses => addresses, - :passphrase => passphrase - }.to_json, - :headers => { 'Content-Type' => 'application/json' } ) + body: { addresses: addresses, + passphrase: passphrase }.to_json, + headers: { 'Content-Type' => 'application/json' }) end - end end end