PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] Why does this ruby statement, which subtracts two identical arrays, fail the .empty? test?

 October 28, 2022     arrays, is-empty, ruby     No comments   

Issue

In this snippet (from line 19 of https://github.com/bitfinexcom/bitfinex-api-rb/blob/master/lib/bitfinex/connection.rb - this is also the source for the class containing the check_params method definition.)

if (params.keys - allowed_params).empty?

params.keys is an array, and allowed_params is an array. Why does the subtraction fail the .empty test?

To set it up to repeat this:

$gem install bitfinex-rb pry

Ruby source

#!/opt/local/bin/ruby

# Annoying!
# Stuff is installed to $GEMPATH/bitfinex-rb-0.0.11
# But you have to invoke it as below.
require 'pry'
require 'bitfinex'

Bitfinex::Client.configure do |conf|
  conf.secret = ENV["BFX_API_SECRET"]
  conf.api_key = ENV["BFX_API_KEY"]
end

client = Bitfinex::Client.new

history_req = {
      'since' => 1444277602,
      'until' => 0,
      'limit' => 500,
      'wallet' => "exchange"
}

puts
print history_req.keys
puts
puts history_req.keys

client.history(
   currency = "USD",
   history_req
)

script output (with extra help from pry)

$ pry wtf.rb {"account"=>{"read"=>true, "write"=>false}, "history"=>{"read"=>true, "write"=>false}, "orders"=>{"read"=>true, "write"=>true}, "positions"=>{"read"=>true, "write"=>false}, "funding"=>{"read"=>true, "write"=>true}, "wallets"=>{"read"=>true, "write"=>false}, "withdraw"=>{"read"=>false, "write"=>false}}

["since", "until", "limit", "wallet"] since until limit wallet

Exception: Bitfinex::ParamsError: Bitfinex::ParamsError -- From: /opt/local/lib/ruby2.4/gems/2.4.0/gems/bitfinex-rb-0.0.11/lib/bitfinex/connection.rb @ line 23 @ level: 0 of backtrace (of 22).

18:     # Make sure parameters are allowed for the HTTP call
19:     def check_params(params, allowed_params)
20:       if (params.keys - allowed_params).empty?
21:         return params
22:       else
23:         raise Bitfinex::ParamsError
24:       end
25:     end
26: 
27:     def rest_connection
28:       @conn ||= new_rest_connection ...exception encountered, going interactive! [71] pry(main)>

Source for the Bitfinex gem on github:

https://github.com/bitfinexcom/bitfinex-api-rb


Solution

Let's a peek at the docs for the history method:

Parameters:

  • currency (string) (defaults to: "usd") — (optional) Specify the currency, default “USD”
  • params (defaults to: {}) — :since [time] (optional) Return only the history after this timestamp.
  • params (defaults to: {}) — :until [time] (optional) Return only the history before this timestamp.
  • params (defaults to: {}) — :limit [int] (optional) Limit the number of entries to return. Default is 500.
  • params (defaults to: {}) — :wallet [string] (optional) Return only entries that took place in this wallet. Accepted inputs are: “trading”, “exchange”, “deposit”

So the allowed keys in params are :since, :until, :limit, and :wallet.

The keys in your params are 'since', 'until', 'limit', and 'wallet'.

allowed_params = [:since, :until, :limit, :wallet]
params = {
  'since' => 1444277602,
  'until' => 0,
  'limit' => 500,
  'wallet' => "exchange"
}

params.keys - allowed_params
# => [:since, :until, :limit, :wallet]

(params.keys - allowed_params).empty?
# => false

The solution is to change your definition of history_req to use Symbol keys instead of Strings.

history_req = {
  since: 1444277602,
  until: 0,
  limit: 500,
  wallet: "exchange"
}

You can see where allowed_params comes from in the history method's source:

def history(currency="usd", params = {})
  check_params(params, %i{since until limit wallet})
  # ...
end

The construct %i{...} (note the i) produces an array of Symbols, ergo [:since, :until, :limit, :wallet].



Answered By - Jordan Running
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing