Skip to content

Commit

Permalink
Improve upgradeability
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniRoman committed Jun 24, 2024
1 parent f1f33d0 commit ed9b8d9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
32 changes: 24 additions & 8 deletions contracts/BackedAutoFeeTokenImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,54 @@ contract BackedAutoFeeTokenImplementation is BackedTokenImplementation {
}

// Initializers:

function initialize(
string memory name_,
string memory symbol_
) public virtual override {
_initialize(name_, symbol_, 24 * 3600, block.timestamp);
super.initialize(name_, symbol_);
_initialize_auto_fee(24 * 3600, block.timestamp, 0);
}

function initialize(
string memory name_,
string memory symbol_,
uint256 _periodLength,
uint256 _lastTimeFeeApplied
uint256 _lastTimeFeeApplied,
uint256 _feePerPeriod
) public virtual {
_initialize(name_, symbol_, _periodLength, _lastTimeFeeApplied);
super.initialize(name_, symbol_);
_initialize_auto_fee(_periodLength, _lastTimeFeeApplied, _feePerPeriod);
}

function initialize_v2(
uint256 _periodLength,
uint256 _lastTimeFeeApplied,
uint256 _feePerPeriod
) public virtual {
_initialize_auto_fee(_periodLength, _lastTimeFeeApplied, _feePerPeriod);
}

function _initialize(
string memory name_,
string memory symbol_,
uint256 _periodLength,
uint256 _lastTimeFeeApplied
string memory symbol_
) public virtual initializer {
__ERC20_init(name_, symbol_);
__Ownable_init();
_buildDomainSeparator();
_setTerms("https://www.backedassets.fi/legal-documentation"); // Default Terms
}

multiplier = 1e18;
function _initialize_auto_fee(
uint256 _periodLength,
uint256 _lastTimeFeeApplied,
uint256 _feePerPeriod
) public virtual {
require(lastTimeFeeApplied == 0, "BackedAutoFeeTokenImplementation already initialized");

multiplier = 1e18;
periodLength = _periodLength;
lastTimeFeeApplied = _lastTimeFeeApplied;
feePerPeriod = _feePerPeriod;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/BackedAutoFeeTokenImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,13 @@ describe("BackedAutoFeeTokenImplementation", function () {
const proxyAdminFactory = new ProxyAdmin__factory(owner.signer)
proxyAdmin = await proxyAdminFactory.deploy();
const tokenProxy = await new BackedTokenProxy__factory(owner.signer).deploy(tokenImplementation.address, proxyAdmin.address, tokenImplementation.interface.encodeFunctionData(
'initialize(string,string,uint256,uint256)',
'initialize(string,string,uint256,uint256,uint256)',
[
tokenName,
tokenSymbol,
24 * 3600,
baseTime
baseTime,
baseFeePerPeriod
]
));
token = BackedAutoFeeTokenImplementation__factory.connect(tokenProxy.address, owner.signer);
Expand All @@ -86,7 +87,6 @@ describe("BackedAutoFeeTokenImplementation", function () {
await token.setMultiplierUpdater(owner.address);
sanctionsList = await new SanctionsListMock__factory(blacklister.signer).deploy();
await token.setSanctionsList(sanctionsList.address);
await token.updateFeePerPeriod(baseFeePerPeriod);

// Chain Id
const network = await ethers.provider.getNetwork();
Expand Down
13 changes: 7 additions & 6 deletions test/Upgradability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe("Upgrade from v1.0.0 to v1.1.0", () => {
});
});

describe("Upgrade from v1.1.0 to v1.2.0", () => {
describe("Upgrade from v1.1.0 to auto fee", () => {
let implementationV2: BackedAutoFeeTokenImplementation;
let tokenV2: BackedAutoFeeTokenImplementation;
let tokenV1: BackedTokenImplementation;
Expand Down Expand Up @@ -165,16 +165,17 @@ describe("Upgrade from v1.1.0 to v1.2.0", () => {
await ethers.getContractFactory("BackedAutoFeeTokenImplementation")
).deploy();

await proxyAdmin.upgrade(tokenV1.address, implementationV2.address);
await proxyAdmin.upgradeAndCall(tokenV1.address, implementationV2.address, implementationV2.interface.encodeFunctionData(
'initialize_v2', [
24 * 3600,
Math.floor(Date.now() / 1000) - 3600,
0
]));

tokenV2 = await ethers.getContractAt(
"BackedAutoFeeTokenImplementation",
tokenV1.address
);
await tokenV2.setMultiplierUpdater(owner.address);
await tokenV2.setLastTimeFeeApplied(Math.floor(Date.now() / 1000) - 3600);
await tokenV2.setPeriodLength(24 * 3600);
await tokenV2.updateMultiplierValue(ethers.BigNumber.from(10).pow(18), 0)
};

beforeEach(async () => {
Expand Down

0 comments on commit ed9b8d9

Please sign in to comment.