-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proxy test: adaptation to Hardhat and support of custom errors
- Loading branch information
Showing
6 changed files
with
113 additions
and
58 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,23 @@ | ||
//SPDX-License-Identifier: MPL-2.0 | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
import "../../CMTAT_PROXY.sol"; | ||
|
||
/** | ||
* @title a contrat used to test the proxy upgrade functionality | ||
*/ | ||
contract CMTAT_PROXY_TEST is CMTAT_PROXY { | ||
/** | ||
* @notice Contract version for the deployment with a proxy | ||
* @param forwarderIrrevocable address of the forwarder, required for the gasless support | ||
*/ | ||
/// @custom:oz-upgrades-unsafe-allow constructor | ||
constructor( | ||
address forwarderIrrevocable | ||
) CMTAT_PROXY(forwarderIrrevocable) { | ||
// Nothing to do | ||
} | ||
|
||
uint256[50] private __gap; | ||
} |
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
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
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 |
---|---|---|
@@ -1,53 +1,83 @@ | ||
const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers') | ||
const { expectEvent, expectRevert, BN } = require('@openzeppelin/test-helpers') | ||
const { should } = require('chai').should() | ||
|
||
const { deployProxy, upgradeProxy, erc1967 } = require('@openzeppelin/truffle-upgrades') | ||
const CMTAT1 = artifacts.require('CMTAT_PROXY') | ||
const CMTAT2 = artifacts.require('CMTAT_PROXY') | ||
const CMTAT2 = artifacts.require('CMTAT_PROXY_TEST') | ||
const { ZERO_ADDRESS } = require('../../utils') | ||
const DECIMAL = 0 | ||
|
||
contract('UpgradeableCMTAT - Proxy', function ([_, admin, address1]) { | ||
const { deployCMTATProxy, DEPLOYMENT_FLAG, DEPLOYMENT_DECIMAL } = require('../../deploymentUtils') | ||
const { ethers, upgrades } = require("hardhat"); | ||
contract('UpgradeableCMTAT - Proxy', function ([_, admin, address1, deployerAddress]) { | ||
/* | ||
Functions used: balanceOf, totalSupply, mint | ||
*/ | ||
it('testKeepStorageForTokens', async function () { | ||
this.flag = 5 | ||
// With the first version of CMTAT | ||
this.CMTAT_PROXY = await deployProxy(CMTAT1, [admin, 'CMTA Token', 'CMTAT', DECIMAL, 'CMTAT_ISIN', 'https://cmta.ch', ZERO_ADDRESS, 'CMTAT_info', this.flag], { | ||
///// ADAPT TRUFFLE TEST TO HARDHAT | ||
/* Factory & Artefact*/ | ||
const ETHERS_CMTAT_PROXY_FACTORY = await ethers.getContractFactory('CMTAT_PROXY'); | ||
const ETHERS_CMTAT_PROXY_FACTORY_2 = await ethers.getContractFactory('CMTAT_PROXY_TEST'); | ||
const TRUFFLE_CMTAT_PROXY = artifacts.require('CMTAT_PROXY') | ||
const TRUFFLE_CMTAT_PROXY_V2 = artifacts.require('CMTAT_PROXY_TEST') | ||
// Deployment | ||
const ETHERS_CMTAT_PROXY = await upgrades.deployProxy(ETHERS_CMTAT_PROXY_FACTORY, [admin, 'CMTA Token', 'CMTAT', DEPLOYMENT_DECIMAL, 'CMTAT_ISIN', 'https://cmta.ch', ZERO_ADDRESS, 'CMTAT_info', DEPLOYMENT_FLAG], { | ||
initializer: 'initialize', | ||
constructorArgs: [_] | ||
}) | ||
const implementationContractAddress1 = erc1967.getImplementationAddress(this.CMTAT_PROXY.address, { | ||
constructorArgs: [_], | ||
from: deployerAddress | ||
}); | ||
|
||
// Get Proxy Address | ||
const TRUFFLE_CMTAT_PROXY_ADDRESS = await TRUFFLE_CMTAT_PROXY.at(await ETHERS_CMTAT_PROXY.getAddress()); | ||
const ETHER_PROXY_ADDRESS = await ETHERS_CMTAT_PROXY.getAddress() | ||
const IMPLEMENTATION_CONTRACT_ADDRESS_V1 = await upgrades.erc1967.getImplementationAddress(ETHER_PROXY_ADDRESS, { | ||
from: admin | ||
}); | ||
(await this.CMTAT_PROXY.balanceOf(admin)).should.be.bignumber.equal('0'); | ||
|
||
// With the first version of CMTAT | ||
(await TRUFFLE_CMTAT_PROXY_ADDRESS.balanceOf(admin)).should.be.bignumber.equal('0'); | ||
|
||
// Issue 20 and check balances and total supply | ||
({ logs: this.logs1 } = await this.CMTAT_PROXY.mint(address1, 20, { | ||
({ logs: this.logs1 } = await TRUFFLE_CMTAT_PROXY_ADDRESS.mint(address1, 20, { | ||
from: admin | ||
})); | ||
(await this.CMTAT_PROXY.balanceOf(address1)).should.be.bignumber.equal('20'); | ||
(await this.CMTAT_PROXY.totalSupply()).should.be.bignumber.equal('20') | ||
|
||
(await TRUFFLE_CMTAT_PROXY_ADDRESS.balanceOf(address1)).should.be.bignumber.equal('20'); | ||
(await TRUFFLE_CMTAT_PROXY_ADDRESS.totalSupply()).should.be.bignumber.equal('20') | ||
|
||
// Upgrade the proxy with a new implementation contract | ||
this.upgradeableCMTATV2Instance = await upgradeProxy(this.CMTAT_PROXY.address, CMTAT2, { | ||
const ETHERS_CMTAT_PROXY_V2 = await upgrades.upgradeProxy(ETHER_PROXY_ADDRESS, ETHERS_CMTAT_PROXY_FACTORY_2, { | ||
constructorArgs: [_] | ||
}) | ||
const PROXY_ADDRESS_V2_INSTANCE = await ETHERS_CMTAT_PROXY_V2.getAddress() | ||
|
||
// Get the new implementation contract address | ||
const implementationContractAddress2 = erc1967.getImplementationAddress(this.CMTAT_PROXY.address, { | ||
const IMPLEMENTATION_CONTRACT_ADDRESS_V2 = await upgrades.erc1967.getImplementationAddress(PROXY_ADDRESS_V2_INSTANCE, { | ||
from: admin | ||
}) | ||
|
||
// Just to be sure that the proxy address remains unchanged | ||
ETHER_PROXY_ADDRESS.should.be.equal(PROXY_ADDRESS_V2_INSTANCE); | ||
|
||
// The address of the implementation contract has changed | ||
implementationContractAddress1.should.not.be.equal(implementationContractAddress2); | ||
IMPLEMENTATION_CONTRACT_ADDRESS_V1.should.not.be.equal(IMPLEMENTATION_CONTRACT_ADDRESS_V2); | ||
|
||
// Just to be sure that the proxy address remains unchanged | ||
const TRUFFLE_CMTAT_PROXY_ADDRESS_2 = await TRUFFLE_CMTAT_PROXY_V2.at(PROXY_ADDRESS_V2_INSTANCE); | ||
//TRUFFLE_CMTAT_PROXY_ADDRESS.should.be.equal(TRUFFLE_CMTAT_PROXY_ADDRESS_2); | ||
|
||
({ logs: this.logs1 } = await TRUFFLE_CMTAT_PROXY_ADDRESS_2.balanceOf(address1)); | ||
|
||
(await TRUFFLE_CMTAT_PROXY_ADDRESS_2.balanceOf(address1)).should.be.bignumber.equal(BN(20)); | ||
|
||
// keep the storage | ||
(await this.upgradeableCMTATV2Instance.balanceOf(address1)).should.be.bignumber.equal('20'); | ||
|
||
|
||
|
||
// Issue 20 and check balances and total supply | ||
({ logs: this.logs1 } = await this.upgradeableCMTATV2Instance.mint(address1, 20, { | ||
({ logs: this.logs1 } = await TRUFFLE_CMTAT_PROXY_ADDRESS_2.mint(address1, 20, { | ||
from: admin | ||
})); | ||
(await this.upgradeableCMTATV2Instance.balanceOf(address1)).should.be.bignumber.equal('40'); | ||
(await this.upgradeableCMTATV2Instance.totalSupply()).should.be.bignumber.equal('40') | ||
(await TRUFFLE_CMTAT_PROXY_ADDRESS_2.balanceOf(address1)).should.be.bignumber.equal('40'); | ||
(await TRUFFLE_CMTAT_PROXY_ADDRESS_2.totalSupply()).should.be.bignumber.equal('40') | ||
}) | ||
}) |