Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Buidler deployProxy for overloaded initializers #149

Merged
merged 4 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/plugin-buidler/src/deploy-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
1 change: 1 addition & 0 deletions packages/plugin-buidler/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ testFile('deploy-validation');
testFile('upgrade-validation');
testFile('upgrade-storage');
testFile('admin-validation');
testFile('initializers');
14 changes: 14 additions & 0 deletions packages/plugin-buidler/test/contracts/Initializers.sol
Original file line number Diff line number Diff line change
@@ -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 {
}
18 changes: 18 additions & 0 deletions packages/plugin-buidler/test/initializers.js
Original file line number Diff line number Diff line change
@@ -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);
});