1
+ import { Contract , providers , Signer } from 'ethers'
1
2
import { HardhatRuntimeEnvironment } from 'hardhat/types'
2
3
import { extendEnvironment } from 'hardhat/config'
3
4
import { lazyObject } from 'hardhat/plugins'
@@ -15,12 +16,29 @@ declare module 'hardhat/types/runtime' {
15
16
}
16
17
}
17
18
19
+ interface ConsoleNetworkContracts extends NetworkContracts {
20
+ connect : ( ) => void
21
+ }
22
+
18
23
extendEnvironment ( ( hre : HardhatRuntimeEnvironment ) => {
19
24
hre [ 'contracts' ] = lazyObject ( ( ) => {
20
- const addressBook = getAddressBook (
21
- cliOpts . addressBook . default ,
22
- hre . network . config . chainId . toString ( ) ,
23
- )
24
- return loadContracts ( addressBook , hre . ethers . provider )
25
+ const chainId = hre . network . config . chainId . toString ( )
26
+ const provider = hre . ethers . provider
27
+ const addressBook = getAddressBook ( cliOpts . addressBook . default , chainId )
28
+ const contracts = loadContracts ( addressBook , provider ) as ConsoleNetworkContracts
29
+
30
+ // Connect contracts to a signing account
31
+ contracts . connect = async function ( n = 0 ) {
32
+ const accounts = await hre . ethers . getSigners ( )
33
+ const senderAccount = accounts [ n ]
34
+ console . log ( `> Sender set to ${ senderAccount . address } ` )
35
+ for ( const [ k , contract ] of Object . entries ( contracts ) ) {
36
+ if ( contract instanceof Contract ) {
37
+ contracts [ k ] = contract . connect ( senderAccount )
38
+ }
39
+ }
40
+ }
41
+
42
+ return contracts
25
43
} )
26
44
} )
0 commit comments