Skip to content

Commit

Permalink
🍎 Allow to pass waffle and ganache providers in opts (#95)
Browse files Browse the repository at this point in the history
* Allow to pass waffle and ganache providers in opts

* Changeset

* Fix lint
  • Loading branch information
dmaretskyi authored Apr 14, 2022
1 parent 91a85f0 commit 4f4e0fa
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-eagles-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ethereum-mars': patch
---

Allow to pass waffle and ganache providers in opts
6 changes: 4 additions & 2 deletions packages/mars/src/options/Options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { BigNumber } from 'ethers'
import { BigNumber, providers } from 'ethers'
import type Ganache from 'ganache-core'

export type NetworkLike = string | Ganache.Provider | providers.JsonRpcProvider

export interface Options {
/**
* Private key of the account to be used to sign deployment transactions.
Expand All @@ -11,7 +13,7 @@ export interface Options {
* If this is a name, then it is used to locate an ethereum network in one of the services (Infura, Alchemy etc.)
* If this is a URL or a provider instance, then it is used to construct an RPC provider to an existing network node.
*/
network?: string | Ganache.Provider
network?: NetworkLike
/**
* API key to Infura service. See: https://infura.io/
*/
Expand Down
9 changes: 7 additions & 2 deletions packages/mars/src/options/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Options } from './Options'
import { ensureMultisigConfig } from '../multisig/multisigConfig'
import { logConfig } from '../logging'
import { chains } from './chain'
import { JsonRpcProvider } from '@ethersproject/providers'

export async function getConfig(options: Options): Promise<ExecuteOptions> {
const merged = {
Expand Down Expand Up @@ -64,7 +65,9 @@ export async function getConfig(options: Options): Promise<ExecuteOptions> {
}
}

function isNetworkProvider(network: string | Ganache.Provider): network is Ganache.Provider {
function isNetworkProvider(
network: string | Ganache.Provider | providers.JsonRpcProvider
): network is Ganache.Provider | providers.JsonRpcProvider {
return !!network && typeof network === 'object' && (network as Ganache.Provider).send !== undefined
}

Expand All @@ -78,7 +81,9 @@ async function getSigner(options: Options) {
let rpcUrl: string | undefined
let provider: providers.JsonRpcProvider | undefined

if (isNetworkProvider(network)) {
if (JsonRpcProvider.isProvider(network)) {
provider = network
} else if (isNetworkProvider(network)) {
// this causes 'MaxListenersExceededWarning: Possible EventEmitter memory leak detected.' when many contracts in use
// details at https://github.com/ChainSafe/web3.js/issues/1648
provider = new providers.Web3Provider(network as any)
Expand Down
25 changes: 25 additions & 0 deletions packages/mars/test/options/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expect } from 'chai'
import { BigNumber, Wallet } from 'ethers'
import Ganache from 'ganache-core'
import { getConfig } from '../../src/options'
import { MockProvider } from 'ethereum-waffle'

const PRIVATE_KEY_1 = `0x${'1'.repeat(64)}`
const PRIVATE_KEY_2 = `0x${'2'.repeat(64)}`
Expand Down Expand Up @@ -44,6 +46,29 @@ describe('getConfig', () => {
expect((result3.signer as Wallet).privateKey).to.equal(PRIVATE_KEY_3)
})

it('can pass ganache as network', async () => {
const ganache = Ganache.provider({ networkId: 1337 })
const config = await getConfig({ ...defaults, network: ganache, privateKey: PRIVATE_KEY_1 })

expect((config.signer as Wallet).privateKey).to.equal(PRIVATE_KEY_1)

const network = await config.provider.getNetwork()
expect(network.chainId).to.equal(1337)

// eslint-disable-next-line @typescript-eslint/no-empty-function
ganache.close(() => {})
})

it('can pass Waffle MockProvider as network', async () => {
const waffle = new MockProvider({ ganacheOptions: { networkId: 1337 } })
const config = await getConfig({ ...defaults, network: waffle, privateKey: PRIVATE_KEY_1 })

expect((config.signer as Wallet).privateKey).to.equal(PRIVATE_KEY_1)

const network = await config.provider.getNetwork()
expect(network.chainId).to.equal(1337)
})

it('dry run implies noConfirm', async () => {
const result = await getConfig({ dryRun: true, ...defaults })
expect(result.noConfirm).to.equal(true)
Expand Down

0 comments on commit 4f4e0fa

Please sign in to comment.