Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed May 27, 2024
1 parent 657428a commit 8c58736
Show file tree
Hide file tree
Showing 19 changed files with 1,983 additions and 46 deletions.
Binary file modified bun.lockb
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"compile": "NODE_OPTIONS=\"--experimental-loader ts-node/esm/transpile-only\" hardhat compile",
"test": "NODE_OPTIONS=\"--experimental-loader ts-node/esm/transpile-only\" TS_NODE_PREFER_TS_EXTS=true hardhat test ./test/root/TestRoot.ts",
"test": "NODE_OPTIONS=\"--experimental-loader ts-node/esm/transpile-only\" TS_NODE_PREFER_TS_EXTS=true hardhat test ./test/wrapper/TestTestUnwrap.ts",
"test:parallel": "NODE_OPTIONS=\"--experimental-loader ts-node/esm/transpile-only\" TS_NODE_PREFER_TS_EXTS=true hardhat test ./test/**/Test*.ts --parallel",
"test:local": "hardhat --network localhost test",
"test:deploy": "hardhat --network hardhat deploy",
Expand All @@ -28,7 +28,7 @@
"main": "index.js",
"devDependencies": {
"@ensdomains/dnsprovejs": "^0.3.7",
"@ensdomains/hardhat-chai-matchers-viem": "^0.0.4",
"@ensdomains/hardhat-chai-matchers-viem": "^0.0.5",
"@ensdomains/test-utils": "^1.3.0",
"@nomicfoundation/hardhat-toolbox-viem": "^3.0.0",
"@openzeppelin/test-helpers": "^0.5.11",
Expand Down
57 changes: 28 additions & 29 deletions test/ethregistrar/TestBaseRegistrar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { loadFixture } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers.js'
import { expect } from 'chai'
import hre from 'hardhat'
import { hexToBigInt, labelhash, namehash, zeroAddress, zeroHash } from 'viem'
import { labelhash, namehash, zeroAddress, zeroHash } from 'viem'
import { toLabelId } from '../fixtures/utils.js'

const getAccounts = async () => {
const [ownerClient, controllerClient, registrantClient, otherClient] =
Expand Down Expand Up @@ -40,16 +41,14 @@ async function fixture() {
async function fixtureWithRegistration() {
const existing = await loadFixture(fixture)
await existing.baseRegistrar.write.register(
[labelId('newname'), existing.registrantAccount.address, 86400n],
[toLabelId('newname'), existing.registrantAccount.address, 86400n],
{
account: existing.controllerAccount,
},
)
return existing
}

const labelId = (label: string) => hexToBigInt(labelhash(label))

describe('BaseRegistrar', () => {
it('should allow new registrations', async () => {
const {
Expand All @@ -61,7 +60,7 @@ describe('BaseRegistrar', () => {
} = await loadFixture(fixture)

const hash = await baseRegistrar.write.register(
[labelId('newname'), registrantAccount.address, 86400n],
[toLabelId('newname'), registrantAccount.address, 86400n],
{
account: controllerAccount,
},
Expand All @@ -73,10 +72,10 @@ describe('BaseRegistrar', () => {
ensRegistry.read.owner([namehash('newname.eth')]),
).resolves.toEqualAddress(registrantAccount.address)
await expect(
baseRegistrar.read.ownerOf([labelId('newname')]),
baseRegistrar.read.ownerOf([toLabelId('newname')]),
).resolves.toEqualAddress(registrantAccount.address)
await expect(
baseRegistrar.read.nameExpires([labelId('newname')]),
baseRegistrar.read.nameExpires([toLabelId('newname')]),
).resolves.toEqual(block.timestamp + 86400n)
})

Expand All @@ -90,7 +89,7 @@ describe('BaseRegistrar', () => {
} = await loadFixture(fixture)

const hash = await baseRegistrar.write.registerOnly(
[labelId('silentname'), registrantAccount.address, 86400n],
[toLabelId('silentname'), registrantAccount.address, 86400n],
{
account: controllerAccount,
},
Expand All @@ -102,10 +101,10 @@ describe('BaseRegistrar', () => {
ensRegistry.read.owner([namehash('silentname.eth')]),
).resolves.toEqualAddress(zeroAddress)
await expect(
baseRegistrar.read.ownerOf([labelId('silentname')]),
baseRegistrar.read.ownerOf([toLabelId('silentname')]),
).resolves.toEqualAddress(registrantAccount.address)
await expect(
baseRegistrar.read.nameExpires([labelId('silentname')]),
baseRegistrar.read.nameExpires([toLabelId('silentname')]),
).resolves.toEqual(block.timestamp + 86400n)
})

Expand All @@ -115,23 +114,23 @@ describe('BaseRegistrar', () => {
)

const oldExpires = await baseRegistrar.read.nameExpires([
labelId('newname'),
toLabelId('newname'),
])

await baseRegistrar.write.renew([labelId('newname'), 86400n], {
await baseRegistrar.write.renew([toLabelId('newname'), 86400n], {
account: controllerAccount,
})

await expect(
baseRegistrar.read.nameExpires([labelId('newname')]),
baseRegistrar.read.nameExpires([toLabelId('newname')]),
).resolves.toEqual(oldExpires + 86400n)
})

it('should only allow the controller to register', async () => {
const { baseRegistrar, otherAccount } = await loadFixture(fixture)

await expect(baseRegistrar)
.write('register', [labelId('foo'), otherAccount.address, 86400n], {
.write('register', [toLabelId('foo'), otherAccount.address, 86400n], {
account: otherAccount,
})
.toBeRevertedWithoutReason()
Expand All @@ -141,7 +140,7 @@ describe('BaseRegistrar', () => {
const { baseRegistrar, otherAccount } = await loadFixture(fixture)

await expect(baseRegistrar)
.write('renew', [labelId('foo'), 86400n], {
.write('renew', [toLabelId('foo'), 86400n], {
account: otherAccount,
})
.toBeRevertedWithoutReason()
Expand All @@ -154,7 +153,7 @@ describe('BaseRegistrar', () => {
await expect(baseRegistrar)
.write(
'register',
[labelId('newname'), registrantAccount.address, 86400n],
[toLabelId('newname'), registrantAccount.address, 86400n],
{
account: controllerAccount,
},
Expand All @@ -166,7 +165,7 @@ describe('BaseRegistrar', () => {
const { baseRegistrar, controllerAccount } = await loadFixture(fixture)

await expect(baseRegistrar)
.write('renew', [labelId('newname'), 86400n], {
.write('renew', [toLabelId('newname'), 86400n], {
account: controllerAccount,
})
.toBeRevertedWithoutReason()
Expand All @@ -181,7 +180,7 @@ describe('BaseRegistrar', () => {
account: registrantAccount,
})
await baseRegistrar.write.reclaim(
[labelId('newname'), registrantAccount.address],
[toLabelId('newname'), registrantAccount.address],
{
account: registrantAccount,
},
Expand All @@ -201,7 +200,7 @@ describe('BaseRegistrar', () => {
})

await expect(baseRegistrar)
.write('reclaim', [labelId('newname'), registrantAccount.address], {
.write('reclaim', [toLabelId('newname'), registrantAccount.address], {
account: otherAccount,
})
.toBeRevertedWithoutReason()
Expand All @@ -212,21 +211,21 @@ describe('BaseRegistrar', () => {
await loadFixture(fixtureWithRegistration)

await baseRegistrar.write.transferFrom(
[registrantAccount.address, otherAccount.address, labelId('newname')],
[registrantAccount.address, otherAccount.address, toLabelId('newname')],
{
account: registrantAccount,
},
)

await expect(
baseRegistrar.read.ownerOf([labelId('newname')]),
baseRegistrar.read.ownerOf([toLabelId('newname')]),
).resolves.toEqualAddress(otherAccount.address)
await expect(
ensRegistry.read.owner([namehash('newname.eth')]),
).resolves.toEqualAddress(registrantAccount.address)

await baseRegistrar.write.transferFrom(
[otherAccount.address, registrantAccount.address, labelId('newname')],
[otherAccount.address, registrantAccount.address, toLabelId('newname')],
{
account: otherAccount,
},
Expand All @@ -241,7 +240,7 @@ describe('BaseRegistrar', () => {
await expect(baseRegistrar)
.write(
'transferFrom',
[otherAccount.address, otherAccount.address, labelId('newname')],
[otherAccount.address, otherAccount.address, toLabelId('newname')],
{
account: otherAccount,
},
Expand All @@ -260,15 +259,15 @@ describe('BaseRegistrar', () => {
await expect(baseRegistrar)
.write(
'transferFrom',
[registrantAccount.address, otherAccount.address, labelId('newname')],
[registrantAccount.address, otherAccount.address, toLabelId('newname')],
{
account: registrantAccount,
},
)
.toBeRevertedWithoutReason()

await expect(baseRegistrar)
.write('reclaim', [labelId('newname'), registrantAccount.address], {
.write('reclaim', [toLabelId('newname'), registrantAccount.address], {
account: registrantAccount,
})
.toBeRevertedWithoutReason()
Expand All @@ -283,7 +282,7 @@ describe('BaseRegistrar', () => {
await testClient.increaseTime({ seconds: 86400 + 3600 })
await testClient.mine({ blocks: 1 })

await baseRegistrar.write.renew([labelId('newname'), 86400n], {
await baseRegistrar.write.renew([toLabelId('newname'), 86400n], {
account: controllerAccount,
})
})
Expand All @@ -301,18 +300,18 @@ describe('BaseRegistrar', () => {
await testClient.mine({ blocks: 1 })

await expect(baseRegistrar)
.read('ownerOf', [labelId('newname')])
.read('ownerOf', [toLabelId('newname')])
.toBeRevertedWithoutReason()

await baseRegistrar.write.register(
[labelId('newname'), otherAccount.address, 86400n],
[toLabelId('newname'), otherAccount.address, 86400n],
{
account: controllerAccount,
},
)

await expect(
baseRegistrar.read.ownerOf([labelId('newname')]),
baseRegistrar.read.ownerOf([toLabelId('newname')]),
).resolves.toEqualAddress(otherAccount.address)
})

Expand Down
16 changes: 1 addition & 15 deletions test/ethregistrar/TestEthRegistrarController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
zeroAddress,
zeroHash,
} from 'viem'
import { DAY, FUSES } from '../fixtures/constants.js'
import { getReverseNode } from '../fixtures/getReverseNode.js'
import {
commitName,
Expand All @@ -18,25 +19,10 @@ import {
registerName,
} from '../fixtures/registerName.js'

const DAY = 24n * 60n * 60n
const REGISTRATION_TIME = 28n * DAY
const BUFFERED_REGISTRATION_COST = REGISTRATION_TIME + 3n * DAY
const GRACE_PERIOD = 90n * DAY

const FUSES = {
CAN_DO_EVERYTHING: 0,
CANNOT_UNWRAP: 1,
CANNOT_BURN_FUSES: 2,
CANNOT_TRANSFER: 4,
CANNOT_SET_RESOLVER: 8,
CANNOT_SET_TTL: 16,
CANNOT_CREATE_SUBDOMAIN: 32,
CANNOT_APPROVE: 64,
PARENT_CANNOT_CONTROL: 2 ** 16,
IS_DOT_ETH: 2 ** 17,
CAN_EXTEND_EXPIRY: 2 ** 18,
} as const

const getAccounts = async () => {
const [ownerClient, registrantClient, otherClient] =
await hre.viem.getWalletClients()
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const FUSES = {
CAN_DO_EVERYTHING: 0,
CANNOT_UNWRAP: 1,
CANNOT_BURN_FUSES: 2,
CANNOT_TRANSFER: 4,
CANNOT_SET_RESOLVER: 8,
CANNOT_SET_TTL: 16,
CANNOT_CREATE_SUBDOMAIN: 32,
CANNOT_APPROVE: 64,
PARENT_CANNOT_CONTROL: 2 ** 16,
IS_DOT_ETH: 2 ** 17,
CAN_EXTEND_EXPIRY: 2 ** 18,
} as const

export const DAY = 24n * 60n * 60n
5 changes: 5 additions & 0 deletions test/fixtures/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { hexToBigInt, labelhash, namehash, type Hex } from 'viem'

export const toTokenId = (hash: Hex) => hexToBigInt(hash)
export const toLabelId = (label: string) => toTokenId(labelhash(label))
export const toNameId = (name: string) => toTokenId(namehash(name))
56 changes: 56 additions & 0 deletions test/utils/TestERC20Recoverable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { loadFixture } from '@nomicfoundation/hardhat-toolbox-viem/network-helpers.js'
import { expect } from 'chai'
import hre from 'hardhat'

async function fixture() {
const accounts = await hre.viem
.getWalletClients()
.then((clients) => clients.map((c) => c.account))
const erc20Recoverable = await hre.viem.deployContract('ERC20Recoverable', [])
const erc20Token = await hre.viem.deployContract('MockERC20', [
'Ethereum Name Service Token',
'ENS',
[],
])

return { erc20Recoverable, erc20Token, accounts }
}

describe('ERC20Recoverable', () => {
it('should recover ERC20 token', async () => {
const { erc20Recoverable, erc20Token, accounts } = await loadFixture(
fixture,
)

await erc20Token.write.transfer([erc20Recoverable.address, 1000n])
await expect(
erc20Token.read.balanceOf([erc20Recoverable.address]),
).resolves.toEqual(1000n)

await erc20Recoverable.write.recoverFunds([
erc20Token.address,
accounts[0].address,
1000n,
])
await expect(
erc20Token.read.balanceOf([erc20Recoverable.address]),
).resolves.toEqual(0n)
})

it('should not allow non-owner to call', async () => {
const { erc20Recoverable, erc20Token, accounts } = await loadFixture(
fixture,
)

await erc20Token.write.transfer([erc20Recoverable.address, 1000n])
await expect(
erc20Token.read.balanceOf([erc20Recoverable.address]),
).resolves.toEqual(1000n)

await expect(erc20Recoverable)
.write('recoverFunds', [erc20Token.address, accounts[1].address, 1000n], {
account: accounts[1],
})
.toBeRevertedWithString('Ownable: caller is not the owner')
})
})
Loading

0 comments on commit 8c58736

Please sign in to comment.