tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
734 stars 265 forks source link

ReferenceError: crypto is not defined (electron/frontend) #356

Open ghost opened 11 months ago

ghost commented 11 months ago

Searched for solutions for electron but they don't help...

Error: ReferenceError: crypto is not defined, method: submitNewOrder; Environment: Electron; Run in: Node; Code:

const client = new USDMClient({
        api_key: apiKey,
        api_secret: secretKey,
        beautifyResponses: true
    });

    const symbol = 'BTCUSDT';

    const orderResult = client.submitNewOrder({
        side: 'BUY',
        symbol: symbol,
        type: 'MARKET',
        quantity: 0.001
    })
        .then(result => result)
        .catch((e) => console.log(e))
tiagosiebler commented 11 months ago

Since an electron app is essentially a "frontend" app like a website in a browser, there are some dependencies that might not work there (my SDKs are primarily built for the backend and use dependencies that are part of nodejs). Some users have gotten it working by pointing these dependencies to browser-compatible alternatives, e.g. using the tsconfig if you're using typescript: https://github.com/tiagosiebler/binance#browserfrontend-usage

ghost commented 11 months ago

Since an electron app is essentially a "frontend" app like a website in a browser, there are some dependencies that might not work there (my SDKs are primarily built for the backend and use dependencies that are part of nodejs). Some users have gotten it working by pointing these dependencies to browser-compatible alternatives, e.g. using the tsconfig if you're using typescript: https://github.com/tiagosiebler/binance#browserfrontend-usage

I’m using it inside node js module, but ye, this is electron issued I guess.

ghost commented 11 months ago

Since an electron app is essentially a "frontend" app like a website in a browser, there are some dependencies that might not work there (my SDKs are primarily built for the backend and use dependencies that are part of nodejs). Some users have gotten it working by pointing these dependencies to browser-compatible alternatives, e.g. using the tsconfig if you're using typescript: https://github.com/tiagosiebler/binance#browserfrontend-usage

Btw, before calling submitNewOrder func, crypto is available main process (nodejs)

app.whenReady().then(() => {
    console.log(crypto) // 

    ipcMain.handle('binanceTrade', async (_event, apiKey, secretKey) => {

        const client = new USDMClient({
            api_key: apiKey,
            api_secret: secretKey,
            beautifyResponses: true
        });

        const symbol = 'BTCUSDT';

        const orderResult = client.submitNewOrder({
            side: 'BUY',
            symbol: symbol,
            type: 'MARKET',
            quantity: 0.001
        })
            .then(result => result)
            .catch((e) => console.log(e))

        return {orderResult}
    })
})
tiagosiebler commented 11 months ago

For auth to work, crypto needs to be available as an importable package - it's used as part of the signing process: https://github.com/tiagosiebler/binance/blob/master/src/util/node-support.ts#L1

Are you using typescript? Or webpack? They're not related technologies, not directly, but both have a means to point any imports of "crypto" to a browser-compatible alternative "crypto-browserify". I have examples for both, the webpack one is in the readme somewhere