diff --git a/packages/plugin-buidler/src/deploy-proxy.ts b/packages/plugin-buidler/src/deploy-proxy.ts index bf3f8ea0d..e1c31e760 100644 --- a/packages/plugin-buidler/src/deploy-proxy.ts +++ b/packages/plugin-buidler/src/deploy-proxy.ts @@ -56,14 +56,16 @@ export function makeDeployProxy(bre: BuidlerRuntimeEnvironment): DeployFunction const allowNoInitialization = initializer === undefined && args.length === 0; initializer = initializer ?? 'initialize'; - const initializers = ImplFactory.interface.fragments.filter(f => f.name === initializer); - - if (initializers.length > 0) { - return ImplFactory.interface.encodeFunctionData(initializer, args); - } else if (allowNoInitialization) { - return '0x'; - } else { - throw new Error(`Contract does not have a function \`${initializer}\``); + try { + const fragment = ImplFactory.interface.getFunction(initializer); + return ImplFactory.interface.encodeFunctionData(fragment, args); + } catch (e: unknown) { + if (e instanceof Error) { + if (allowNoInitialization && e.message.includes('no matching function')) { + return '0x'; + } + } + throw e; } } } diff --git a/packages/plugin-buidler/test.js b/packages/plugin-buidler/test.js index 3841ca2bb..708db7767 100644 --- a/packages/plugin-buidler/test.js +++ b/packages/plugin-buidler/test.js @@ -40,3 +40,4 @@ testFile('deploy-validation'); testFile('upgrade-validation'); testFile('upgrade-storage'); testFile('admin-validation'); +testFile('initializers'); diff --git a/packages/plugin-buidler/test/contracts/Initializers.sol b/packages/plugin-buidler/test/contracts/Initializers.sol new file mode 100644 index 000000000..f44973844 --- /dev/null +++ b/packages/plugin-buidler/test/contracts/Initializers.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.5.1; + +contract InitializerOverloaded { + uint public x; + + function initialize(uint _x) public { + x = _x; + } + + function initialize(string memory) public {} +} + +contract InitializerMissing { +} diff --git a/packages/plugin-buidler/test/initializers.js b/packages/plugin-buidler/test/initializers.js new file mode 100644 index 000000000..a2a4f49af --- /dev/null +++ b/packages/plugin-buidler/test/initializers.js @@ -0,0 +1,18 @@ +const assert = require('assert'); +const { ethers, upgrades } = require('@nomiclabs/buidler'); + +async function main() { + const InitializerOverloaded = await ethers.getContractFactory('InitializerOverloaded'); + const instance = await upgrades.deployProxy(InitializerOverloaded, [42], { initializer: 'initialize(uint256)' }); + assert.strictEqual('42', (await instance.x()).toString()); + + const InitializerMissing = await ethers.getContractFactory('InitializerMissing'); + await upgrades.deployProxy(InitializerMissing); +} + +main() + .then(() => process.exit(0)) + .catch(error => { + console.error(error); + process.exit(1); + });