Class: Stellar::Client
- Inherits:
-
Object
- Object
- Stellar::Client
- 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
-
#horizon ⇒ Object
readonly
Returns the value of attribute horizon.
Class Method Summary collapse
- .default(options = {}) ⇒ Object
- .default_testnet(options = {}) ⇒ Object
- .localhost(options = {}) ⇒ Object
Instance Method Summary collapse
- #account_info(account_or_address) ⇒ Object
- #account_merge(options = {}) ⇒ Object
-
#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.
- #change_trust(asset:, source:, sequence: nil, fee: DEFAULT_FEE, limit: nil) ⇒ Object
-
#check_memo_required(tx_envelope) ⇒ Object
Required by SEP-0029.
- #create_account(options = {}) ⇒ Object
- #friendbot(account) ⇒ Object
-
#initialize(options) ⇒ Client
constructor
A new instance of Client.
- #send_payment(options = {}) ⇒ Object
- #submit_transaction(tx_envelope:, options: {skip_memo_required_check: false}) ⇒ Object
- #transactions(options = {}) ⇒ Stellar::TransactionPage
-
#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.
-
#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.
Constructor Details
#initialize(options) ⇒ Client
Returns a new instance of Client.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/stellar/client.rb', line 46 def initialize() @options = @horizon = Hyperclient.new([: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
#horizon ⇒ Object (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( = {}) new .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( = {}) new .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( = {}) new .merge( horizon: HORIZON_LOCALHOST_URL ) end |
Instance Method Details
#account_info(account_or_address) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/stellar/client.rb', line 65 def account_info(account_or_address) account_id = if account_or_address.is_a?(Stellar::Account) account_or_address.address else account_or_address end @horizon.account(account_id: account_id)._get end |
#account_merge(options = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/stellar/client.rb', line 76 def account_merge( = {}) account = [:account] destination = [:destination] sequence = [:sequence] || (account_info(account).sequence.to_i + 1) transaction = Stellar::TransactionBuilder.account_merge( source_account: destination.keypair, sequence_number: sequence, destination: destination.keypair ) envelope = transaction.to_envelope(account.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.
264 265 266 267 268 |
# File 'lib/stellar/client.rb', line 264 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
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/stellar/client.rb', line 169 def change_trust( asset:, source:, sequence: nil, fee: DEFAULT_FEE, limit: nil ) sequence ||= (account_info(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
206 207 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 |
# File 'lib/stellar/client.rb', line 206 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.account_merge # 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 = account_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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/stellar/client.rb', line 100 def create_account( = {}) funder = [:funder] sequence = [:sequence] || (account_info(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 = [:fee] || DEFAULT_FEE payment = Stellar::TransactionBuilder.create_account( source_account: funder.keypair, sequence_number: sequence, base_fee: fee, destination: [:account].keypair, starting_balance: [:starting_balance] ) envelope = payment.to_envelope(funder.keypair) submit_transaction(tx_envelope: envelope) end |
#friendbot(account) ⇒ Object
91 92 93 94 95 |
# File 'lib/stellar/client.rb', line 91 def friendbot(account) uri = URI.parse(FRIENDBOT_URL) uri.query = "addr=#{account.address}" Faraday.post(uri.to_s) end |
#send_payment(options = {}) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/stellar/client.rb', line 121 def send_payment( = {}) from_account = [:from] tx_source_account = [:transaction_source] || from_account op_source_account = from_account if tx_source_account.present? sequence = [:sequence] || (account_info(tx_source_account).sequence.to_i + 1) payment = Stellar::TransactionBuilder.new( source_account: tx_source_account.keypair, sequence_number: sequence ).add_operation( Stellar::Operation.payment( source_account: op_source_account.keypair, destination: [:to].keypair, amount: [:amount].to_payment ) ).set_memo([:memo]).set_timeout(0).build signers = [tx_source_account, op_source_account].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
197 198 199 200 201 202 |
# File 'lib/stellar/client.rb', line 197 def submit_transaction(tx_envelope:, options: {skip_memo_required_check: false}) unless [:skip_memo_required_check] check_memo_required(tx_envelope) end @horizon.transactions._post(tx: tx_envelope.to_xdr(:base64)) end |
#transactions(options = {}) ⇒ Stellar::TransactionPage
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/stellar/client.rb', line 151 def transactions( = {}) args = .slice(:limit, :cursor) resource = if [:account] args = args.merge(account_id: [:account].address) @horizon.account_transactions(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
279 280 281 282 |
# File 'lib/stellar/client.rb', line 279 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.
292 293 294 295 296 |
# File 'lib/stellar/client.rb', line 292 def verify_tx_signed_by(transaction_envelope:, keypair:) Stellar::SEP10.verify_tx_signed_by( tx_envelope: transaction_envelope, keypair: keypair ) end |