spec/lib/stellar/client_spec.rb in stellar-sdk-0.4.0 vs spec/lib/stellar/client_spec.rb in stellar-sdk-0.5.0

- old
+ new

@@ -2,10 +2,39 @@ describe Stellar::Client do subject(:client) { Stellar::Client.default_testnet } + describe "#default_testnet" do + it 'instantiates a client pointing to horizon testnet' do + client = described_class.default_testnet + expect(client.horizon._url).to eq(described_class::HORIZON_TESTNET_URL) + end + end + + describe "#default" do + it 'instantiates a client pointing to horizon mainnet' do + client = described_class.default + expect(client.horizon._url).to eq(described_class::HORIZON_MAINNET_URL) + end + end + + describe "#localhost" do + it 'instantiates a client pointing to localhost horizon' do + client = described_class.localhost + expect(client.horizon._url).to eq(described_class::HORIZON_LOCALHOST_URL) + end + end + + describe "#initialize" do + let(:custom_horizon_url) { 'https://horizon.domain.com' } + it 'instantiates a client accepting custom options' do + client = described_class.new(horizon: custom_horizon_url) + expect(client.horizon._url).to eq(custom_horizon_url) + end + end + describe "#create_account" do let(:source) { Stellar::Account.from_seed(CONFIG[:source_seed]) } let(:destination) { Stellar::Account.random } it "creates the account", vcr: {record: :once, match_requests_on: [:method]} do @@ -30,27 +59,56 @@ let(:client) { Stellar::Client.default_testnet } it "returns the current details for the account", vcr: { record: :once, match_requests_on: [:method]} do response = client.account_info(account) - expect(response.id).to eq "GCQSESW66AX4ZRZB7QWCIXSPX2BD7KLOYSS33IUGDCLO4XCPURZEEC6R" + expect(response.id).to eq CONFIG[:source_address] expect(response.paging_token).to be_empty expect(response.sequence).to eq "346973227974715" expect(response.subentry_count).to eq 0 expect(response.thresholds).to include("low_threshold" => 0, "med_threshold" => 0, "high_threshold" => 0) expect(response.flags).to include("auth_required" => false, "auth_revocable" => false) expect(response.balances).to include("balance" => "3494.9997500", "asset_type" => "native") expect(response.signers).to include( - "public_key" => "GCQSESW66AX4ZRZB7QWCIXSPX2BD7KLOYSS33IUGDCLO4XCPURZEEC6R", + "public_key" => CONFIG[:source_address], "weight" => 1, "type" => "ed25519_public_key", - "key"=>"GCQSESW66AX4ZRZB7QWCIXSPX2BD7KLOYSS33IUGDCLO4XCPURZEEC6R" + "key" => CONFIG[:source_address] ) expect(response.data).to be_empty end end + describe "#account_merge" do + let(:funder) { Stellar::Account.from_seed(CONFIG[:source_seed]) } + let(:client) { Stellar::Client.default_testnet } + let(:source) { Stellar::Account.random } + let(:destination) { Stellar::Account.random } + + it "merges source account into destination", vcr: { record: :once, match_requests_on: [:method]} do + [source, destination].each do |account| + account = client.create_account( + funder: funder, + account: account, + starting_balance: 100, + ) + end + + client.account_merge( + account: source, + destination: destination + ) + + destination_info = client.account_info(destination) + native_asset_balance_info = destination_info.balances.find do |b| + b["asset_type"] == "native" + end + # balance of merged account is the balance of both accounts minus transaction fee for merge + expect(native_asset_balance_info["balance"].to_f).to eq 199.99999 + end + end + describe "#send_payment" do let(:source) { Stellar::Account.from_seed(CONFIG[:source_seed]) } context "native asset" do let(:destination) { Stellar::Account.random } @@ -79,64 +137,77 @@ expect(native_asset_balance_info["balance"].to_f).to eq 250.0 end end context "alphanum4 asset" do - let(:destination) { Stellar::Account.from_seed(CONFIG[:destination_seed]) } + let(:issuer) { Stellar::Account.from_seed(CONFIG[:source_seed]) } + let(:destination) { Stellar::Account.random } - it "sends a alphanum4 asset to the destination", vcr: {record: :once, match_requests_on: [:method]} do - destination_info = client.account_info(destination) - old_balances = destination_info.balances - old_btc_balance = old_balances.find do |b| - b["asset_code"] == "BTC" - end["balance"].to_f + it("sends a alphanum4 asset to the destination", { + vcr: {record: :once, match_requests_on: [:method]}, + }) do + client.create_account( + funder: issuer, + account: destination, + starting_balance: 2, + ) + client.change_trust( + asset: [:alphanum4, "BTC", issuer.keypair], + source: destination, + ) + asset = Stellar::Asset.alphanum4("BTC", source.keypair) amount = Stellar::Amount.new(150, asset) - client.send_payment( from: source, to: destination, amount: amount, ) destination_info = client.account_info(destination) - new_balances = destination_info.balances - new_btc_balance = new_balances.find do |b| + btc_balance = destination_info.balances.find do |b| b["asset_code"] == "BTC" end["balance"].to_f - expect(new_btc_balance - old_btc_balance).to eq 150.0 + expect(btc_balance).to eq 150.0 end end context "alphanum12 asset" do - let(:destination) { Stellar::Account.from_seed(CONFIG[:destination_seed]) } + let(:issuer) { Stellar::Account.from_seed(CONFIG[:source_seed]) } + let(:destination) { Stellar::Account.random } - it "sends a alphanum12 asset to the destination", vcr: {record: :once, match_requests_on: [:method]} do - destination_info = client.account_info(destination) - old_balances = destination_info.balances - old_btc_balance = old_balances.find do |b| - b["asset_code"] == "LONGNAME" - end["balance"].to_f + it("sends a alphanum12 asset to the destination", { + vcr: {record: :once, match_requests_on: [:method]}, + }) do + client.create_account( + funder: issuer, + account: destination, + starting_balance: 2, + ) + client.change_trust( + asset: [:alphanum12, "LONGNAME", issuer.keypair], + source: destination, + ) + asset = Stellar::Asset.alphanum12("LONGNAME", source.keypair) amount = Stellar::Amount.new(150, asset) client.send_payment( from: source, to: destination, amount: amount, ) destination_info = client.account_info(destination) - new_balances = destination_info.balances - new_btc_balance = new_balances.find do |b| + btc_balance = destination_info.balances.find do |b| b["asset_code"] == "LONGNAME" end["balance"].to_f - expect(new_btc_balance - old_btc_balance).to eq 150.0 + expect(btc_balance).to eq 150.0 end end end describe "#transactions" do @@ -164,9 +235,67 @@ end it "accepts a cursor to return less data", vcr: {record: :once, match_requests_on: [:method]} do response = client.transactions(cursor: cursor) expect(response).to be_a(Stellar::TransactionPage) + end + end + end + + describe "#change_trust" do + context "given an asset described as an array" do + let(:issuer) { Stellar::Account.from_seed(CONFIG[:source_seed]) } + let(:truster) { Stellar::Account.random } + + it("creates, updates, or deletes a trustline", { + vcr: {record: :once, match_requests_on: [:method]}, + }) do + client.create_account( + funder: issuer, + account: truster, + starting_balance: 2, + ) + + # Create trustline + client.change_trust( + asset: [:alphanum4, "BTC", issuer.keypair], + source: truster, + ) + + truster_info = client.account_info(truster) + btc_balance = truster_info.balances.find do |b| + b["asset_code"] == "BTC" && b["asset_issuer"] == issuer.address + end + + expect(btc_balance).to_not be_nil + + # Update trustline + client.change_trust( + asset: [:alphanum4, "BTC", issuer.keypair], + source: truster, + limit: 100, + ) + + truster_info = client.account_info(truster) + btc_balance = truster_info.balances.find do |b| + b["asset_code"] == "BTC" && b["asset_issuer"] == issuer.address + end + + expect(btc_balance["limit"].to_f).to eq 100 + + # Delete trustline + client.change_trust( + asset: [:alphanum4, "BTC", issuer.keypair], + source: truster, + limit: 0, + ) + + truster_info = client.account_info(truster) + btc_balance = truster_info.balances.find do |b| + b["asset_code"] == "BTC" && b["asset_issuer"] == issuer.address + end + + expect(btc_balance).to be_nil end end end end