-1

I'm trying to make a desktop bybit trading app... and I can't figure it out how to make the code wait for the response from the main script..... I need to wait for a response with the needed info for example like wallet balance. Instead the code runs asynchronously and I the var I need it to render is undefined.... I found the article about the vulnerability of nodeIntegration: true from the author of electron security or smth. So I did everything like he said... but now I can't "pause" the code for the data to receive and render.... here's the code

main.js

const { app, BrowserWindow, ipcMain, Menu } = require('electron');
const path = require('path');

const CryptoJS = require('crypto-js');
const axios = require('axios');

let win;

async function createWindow() {
    // Create the browser window.
    win = new BrowserWindow({
        width: 385,
        height: 200,
        titleBarStyle: 'hidden',

        webPreferences: {
            nodeIntegration: false, // is default value after Electron v5
            contextIsolation: true, // protect against prototype pollution
            enableRemoteModule: false, // turn off remote
            preload: path.join(app.getAppPath(), 'preload.js'), // use a preload script
            backgroundThrottling: false,
        },
    });

    // Load app
    win.loadFile(path.join(__dirname, './index.html'));
    win.setAlwaysOnTop(true, 'screen');
    // win.removeMenu();
    // rest of code..
}

app.on('ready', createWindow);

ipcMain.on('giveBalance', async e => {
    const apiKey = '';
    const secret = '';
    const timestamp = Date.now().toString();

    const params = {
        api_key: apiKey,
        timestamp: timestamp,
    };
    let orderedParams = '';
    Object.keys(params)
        .sort()
        .forEach(function (key) {
            orderedParams += key + '=' + params[key] + '&';
        });
    orderedParams = orderedParams.substring(0, orderedParams.length - 1);

    var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secret);
    hmac.update(orderedParams);
    const sign = hmac.finalize().toString(CryptoJS.enc.Hex);

    const res = await axios.get(`https://api.bybit.com/v2/private/wallet/balance?&api_key=${apiKey}&sign=${sign}&timestamp=${timestamp}`);
    let responseObj = res.data.result.USDT.equity.toFixed(2);
    console.log(res.data.result.USDT.equity.toFixed(2));
    win.webContents.send('balance', { responseObj });
});

preload.js

const { contextBridge, ipcRenderer } = require('electron');

// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
contextBridge.exposeInMainWorld('api', {
    send: (channel, data) => {
        // whitelist channels
        let validChannels = ['giveBalance', 'givePosition'];
        if (validChannels.includes(channel)) {
            ipcRenderer.send(channel, data);
        }
    },
    receive: (channel, func) => {
        let validChannels = ['balance', 'position'];
        if (validChannels.includes(channel)) {
            // Deliberately strip event as it includes `sender`
            ipcRenderer.on(channel, (event, ...args) => func(...args));
        }
    },
});

script.js

const getBalance = () => {
    window.api.send('giveBalance');
    const res = window.api.receive('balance', data => {
        console.log('hi');
        return data.responseObj;
    });

    const myBalance = '$' + res;
    document.querySelector('.myBalance').textContent = `My Balance: ${myBalance}`;
    console.log('not in time');
};

getBalance();

I need to get the balance in the scipt.js, to stop the code untill the res var has received the data... but all of the code gets executed right away and only then it receives a message... thank you..

if I change the part in preload.js to this changing .on to .sendSync

receive: (channel, func) => {
        let validChannels = ['balance', 'position'];
        if (validChannels.includes(channel)) {
            // Deliberately strip event as it includes `sender`
            ipcRenderer.sendSync(channel, (event, ...args) => func(...args));
        }
    },

then I get

Uncaught Error: An object could not be cloned.
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Eugene1111
  • 27
  • 2
  • 7

1 Answers1

0

I just needed to put the rest logic inside of the receive message scope...

Vincent
  • 2,963
  • 1
  • 19
  • 26
Eugene1111
  • 27
  • 2
  • 7