Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions app/util/test/ganache.js → app/util/test/ganache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { getGanachePort } from '../../../e2e/fixtures/utils';
import ganache from 'ganache';
import ganache, { Server, ServerOptions, EthereumProvider } from 'ganache';

export const DEFAULT_GANACHE_PORT = 8545;

const defaultOptions = {
interface GanacheOptions extends ServerOptions {
mnemonic: string;
blockTime?: number;
network_id?: number;
port?: number;
vmErrorsOnRPCResponse?: boolean;
hardfork?: string;
quiet?: boolean;
}

const defaultOptions: Omit<GanacheOptions, 'mnemonic'> = {
blockTime: 2,
network_id: 1337,
port: DEFAULT_GANACHE_PORT,
Expand All @@ -13,7 +23,9 @@ const defaultOptions = {
};

export default class Ganache {
async start(opts) {
private _server: Server | undefined;

async start(opts: GanacheOptions): Promise<void> {
if (!opts.mnemonic) {
throw new Error('Missing required mnemonic');
}
Expand All @@ -28,32 +40,40 @@ export default class Ganache {
}
}

getProvider() {
getProvider(): EthereumProvider | undefined {
return this._server?.provider;
}

async getAccounts() {
return await this.getProvider().request({
async getAccounts(): Promise<string[]> {
const provider = this.getProvider();
if (!provider) {
throw new Error('Provider not available');
}
return await provider.request({
method: 'eth_accounts',
params: [],
});
}

async getBalance() {
async getBalance(): Promise<string | number> {
const accounts = await this.getAccounts();
const balanceHex = await this.getProvider().request({
const provider = this.getProvider();
if (!provider) {
throw new Error('Provider not available');
}
const balanceHex = await provider.request({
method: 'eth_getBalance',
params: [accounts[0], 'latest'],
});
const balanceInt = parseInt(balanceHex, 16) / 10 ** 18;
const balanceInt = parseInt(balanceHex as string, 16) / 10 ** 18;

const balanceFormatted =
balanceInt % 1 === 0 ? balanceInt : balanceInt.toFixed(4);

return balanceFormatted;
}

async quit() {
async quit(): Promise<void> {
if (!this._server) {
throw new Error('Server not running yet');
}
Expand Down
Loading