Skip to content

Commit

Permalink
chore(smart-contracts): cleanup tests from remains of Truffle (#13365)
Browse files Browse the repository at this point in the history
* refactor hook tests

* some more hooks

* udt tests

* add note to clean that up

* fix some more tests

* more fixes

* parse multiple signatures correctly

* remove truffle fixture from main deployment task

* all deployed locks are ethers

* all deployed ERC20 are ethers

* refactor behaviors tests

* fix erc721 tests

* remove BigNumber.js balance helper

* fix some more balance helpers

* deploy all locks using truffle by default

* some more changes in Unlock tests

* refactor gas tests

* keyManager tests

* fix all permissions scripts

* cleanup some mroe

* fix a bunch more

* fix more tests

* fix tests again

* behaviors fixes

* skip grant tokens test by default

* use qualified path

* skip grant tokens test entirely

* fix tokenURI test

* actually no need to skip

* remove truffle from hardhat config - replace all `contract(` by `describe(`

* remove outdated OZ test helpers

* fix remaining artifacts/accounts patterns

* literal import of `assert`

* bunch of fixes

* cleanup package.json

* one last test

* delete unsued file

* skip grant tokens on coverage (missing baseFee)
  • Loading branch information
clemsos committed Mar 5, 2024
1 parent 8d422f7 commit 61675d2
Show file tree
Hide file tree
Showing 140 changed files with 8,723 additions and 9,952 deletions.
4 changes: 0 additions & 4 deletions smart-contracts/.eslintrc.js
Expand Up @@ -20,13 +20,9 @@ module.exports = {
extends: ['@unlock-protocol/eslint-config'],
globals: {
it: true,
artifacts: true,
contract: true,
describe: true,
before: true,
beforeEach: true,
web3: true,
assert: true,
abi: true,
after: true,
afterEach: true,
Expand Down
14 changes: 7 additions & 7 deletions smart-contracts/contracts/interfaces/IPublicLock.sol
Expand Up @@ -464,6 +464,13 @@ interface IPublicLock {
*/
function safeTransferFrom(address from, address to, uint256 tokenId) external;

function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;

/**
* an ERC721-like function to transfer a token from one account to another.
* @param from the owner of token to transfer
Expand Down Expand Up @@ -527,13 +534,6 @@ interface IPublicLock {
address _operator
) external view returns (bool);

function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;

/**
* Returns the total number of keys, including non-valid ones
* @return _totalKeysCreated the total number of keys, valid or not
Expand Down
1 change: 0 additions & 1 deletion smart-contracts/hardhat.config.js
Expand Up @@ -6,7 +6,6 @@ const {
} = require('@unlock-protocol/hardhat-helpers')

require('@nomiclabs/hardhat-ethers')
require('@nomiclabs/hardhat-truffle5')

// full stack trace if needed
require('hardhat-tracer')
Expand Down
13 changes: 1 addition & 12 deletions smart-contracts/package.json
Expand Up @@ -11,13 +11,10 @@
"@connext/interfaces": "2.0.5",
"@nomiclabs/hardhat-ethers": "2.2.3",
"@nomiclabs/hardhat-etherscan": "3.1.8",
"@nomiclabs/hardhat-truffle5": "2.0.7",
"@nomiclabs/hardhat-waffle": "2.0.6",
"@nomiclabs/hardhat-web3": "2.0.0",
"@openzeppelin/contracts": "4.9.5",
"@openzeppelin/contracts-upgradeable": "4.9.5",
"@openzeppelin/hardhat-upgrades": "1.28.0",
"@openzeppelin/test-helpers": "0.5.16",
"@openzeppelin/upgrades-core": "1.27.3",
"@safe-global/safe-core-sdk": "3.3.5",
"@safe-global/safe-ethers-lib": "1.9.3",
Expand All @@ -33,11 +30,9 @@
"@unlock-protocol/networks": "workspace:./packages/networks",
"babel-polyfill": "6.26.0",
"babel-register": "6.26.0",
"bignumber.js": "9.1.2",
"debug": "4.3.4",
"chai": "4.3.10",
"eslint": "8.46.0",
"eth-gas-reporter": "0.2.27",
"eth-sig-util": "3.0.1",
"ethers": "5.7.2",
"fs-extra": "11.1.1",
"hardhat": "2.20.1",
Expand All @@ -46,18 +41,12 @@
"hardhat-gas-reporter": "1.0.10",
"hardhat-storage-layout": "0.1.7",
"hardhat-tracer": "2.8.0",
"hardlydifficult-eth": "1.1.4",
"hardlydifficult-ethereum-contracts": "0.11.1",
"prettier": "3.0.0",
"prettier-plugin-solidity": "1.1.3",
"prompt": "1.3.0",
"solhint": "4.0.0",
"solidity-coverage": "0.8.6",
"yesno": "0.4.0"
},
"devDependencies": {
"chai": "4.3.10"
},
"scripts": {
"test": "hardhat test",
"clean": "rm -rf artifacts",
Expand Down
2 changes: 1 addition & 1 deletion smart-contracts/test/CardPurchaser/purchase.mainnet.js
Expand Up @@ -106,7 +106,7 @@ const signLockPurchase = async ({
return { signature, message, domain, types }
}

describe(`purchase`, function () {
describe(`CardPurchaser / purchase (mainnet only)`, function () {
let chainId, unlock, cardPurchaser, signer, lock, unlockAddress
before(async function () {
if (!process.env.RUN_FORK) {
Expand Down
29 changes: 15 additions & 14 deletions smart-contracts/test/KeyManager/lockmsiths.js
Expand Up @@ -3,48 +3,49 @@ const { setup } = require('./setup')
const { ethers } = require('hardhat')
const { reverts } = require('../helpers')

let keyManager
let keyManager, someAccount

contract('KeyManager / locksmiths', (accounts) => {
describe('KeyManager / locksmiths', () => {
beforeEach(async () => {
;[, , , { address: someAccount }] = await ethers.getSigners()
;[keyManager] = await setup()
})

it('should let the owner add signer', async () => {
expect(await keyManager.locksmiths(accounts[1])).to.equal(false)
await keyManager.addLocksmith(accounts[1])
expect(await keyManager.locksmiths(accounts[1])).to.equal(true)
expect(await keyManager.locksmiths(someAccount)).to.equal(false)
await keyManager.addLocksmith(someAccount)
expect(await keyManager.locksmiths(someAccount)).to.equal(true)
})

it('should not let someone who is not owner add a signer', async () => {
const [, newOwner] = await ethers.getSigners()
expect(await keyManager.locksmiths(accounts[1])).to.equal(false)
expect(await keyManager.locksmiths(someAccount)).to.equal(false)

await reverts(
keyManager.connect(newOwner).addLocksmith(newOwner.address),
`Ownable: caller is not the owner`
)

expect(await keyManager.locksmiths(accounts[1])).to.equal(false)
expect(await keyManager.locksmiths(someAccount)).to.equal(false)
})

it('should let the owner remove signer', async () => {
await keyManager.addLocksmith(accounts[1])
expect(await keyManager.locksmiths(accounts[1])).to.equal(true)
await keyManager.removeLocksmith(accounts[1])
expect(await keyManager.locksmiths(accounts[1])).to.equal(false)
await keyManager.addLocksmith(someAccount)
expect(await keyManager.locksmiths(someAccount)).to.equal(true)
await keyManager.removeLocksmith(someAccount)
expect(await keyManager.locksmiths(someAccount)).to.equal(false)
})

it('should not let someone who is not owner remove a signer', async () => {
await keyManager.addLocksmith(accounts[1])
expect(await keyManager.locksmiths(accounts[1])).to.equal(true)
await keyManager.addLocksmith(someAccount)
expect(await keyManager.locksmiths(someAccount)).to.equal(true)

const [, newOwner] = await ethers.getSigners()
await reverts(
keyManager.connect(newOwner).removeLocksmith(newOwner.address),
`Ownable: caller is not the owner`
)

expect(await keyManager.locksmiths(accounts[1])).to.equal(true)
expect(await keyManager.locksmiths(someAccount)).to.equal(true)
})
})
15 changes: 8 additions & 7 deletions smart-contracts/test/KeyManager/owner.js
Expand Up @@ -3,32 +3,33 @@ const { ethers } = require('hardhat')
const { setup } = require('./setup')
const { reverts } = require('../helpers')

let keyManager
let keyManager, keyReceiver, firstAccount

contract('KeyManager / Ownable', (accounts) => {
describe('KeyManager / Ownable', () => {
beforeEach(async () => {
;[firstAccount, keyReceiver] = await ethers.getSigners()
;[keyManager] = await setup()
})

it('should be owned by the owner', async () => {
const owner = await keyManager.owner()
expect(owner).to.equal(accounts[0])
expect(owner).to.equal(firstAccount.address)
})

it('should be transferable by the owner to a new owner', async () => {
await keyManager.transferOwnership(accounts[1])
expect(await keyManager.owner()).to.equal(accounts[1])
await keyManager.transferOwnership(keyReceiver.address)
expect(await keyManager.owner()).to.equal(keyReceiver.address)
})

it('should not be transferable by someone who is not an owner', async () => {
const [, newOwner] = await ethers.getSigners()
expect(await keyManager.owner()).to.equal(accounts[0])
expect(await keyManager.owner()).to.equal(firstAccount.address)

await reverts(
keyManager.connect(newOwner).transferOwnership(newOwner.address),
`Ownable: caller is not the owner`
)

expect(await keyManager.owner()).to.equal(accounts[0])
expect(await keyManager.owner()).to.equal(firstAccount.address)
})
})
34 changes: 20 additions & 14 deletions smart-contracts/test/KeyManager/transfer.js
@@ -1,9 +1,10 @@
const { ethers } = require('hardhat')
const { setup } = require('./setup')
const { reverts } = require('../helpers')
const { assert } = require('chai')

let lock
let keyManager
let keyManager, locksmith, grantee, attacker, realUser
const OneMonthFromNow =
Math.floor(new Date().getTime() / 1000) + 60 * 60 * 24 * 30
const OneDayAgo = Math.floor(new Date().getTime() / 1000) - 60 * 60 * 24
Expand All @@ -18,12 +19,17 @@ const types = {
{ name: 'deadline', type: 'uint256' },
],
}
contract('KeyManager', ([, locksmith, grantee, attacker, realUser]) => {
describe('KeyManager', () => {
beforeEach(async () => {
;[keyManager, lock] = await setup()
;[, locksmith, grantee, attacker, realUser] = await ethers.getSigners()
// Let's now aidrop a key to an address and set the keyManager as... keyManager!
await keyManager.addLocksmith(locksmith)
await lock.grantKeys([grantee], [OneMonthFromNow], [keyManager.address])
await keyManager.addLocksmith(locksmith.address)
await lock.grantKeys(
[grantee.address],
[OneMonthFromNow],
[keyManager.address]
)
const { chainId } = await ethers.provider.getNetwork()
domain = {
name: 'KeyManager',
Expand All @@ -37,10 +43,10 @@ contract('KeyManager', ([, locksmith, grantee, attacker, realUser]) => {
const transfer = {
lock: lock.address,
token: 1,
owner: grantee,
owner: grantee.address,
deadline: OneDayAgo,
}
const locksmithSigner = await ethers.getSigner(locksmith)
const locksmithSigner = await ethers.getSigner(locksmith.address)
const signature = await locksmithSigner._signTypedData(
domain,
types,
Expand All @@ -62,10 +68,10 @@ contract('KeyManager', ([, locksmith, grantee, attacker, realUser]) => {
const transfer = {
lock: lock.address,
token: 1,
owner: grantee,
owner: grantee.address,
deadline: OneHourFromNow,
}
const attackerSigner = await ethers.getSigner(attacker)
const attackerSigner = await ethers.getSigner(attacker.address)
const signature = await attackerSigner._signTypedData(
domain,
types,
Expand All @@ -87,21 +93,21 @@ contract('KeyManager', ([, locksmith, grantee, attacker, realUser]) => {
const transfer = {
lock: lock.address,
token: 1,
owner: grantee,
owner: grantee.address,
deadline: OneHourFromNow,
}
assert.notEqual(await lock.ownerOf(1), realUser)
const locksmithSigner = await ethers.getSigner(locksmith)
assert.notEqual(await lock.ownerOf(1), realUser.address)
const locksmithSigner = await ethers.getSigner(locksmith.address)
const signature = await locksmithSigner._signTypedData(
domain,
types,
transfer
)
assert.equal(
ethers.utils.verifyTypedData(domain, types, transfer, signature),
locksmith
locksmith.address
)
const realUserSigner = await ethers.getSigner(realUser)
const realUserSigner = await ethers.getSigner(realUser.address)
await keyManager
.connect(realUserSigner)
.transfer(
Expand All @@ -111,7 +117,7 @@ contract('KeyManager', ([, locksmith, grantee, attacker, realUser]) => {
transfer.deadline,
signature
)
assert.equal(await lock.ownerOf(1), realUser)
assert.equal(await lock.ownerOf(1), realUser.address)
assert.equal(await lock.keyManagerOf(1), keyManager.address)
})
})
4 changes: 2 additions & 2 deletions smart-contracts/test/KeyManager/upgrades.js
Expand Up @@ -4,7 +4,7 @@ const { reverts } = require('../helpers')

let proxy

contract('KeyManager', (accounts) => {
describe('KeyManager', () => {
beforeEach(async () => {
// deploy proxy
const KeyManager = await ethers.getContractFactory('KeyManager')
Expand All @@ -27,7 +27,7 @@ contract('KeyManager', (accounts) => {
})

it('should not be upgradable by an attacker', async () => {
const attacker = await ethers.getSigner(accounts[3])
const [, , , attacker] = await ethers.getSigners()
const KeyManagerV2 = await ethers.getContractFactory(
'KeyManagerV2',
attacker
Expand Down
27 changes: 10 additions & 17 deletions smart-contracts/test/Lock/Lock.js
@@ -1,12 +1,12 @@
const BigNumber = require('bignumber.js')
const { assert } = require('chai')
const { ethers } = require('hardhat')
const { reverts, deployLock } = require('../helpers')
const { reverts, deployLock, compareBigNumbers } = require('../helpers')

const erc777abi = require('@unlock-protocol/hardhat-helpers/dist/ABIs/erc777.json')

let lock

contract('Lock / Lock', (accounts) => {
describe('Lock / Lock', () => {
before(async () => {
lock = await deployLock()
})
Expand All @@ -25,23 +25,16 @@ contract('Lock / Lock', (accounts) => {
lock.totalSupply(),
lock.numberOfOwners(),
])
expirationDuration = new BigNumber(expirationDuration)
maxNumberOfKeys = new BigNumber(maxNumberOfKeys)
totalSupply = new BigNumber(totalSupply)
numberOfOwners = new BigNumber(numberOfOwners)
assert.equal(expirationDuration.toFixed(), 60 * 60 * 24 * 30)
assert.strictEqual(
ethers.utils.formatUnits(ethers.BigNumber.from(keyPrice.toString())),
'0.01'
)
assert.equal(maxNumberOfKeys.toFixed(), 10)
assert.equal(totalSupply.toFixed(), 0)
assert.equal(numberOfOwners.toFixed(), 0)
compareBigNumbers(expirationDuration, 60 * 60 * 24 * 30)
assert.strictEqual(ethers.utils.formatUnits(keyPrice.toString()), '0.01')
compareBigNumbers(maxNumberOfKeys, 10)
compareBigNumbers(totalSupply, 0)
compareBigNumbers(numberOfOwners, 0)
})

it('Should fail on unknown calls', async () => {
const [, recipient] = accounts
const [, recipient] = await ethers.getSigners()
const mock777 = await ethers.getContractAt(erc777abi, lock.address)
await reverts(mock777.send(recipient, 1, '0x'))
await reverts(mock777.send(recipient.address, 1, '0x'))
})
})
1 change: 1 addition & 0 deletions smart-contracts/test/Lock/behaviors/directTips.js
@@ -1,3 +1,4 @@
const { assert } = require('chai')
const { ethers } = require('hardhat')
const { ADDRESS_ZERO } = require('../../helpers')
const { getBalance } = require('../../helpers')
Expand Down

0 comments on commit 61675d2

Please sign in to comment.