Class: Stellar::Client

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

Constant Summary collapse

DEFAULT_FEE =
100
HORIZON_LOCALHOST_URL =
"http://127.0.0.1:8000"
HORIZON_MAINNET_URL =
"https://horizon.stellar.org"
HORIZON_TESTNET_URL =
"https://horizon-testnet.stellar.org"
FRIENDBOT_URL =
"https://friendbot.stellar.org".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :horizon (String)

    The Horizon server URL.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stellar/client.rb', line 46

def initialize(options)
  ActiveSupport::Deprecation.warn("`Stellar::Horizon` is deprecated and will be removed from `stellar-sdk` next relase. Use `stellar-horizon` gem for requesting Horizon API")

  @options = options
  @horizon = Hyperclient.new(options[:horizon]) { |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",
      "X-Client-Name" => "ruby-stellar-sdk",
      "X-Client-Version" => VERSION
    }
  }
end

Instance Attribute Details

#horizonObject (readonly)

Returns the value of attribute horizon.



43
44
45
# File 'lib/stellar/client.rb', line 43

def horizon
  @horizon
end

Class Method Details

.default(options = {}) ⇒ Object



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

def self.default(options = {})
  new options.merge(
    horizon: HORIZON_MAINNET_URL
  )
end

.default_testnet(options = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/stellar/client.rb', line 30

def self.default_testnet(options = {})
  new options.merge(
    horizon: HORIZON_TESTNET_URL,
    friendbot: HORIZON_TESTNET_URL
  )
end

.localhost(options = {}) ⇒ Object



37
38
39
40
41
# File 'lib/stellar/client.rb', line 37

def self.localhost(options = {})
  new options.merge(
    horizon: HORIZON_LOCALHOST_URL
  )
end

Instance Method Details

#account_info(account_or_address) ⇒ Object

Parameters:



67
68
69
70
71
72
73
74
# File 'lib/stellar/client.rb', line 67

def ()
   = if .is_a?(Stellar::Account)
    .address
  else
    
  end
  @horizon.(account_id: )._get
end

#account_merge(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):



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

def (options = {})
   = options[:account]
  destination = options[:destination]
  sequence = options[:sequence] || (().sequence.to_i + 1)

  transaction = Stellar::TransactionBuilder.(
    source_account: destination.keypair,
    sequence_number: sequence,
    destination: destination.keypair
  )

  envelope = transaction.to_envelope(.keypair)
  submit_transaction(tx_envelope: envelope)
end

#build_challenge_tx(server:, client:, anchor_name:, timeout: 300) ⇒ String

DEPRECATED: this function has been moved Stellar::SEP10.build_challenge_tx and will be removed in the next major version release.

A wrapper function for Stellar::SEP10::build_challenge_tx.

Parameters:

  • server (Stellar::KeyPair)

    Keypair for server’s signing account.

  • client (Stellar::KeyPair)

    Keypair for the account whishing to authenticate with the server.

  • anchor_name (String)

    Anchor’s name to be used in the manage_data key.

  • timeout (Integer) (defaults to: 300)

    Challenge duration (default to 5 minutes).

Returns:

  • (String)

    A base64 encoded string of the raw TransactionEnvelope xdr struct for the transaction.



266
267
268
269
270
# File 'lib/stellar/client.rb', line 266

def build_challenge_tx(server:, client:, anchor_name:, timeout: 300)
  Stellar::SEP10.build_challenge_tx(
    server: server, client: client, anchor_name: anchor_name, timeout: timeout
  )
end

#change_trust(asset:, source:, sequence: nil, fee: DEFAULT_FEE, limit: nil) ⇒ Object

Parameters:

  • asset (Array(Symbol,String,Stellar::KeyPair|Stellar::Account))
  • source (Stellar::Account)
  • sequence (Integer) (defaults to: nil)
  • fee (Integer) (defaults to: DEFAULT_FEE)
  • limit (Integer) (defaults to: nil)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/stellar/client.rb', line 171

def change_trust(
  asset:,
  source:,
  sequence: nil,
  fee: DEFAULT_FEE,
  limit: nil
)
  sequence ||= ((source).sequence.to_i + 1)

  op_args = {
    account: source.keypair,
    sequence: sequence,
    line: asset
  }
  op_args[:limit] = limit unless limit.nil?

  tx = Stellar::TransactionBuilder.change_trust(
    source_account: source.keypair,
    sequence_number: sequence,
    **op_args
  )

  envelope = tx.to_envelope(source.keypair)
  submit_transaction(tx_envelope: envelope)
end

#check_memo_required(tx_envelope) ⇒ Object

Required by SEP-0029

Parameters:

  • tx_envelope (Stellar::TransactionEnvelope)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/stellar/client.rb', line 208

def check_memo_required(tx_envelope)
  tx = tx_envelope.tx

  if tx.is_a?(Stellar::FeeBumpTransaction)
    tx = tx.inner_tx.v1!.tx
  end

  # Check transactions where the .memo field is nil or of type MemoType.memo_none
  if !tx.memo.nil? && tx.memo.type != Stellar::MemoType.memo_none
    return
  end

  destinations = Set.new
  ot = Stellar::OperationType

  tx.operations.each_with_index do |op, idx|
    destination = case op.body.type
    when ot.payment, ot.path_payment_strict_receive, ot.path_payment_strict_send
      op.body.value.destination
    when ot.
      # There is no AccountMergeOp, op.body is an Operation object
      # and op.body.value is a PublicKey (or AccountID) object.
      op.body.value
    else
      next
    end

    if destinations.include?(destination) || destination.switch == Stellar::CryptoKeyType.key_type_muxed_ed25519
      next
    end

    destinations.add(destination)
    kp = Stellar::KeyPair.from_public_key(destination.value)

    begin
      info = (kp.address)
    rescue Faraday::ResourceNotFound
      # Don't raise an error if its a 404, but throw one otherwise
      next
    end
    if info.data["config.memo_required"] == "MQ=="
      # MQ== is the base64 encoded string for the string "1"
      raise AccountRequiresMemoError.new("account requires memo", destination, idx)
    end
  end
end

#create_account(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/stellar/client.rb', line 102

def (options = {})
  funder = options[:funder]
  sequence = options[:sequence] || ((funder).sequence.to_i + 1)
  # In the future, the fee should be grabbed from the network's last transactions,
  # instead of using a hard-coded default value.
  fee = options[:fee] || DEFAULT_FEE

  payment = Stellar::TransactionBuilder.(
    source_account: funder.keypair,
    sequence_number: sequence,
    base_fee: fee,
    destination: options[:account].keypair,
    starting_balance: options[:starting_balance]
  )
  envelope = payment.to_envelope(funder.keypair)
  submit_transaction(tx_envelope: envelope)
end

#friendbot(account) ⇒ Object



93
94
95
96
97
# File 'lib/stellar/client.rb', line 93

def friendbot()
  uri = URI.parse(FRIENDBOT_URL)
  uri.query = "addr=#{.address}"
  Faraday.post(uri.to_s)
end

#send_payment(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/stellar/client.rb', line 123

def send_payment(options = {})
   = options[:from]
   = options[:transaction_source] || 
   =  if .present?

  sequence = options[:sequence] ||
    (().sequence.to_i + 1)

  payment = Stellar::TransactionBuilder.new(
    source_account: .keypair,
    sequence_number: sequence
  ).add_operation(
    Stellar::Operation.payment(
      source_account: .keypair,
      destination: options[:to].keypair,
      amount: options[:amount].to_payment
    )
  ).set_memo(options[:memo]).set_timeout(0).build

  signers = [, ].uniq(&:address)
  to_envelope_args = signers.map(&:keypair)

  envelope = payment.to_envelope(*to_envelope_args)
  submit_transaction(tx_envelope: envelope)
end

#submit_transaction(tx_envelope:, options: {skip_memo_required_check: false}) ⇒ Object

Parameters:

  • tx_envelope (Stellar::TransactionEnvelope)
  • options (Hash) (defaults to: {skip_memo_required_check: false})

    a customizable set of options

Options Hash (options:):

  • :skip_memo_required_check (Boolean) — default: false


199
200
201
202
203
204
# File 'lib/stellar/client.rb', line 199

def submit_transaction(tx_envelope:, options: {skip_memo_required_check: false})
  unless options[:skip_memo_required_check]
    check_memo_required(tx_envelope)
  end
  @horizon.transactions._post(tx: tx_envelope.to_xdr(:base64))
end

#transactions(options = {}) ⇒ Stellar::TransactionPage

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

Returns:



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/stellar/client.rb', line 153

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

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

  TransactionPage.new(resource)
end

#verify_challenge_tx(challenge:, server:) ⇒ Boolean

DEPRECATED: this function has been moved to Stellar::SEP10::read_challenge_tx and will be removed in the next major version release.

A wrapper function for Stellar::SEP10.verify_challenge_transaction

Parameters:

  • challenge (String)

    SEP0010 transaction challenge in base64.

  • server (Stellar::KeyPair)

    Stellar::KeyPair for server where the challenge was generated.

Returns:

  • (Boolean)


281
282
283
284
# File 'lib/stellar/client.rb', line 281

def verify_challenge_tx(challenge:, server:)
  Stellar::SEP10.verify_challenge_tx(challenge_xdr: challenge, server: server)
  true
end

#verify_tx_signed_by(transaction_envelope:, keypair:) ⇒ Boolean

DEPRECATED: this function has been moved to Stellar::SEP10::verify_tx_signed_by and will be removed in the next major version release.

Parameters:

  • transaction_envelope (Stellar::TransactionEnvelope)
  • keypair (Stellar::KeyPair)

Returns:

  • (Boolean)


294
295
296
297
298
# File 'lib/stellar/client.rb', line 294

def verify_tx_signed_by(transaction_envelope:, keypair:)
  Stellar::SEP10.verify_tx_signed_by(
    tx_envelope: transaction_envelope, keypair: keypair
  )
end