Skip to content

Commit d258fd1

Browse files
committed
cli: expose a function in the console to connect contracts with a signer
1 parent f377650 commit d258fd1

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

tasks/gre.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Contract, providers, Signer } from 'ethers'
12
import { HardhatRuntimeEnvironment } from 'hardhat/types'
23
import { extendEnvironment } from 'hardhat/config'
34
import { lazyObject } from 'hardhat/plugins'
@@ -15,12 +16,29 @@ declare module 'hardhat/types/runtime' {
1516
}
1617
}
1718

19+
interface ConsoleNetworkContracts extends NetworkContracts {
20+
connect: () => void
21+
}
22+
1823
extendEnvironment((hre: HardhatRuntimeEnvironment) => {
1924
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
2543
})
2644
})

0 commit comments

Comments
 (0)