harsta
Version:
Harsta is a contract development tool based on Hardhat, designed to streamline the development, testing, and referencing of contracts, addresses, ABIs, and contract instances.
34 lines (30 loc) • 1.06 kB
text/typescript
import { UpgradesError } from '@openzeppelin/upgrades-core'
import type { Interface } from 'ethers'
import type { UserDeploymentConfig } from '../../types'
export function getInitializerData(
contractInterface: Interface,
args: unknown[],
{ initializer }: UserDeploymentConfig = {},
): string {
if (initializer === false) {
return '0x'
}
const allowNoInitialization = initializer === undefined && args.length === 0
initializer = initializer ?? 'initialize'
const fragment = contractInterface.getFunction(initializer)
if (fragment === null) {
if (allowNoInitialization) {
return '0x'
}
else {
throw new UpgradesError(
`The contract has no initializer function matching the name or signature: ${initializer}`,
() =>
`Ensure that the initializer function exists, specify an existing function with the 'initializer' option, or set the 'initializer' option to false to omit the initializer call.`,
)
}
}
else {
return contractInterface.encodeFunctionData(fragment, args)
}
}