This repository was archived by the owner on Apr 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
90 lines (82 loc) · 3.73 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { spawn } from 'child_process'
import fs from 'fs'
import { exit } from 'process'
import { getAddressBookConfig } from './config'
import { FunctionSelector, IContractAddressDeployed, IInquirerListField } from './types'
let contractsAddressDeployed: IContractAddressDeployed[] = []
let contractsAddressDeployedHistory: IContractAddressDeployed[] = []
const addressBookConfig = getAddressBookConfig()
export const inquirerRunTests: IInquirerListField = { name: 'Run tests' }
if (!fs.existsSync('test')) inquirerRunTests.disabled = "We can't run tests without a test/ directory"
export const inquirerRunScripts: IInquirerListField = { name: 'Run scripts' }
if (!fs.existsSync('scripts')) inquirerRunScripts.disabled = "We can't run scripts without a scripts/ directory"
export const inquirerFlattenContracts: IInquirerListField = { name: 'Flatten contracts' }
export const inquirerRunMockContractCreator: IInquirerListField = { name: 'Create Mock contracts' }
export let inquirerRunFoundryTest: string = ''
if (!fs.existsSync('contracts')) {
inquirerFlattenContracts.disabled = "We can't flatten contracts without a contracts/ directory"
inquirerRunMockContractCreator.disabled = "We can't create Mock contracts without a contracts/ directory"
}
if (fs.existsSync('contracts/test') && fs.existsSync('foundry.toml')) {
inquirerRunFoundryTest = 'Run Foundry Forge tests'
}
export let inquirerFileContractsAddressDeployed: IInquirerListField | string = {
name: 'Get the previously deployed contracts address',
disabled: 'Please deploy the contracts first'
}
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)) {
const rawdata: any = fs.readFileSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployed)
try {
contractsAddressDeployed = JSON.parse(rawdata)
inquirerFileContractsAddressDeployed = 'Get the previously deployed contracts address'
} catch {}
}
export let inquirerFileContractsAddressDeployedHistory: IInquirerListField | string = {
name: 'Get all the previously deployed contracts address',
disabled: 'Please deploy the contracts first'
}
if (fs.existsSync(addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory)) {
try {
const rawdata: any = fs.readFileSync(
addressBookConfig.savePath + addressBookConfig.fileContractsAddressDeployedHistory
)
contractsAddressDeployedHistory = JSON.parse(rawdata)
inquirerFileContractsAddressDeployedHistory = 'Get all the previously deployed contracts address'
} catch {}
}
export const runCommand = async (
command: string,
firstCommand: string,
commandFlags: string,
thenExit: boolean = true
) => {
let commandToRun = command + commandFlags
if (firstCommand) {
commandToRun = firstCommand + commandFlags + ' && ' + commandToRun
}
console.log('\x1b[33m%s\x1b[0m', 'Command to run: ', '\x1b[97m\x1b[0m', commandToRun)
console.log(`Please wait...
`)
const runSpawn = spawn(commandToRun, {
stdio: 'inherit',
shell: true
})
runSpawn.on('exit', (code) => {
if (thenExit) exit()
})
}
export const listAllFunctionSelectors = async (hre: any, contractName: string) => {
const factory = await hre.ethers.getContractFactory(contractName)
const functions: FunctionSelector[] = []
for (const [name] of Object.entries(factory.interface.functions)) {
functions.push({
name,
selector: hre.ethers.utils.id(name).substring(0, 10)
})
}
functions.sort((a, b) => {
return a.selector.localeCompare(b.selector)
})
return functions
}
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))