Rbitcoin icon indicating copy to clipboard operation
Rbitcoin copied to clipboard

add coinbase exchange

Open jangorecki opened this issue 10 years ago • 0 comments

Low level communication to coinbase. Thanks to gsee: https://gist.github.com/gsee/b20b3b9893cd74e462a8

library(RCurl)   # for base64Decode(), base64Encode(), getURLContent(), and getCurlHandle()
library(digest)  # for hmac()
library(RJSONIO) # for toJSON() and fromJSON()

api.key <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
secret <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
passphrase <- "your passphrase"

api.url <- "https://api.exchange.coinbase.com"
req.url <- "/orders"
method <- "POST"

timestamp <- format(as.numeric(Sys.time()), digits=13)
key <- base64Decode(secret, mode="raw")
order <- list(price="1.0", size="0.01", side="buy", product_id="BTC-USD")
body <- toJSON(order, collapse="")
what <- paste0(timestamp, toupper(method), req.url, body)

sign <- base64Encode(hmac(key, what, algo="sha256", raw=TRUE))

# set headers
httpheader <- list('CB-ACCESS-KEY'=api.key,
                'CB-ACCESS-SIGN'=sign,
                'CB-ACCESS-TIMESTAMP'=timestamp,
                'CB-ACCESS-PASSPHRASE'=passphrase,
                'Content-Type'='application/json')

# place an order
(order <- fromJSON(getURLContent(url=paste0(api.url, req.url), 
                                curl=getCurlHandle(useragent="R"),
                                httpheader=httpheader, 
                                postfields=body)))

jangorecki avatar Mar 09 '15 00:03 jangorecki