brighton36 / CoinPost

A Ruby-On-Rails-based Bitcoin Item listing and marketplace system
50 stars 28 forks source link

MtGox API not working #3

Open markkuit opened 9 years ago

markkuit commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

markkuit commented 9 years ago

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

rogueops commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

markkuit commented 9 years ago

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

markkuit commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

@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 commented 9 years ago

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

DaGitNah commented 9 years ago

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