Skip to content
Browse files

Merge pull request #1 from demonbane/apiv1

Apiv1
  • Loading branch information...
2 parents f2e8e14 + 069defb commit bf8f1220cccb8c78754bfebe433495fae49044bb @SKoschnicke committed Apr 5, 2013
View
0 bin/btc 100644 → 100755
File mode changed.
View
51 lib/mtgox/client.rb
@@ -16,7 +16,8 @@ class Client
include MtGox::Connection
include MtGox::Request
- ORDER_TYPES = {sell: 1, buy: 2}
+ ORDER_TYPES = {sell: "ask", buy: "bid"}
+ INT_MULTIPLIERS = {btc: 100000000, usd: 100000, jpy: 1000}
# Fetch a deposit address
# @authenticated true
@@ -132,7 +133,7 @@ def trades
# @example
# MtGox.balance
def balance
- parse_balance(post('/api/0/getFunds.php', {}))
+ parse_balance(post('/api/1/generic/private/info', {})['return'])
end
# Fetch your open orders, both buys and sells, for network efficiency
@@ -142,7 +143,7 @@ def balance
# @example
# MtGox.orders
def orders
- parse_orders(post('/api/0/getOrders.php', {})['orders'])
+ parse_orders(post('/api/1/generic/private/orders', {})['return'])
end
# Fetch your open buys
@@ -170,25 +171,39 @@ def sells
# @authenticated true
# @param amount [Numeric] the number of bitcoins to purchase
# @param price [Numeric] the bid price in US dollars
- # @return [Hash] with keys :buys and :sells, which contain arrays as described in {MtGox::Client#buys} and {MtGox::Clients#sells}
+ # @return [String] order ID for the buy, can be inspected using order_result
# @example
# # Buy one bitcoin for $0.011
# MtGox.buy! 1.0, 0.011
def buy!(amount, price)
- parse_orders(post('/api/0/buyBTC.php', {amount: amount, price: price})['orders'])
+ addorder!(:buy, amount, price)
end
# Place a limit order to sell BTC
#
# @authenticated true
# @param amount [Numeric] the number of bitcoins to sell
# @param price [Numeric] the ask price in US dollars
- # @return [Hash] with keys :buys and :sells, which contain arrays as described in {MtGox::Client#buys} and {MtGox::Clients#sells}
+ # @return [String] order ID for the sell, can be inspected using order_result
# @example
# # Sell one bitcoin for $100
# MtGox.sell! 1.0, 100.0
def sell!(amount, price)
- parse_orders(post('/api/0/sellBTC.php', {amount: amount, price: price})['orders'])
+ addorder!(:sell, amount, price)
+ end
+
+ # Create a new order
+ #
+ # @authenticated true
+ # @param type [String] the type of order to create, either "buy" or "sell"
+ # @param amount [Numberic] the number of bitcoins to buy/sell
+ # @param price [Numeric] the bid/ask price in USD
+ # @return [String] order ID for the order, can be inspected using order_result
+ # @example
+ # # Sell one bitcoin for $123
+ # MtGox.addorder! :sell, 1.0, 123.0
+ def addorder!(type, amount, price)
+ post('/api/1/BTCUSD/private/order/add', {type: order_type(type), amount_int: intify(amount,:btc), price_int: intify(price, :usd)})['return']
end
# Cancel an open order
@@ -229,20 +244,20 @@ def cancel(args)
# @authenticated true
# @param amount [Numeric] the number of bitcoins to withdraw
# @param btca [String] the bitcoin address to send to
- # @return [Array<MtGox::Balance>]
+ # @return [String] completed transaction ID
# @example
# # Withdraw 1 BTC from your account
# MtGox.withdraw! 1.0, '1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L'
- def withdraw!(amount, btca)
- parse_balance(post('/api/0/withdraw.php', {group1: 'BTC', amount: amount, btca: btca}))
+ def withdraw!(amount, address)
+ post('/api/1/generic/bitcoin/send_simple', {amount_int: intify(amount, :btc), address: address})['return']['trx']
end
private
def parse_balance(balance)
balances = []
- balances << Balance.new('BTC', balance['btcs'])
- balances << Balance.new('USD', balance['usds'])
+ balances << Balance.new('BTC', balance['Wallets']['BTC']['Balance']['value'])
+ balances << Balance.new('USD', balance['Wallets']['USD']['Balance']['value'])
balances
end
@@ -259,5 +274,17 @@ def parse_orders(orders)
end
{buys: buys, sells: sells}
end
+
+ def intify(float, currency)
+ (float * INT_MULTIPLIERS[currency]).to_i
+ end
+
+ def order_type(type)
+ unless ["bid", "ask"].include?(type.to_s)
+ ORDER_TYPES[type.downcase.to_sym]
+ else
+ type
+ end
+ end
end
end
View
4 lib/mtgox/order.rb
@@ -7,8 +7,8 @@ class Order < Offer
def initialize(order={})
self.id = order['oid']
self.date = Time.at(order['date'].to_i)
- self.amount = order['amount'].to_f
- self.price = order['price'].to_f
+ self.amount = order['amount']['value'].to_f
+ self.price = order['price']['value'].to_f
end
end
end
View
1 spec/fixtures/balance.json
@@ -1 +0,0 @@
-{"usds":3.7,"btcs":22}
View
2 spec/fixtures/buy.json
@@ -1 +1 @@
-{"status":"Your order has been queued for execution","orders":[{"oid":"9f9983fd-5358-4e68-909e-42e69c7ded3f","currency":"USD","item":"BTC","type":2,"amount":1,"price":2,"status":2,"dark":0,"date":1309199181,"priority":"1309199181788900"},{"oid":"3b0f10d7-251d-4a1b-ad6d-4f03877b85d1","currency":"USD","item":"BTC","type":2,"amount":0.1,"price":6,"status":1,"dark":0,"date":1309198830,"priority":"1309198830086400"},{"oid":"490a214f-9a30-449f-acb8-780f9046502f","currency":"USD","item":"BTC","type":2,"amount":0.2,"price":7,"status":1,"dark":0,"date":1309198838,"priority":"1309198838630500"},{"oid":"a20329fe-c0d5-4378-b204-79a7800d41e7","currency":"USD","item":"BTC","type":1,"amount":0.2,"price":99,"status":1,"dark":0,"date":1309198820,"priority":"1309198820706100"},{"oid":"7eac0bb4-b07c-42c2-936a-bb0a33ab2f64","currency":"USD","item":"BTC","type":1,"amount":0.659,"price":100,"status":1,"dark":0,"date":1309198722,"priority":"1309198722477200"},{"oid":"c35abb5b-42fc-43be-ad4e-60bfed184440","currency":"USD","item":"BTC","type":1,"amount":0.2,"price":100,"status":0,"dark":0,"date":1309198722,"priority":"1309198722477200"},{"oid":"e24b7470-ea06-4c78-b5d0-0ab12826b626","currency":"USD","item":"BTC","type":1,"amount":0.1,"price":101,"status":0,"dark":0,"date":1309198807,"priority":"1309198807803300"}]}
+{"result":"success","return":"490a214f-9a30-449f-acb8-780f9046502f"}
View
1 spec/fixtures/info.json
@@ -0,0 +1 @@
+{"result":"success","return":{"Login":"deadbeef","Index":"1","Id":"deadbeef-111c-2221-3334-deadbeef1122","Rights":["get_info"],"Language":"en_US","Created":"2012-01-02 01:02:03","Last_Login":"2013-03-24 01:00:00","Wallets":{"BTC":{"Balance":{"value":"22.00000000","value_int":"2200000000","display":"22.00000000 BTC","display_short":"22.00 BTC","currency":"BTC"},"Operations":101,"Daily_Withdraw_Limit":{"value":"100.00000000","value_int":"10000000000","display":"100.00000000 BTC","display_short":"100.00 BTC","currency":"BTC"},"Monthly_Withdraw_Limit":null,"Max_Withdraw":{"value":"100.00000000","value_int":"10000000000","display":"100.00000000 BTC","display_short":"100.00 BTC","currency":"BTC"},"Open_Orders":{"value":"0.00000000","value_int":"0","display":"0.00000000 BTC","display_short":"0.00 BTC","currency":"BTC"}},"USD":{"Balance":{"value":"3.70000","value_int":"370000000","display":"$3.70000","display_short":"$3.70","currency":"USD"},"Operations":101,"Daily_Withdraw_Limit":{"value":"10000.00000","value_int":"1000000000","display":"$10,000.00000","display_short":"$10,000.00","currency":"USD"},"Monthly_Withdraw_Limit":{"value":"50000.00000","value_int":"5000000000","display":"$50,000.00000","display_short":"$50,000.00","currency":"USD"},"Max_Withdraw":{"value":"10000.00000","value_int":"1000000000","display":"$10,000.00000","display_short":"$10,000.00","currency":"USD"},"Open_Orders":{"value":"0.00000","value_int":"0","display":"$0.00000","display_short":"$0.00","currency":"USD"}}},"Monthly_Volume":{"value":"700.00000000","value_int":"70000000000","display":"700.00000000 BTC","display_short":"700.00 BTC","currency":"BTC"},"Trade_Fee":0.5}}
View
2 spec/fixtures/orders.json
@@ -1 +1 @@
-{"usds":199.9959,"btcs":0.859,"orders":[{"oid":"bddd042c-e837-4a88-a92e-3b7c05e483df","currency":"USD","item":"BTC","type":2,"amount":3,"price":5,"status":1,"dark":0,"date":1309198768,"priority":"1309198768104500"},{"oid":"3b0f10d7-251d-4a1b-ad6d-4f03877b85d1","currency":"USD","item":"BTC","type":2,"amount":0.1,"price":6,"status":1,"dark":0,"date":1309198830,"priority":"1309198830086400"},{"oid":"490a214f-9a30-449f-acb8-780f9046502f","currency":"USD","item":"BTC","type":2,"amount":0.2,"price":7,"status":1,"dark":0,"date":1309198838,"priority":"1309198838630500"},{"oid":"a20329fe-c0d5-4378-b204-79a7800d41e7","currency":"USD","item":"BTC","type":1,"amount":0.2,"price":99,"status":1,"dark":0,"date":1309198820,"priority":"1309198820706100"},{"oid":"7eac0bb4-b07c-42c2-936a-bb0a33ab2f64","currency":"USD","item":"BTC","type":1,"amount":0.659,"price":100,"status":1,"dark":0,"date":1309198722,"priority":"1309198722477200"},{"oid":"c35abb5b-42fc-43be-ad4e-60bfed184440","currency":"USD","item":"BTC","type":1,"amount":0.2,"price":100,"status":0,"dark":0,"date":1309198722,"priority":"1309198722477200"},{"oid":"e24b7470-ea06-4c78-b5d0-0ab12826b626","currency":"USD","item":"BTC","type":1,"amount":0.1,"price":101,"status":0,"dark":0,"date":1309198807,"priority":"1309198807803300"}]}
+{"result":"success","return":[{"oid":"490a214f-9a30-449f-acb8-780f9046502f","currency":"USD","item":"BTC","type":"bid","amount":{"value":"0.20000000","value_int":"20000000","display":"0.20000000\u00a0BTC","display_short":"0.20\u00a0BTC","currency":"BTC"},"effective_amount":{"value":"0.20000000","value_int":"20000000","display":"0.20000000\u00a0BTC","display_short":"0.20\u00a0BTC","currency":"BTC"},"price":{"value":"7.00000","value_int":"700000","display":"$7.00000","display_short":"$7.00","currency":"USD"},"status":"open","date":1309198838,"priority":"1364116780853497","actions":[]},{"oid":"a20329fe-c0d5-4378-b204-79a7800d41e7","currency":"USD","item":"BTC","type":"ask","amount":{"value":"0.20000000","value_int":"20000000","display":"0.20000000\u00a0BTC","display_short":"0.20\u00a0BTC","currency":"BTC"},"effective_amount":{"value":"0.20000000","value_int":"20000000","display":"0.20000000\u00a0BTC","display_short":"0.20\u00a0BTC","currency":"BTC"},"price":{"value":"99.00000","value_int":"9900000","display":"$99.00000","display_short":"$99.00","currency":"USD"},"status":"open","date":1309198820,"priority":"1364118432445268","actions":[]}]}
View
2 spec/fixtures/sell.json
@@ -1 +1 @@
-{"status":"Your order has been queued for execution","orders":[{"oid":"9f9983fd-5358-4e68-909e-42e69c7ded3f","currency":"USD","item":"BTC","type":2,"amount":1,"price":2,"status":1,"dark":0,"date":1309199181,"priority":"1309199181788900"},{"oid":"3b0f10d7-251d-4a1b-ad6d-4f03877b85d1","currency":"USD","item":"BTC","type":2,"amount":0.1,"price":6,"status":1,"dark":0,"date":1309198830,"priority":"1309198830086400"},{"oid":"490a214f-9a30-449f-acb8-780f9046502f","currency":"USD","item":"BTC","type":2,"amount":0.2,"price":7,"status":1,"dark":0,"date":1309198838,"priority":"1309198838630500"},{"oid":"a20329fe-c0d5-4378-b204-79a7800d41e7","currency":"USD","item":"BTC","type":1,"amount":0.2,"price":99,"status":1,"dark":0,"date":1309198820,"priority":"1309198820706100"},{"oid":"7eac0bb4-b07c-42c2-936a-bb0a33ab2f64","currency":"USD","item":"BTC","type":1,"amount":0.659,"price":100,"status":1,"dark":0,"date":1309198722,"priority":"1309198722477200"},{"oid":"c35abb5b-42fc-43be-ad4e-60bfed184440","currency":"USD","item":"BTC","type":1,"amount":0.2,"price":100,"status":0,"dark":0,"date":1309198722,"priority":"1309198722477200"},{"oid":"e24b7470-ea06-4c78-b5d0-0ab12826b626","currency":"USD","item":"BTC","type":1,"amount":0.1,"price":101,"status":0,"dark":0,"date":1309198807,"priority":"1309198807803300"},{"oid":"fdf0b134-7336-49c1-9d94-6ec641cffc74","currency":"USD","item":"BTC","type":1,"amount":1.7,"price":200,"status":2,"dark":0,"date":1309199274,"priority":"1309199274263700"}]}
+{"result":"success","return":"a20329fe-c0d5-4378-b204-79a7800d41e7"}
View
2 spec/fixtures/withdraw.json
@@ -1 +1 @@
-{"usds":64.59,"btcs":9,"status":"Your funds are on their way..."}
+{"result":"success","return":{"trx":"311295deadbeef390a13c038e2b8ba77feebdaed2c1a59e6e0bdf001656e1314"}}
View
53 spec/mtgox/client_spec.rb
@@ -139,14 +139,14 @@
describe '#balance' do
before do
- stub_post('/api/0/getFunds.php').
+ stub_post('/api/1/generic/private/info').
with(body: test_body, headers: test_headers).
- to_return(status: 200, body: fixture('balance.json'))
+ to_return(status: 200, body: fixture('info.json'))
end
it "should fetch balance" do
balance = @client.balance
- a_post("/api/0/getFunds.php").
+ a_post("/api/1/generic/private/info").
with(body: test_body, headers: test_headers).
should have_been_made
balance.first.currency.should == "BTC"
@@ -158,15 +158,15 @@
describe "order methods" do
before :each do
- stub_post('/api/0/getOrders.php').
+ stub_post('/api/1/generic/private/orders').
with(body: test_body, headers: test_headers).
to_return(status: 200, body: fixture('orders.json'))
end
describe "#buys" do
it "should fetch orders" do
buys = @client.buys
- a_post("/api/0/getOrders.php").
+ a_post("/api/1/generic/private/orders").
with(body: test_body, headers: test_headers).
should have_been_made
buys.last.price.should == 7
@@ -177,7 +177,7 @@
describe "#sells" do
it "should fetch sells" do
sells = @client.sells
- a_post("/api/0/getOrders.php").
+ a_post("/api/1/generic/private/orders").
with(body: test_body, headers: test_headers).
should have_been_made
sells.last.price.should == 99.0
@@ -188,7 +188,7 @@
describe "#orders" do
it "should fetch both buys and sells, with only one call" do
orders = @client.orders
- a_post("/api/0/getOrders.php").
+ a_post("/api/1/generic/private/orders").
with(body: test_body, headers: test_headers).
should have_been_made
orders[:buys].last.price.should == 7.0
@@ -201,43 +201,37 @@
describe "#buy!" do
before do
- body = test_body({"amount" => "0.88", "price" => "0.89"})
- stub_post('/api/0/buyBTC.php').
+ body = test_body({"type" => "bid", "amount_int" => "88000000", "price_int" => "89000"})
+ stub_post('/api/1/BTCUSD/private/order/add').
with(body: body, headers: test_headers(body)).
to_return(status: 200, body: fixture('buy.json'))
end
it "should place a bid" do
buy = @client.buy!(0.88, 0.89)
- body = test_body({"amount" => "0.88", "price" => "0.89"})
- a_post("/api/0/buyBTC.php").
+ body = test_body({"type" => "bid", "amount_int" => "88000000", "price_int" => "89000"})
+ a_post("/api/1/BTCUSD/private/order/add").
with(body: body, headers: test_headers(body)).
should have_been_made
- buy[:buys].last.price.should == 2.0
- buy[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 26, 21)
- buy[:sells].last.price.should == 99.0
- buy[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 20, 20)
+ buy.should == "490a214f-9a30-449f-acb8-780f9046502f"
end
end
describe "#sell!" do
before do
- body = test_body({"amount" => "0.88", "price" => "89.0"})
- stub_post('/api/0/sellBTC.php').
+ body = test_body({"type" => "ask", "amount_int" => "88000000", "price_int" => "8900000"})
+ stub_post('/api/1/BTCUSD/private/order/add').
with(body: body, headers: test_headers(body)).
to_return(status: 200, body: fixture('sell.json'))
end
it "should place an ask" do
- body = test_body({"amount" => "0.88", "price" => "89.0"})
+ body = test_body({"type" => "ask", "amount_int" => "88000000", "price_int" => "8900000"})
sell = @client.sell!(0.88, 89.0)
- a_post("/api/0/sellBTC.php").
+ a_post("/api/1/BTCUSD/private/order/add").
with(body: body, headers: test_headers(body)).
should have_been_made
- sell[:buys].last.price.should == 2.0
- sell[:buys].last.date.should == Time.utc(2011, 6, 27, 18, 26, 21)
- sell[:sells].last.price.should == 200
- sell[:sells].last.date.should == Time.utc(2011, 6, 27, 18, 27, 54)
+ sell.should == "a20329fe-c0d5-4378-b204-79a7800d41e7"
end
end
@@ -294,22 +288,19 @@
describe "#withdraw!" do
before do
- body = test_body({"group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
- stub_post('/api/0/withdraw.php').
+ body = test_body({"amount_int" => "100000000", "address" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
+ stub_post('/api/1/generic/bitcoin/send_simple').
with(body: body, headers: test_headers(body)).
to_return(status: 200, body: fixture('withdraw.json'))
end
it "should withdraw funds" do
withdraw = @client.withdraw!(1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L")
- body = test_body({"group1" => "BTC", "amount" => "1.0", "btca" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
- a_post("/api/0/withdraw.php").
+ body = test_body({"amount_int" => "100000000", "address" => "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"})
+ a_post("/api/1/generic/bitcoin/send_simple").
with(body: body, headers: test_headers(body)).
should have_been_made
- withdraw.first.currency.should == "BTC"
- withdraw.first.amount.should == 9.0
- withdraw.last.currency.should == "USD"
- withdraw.last.amount.should == 64.59
+ withdraw.should == "311295deadbeef390a13c038e2b8ba77feebdaed2c1a59e6e0bdf001656e1314"
end
end
end

0 comments on commit bf8f122

Please sign in to comment.
Something went wrong with that request. Please try again.