-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #387 from ensdomains/migration-helper
Initial revision of MigrationHelper
- Loading branch information
Showing
4 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.4; | ||
|
||
import {IBaseRegistrar} from "../ethregistrar/IBaseRegistrar.sol"; | ||
import {INameWrapper} from "../wrapper/INameWrapper.sol"; | ||
import {Controllable} from "../wrapper/Controllable.sol"; | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract MigrationHelper is Ownable, Controllable { | ||
IBaseRegistrar public immutable registrar; | ||
INameWrapper public immutable wrapper; | ||
address public migrationTarget; | ||
|
||
error MigrationTargetNotSet(); | ||
|
||
event MigrationTargetUpdated(address indexed target); | ||
|
||
constructor(IBaseRegistrar _registrar, INameWrapper _wrapper) { | ||
registrar = _registrar; | ||
wrapper = _wrapper; | ||
} | ||
|
||
function setMigrationTarget(address target) external onlyOwner { | ||
migrationTarget = target; | ||
emit MigrationTargetUpdated(target); | ||
} | ||
|
||
function migrateNames( | ||
address nameOwner, | ||
uint256[] memory tokenIds, | ||
bytes memory data | ||
) external onlyController { | ||
if (migrationTarget == address(0)) { | ||
revert MigrationTargetNotSet(); | ||
} | ||
|
||
for (uint256 i = 0; i < tokenIds.length; i++) { | ||
registrar.safeTransferFrom( | ||
nameOwner, | ||
migrationTarget, | ||
tokenIds[i], | ||
data | ||
); | ||
} | ||
} | ||
|
||
function migrateWrappedNames( | ||
address nameOwner, | ||
uint256[] memory tokenIds, | ||
bytes memory data | ||
) external onlyController { | ||
if (migrationTarget == address(0)) { | ||
revert MigrationTargetNotSet(); | ||
} | ||
|
||
uint256[] memory amounts = new uint256[](tokenIds.length); | ||
for (uint256 i = 0; i < amounts.length; i++) { | ||
amounts[i] = 1; | ||
} | ||
wrapper.safeBatchTransferFrom( | ||
nameOwner, | ||
migrationTarget, | ||
tokenIds, | ||
amounts, | ||
data | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { DeployFunction } from 'hardhat-deploy/types.js' | ||
|
||
const func: DeployFunction = async function (hre) { | ||
const { deployments, viem } = hre | ||
const { deploy } = deployments | ||
|
||
const { deployer, owner } = await viem.getNamedClients() | ||
|
||
const registrar = await viem.getContract('BaseRegistrarImplementation') | ||
const wrapper = await viem.getContract('NameWrapper') | ||
|
||
await viem.deploy('MigrationHelper', [registrar.address, wrapper.address]) | ||
|
||
if (owner !== undefined && owner.address !== deployer.address) { | ||
const migrationHelper = await viem.getContract('MigrationHelper') | ||
const hash = await migrationHelper.write.transferOwnership([owner.address]) | ||
console.log(`Transfer ownership to ${owner.address} (tx: ${hash})...`) | ||
await viem.waitForTransactionSuccess(hash) | ||
} | ||
} | ||
|
||
func.id = 'migration-helper' | ||
func.tags = ['utils', 'MigrationHelper'] | ||
func.dependencies = ['BaseRegistrarImplementation', 'NameWrapper'] | ||
|
||
export default func |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,278 @@ | ||
import { loadFixture } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers.js' | ||
import { expect } from 'chai' | ||
import hre from 'hardhat' | ||
import { | ||
hexToBigInt, | ||
labelhash, | ||
namehash, | ||
stringToHex, | ||
zeroAddress, | ||
zeroHash, | ||
} from 'viem' | ||
|
||
const getAccounts = async () => { | ||
const [ownerClient, registrantClient, otherClient] = | ||
await hre.viem.getWalletClients() | ||
return { | ||
ownerAccount: ownerClient.account, | ||
ownerClient, | ||
registrantAccount: registrantClient.account, | ||
registrantClient, | ||
otherAccount: otherClient.account, | ||
otherClient, | ||
} | ||
} | ||
|
||
async function fixture() { | ||
const accounts = await getAccounts() | ||
const ensRegistry = await hre.viem.deployContract('ENSRegistry', []) | ||
const baseRegistrar = await hre.viem.deployContract( | ||
'BaseRegistrarImplementation', | ||
[ensRegistry.address, namehash('eth')], | ||
) | ||
const reverseRegistrar = await hre.viem.deployContract('ReverseRegistrar', [ | ||
ensRegistry.address, | ||
]) | ||
|
||
await ensRegistry.write.setSubnodeOwner([ | ||
zeroHash, | ||
labelhash('reverse'), | ||
accounts.ownerAccount.address, | ||
]) | ||
await ensRegistry.write.setSubnodeOwner([ | ||
namehash('reverse'), | ||
labelhash('addr'), | ||
reverseRegistrar.address, | ||
]) | ||
|
||
const nameWrapper = await hre.viem.deployContract('NameWrapper', [ | ||
ensRegistry.address, | ||
baseRegistrar.address, | ||
accounts.ownerAccount.address, | ||
]) | ||
|
||
await ensRegistry.write.setSubnodeOwner([ | ||
zeroHash, | ||
labelhash('eth'), | ||
baseRegistrar.address, | ||
]) | ||
|
||
await baseRegistrar.write.addController([nameWrapper.address]) | ||
await baseRegistrar.write.addController([accounts.ownerAccount.address]) | ||
await nameWrapper.write.setController([accounts.ownerAccount.address, true]) | ||
|
||
const migrationHelper = await hre.viem.deployContract('MigrationHelper', [ | ||
baseRegistrar.address, | ||
nameWrapper.address, | ||
]) | ||
await migrationHelper.write.setController([ | ||
accounts.ownerAccount.address, | ||
true, | ||
]) | ||
|
||
return { | ||
ensRegistry, | ||
baseRegistrar, | ||
reverseRegistrar, | ||
nameWrapper, | ||
migrationHelper, | ||
...accounts, | ||
} | ||
} | ||
|
||
describe('MigrationHelper', () => { | ||
it('should allow the owner to set a migration target', async () => { | ||
const { migrationHelper, ownerAccount } = await loadFixture(fixture) | ||
|
||
await expect(migrationHelper) | ||
.write('setMigrationTarget', [ownerAccount.address]) | ||
.toEmitEvent('MigrationTargetUpdated') | ||
.withArgs(ownerAccount.address) | ||
expect(await migrationHelper.read.migrationTarget()).toEqualAddress( | ||
ownerAccount.address, | ||
) | ||
}) | ||
|
||
it('should not allow non-owners to set migration targets', async () => { | ||
const { migrationHelper, ownerAccount, registrantAccount } = | ||
await loadFixture(fixture) | ||
await expect(migrationHelper) | ||
.write('setMigrationTarget', [ownerAccount.address], { | ||
account: registrantAccount, | ||
}) | ||
.toBeRevertedWithString('Ownable: caller is not the owner') | ||
}) | ||
|
||
it('should refuse to migrate unwrapped names to the zero address', async () => { | ||
const { baseRegistrar, migrationHelper, registrantAccount } = | ||
await loadFixture(fixture) | ||
const ids = [labelhash('test'), labelhash('test2')].map((v) => | ||
hexToBigInt(v), | ||
) | ||
for (let id of ids) { | ||
await baseRegistrar.write.register([ | ||
id, | ||
registrantAccount.address, | ||
86400n, | ||
]) | ||
} | ||
await baseRegistrar.write.setApprovalForAll( | ||
[migrationHelper.address, true], | ||
{ account: registrantAccount }, | ||
) | ||
await expect(migrationHelper) | ||
.write('migrateNames', [ | ||
registrantAccount.address, | ||
ids, | ||
stringToHex('test'), | ||
]) | ||
.toBeRevertedWithCustomError('MigrationTargetNotSet') | ||
}) | ||
|
||
it('should migrate unwrapped names', async () => { | ||
const { baseRegistrar, migrationHelper, ownerAccount, registrantAccount } = | ||
await loadFixture(fixture) | ||
const ids = [labelhash('test'), labelhash('test2')].map((v) => | ||
hexToBigInt(v), | ||
) | ||
for (let id of ids) { | ||
await baseRegistrar.write.register([ | ||
id, | ||
registrantAccount.address, | ||
86400n, | ||
]) | ||
} | ||
await baseRegistrar.write.setApprovalForAll( | ||
[migrationHelper.address, true], | ||
{ account: registrantAccount }, | ||
) | ||
await migrationHelper.write.setMigrationTarget([ownerAccount.address]) | ||
const tx = await migrationHelper.write.migrateNames([ | ||
registrantAccount.address, | ||
ids, | ||
stringToHex('test'), | ||
]) | ||
await expect(migrationHelper) | ||
.transaction(tx) | ||
.toEmitEventFrom(baseRegistrar, 'Transfer') | ||
.withArgs(registrantAccount.address, ownerAccount.address, ids[0]) | ||
await expect(migrationHelper) | ||
.transaction(tx) | ||
.toEmitEventFrom(baseRegistrar, 'Transfer') | ||
.withArgs(registrantAccount.address, ownerAccount.address, ids[1]) | ||
}) | ||
|
||
it('should only allow controllers to migrate unwrapped names', async () => { | ||
const { baseRegistrar, migrationHelper, ownerAccount, registrantAccount } = | ||
await loadFixture(fixture) | ||
const ids = [labelhash('test'), labelhash('test2')].map((v) => | ||
hexToBigInt(v), | ||
) | ||
for (let id of ids) { | ||
await baseRegistrar.write.register([ | ||
id, | ||
registrantAccount.address, | ||
86400n, | ||
]) | ||
} | ||
await migrationHelper.write.setMigrationTarget([ownerAccount.address]) | ||
await baseRegistrar.write.setApprovalForAll( | ||
[migrationHelper.address, true], | ||
{ account: registrantAccount }, | ||
) | ||
await expect(migrationHelper) | ||
.write( | ||
'migrateNames', | ||
[registrantAccount.address, ids, stringToHex('test')], | ||
{ account: registrantAccount }, | ||
) | ||
.toBeRevertedWithString('Controllable: Caller is not a controller') | ||
}) | ||
|
||
it('should migrate wrapped names', async () => { | ||
const { nameWrapper, migrationHelper, ownerAccount, registrantAccount } = | ||
await loadFixture(fixture) | ||
const labels = ['test', 'test2'] | ||
const ids = labels.map((label) => hexToBigInt(namehash(label + '.eth'))) | ||
for (let label of labels) { | ||
await nameWrapper.write.registerAndWrapETH2LD([ | ||
label, | ||
registrantAccount.address, | ||
86400n, | ||
zeroAddress, | ||
0, | ||
]) | ||
} | ||
await migrationHelper.write.setMigrationTarget([ownerAccount.address]) | ||
await nameWrapper.write.setApprovalForAll([migrationHelper.address, true], { | ||
account: registrantAccount, | ||
}) | ||
await expect(migrationHelper) | ||
.write('migrateWrappedNames', [ | ||
registrantAccount.address, | ||
ids, | ||
stringToHex('test'), | ||
]) | ||
.toEmitEventFrom(nameWrapper, 'TransferBatch') | ||
.withArgs( | ||
migrationHelper.address, | ||
registrantAccount.address, | ||
ownerAccount.address, | ||
ids, | ||
ids.map(() => 1n), | ||
) | ||
}) | ||
|
||
it('should refuse to migrate wrapped names to the zero address', async () => { | ||
const { nameWrapper, migrationHelper, registrantAccount } = | ||
await loadFixture(fixture) | ||
const labels = ['test', 'test2'] | ||
const ids = labels.map((label) => hexToBigInt(namehash(label + '.eth'))) | ||
for (let label of labels) { | ||
await nameWrapper.write.registerAndWrapETH2LD([ | ||
label, | ||
registrantAccount.address, | ||
86400n, | ||
zeroAddress, | ||
0, | ||
]) | ||
} | ||
await nameWrapper.write.setApprovalForAll([migrationHelper.address, true], { | ||
account: registrantAccount, | ||
}) | ||
await expect(migrationHelper) | ||
.write('migrateWrappedNames', [ | ||
registrantAccount.address, | ||
ids, | ||
stringToHex('test'), | ||
]) | ||
.toBeRevertedWithCustomError('MigrationTargetNotSet') | ||
}) | ||
|
||
it('should only allow controllers to migrate wrapped names', async () => { | ||
const { nameWrapper, migrationHelper, ownerAccount, registrantAccount } = | ||
await loadFixture(fixture) | ||
const labels = ['test', 'test2'] | ||
const ids = labels.map((label) => hexToBigInt(namehash(label + '.eth'))) | ||
for (let label of labels) { | ||
await nameWrapper.write.registerAndWrapETH2LD([ | ||
label, | ||
registrantAccount.address, | ||
86400n, | ||
zeroAddress, | ||
0, | ||
]) | ||
} | ||
await migrationHelper.write.setMigrationTarget([ownerAccount.address]) | ||
await nameWrapper.write.setApprovalForAll([migrationHelper.address, true], { | ||
account: registrantAccount, | ||
}) | ||
await expect(migrationHelper) | ||
.write( | ||
'migrateWrappedNames', | ||
[registrantAccount.address, ids, stringToHex('test')], | ||
{ account: registrantAccount }, | ||
) | ||
.toBeRevertedWithString('Controllable: Caller is not a controller') | ||
}) | ||
}) |