tiagosiebler / ftx-api

Node.js connector for the FTX.com & FTX.us APIs and WebSockets, with TypeScript & browser support.
https://www.npmjs.com/package/ftx-api
MIT License
123 stars 47 forks source link

[ReferenceError: RestClient is not defined] Using webpacked ftxapi #64

Closed 0x520x650x78 closed 2 years ago

0x520x650x78 commented 2 years ago

I encountered this error while trying to integrate this package in browser. After running webpack and put it in browser script, I got ReferenceError: RestClient is not defined error.

Below is a snippet of code that I run:

const test_client = new RestClient(credential.test[0], credential.test[1], {subAccountName: 'test'});

The webpacked .js file is as following: https://gist.githubusercontent.com/0x520x650x78/cc6538ac9c6b913ac4ebba72a876c0df/raw/cd0f4f2afaf8b129341e2f7855ccb18615f7c9b8/ftxapi

Thanks!

tiagosiebler commented 2 years ago

In the webpack config, the library is packaged as a global under the name ftxapi when loaded in the browser: https://github.com/tiagosiebler/ftx-api/blob/master/webpack/webpack.config.js#L12

You should be able to do something like

<script type="module" src="./src/ftxapi.js"></script>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

const { RestClient } = ftxapi;// or window.ftxapi

const test_client = new RestClient(credential.test[0], credential.test[1], {subAccountName: 'test'});
0x520x650x78 commented 2 years ago

In the webpack config, the library is packaged as a global under the name ftxapi when loaded in the browser: https://github.com/tiagosiebler/ftx-api/blob/master/webpack/webpack.config.js#L12

You should be able to do something like

<script type="module" src="./src/ftxapi.js"></script>
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>

const { RestClient } = ftxapi;// or window.ftxapi

const test_client = new RestClient(credential.test[0], credential.test[1], {subAccountName: 'test'});

Thanks for the help. When I define the restclient in this way and run it I get a network error. image

However, when I run the with the same key and secret with nodejs, it seems to work out fine.

tiagosiebler commented 2 years ago

Could it be CORS? You could try to catch the exception and logging it for more detail, and/or check the network tab in your browser's dev tools to see what request happened and if it failed.

RE cors, lots of context about it in a thread in my bybit connector: https://github.com/tiagosiebler/bybit-api/issues/79

0x520x650x78 commented 2 years ago

OK I will take a look and update the progress I make here. (Maybe a pull request if I spot where the issue is)

Thanks for the reference and the great package.

0x520x650x78 commented 2 years ago

OK I found the issue. It is the CORs problem and since I'm using the browser for the dashboard, I just need to allow all CORs and then the issue will be resolved.