From bfc1ee1b9ab87aa450e354e64c103fed403650f8 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 6 Mar 2023 12:12:28 +0100 Subject: [PATCH] use await for getBalance() --- apps/remix-ide/src/blockchain/blockchain.js | 4 +-- .../src/blockchain/providers/injected.js | 10 ++----- .../src/blockchain/providers/node.js | 10 ++----- apps/remix-ide/src/blockchain/providers/vm.js | 10 ++----- .../run-tab/src/lib/actions/account.ts | 30 +++++++------------ .../run-tab/src/lib/actions/deploy.ts | 16 +++++----- .../run-tab/src/lib/types/blockchain.d.ts | 2 +- .../run-tab/src/lib/types/injected.d.ts | 2 +- libs/remix-ui/run-tab/src/lib/types/node.d.ts | 2 +- libs/remix-ui/run-tab/src/lib/types/vm.d.ts | 2 +- 10 files changed, 33 insertions(+), 55 deletions(-) diff --git a/apps/remix-ide/src/blockchain/blockchain.js b/apps/remix-ide/src/blockchain/blockchain.js index 3018606115..d5f747d342 100644 --- a/apps/remix-ide/src/blockchain/blockchain.js +++ b/apps/remix-ide/src/blockchain/blockchain.js @@ -567,8 +567,8 @@ export class Blockchain extends Plugin { } /** Get the balance of an address, and convert wei to ether */ - getBalanceInEther (address, cb) { - this.getCurrentProvider().getBalanceInEther(address, cb) + getBalanceInEther (address) { + return this.getCurrentProvider().getBalanceInEther(address) } pendingTransactionsCount () { diff --git a/apps/remix-ide/src/blockchain/providers/injected.js b/apps/remix-ide/src/blockchain/providers/injected.js index 64c5094694..dbfc53f9de 100644 --- a/apps/remix-ide/src/blockchain/providers/injected.js +++ b/apps/remix-ide/src/blockchain/providers/injected.js @@ -20,13 +20,9 @@ class InjectedProvider { /* Do nothing. */ } - getBalanceInEther (address, cb) { - this.executionContext.web3().eth.getBalance(address, (err, res) => { - if (err) { - return cb(err) - } - cb(null, Web3.utils.fromWei(res.toString(10), 'ether')) - }) + async getBalanceInEther (address) { + const balance = await this.executionContext.web3().eth.getBalance(address) + return Web3.utils.fromWei(balance.toString(10), 'ether') } getGasPrice (cb) { diff --git a/apps/remix-ide/src/blockchain/providers/node.js b/apps/remix-ide/src/blockchain/providers/node.js index bcc00b553f..14350a45f7 100644 --- a/apps/remix-ide/src/blockchain/providers/node.js +++ b/apps/remix-ide/src/blockchain/providers/node.js @@ -28,13 +28,9 @@ class NodeProvider { /* Do nothing. */ } - getBalanceInEther (address, cb) { - this.executionContext.web3().eth.getBalance(address, (err, res) => { - if (err) { - return cb(err) - } - cb(null, Web3.utils.fromWei(res.toString(10), 'ether')) - }) + async getBalanceInEther (address) { + const balance = await this.executionContext.web3().eth.getBalance(address) + return Web3.utils.fromWei(balance.toString(10), 'ether') } getGasPrice (cb) { diff --git a/apps/remix-ide/src/blockchain/providers/vm.js b/apps/remix-ide/src/blockchain/providers/vm.js index 527bdf1fbc..f12d44c7b9 100644 --- a/apps/remix-ide/src/blockchain/providers/vm.js +++ b/apps/remix-ide/src/blockchain/providers/vm.js @@ -68,13 +68,9 @@ class VMProvider { this.RemixSimulatorProvider.Accounts.newAccount(cb) } - getBalanceInEther (address, cb) { - this.web3.eth.getBalance(address, (err, res) => { - if (err) { - return cb(err) - } - cb(null, Web3.utils.fromWei(new BN(res).toString(10), 'ether')) - }) + async getBalanceInEther (address) { + const balance = await this.web3.eth.getBalance(address) + return Web3.utils.fromWei(new BN(balance).toString(10), 'ether') } getGasPrice (cb) { diff --git a/libs/remix-ui/run-tab/src/lib/actions/account.ts b/libs/remix-ui/run-tab/src/lib/actions/account.ts index 461f928d43..c5c3046078 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/account.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/account.ts @@ -3,17 +3,14 @@ import { RunTab } from "../types/run-tab" import { clearInstances, setAccount, setExecEnv } from "./actions" import { displayNotification, displayPopUp, fetchAccountsListFailed, fetchAccountsListRequest, fetchAccountsListSuccess, setExternalEndpoint, setMatchPassphrase, setPassphrase } from "./payload" -export const updateAccountBalances = (plugin: RunTab, dispatch: React.Dispatch) => { +export const updateAccountBalances = async (plugin: RunTab, dispatch: React.Dispatch) => { const accounts = plugin.REACT_API.accounts.loadedAccounts - Object.keys(accounts).map((value) => { - plugin.blockchain.getBalanceInEther(value, (err, balance) => { - if (err) return - const updated = shortenAddress(value, balance) - - accounts[value] = updated - }) - }) + for (const account of Object.keys(accounts)) { + const balance = await plugin.blockchain.getBalanceInEther(account) + const updated = shortenAddress(account, balance) + accounts[account] = updated + } dispatch(fetchAccountsListSuccess(accounts)) } @@ -31,17 +28,10 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch< // - all the promises resolve // - at least one reject // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all - await (Promise as any).all(accounts.map((account) => { - return new Promise((resolve, reject) => { - plugin.blockchain.getBalanceInEther(account, (err, balance) => { - if (err) return reject(err) - const updated = shortenAddress(account, balance) - - loadedAccounts[account] = updated - resolve(account) - }) - }) - })) + for (const account of accounts) { + const balance = await plugin.blockchain.getBalanceInEther(account) + loadedAccounts[account] = shortenAddress(account, balance) + } const provider = plugin.blockchain.getProvider() if (provider === 'injected') { diff --git a/libs/remix-ui/run-tab/src/lib/actions/deploy.ts b/libs/remix-ui/run-tab/src/lib/actions/deploy.ts index 9a02243b01..d2efbe71c1 100644 --- a/libs/remix-ui/run-tab/src/lib/actions/deploy.ts +++ b/libs/remix-ui/run-tab/src/lib/actions/deploy.ts @@ -6,7 +6,7 @@ import { SolcInput, SolcOutput } from "@openzeppelin/upgrades-core" // Used direct path to UpgradeableContract class to fix cyclic dependency error from @openzeppelin/upgrades-core library import { UpgradeableContract } from '../../../../../../node_modules/@openzeppelin/upgrades-core/dist/standalone' import { DeployMode, MainnetPrompt } from "../types" -import { displayNotification, displayPopUp, fetchProxyDeploymentsSuccess, setDecodedResponse } from "./payload" +import { displayNotification, displayPopUp, fetchProxyDeploymentsSuccess, setDecodedResponse, updateInstancesBalance } from "./payload" import { addInstance } from "./actions" import { addressToString, logBuilder } from "@remix-ui/helper" import Web3 from "web3" @@ -318,15 +318,15 @@ export const getFuncABIInputs = (plugin: RunTab, funcABI: FuncABI) => { return plugin.blockchain.getInputs(funcABI) } -export const updateInstanceBalance = (plugin: RunTab) => { +export const updateInstanceBalance = async (plugin: RunTab, dispatch: React.Dispatch) => { if (plugin.REACT_API?.instances?.instanceList?.length) { - for (const instance of plugin.REACT_API.instances.instanceList) { - plugin.blockchain.getBalanceInEther(instance.address, (err, balInEth) => { - console.log('balance of ',instance.address, balInEth) - if (!err) instance.balance = balInEth - }) + const instances = plugin.REACT_API?.instances?.instanceList + for (const instance of instances) { + const balInEth = await plugin.blockchain.getBalanceInEther(instance.address) + instance.balance = balInEth } - } + dispatch(updateInstanceBalance(instances, dispatch)) + } } export const isValidContractAddress = async (plugin: RunTab, address: string) => { diff --git a/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts b/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts index 03b4560514..137ff0e92c 100644 --- a/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/blockchain.d.ts @@ -71,7 +71,7 @@ export class Blockchain extends Plugin { }): any; newAccount(_password: any, passwordPromptCb: any, cb: any): any; /** Get the balance of an address, and convert wei to ether */ - getBalanceInEther(address: any, cb: any): void; + getBalanceInEther(address: any): Promise; pendingTransactionsCount(): number; /** * This function send a tx only to Remix VM or testnet, will return an error for the mainnet diff --git a/libs/remix-ui/run-tab/src/lib/types/injected.d.ts b/libs/remix-ui/run-tab/src/lib/types/injected.d.ts index 2cc804c792..085faca4c5 100644 --- a/libs/remix-ui/run-tab/src/lib/types/injected.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/injected.d.ts @@ -5,7 +5,7 @@ declare class InjectedProvider { getAccounts(cb: any): any; newAccount(passwordPromptCb: any, cb: any): void; resetEnvironment(): Promise; - getBalanceInEther(address: any, cb: any): void; + getBalanceInEther(address: any): Promise; getGasPrice(cb: any): void; signMessage(message: any, account: any, _passphrase: any, cb: any): void; getProvider(): string; diff --git a/libs/remix-ui/run-tab/src/lib/types/node.d.ts b/libs/remix-ui/run-tab/src/lib/types/node.d.ts index 726a4347eb..365c95a2cb 100644 --- a/libs/remix-ui/run-tab/src/lib/types/node.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/node.d.ts @@ -6,7 +6,7 @@ declare class NodeProvider { getAccounts(cb: any): any; newAccount(passwordPromptCb: any, cb: any): any; resetEnvironment(): Promise; - getBalanceInEther(address: any, cb: any): void; + getBalanceInEther(address: any): Promise; getGasPrice(cb: any): void; signMessage(message: any, account: any, passphrase: any, cb: any): void; getProvider(): any; diff --git a/libs/remix-ui/run-tab/src/lib/types/vm.d.ts b/libs/remix-ui/run-tab/src/lib/types/vm.d.ts index 1f8034d417..5b94d5315d 100644 --- a/libs/remix-ui/run-tab/src/lib/types/vm.d.ts +++ b/libs/remix-ui/run-tab/src/lib/types/vm.d.ts @@ -9,7 +9,7 @@ declare class VMProvider { web3: any; createVMAccount(newAccount: any): string; newAccount(_passwordPromptCb: any, cb: any): void; - getBalanceInEther(address: any, cb: any): void; + getBalanceInEther(address: any): Promise; getGasPrice(cb: any): void; signMessage(message: any, account: any, _passphrase: any, cb: any): void; getProvider(): string;