Class: Stellar::Client

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
lib/stellar/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stellar/client.rb', line 29

def initialize(options)
  @options = options
  @horizon = Hyperclient.new(options[:horizon]) do |client|
    client.faraday_block = lambda do |conn|
      conn.use Faraday::Response::RaiseError
      conn.use FaradayMiddleware::FollowRedirects
      conn.request :url_encoded
      conn.response :hal_json, content_type: /\bjson$/
      conn.adapter :excon
    end
    client.headers = { 
      'Accept' => 'application/hal+json,application/problem+json,application/json' 
    }
  end
end

Instance Attribute Details

#horizonObject (readonly)

Returns the value of attribute horizon.



26
27
28
# File 'lib/stellar/client.rb', line 26

def horizon
  @horizon
end

Class Method Details

.default(options = {}) ⇒ Object



7
8
9
10
11
# File 'lib/stellar/client.rb', line 7

def self.default(options={})
  new options.merge({
    horizon:   "https://horizon.stellar.org"
  })
end

.default_testnet(options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/stellar/client.rb', line 13

def self.default_testnet(options={})
  new options.merge({
    horizon:   "https://horizon-testnet.stellar.org",
    friendbot: "https://horizon-testnet.stellar.org",
  })
end

.localhost(options = {}) ⇒ Object



20
21
22
23
24
# File 'lib/stellar/client.rb', line 20

def self.localhost(options={})
  new options.merge({
    horizon: "http://127.0.0.1:3000", #TODO: figure out a real port
  })
end

Instance Method Details

#account_info(account) ⇒ Object



51
52
53
54
# File 'lib/stellar/client.rb', line 51

def ()
  address  = .address
  @horizon.(address:address)
end

#friendbot(account) ⇒ Object

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/stellar/client.rb', line 45

def friendbot()
  raise NotImplementedError
end

#send_payment(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/stellar/client.rb', line 61

def send_payment(options={})
  from     = options[:from]
  sequence = options[:sequence] || ((from).sequence + 1)

  payment = Stellar::Transaction.payment({
    account:     from.keypair,
    destination: options[:to].keypair,
    sequence:    sequence,
    amount:      options[:amount].to_payment,
  })

  envelope_hex = payment.to_envelope(from.keypair).to_xdr(:hex)
  @horizon.transactions._post(tx: envelope_hex)
end

#transactions(options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/stellar/client.rb', line 80

def transactions(options={})
  args = options.slice(:limit)

  resource = if options[:account]
    args = args.merge(address: options[:account].address)
    @horizon.(args)
  else
    @horizon.transactions(args)
  end

  TransactionPage.new(resource)
end