Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MtGox API not working #3

Open
markkuit opened this issue Mar 31, 2015 · 14 comments
Open

MtGox API not working #3

markkuit opened this issue Mar 31, 2015 · 14 comments

Comments

@markkuit
Copy link
Collaborator

MtGox API doesn't seem to be working. (http://data.mtgox.com/api/1/BTCUSD/ticker - error 403).

Is there any fix for it / alternative API? What happened to it?

@brighton36
Copy link
Owner

I lost interest in the project a good while back, and never committed this change. Want to clean this up and submit a PR on this coinbase version for me?

require 'money'
#require 'mtgox'
require 'coinbase'
require 'money/bank/google_currency'

class MtgoxBank < Money::Bank::VariableExchange
  CACHE_EXPIRATION = 15.minutes

  SUPPORTED_CURRENCIES = %w(USD EUR GBP AUD CAD CHF CNY DKK HKD JPY NZD PLN RUB SEK SGD THB)

  # We define this here so that we can run tests on the Exchange rates without
  # having to worry about whatever the actual exchange rate might be
  cattr_accessor :ticker_rates

  # Seemingly, floats are what the VariableExchange wants to have returned
  # these are then cast to strings and fed to BigDecimal before being applied
  # to exchange translations. (Returning strings or BigDecimals will work too) 
  def get_rate(from, to)
    @mutex.synchronize do
      case rate_key_for(from, to)
        when /\A(#{SUPPORTED_CURRENCIES.join('|')})_TO_BTC\Z/
          ( BigDecimal(1) / BigDecimal(ticker($1).to_s) ).to_s
        when /\ABTC_TO_(#{SUPPORTED_CURRENCIES.join('|')})\Z/
          ticker $1
        else
          # This means we're converting between the exchange currencies, using
          # btc as the intermediary
          (ticker(to) / ticker(from)).to_s
      end 
    end
  end

  # If we're converting to BTC, we want to round our irrational numbers to 
  # something more reasonable. The definition of 'reasonable' is tied to the
  # number of digits in the exchange rate.
  def exchange_with(from, to_currency)
    ret = super from, to_currency

    if to_currency.symbol == 'BTC'
      # We need to round based on the strength of the source currency
      # But many times our source currency is simply btc. So, let's use USD then:
      using_currency = (from.currency.iso_code == 'BTC') ? 'USD' : from.currency.iso_code

      # This determines the number of decimal digits in the ticker 
      ticker_digits = Math.log10(ticker(using_currency)).floor

      # When we're totally in the decimal digits, we don't need to round
      ticker_digits = -3 if ticker_digits < -3

      by_mod = 10 ** (5 - ticker_digits)
      return Money.new(ret.cents - (ret.cents % by_mod), 'BTC')
    end 

    ret
  end

  private 

  def ticker(for_cur)
    for_cur = for_cur.to_s.downcase
    (self.class.ticker_rates.try(:[],for_cur)) ? 
      self.class.ticker_rates[for_cur].to_f :
      Rails.cache.fetch('mtgox/ticker/%s' % for_cur, :expires_in => CACHE_EXPIRATION){
        Rails.logger.info "MtgoxBank is querying for %s" % for_cur
        begin
          # TODO: clean this up!
          coinbase = Coinbase::Client.new('user', 'pass')
          usd_rate = coinbase.buy_price(1).to_f

          if for_cur == 'USD'
            usd_rate
          else
            goog_rate = Money::Bank::GoogleCurrency.new.get_rate('USD', for_cur).to_f
            goog_rate * usd_rate
          end

        rescue Errno::ETIMEDOUT, Errno::ECONNRESET, Faraday::Error::TimeoutError
          # Note/TODO: MtGox is down
        end
      }
  end
end

@markkuit
Copy link
Collaborator Author

markkuit commented Apr 5, 2015

Still trying to make this work. I keep getting the following error after having replaced the original code with the one you pasted:

can't convert Symbol into Integer

lib/mtgox_bank.rb:67:in `new'
lib/mtgox_bank.rb:67:in `block in ticker'
lib/mtgox_bank.rb:63:in `ticker'
lib/mtgox_bank.rb:24:in `block in get_rate'
lib/mtgox_bank.rb:19:in `get_rate'
lib/mtgox_bank.rb:37:in `exchange_with'

Of course, I put coinbase API credentials in place. You sure you didn't miss anything apart of that file? and as a side note, why not just upload the whole system coinpost.com is using right now so that I could get a fresh one? I would be willing to eventually review the code to polish and PR it if needed.

@rogueops
Copy link

Could you help me getting this initially setup, never done a ruby site/project before.

@markkuit
Copy link
Collaborator Author

@gh0stshell I'll gladly help you with this once I've sorted the issues out with @brighton36 in order to make CoinPost work

@rogueops
Copy link

Sounds great! I just dont know how to install a Ruby site, have Ruby installed, just never worked with it, wish I had it installed or I maybe be able to help fix the MtGox code.

@brighton36
Copy link
Owner

So, I believe line 67 is the line where the Coinbase object is instantiated. Probably you have a newer version of the Coinbase lib than I'm using. Here's what I have:
cderose@hamlin /v/w/coinpost.com> cat Gemfile.lock | grep -i coinbase
coinbase (1.3.0)
coinbase (= 1.3.0)

What version are you running? (and is line 67 the line where you intialize Coinbase? Can I see that line, with the credentials replaced by X's)

@brighton36
Copy link
Owner

Ok - I just posted a bunch of changes from the live production site. Take a look now and see what you think?

@markkuit
Copy link
Collaborator Author

OK, thank you very much, I'll keep you updated

@markkuit
Copy link
Collaborator Author

Way better now. Got it up and running quite easily. Got 3 things to ask you.

This comes up during bundle install:

Your Gemfile lists the gem sqlite3 (>= 0) more than once.
You should probably keep only one of them.

Should be easy fix.

I had to..

mv config/initializers/mtgox_connection.rb config/initializers/mtgox_connection.rb.DISABLE

..to avoid the app to initialize up mtgox. Are you doing the same on production to avoid it?

And last - I'm quite new to Rails itself or maybe I'm just stupid - where'd you go to manage the site?

As a side note, since the README is kind of lackluster on the setting up part, I'm taking note of every step I'm doing to set CoinPost so that I can create a wiki page for those in need of. Are you ok with me doing so?
Thank you very much for your time, much appreciated!

@brighton36
Copy link
Owner

RE: Bundle install issues
Seems like sqlite is erroneously listed twice. You can nix the dupe in :test. Want me to fix, or submit a PR?

RE: config/initializers/mtgox_connection.rb
Yep, I deleted my copy, sorry for not deleting in the repo. Want to submit a PR, or want me to do that?

RE: Admin
I did most/all of the admin from the rails console. There's a coinpost admin account defined in the spec/factories/user.rb which has a couple small things he can do. For some examples of commands that work for admin-ing, check out the db/seeds.rb

RE: README
Are you kidding?! Any work you want to contribute would be awesome! I'm not opposed to making you a maintainer if you like this codebase and want to put time into it...

@markkuit
Copy link
Collaborator Author

If you feel like trusting me in adding me as mantainer, just know I'm not much of a Ruby developer but I'd be glad to help with adding some documentation and helping out users. :)

If in need of, my Skype account name is the same as GitHub's.

@rogueops
Copy link

@markkuit - is it ready for you to walk me through the install? I am proficient with linux, BSD and web sites just never installed or setup a ruby site before. If there is info on Google I wont bother you with these noob questions.

@brighton36
Copy link
Owner

There you go @markkuit - Added. Let's see what you come up with? I'd very much appreciate your help with this project.

@DaGitNah
Copy link

DaGitNah commented Aug 3, 2015

Mt.Gox is dead... long live coinbase!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants