mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 07:16:36 +01:00
c940f744a5
* Using ganache requests to get balances * Replace getBalance getAccounts with ganache funcs * Add secondary ganache server to testsuite
56 lines
1.2 KiB
JavaScript
56 lines
1.2 KiB
JavaScript
const ganache = require('ganache');
|
|
|
|
const defaultOptions = {
|
|
blockTime: 2,
|
|
network_id: 1337,
|
|
mnemonic:
|
|
'phrase upgrade clock rough situate wedding elder clever doctor stamp excess tent',
|
|
port: 8545,
|
|
vmErrorsOnRPCResponse: false,
|
|
hardfork: 'muirGlacier',
|
|
quiet: true,
|
|
};
|
|
|
|
class Ganache {
|
|
async start(opts) {
|
|
const options = { ...defaultOptions, ...opts };
|
|
const { port } = options;
|
|
this._server = ganache.server(options);
|
|
await this._server.listen(port);
|
|
}
|
|
|
|
getProvider() {
|
|
return this._server.provider;
|
|
}
|
|
|
|
async getAccounts() {
|
|
return await this.getProvider().request({
|
|
method: 'eth_accounts',
|
|
params: [],
|
|
});
|
|
}
|
|
|
|
async getBalance() {
|
|
const accounts = await this.getAccounts();
|
|
const balanceHex = await this.getProvider().request({
|
|
method: 'eth_getBalance',
|
|
params: [accounts[0], 'latest'],
|
|
});
|
|
const balanceInt = parseInt(balanceHex, 16) / 10 ** 18;
|
|
|
|
const balanceFormatted =
|
|
balanceInt % 1 === 0 ? balanceInt : balanceInt.toFixed(4);
|
|
|
|
return balanceFormatted;
|
|
}
|
|
|
|
async quit() {
|
|
if (!this._server) {
|
|
throw new Error('Server not running yet');
|
|
}
|
|
await this._server.close();
|
|
}
|
|
}
|
|
|
|
module.exports = Ganache;
|