Skip to content

Commit

Permalink
Add admin.getInstance (#274)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <[email protected]>
  • Loading branch information
pinglamb and frangio authored Jan 15, 2021
1 parent ec931ce commit 0d62287
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/plugin-hardhat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Add `upgrades.admin.getInstance()` to retrieve the instance of `ProxyAdmin` that is in use. ([#274](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/274))

## 1.4.3 (2020-12-16)

- Fix an error in the `unsafeAllowCustomTypes` flag that would cause other storage layout incompatibilities to be ignored. ([#259](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/259))
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-hardhat/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getProxyAdminFactory } from './proxy-factory';

export type ChangeAdminFunction = (proxyAddress: string, newAdmin: string) => Promise<void>;
export type TransferProxyAdminOwnershipFunction = (newOwner: string) => Promise<void>;
export type GetInstanceFunction = () => Promise<Contract>;

export function makeChangeProxyAdmin(hre: HardhatRuntimeEnvironment): ChangeAdminFunction {
return async function changeProxyAdmin(proxyAddress, newAdmin) {
Expand All @@ -26,6 +27,12 @@ export function makeTransferProxyAdminOwnership(hre: HardhatRuntimeEnvironment):
};
}

export function makeGetInstanceFunction(hre: HardhatRuntimeEnvironment): GetInstanceFunction {
return async function getInstance() {
return await getManifestAdmin(hre);
};
}

export async function getManifestAdmin(hre: HardhatRuntimeEnvironment): Promise<Contract> {
const manifest = await Manifest.forNetwork(hre.network.provider);
const manifestAdmin = await manifest.getAdmin();
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import { writeValidations } from './validations';
import type { silenceWarnings } from '@openzeppelin/upgrades-core';
import type { DeployFunction } from './deploy-proxy';
import type { UpgradeFunction, PrepareUpgradeFunction } from './upgrade-proxy';
import type { ChangeAdminFunction, TransferProxyAdminOwnershipFunction } from './admin';
import type { ChangeAdminFunction, TransferProxyAdminOwnershipFunction, GetInstanceFunction } from './admin';

export interface HardhatUpgrades {
deployProxy: DeployFunction;
upgradeProxy: UpgradeFunction;
prepareUpgrade: PrepareUpgradeFunction;
silenceWarnings: typeof silenceWarnings;
admin: {
getInstance: GetInstanceFunction;
changeProxyAdmin: ChangeAdminFunction;
transferProxyAdminOwnership: TransferProxyAdminOwnershipFunction;
};
Expand All @@ -45,7 +46,7 @@ subtask(TASK_COMPILE_SOLIDITY_COMPILE, async (args: RunCompilerArgs, hre, runSup
extendEnvironment(hre => {
hre.upgrades = lazyObject(
(): HardhatUpgrades => {
const { makeChangeProxyAdmin, makeTransferProxyAdminOwnership } = require('./admin');
const { makeChangeProxyAdmin, makeTransferProxyAdminOwnership, makeGetInstanceFunction } = require('./admin');
const { makeDeployProxy } = require('./deploy-proxy');
const { makeUpgradeProxy, makePrepareUpgrade } = require('./upgrade-proxy');
const { silenceWarnings } = require('@openzeppelin/upgrades-core');
Expand All @@ -56,6 +57,7 @@ extendEnvironment(hre => {
upgradeProxy: makeUpgradeProxy(hre),
prepareUpgrade: makePrepareUpgrade(hre),
admin: {
getInstance: makeGetInstanceFunction(hre),
changeProxyAdmin: makeChangeProxyAdmin(hre),
transferProxyAdminOwnership: makeTransferProxyAdminOwnership(hre),
},
Expand Down
17 changes: 17 additions & 0 deletions packages/plugin-hardhat/test/admin-instance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const test = require('ava');

const { ethers, upgrades } = require('hardhat');

test.before(async t => {
t.context.Greeter = await ethers.getContractFactory('Greeter');
});

test('admin.getInstance', async t => {
await t.throwsAsync(upgrades.admin.getInstance(), undefined, 'No ProxyAdmin was found in the network manifest');

const { Greeter } = t.context;
const greeter = await upgrades.deployProxy(Greeter, ['Hola admin!']);
const adminInstance = await upgrades.admin.getInstance();
const adminAddress = await adminInstance.getProxyAdmin(greeter.address);
t.is(adminInstance.address, adminAddress);
});
4 changes: 4 additions & 0 deletions packages/plugin-truffle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# Unreleased

- Add `upgrades.admin.getInstance()` to retrieve the instance of `ProxyAdmin` that is in use. ([#274](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/274))

## 1.3.1 (2020-12-16)

- Fix an error in the `unsafeAllowCustomTypes` flag that would cause other storage layout incompatibilities to be ignored. ([#259](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/259))
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-truffle/src/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ async function transferProxyAdminOwnership(newOwner: string, opts: Options = {})
await admin.transferOwnership(newOwner);
}

async function getInstance(opts: Options = {}): Promise<ContractInstance> {
const { deployer } = withDefaults(opts);
const provider = wrapProvider(deployer.provider);
return await getManifestAdmin(provider);
}

export async function getManifestAdmin(provider: EthereumProvider): Promise<ContractInstance> {
const manifest = await Manifest.forNetwork(provider);
const manifestAdmin = await manifest.getAdmin();
Expand All @@ -39,6 +45,7 @@ export async function getManifestAdmin(provider: EthereumProvider): Promise<Cont
}

export const admin = {
getInstance,
transferProxyAdminOwnership,
changeProxyAdmin,
};
7 changes: 7 additions & 0 deletions packages/plugin-truffle/test/test/admin-happy-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ contract('Admin', function () {
const provider = wrapProvider(deployer.provider);
const testAddress = '0x1E6876a6C2757de611c9F12B23211dBaBd1C9028';

it('getInstance', async function () {
const greeter = await upgrades.deployProxy(Greeter, ['Hello Truffle']);
const adminInstance = await upgrades.admin.getInstance();
const adminAddress = await adminInstance.getProxyAdmin(greeter.address);
assert.strictEqual(adminInstance.address, adminAddress);
});

it('changeProxyAdmin', async function () {
const greeter = await upgrades.deployProxy(Greeter, ['Hello Truffle']);
await upgrades.admin.changeProxyAdmin(greeter.address, testAddress);
Expand Down

0 comments on commit 0d62287

Please sign in to comment.