coinbase / coinbase-pro-node

DEPRECATED — The official Node.js library for Coinbase Pro
Apache License 2.0
844 stars 316 forks source link

placeOrder throws Internal server error #384

Closed damianobarbati closed 5 years ago

damianobarbati commented 5 years ago

The following snippet:

import { AuthenticatedClient } from 'coinbase-pro';

const client = new AuthenticatedClient(process.env.COINBASE_KEY, process.env.COINBASE_SECRET, process.env.COINBASE_PASSPHRASE, process.env.COINBASE_API_URL);

// this works without any problem...
// client.getAccounts().then(console.log);

client.placeOrder({
    client_oid: 1,
    side: 'buy',
    type: 'market',
    product_id: 'BTC-EUR',
    size: 0.1,
    stp: 'dc',
}).then(console.log).catch(console.error);

Results in:

Error: HTTP 500 Error: Internal server error
    at Request._callback (/Users/damz/Desktop/rocketcapital-broker/node_modules/coinbase-pro/lib/clients/public.js:68:15)
    at Request.self.callback (/Users/damz/Desktop/rocketcapital-broker/node_modules/request/request.js:185:22)
    at Request.emit (events.js:210:5)
    at Request.EventEmitter.emit (domain.js:476:20)
    at Request.<anonymous> (/Users/damz/Desktop/rocketcapital-broker/node_modules/request/request.js:1161:10)
    at Request.emit (events.js:210:5)
    at Request.EventEmitter.emit (domain.js:476:20)
    at IncomingMessage.<anonymous> (/Users/damz/Desktop/rocketcapital-broker/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:299:28)
    at IncomingMessage.emit (events.js:215:7) {
  response: IncomingMessage {
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: null,
      pipesCount: 0,
      flowing: true,
      ended: true,
      endEmitted: true,
      reading: false,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      paused: false,
      emitClose: true,
      autoDestroy: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrain: 0,
      readingMore: true,
      decoder: null,
      encoding: null
    },
    readable: false,
    _events: [Object: null prototype] {
      end: [Array],
      close: [Array],
      data: [Function],
      error: [Function]
    },
    _eventsCount: 4,
    _maxListeners: undefined,
    socket: TLSSocket {
      _tlsOptions: [Object],
      _secureEstablished: true,
      _securePending: false,
      _newSessionPending: false,
      _controlReleased: true,
      _SNICallback: null,
      servername: 'api-public.sandbox.pro.coinbase.com',
      alpnProtocol: false,
      authorized: true,
      authorizationError: null,
      encrypted: true,
      _events: [Object: null prototype],
      _eventsCount: 9,
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'api-public.sandbox.pro.coinbase.com',
      _readableState: [ReadableState],
      readable: true,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: undefined,
      _server: null,
      ssl: [TLSWrap],
      _requestCert: true,
      _rejectUnauthorized: true,
      parser: null,
      _httpMessage: [ClientRequest],
      timeout: 10000,
      [Symbol(res)]: [TLSWrap],
      [Symbol(asyncId)]: 8,
      [Symbol(kHandle)]: [TLSWrap],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 10000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 504,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(asyncId)]: 18,
        [Symbol(triggerId)]: 16
      },
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(connect-options)]: [Object]
    },
    connection: TLSSocket {
      _tlsOptions: [Object],
      _secureEstablished: true,
      _securePending: false,
      _newSessionPending: false,
      _controlReleased: true,
      _SNICallback: null,
      servername: 'api-public.sandbox.pro.coinbase.com',
      alpnProtocol: false,
      authorized: true,
      authorizationError: null,
      encrypted: true,
      _events: [Object: null prototype],
      _eventsCount: 9,
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'api-public.sandbox.pro.coinbase.com',
      _readableState: [ReadableState],
      readable: true,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: undefined,
      _server: null,
      ssl: [TLSWrap],
      _requestCert: true,
      _rejectUnauthorized: true,
      parser: null,
      _httpMessage: [ClientRequest],
      timeout: 10000,
      [Symbol(res)]: [TLSWrap],
      [Symbol(asyncId)]: 8,
      [Symbol(kHandle)]: [TLSWrap],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 10000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 504,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(asyncId)]: 18,
        [Symbol(triggerId)]: 16
      },
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(connect-options)]: [Object]
    },
    httpVersionMajor: 1,
    httpVersionMinor: 1,
    httpVersion: '1.1',
    complete: true,
    headers: {
      date: 'Fri, 27 Sep 2019 21:04:35 GMT',
      'content-type': 'application/json; charset=utf-8',
      'content-length': '35',
      connection: 'close',
      'set-cookie': [Array],
      'access-control-allow-headers': 'Content-Type, Accept, cb-session, cb-fp, cb-form-factor',
      'access-control-allow-methods': 'GET,POST,DELETE,PUT',
      'access-control-allow-origin': '*',
      'access-control-expose-headers': 'cb-before, cb-after, cb-gdpr',
      'access-control-max-age': '7200',
      'cache-control': 'no-store',
      etag: 'W/"23-8NdEwgOr82p+EmFyYngVj/j+ywQ"',
      'strict-transport-security': 'max-age=15552000; includeSubDomains',
      vary: 'Accept-Encoding',
      'x-content-type-options': 'nosniff',
      'x-dns-prefetch-control': 'off',
      'x-download-options': 'noopen',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '1; mode=block',
      'expect-ct': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
      server: 'cloudflare',
      'cf-ray': '51d06e4d5db1be5f-MXP'
    },
    rawHeaders: [
      'Date',
      'Fri, 27 Sep 2019 21:04:35 GMT',
      'Content-Type',
      'application/json; charset=utf-8',
      'Content-Length',
      '35',
      'Connection',
      'close',
      'Set-Cookie',
      '__cfduid=d7618724a45e12cb1c0a867afb709f77c1569618275; expires=Sat, 26-Sep-20 21:04:35 GMT; path=/; domain=.pro.coinbase.com; HttpOnly',
      'Access-Control-Allow-Headers',
      'Content-Type, Accept, cb-session, cb-fp, cb-form-factor',
      'Access-Control-Allow-Methods',
      'GET,POST,DELETE,PUT',
      'Access-Control-Allow-Origin',
      '*',
      'Access-Control-Expose-Headers',
      'cb-before, cb-after, cb-gdpr',
      'Access-Control-Max-Age',
      '7200',
      'Cache-Control',
      'no-store',
      'ETag',
      'W/"23-8NdEwgOr82p+EmFyYngVj/j+ywQ"',
      'Strict-Transport-Security',
      'max-age=15552000; includeSubDomains',
      'Vary',
      'Accept-Encoding',
      'X-Content-Type-Options',
      'nosniff',
      'X-DNS-Prefetch-Control',
      'off',
      'X-Download-Options',
      'noopen',
      'X-Frame-Options',
      'SAMEORIGIN',
      'X-XSS-Protection',
      '1; mode=block',
      'Expect-CT',
      'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
      'Server',
      'cloudflare',
      'CF-RAY',
      '51d06e4d5db1be5f-MXP'
    ],
    trailers: {},
    rawTrailers: [],
    aborted: false,
    upgrade: false,
    url: '',
    method: null,
    statusCode: 500,
    statusMessage: 'Internal Server Error',
    client: TLSSocket {
      _tlsOptions: [Object],
      _secureEstablished: true,
      _securePending: false,
      _newSessionPending: false,
      _controlReleased: true,
      _SNICallback: null,
      servername: 'api-public.sandbox.pro.coinbase.com',
      alpnProtocol: false,
      authorized: true,
      authorizationError: null,
      encrypted: true,
      _events: [Object: null prototype],
      _eventsCount: 9,
      connecting: false,
      _hadError: false,
      _parent: null,
      _host: 'api-public.sandbox.pro.coinbase.com',
      _readableState: [ReadableState],
      readable: true,
      _maxListeners: undefined,
      _writableState: [WritableState],
      writable: false,
      allowHalfOpen: false,
      _sockname: null,
      _pendingData: null,
      _pendingEncoding: '',
      server: undefined,
      _server: null,
      ssl: [TLSWrap],
      _requestCert: true,
      _rejectUnauthorized: true,
      parser: null,
      _httpMessage: [ClientRequest],
      timeout: 10000,
      [Symbol(res)]: [TLSWrap],
      [Symbol(asyncId)]: 8,
      [Symbol(kHandle)]: [TLSWrap],
      [Symbol(lastWriteQueueSize)]: 0,
      [Symbol(timeout)]: Timeout {
        _idleTimeout: 10000,
        _idlePrev: [TimersList],
        _idleNext: [TimersList],
        _idleStart: 504,
        _onTimeout: [Function: bound ],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: false,
        [Symbol(asyncId)]: 18,
        [Symbol(triggerId)]: 16
      },
      [Symbol(kBuffer)]: null,
      [Symbol(kBufferCb)]: null,
      [Symbol(kBufferGen)]: null,
      [Symbol(kBytesRead)]: 0,
      [Symbol(kBytesWritten)]: 0,
      [Symbol(connect-options)]: [Object]
    },
    _consuming: false,
    _dumped: false,
    req: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 6,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: false,
      useChunkedEncodingByDefault: true,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: null,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      socket: [TLSSocket],
      connection: [TLSSocket],
      _header: 'POST /orders HTTP/1.1\r\n' +
        'User-Agent: coinbase-pro-node-client\r\n' +
        'Accept: application/json\r\n' +
        'Content-Type: application/json\r\n' +
        'CB-ACCESS-KEY: xxx\r\n' +
        'CB-ACCESS-SIGN: xxx/lZzQ=\r\n' +
        'CB-ACCESS-TIMESTAMP: 1569618275.323\r\n' +
        'CB-ACCESS-PASSPHRASE: xxx\r\n' +
        'host: api-public.sandbox.pro.coinbase.com\r\n' +
        'content-length: 90\r\n' +
        'Connection: close\r\n' +
        '\r\n',
      _onPendingData: [Function: noopPendingOutput],
      agent: [Agent],
      socketPath: undefined,
      method: 'POST',
      path: '/orders',
      _ended: true,
      res: [Circular],
      aborted: false,
      timeoutCb: [Function: emitRequestTimeout],
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      [Symbol(kNeedDrain)]: false,
      [Symbol(isCorked)]: false,
      [Symbol(kOutHeaders)]: [Object: null prototype]
    },
    request: Request {
      _events: [Object: null prototype],
      _eventsCount: 5,
      _maxListeners: undefined,
      body: '{"client_oid":1,"side":"buy","type":"market","product_id":"BTC-EUR","size":0.1,"stp":"dc"}',
      headers: [Object],
      method: 'POST',
      uri: [Url],
      qsStringifyOptions: [Object],
      timeout: 10000,
      callback: [Function],
      readable: true,
      writable: true,
      explicitMethod: true,
      _qs: [Querystring],
      _auth: [Auth],
      _oauth: [OAuth],
      _multipart: [Multipart],
      _redirect: [Redirect],
      _tunnel: [Tunnel],
      setHeader: [Function],
      hasHeader: [Function],
      getHeader: [Function],
      removeHeader: [Function],
      localAddress: undefined,
      pool: {},
      dests: [],
      __isRequestRequest: true,
      _callback: [Function],
      proxy: null,
      tunnel: true,
      setHost: true,
      originalCookieHeader: undefined,
      _disableCookies: true,
      _jar: undefined,
      port: 443,
      host: 'api-public.sandbox.pro.coinbase.com',
      path: '/orders',
      httpModule: [Object],
      agentClass: [Function: Agent],
      agent: [Agent],
      _started: true,
      href: 'https://api-public.sandbox.pro.coinbase.com/orders',
      req: [ClientRequest],
      ntick: true,
      timeoutTimer: null,
      response: [Circular],
      originalHost: 'api-public.sandbox.pro.coinbase.com',
      originalHostHeaderName: 'host',
      responseContent: [Circular],
      _destdata: true,
      _ended: true,
      _callbackCalled: true
    },
    toJSON: [Function: responseToJSON],
    caseless: Caseless { dict: [Object] },
    body: '{"message":"Internal server error"}'
  },
  data: { message: 'Internal server error' }
}

But the order is correctly placed on the order book, as I can see funds removed from my account!

vansergen commented 5 years ago

@damianobarbati client_oid is an optional parameter. You receive HTTP 500 Error because it must be a string. But if you simply change it to

client_oid='1'

you will receive HTTP 400 Error: Invalid client_oid (see https://github.com/coinbase/coinbase-pro-node/issues/345)

damianobarbati commented 5 years ago

Thanks @vansergen