Skip to content

Commit

Permalink
test(fork): refactor hre import
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviera9 committed Feb 22, 2024
1 parent abae978 commit 1de4ad6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 81 deletions.
122 changes: 61 additions & 61 deletions test/fork/dao.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { time, mineUpTo } = require('@nomicfoundation/hardhat-network-helpers')
const { expect } = require('chai')
const hre = require('hardhat')
const { config, ethers, network, upgrades } = require('hardhat')

const {
SAFE_ADDRESS,
Expand Down Expand Up @@ -45,13 +45,13 @@ const USER_ADDRESS = '0xdDb5f4535123DAa5aE343c24006F4075aBAF5F7B'

const getBytes = (_hexString) => Buffer.from(_hexString.slice(2), 'hex')

const parseEther = (_input) => hre.ethers.parseEther(_input)
const parseEther = (_input) => ethers.parseEther(_input)

const createExecutorId = (id) => `0x${String(id).padStart(8, '0')}`

const encodeCallScript = (actions, specId = 1) =>
actions.reduce((script, { to, calldata }) => {
const encoder = new hre.ethers.AbiCoder()
const encoder = new ethers.AbiCoder()
const addr = encoder.encode(['address'], [to])
const length = encoder.encode(['uint256'], [(calldata.length - 2) / 2])
// Remove 12 first 0s of padding for addr and 28 0s for uint32
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('Integration tests on Gnosis deployment', () => {

const missingSteps = async () => {
await upgradeContracts()
const MockPToken = await hre.ethers.getContractFactory('MockPTokenERC20')
const MockPToken = await ethers.getContractFactory('MockPTokenERC20')
pntOnGnosis = await MockPToken.deploy(
'Host Token (pToken)',
'HTKN',
Expand Down Expand Up @@ -170,36 +170,36 @@ describe('Integration tests on Gnosis deployment', () => {
await lendingManager.connect(daoOwner).grantRole(UPGRADE_ROLE, faucet.address)
await registrationManager.connect(daoOwner).grantRole(UPGRADE_ROLE, faucet.address)
await rewardsManager.connect(daoOwner).grantRole(UPGRADE_ROLE, faucet.address)
await hre.upgrades.upgradeProxy(stakingManager, StakingManager)
await hre.upgrades.upgradeProxy(stakingManagerLm, StakingManagerPermissioned)
await hre.upgrades.upgradeProxy(stakingManagerRm, StakingManagerPermissioned)
await hre.upgrades.upgradeProxy(lendingManager, LendingManager)
await hre.upgrades.upgradeProxy(registrationManager, RegistrationManager)
await hre.upgrades.upgradeProxy(rewardsManager, RewardsManager)
await upgrades.upgradeProxy(stakingManager, StakingManager)
await upgrades.upgradeProxy(stakingManagerLm, StakingManagerPermissioned)
await upgrades.upgradeProxy(stakingManagerRm, StakingManagerPermissioned)
await upgrades.upgradeProxy(lendingManager, LendingManager)
await upgrades.upgradeProxy(registrationManager, RegistrationManager)
await upgrades.upgradeProxy(rewardsManager, RewardsManager)
}

beforeEach(async () => {
const rpc = hre.config.networks.hardhat.forking.url
const blockToForkFrom = hre.config.networks.hardhat.forking.blockNumber
await hardhatReset(hre.network.provider, rpc, blockToForkFrom)
;[faucet] = await hre.ethers.getSigners()
tokenHolders = await Promise.all(TOKEN_HOLDERS_ADDRESSES.map(hre.ethers.getImpersonatedSigner))
user = await hre.ethers.getImpersonatedSigner(USER_ADDRESS)
daoOwner = await hre.ethers.getImpersonatedSigner(SAFE_ADDRESS)
await sendEth(hre.ethers, faucet, daoOwner.address, '5')
pntMinter = await hre.ethers.getImpersonatedSigner(PNT_ON_GNOSIS_MINTER)

StakingManager = await hre.ethers.getContractFactory('StakingManager')
StakingManagerPermissioned = await hre.ethers.getContractFactory('StakingManagerPermissioned')
RegistrationManager = await hre.ethers.getContractFactory('RegistrationManager')
LendingManager = await hre.ethers.getContractFactory('LendingManager')
RewardsManager = await hre.ethers.getContractFactory('RewardsManager')

acl = await hre.ethers.getContractAt(AclAbi, ACL_ADDRESS)
daoVoting = await hre.ethers.getContractAt(DandelionVotingAbi, DANDELION_VOTING_ADDRESS)
daoTreasury = await hre.ethers.getContractAt(VaultAbi, FINANCE_VAULT)
finance = await hre.ethers.getContractAt(FinanceAbi, FINANCE)
daoPNT = await hre.ethers.getContractAt(DaoPntAbi, DAOPNT_ON_GNOSIS_ADDRESS)
const rpc = config.networks.hardhat.forking.url
const blockToForkFrom = config.networks.hardhat.forking.blockNumber
await hardhatReset(network.provider, rpc, blockToForkFrom)
;[faucet] = await ethers.getSigners()
tokenHolders = await Promise.all(TOKEN_HOLDERS_ADDRESSES.map(ethers.getImpersonatedSigner))
user = await ethers.getImpersonatedSigner(USER_ADDRESS)
daoOwner = await ethers.getImpersonatedSigner(SAFE_ADDRESS)
await sendEth(ethers, faucet, daoOwner.address, '5')
pntMinter = await ethers.getImpersonatedSigner(PNT_ON_GNOSIS_MINTER)

StakingManager = await ethers.getContractFactory('StakingManager')
StakingManagerPermissioned = await ethers.getContractFactory('StakingManagerPermissioned')
RegistrationManager = await ethers.getContractFactory('RegistrationManager')
LendingManager = await ethers.getContractFactory('LendingManager')
RewardsManager = await ethers.getContractFactory('RewardsManager')

acl = await ethers.getContractAt(AclAbi, ACL_ADDRESS)
daoVoting = await ethers.getContractAt(DandelionVotingAbi, DANDELION_VOTING_ADDRESS)
daoTreasury = await ethers.getContractAt(VaultAbi, FINANCE_VAULT)
finance = await ethers.getContractAt(FinanceAbi, FINANCE)
daoPNT = await ethers.getContractAt(DaoPntAbi, DAOPNT_ON_GNOSIS_ADDRESS)
stakingManager = StakingManager.attach(STAKING_MANAGER)
stakingManagerLm = StakingManagerPermissioned.attach(STAKING_MANAGER_LM)
stakingManagerRm = StakingManagerPermissioned.attach(STAKING_MANAGER_RM)
Expand All @@ -209,7 +209,7 @@ describe('Integration tests on Gnosis deployment', () => {

await missingSteps()

await Promise.all(tokenHolders.map((_holder) => sendEth(hre.ethers, faucet, _holder.address, '5')))
await Promise.all(tokenHolders.map((_holder) => sendEth(ethers, faucet, _holder.address, '5')))
await Promise.all(tokenHolders.map((_holder) => mintPntOnGnosis(_holder.address, 10000n)))
await Promise.all(tokenHolders.map((_holder) => stake(_holder, 5000)))
})
Expand Down Expand Up @@ -239,11 +239,11 @@ describe('Integration tests on Gnosis deployment', () => {
(_args) => encodeFunctionCall(..._args)
)
)
let currentBlock = await hre.ethers.provider.getBlockNumber()
let currentBlock = await ethers.provider.getBlockNumber()
expect(await daoPNT.totalSupplyAt(currentBlock)).to.be.eq(20000)
await mintPntOnGnosis(faucet.address, 10000n)
await stake(faucet, 10000)
currentBlock = await hre.ethers.provider.getBlockNumber()
currentBlock = await ethers.provider.getBlockNumber()
expect(await daoPNT.totalSupplyAt(currentBlock)).to.be.eq(30000)

await grantCreateVotesPermission(acl, daoOwner, tokenHolders[0].address)
Expand All @@ -255,8 +255,8 @@ describe('Integration tests on Gnosis deployment', () => {
})

it('should lend PNTs and register a borrowing sentinel', async () => {
const amount = hre.ethers.parseEther('200000', await pntOnGnosis.decimals())
await mintPntOnGnosis(faucet.address, hre.ethers.parseEther('400000', await pntOnGnosis.decimals()))
const amount = ethers.parseEther('200000', await pntOnGnosis.decimals())
await mintPntOnGnosis(faucet.address, ethers.parseEther('400000', await pntOnGnosis.decimals()))
await pntOnGnosis.connect(faucet).approve(LENDING_MANAGER, amount)
const balancePre = await pntOnGnosis.balanceOf(faucet.address)
await expect(lendingManager.lend(faucet.address, amount, 86400 * 90))
Expand All @@ -267,7 +267,7 @@ describe('Integration tests on Gnosis deployment', () => {
const balancePost = await pntOnGnosis.balanceOf(faucet.address)
expect(balancePre - amount).to.be.eq(balancePost)

const sentinel = hre.ethers.Wallet.createRandom()
const sentinel = ethers.Wallet.createRandom()
const signature = await sentinel.signMessage('test')
expect(
await registrationManager
Expand All @@ -279,9 +279,9 @@ describe('Integration tests on Gnosis deployment', () => {
})

it('should register a staking sentinel', async () => {
const amount = hre.ethers.parseEther('200000', await pntOnGnosis.decimals())
await mintPntOnGnosis(user.address, hre.ethers.parseEther('400000', await pntOnGnosis.decimals()))
const sentinel = hre.ethers.Wallet.createRandom()
const amount = ethers.parseEther('200000', await pntOnGnosis.decimals())
await mintPntOnGnosis(user.address, ethers.parseEther('400000', await pntOnGnosis.decimals()))
const sentinel = ethers.Wallet.createRandom()
const signature = await sentinel.signMessage('test')
await pntOnGnosis.connect(user).approve(REGISTRATION_MANAGER, amount)
expect(
Expand All @@ -294,7 +294,7 @@ describe('Integration tests on Gnosis deployment', () => {
})
;['MockPTokenERC777', 'MockPTokenERC20'].map((_pTokenContract) =>
it('should correctly stake after token has been changed', async () => {
const ERC20Factory = await hre.ethers.getContractFactory(_pTokenContract)
const ERC20Factory = await ethers.getContractFactory(_pTokenContract)
const newToken = await ERC20Factory.deploy('new PNT', 'nPNT', faucet.address, '0x00112233')

await pntOnGnosis.connect(faucet).approve(await stakingManager.getAddress(), 10000)
Expand All @@ -303,7 +303,7 @@ describe('Integration tests on Gnosis deployment', () => {
await expect(stakingManager.connect(daoOwner).changeToken(await newToken.getAddress()))
.to.emit(stakingManager, 'TokenChanged')
.withArgs(await pntOnGnosis.getAddress(), await newToken.getAddress())
await newToken.connect(faucet).mint(faucet.address, hre.ethers.parseEther('200000'), '0x', '0x')
await newToken.connect(faucet).mint(faucet.address, ethers.parseEther('200000'), '0x', '0x')
await newToken.connect(faucet).approve(await stakingManager.getAddress(), 10000)
await expect(stakingManager.connect(faucet).stake(faucet.address, 10000, 86400 * 7))
.to.be.revertedWithCustomError(stakingManager, 'InvalidToken')
Expand Down Expand Up @@ -403,9 +403,9 @@ describe('Integration tests on Gnosis deployment', () => {
[
[ETH_PTN_ADDRESS, ETH_PTN_ADDRESS, ERC20_VAULT],
[
new hre.ethers.Interface(EthPntAbi).encodeFunctionData('withdrawInflation', [RECEIVER, 10]),
new hre.ethers.Interface(EthPntAbi).encodeFunctionData('approve', [ERC20_VAULT, 10]),
new hre.ethers.Interface(ERC20VaultAbi).encodeFunctionData('pegIn(uint256,address,string,bytes,bytes4)', [
new ethers.Interface(EthPntAbi).encodeFunctionData('withdrawInflation', [RECEIVER, 10]),
new ethers.Interface(EthPntAbi).encodeFunctionData('approve', [ERC20_VAULT, 10]),
new ethers.Interface(ERC20VaultAbi).encodeFunctionData('pegIn(uint256,address,string,bytes,bytes4)', [
10,
ETHPNT_ADDRESS,
FINANCE_VAULT,
Expand All @@ -428,12 +428,12 @@ describe('Integration tests on Gnosis deployment', () => {
]
].map((_args) => encodeFunctionCall(..._args))
)
let currentBlock = await hre.ethers.provider.getBlockNumber()
let currentBlock = await ethers.provider.getBlockNumber()
expect(await daoPNT.totalSupplyAt(currentBlock)).to.be.eq(20000)
await mintPntOnGnosis(faucet.address, 10000n)
await mintPntOnGnosis(DANDELION_VOTING_ADDRESS, 10000n)
await stake(faucet, 10000)
currentBlock = await hre.ethers.provider.getBlockNumber()
currentBlock = await ethers.provider.getBlockNumber()
expect(await daoPNT.totalSupplyAt(currentBlock)).to.be.eq(30000)
await grantCreateVotesPermission(acl, daoOwner, tokenHolders[0].address)
const voteId = await openNewVoteAndReachQuorum(daoVoting, tokenHolders[0], tokenHolders, executionScript, metadata)
Expand Down Expand Up @@ -528,10 +528,10 @@ describe('Integration tests on Ethereum deployment', () => {
]

const missingSteps = async () => {
const CrossExecutor = await hre.ethers.getContractFactory('CrossExecutor')
const CrossExecutor = await ethers.getContractFactory('CrossExecutor')
crossExecutor = await CrossExecutor.deploy(PNT_ON_ETH_ADDRESS, ERC20_VAULT)
await crossExecutor.whitelistOriginAddress(DANDELION_VOTING_ADDRESS)
daoVotingV1 = await hre.ethers.getContractAt(DandelionVotingAbi, DANDELION_VOTING_V1_ADDRESS)
daoVotingV1 = await ethers.getContractAt(DandelionVotingAbi, DANDELION_VOTING_V1_ADDRESS)
// open vote to change inflationOwner
const executionScript = encodeCallScript(
[
Expand Down Expand Up @@ -561,17 +561,17 @@ describe('Integration tests on Ethereum deployment', () => {
}

beforeEach(async () => {
const rpc = hre.config.networks.mainnet.url
await hardhatReset(hre.network.provider, rpc)
;[faucet] = await hre.ethers.getSigners()
tokenHolders = await Promise.all(TOKEN_HOLDERS_ADDRESSES.map(hre.ethers.getImpersonatedSigner))
pnetwork = await hre.ethers.getImpersonatedSigner(PNETWORK_ADDRESS)
association = await hre.ethers.getImpersonatedSigner('0xf1f6568a76559d85cF68E6597fA587544184dD46')
ethPnt = await hre.ethers.getContractAt(EthPntAbi, ETHPNT_ADDRESS)
vault = await hre.ethers.getContractAt('IErc20Vault', ERC20_VAULT)
await sendEth(hre.ethers, faucet, pnetwork.address, '10')
await sendEth(hre.ethers, faucet, association.address, '10')
await Promise.all(TOKEN_HOLDERS_ADDRESSES.map((_address) => sendEth(hre.ethers, faucet, _address, '10')))
const rpc = config.networks.mainnet.url
await hardhatReset(network.provider, rpc)
;[faucet] = await ethers.getSigners()
tokenHolders = await Promise.all(TOKEN_HOLDERS_ADDRESSES.map(ethers.getImpersonatedSigner))
pnetwork = await ethers.getImpersonatedSigner(PNETWORK_ADDRESS)
association = await ethers.getImpersonatedSigner('0xf1f6568a76559d85cF68E6597fA587544184dD46')
ethPnt = await ethers.getContractAt(EthPntAbi, ETHPNT_ADDRESS)
vault = await ethers.getContractAt('IErc20Vault', ERC20_VAULT)
await sendEth(ethers, faucet, pnetwork.address, '10')
await sendEth(ethers, faucet, association.address, '10')
await Promise.all(TOKEN_HOLDERS_ADDRESSES.map((_address) => sendEth(ethers, faucet, _address, '10')))
await missingSteps()
})

Expand Down Expand Up @@ -609,7 +609,7 @@ describe('Integration tests on Ethereum deployment', () => {
})

it('should not process pegout if not required by dandelion voting', async () => {
const attacker = hre.ethers.Wallet.createRandom().connect(hre.ethers.provider)
const attacker = ethers.Wallet.createRandom().connect(ethers.provider)
const metadata = encode(
['bytes1', 'bytes', 'bytes4', 'address', 'bytes4', 'address', 'bytes', 'bytes'],
[
Expand Down
40 changes: 20 additions & 20 deletions test/fork/forwarders.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai')
const hre = require('hardhat')
const { config, ethers, network } = require('hardhat')

const {
PNT_ON_GNOSIS_ADDRESS,
Expand All @@ -18,17 +18,17 @@ const { sendEth } = require('../utils/send-eth')

describe('Polygon Forwarder', () => {
beforeEach(async () => {
const rpc = hre.config.networks.polygon.url
await hardhatReset(hre.network.provider, rpc)
const rpc = config.networks.polygon.url
await hardhatReset(network.provider, rpc)
})

it('should call forwarder for staking', async () => {
const [owner] = await hre.ethers.getSigners()
const daoRoot = await hre.ethers.getImpersonatedSigner('0xdDb5f4535123DAa5aE343c24006F4075aBAF5F7B')
await sendEth(hre.ethers, owner, daoRoot, hre.ethers.parseEther('7'))
await hre.ethers.provider.getBalance('0xdDb5f4535123DAa5aE343c24006F4075aBAF5F7B')
const forwarder = await hre.ethers.getContractAt('IForwarder', FORWARDER_ON_POLYGON)
const pToken = await hre.ethers.getContractAt(pntOnPolygonAbi, PNT_ON_POLYGON_ADDRESS)
const [owner] = await ethers.getSigners()
const daoRoot = await ethers.getImpersonatedSigner('0xdDb5f4535123DAa5aE343c24006F4075aBAF5F7B')
await sendEth(ethers, owner, daoRoot, ethers.parseEther('7'))
await ethers.provider.getBalance('0xdDb5f4535123DAa5aE343c24006F4075aBAF5F7B')
const forwarder = await ethers.getContractAt('IForwarder', FORWARDER_ON_POLYGON)
const pToken = await ethers.getContractAt(pntOnPolygonAbi, PNT_ON_POLYGON_ADDRESS)
await expect(
forwarder.connect(daoRoot).call(
'100000000000000000',
Expand All @@ -53,24 +53,24 @@ describe('Polygon Forwarder', () => {

describe('Gnosis Forwarder', () => {
const missingSteps = async () => {
const owner = await hre.ethers.getImpersonatedSigner('0xfe0BC5fAc8f624093C9CeaeCF1EF14B4a5F84cE9')
const forwarder = await hre.ethers.getContractAt('ForwarderHost', FORWARDER_ON_GNOSIS)
const owner = await ethers.getImpersonatedSigner('0xfe0BC5fAc8f624093C9CeaeCF1EF14B4a5F84cE9')
const forwarder = await ethers.getContractAt('ForwarderHost', FORWARDER_ON_GNOSIS)
await forwarder.connect(owner).whitelistOriginAddress(FORWARDER_ON_POLYGON)
}

beforeEach(async () => {
const rpc = hre.config.networks.gnosis.url
await hardhatReset(hre.network.provider, rpc)
const rpc = config.networks.gnosis.url
await hardhatReset(network.provider, rpc)
await missingSteps()
})

it('should stake from forwarder call', async () => {
const USER_ADDRESS = '0xdDb5f4535123DAa5aE343c24006F4075aBAF5F7B'
const pnetwork = await hre.ethers.getImpersonatedSigner('0x53d51f8801f40657ca566a1ae25b27eada97413c')
const stakingManager = await hre.ethers.getContractAt('StakingManager', STAKING_MANAGER)
await hre.ethers.provider.getBalance(USER_ADDRESS)
const pToken = await hre.ethers.getContractAt(pntOnGnosisAbi, PNT_ON_GNOSIS_ADDRESS)
const daoPNT = await hre.ethers.getContractAt('ERC20', DAOPNT_ON_GNOSIS_ADDRESS)
const pnetwork = await ethers.getImpersonatedSigner('0x53d51f8801f40657ca566a1ae25b27eada97413c')
const stakingManager = await ethers.getContractAt('StakingManager', STAKING_MANAGER)
await ethers.provider.getBalance(USER_ADDRESS)
const pToken = await ethers.getContractAt(pntOnGnosisAbi, PNT_ON_GNOSIS_ADDRESS)
const daoPNT = await ethers.getContractAt('ERC20', DAOPNT_ON_GNOSIS_ADDRESS)
const metadata = encode(
['bytes1', 'bytes', 'bytes4', 'address', 'bytes4', 'address', 'bytes', 'bytes'],
[
Expand All @@ -89,7 +89,7 @@ describe('Gnosis Forwarder', () => {
stakingManager,
'Staked'
)
expect(await daoPNT.balanceOf(USER_ADDRESS)).to.be.eq(hre.ethers.parseUnits('0.0999'))
expect(await pToken.balanceOf(STAKING_MANAGER)).to.be.eq(hre.ethers.parseUnits('0.0999'))
expect(await daoPNT.balanceOf(USER_ADDRESS)).to.be.eq(ethers.parseUnits('0.0999'))
expect(await pToken.balanceOf(STAKING_MANAGER)).to.be.eq(ethers.parseUnits('0.0999'))
})
})

0 comments on commit 1de4ad6

Please sign in to comment.