diff --git a/contracts/exchange.sol b/contracts/exchange.sol index 8a7572d..0a68703 100644 --- a/contracts/exchange.sol +++ b/contracts/exchange.sol @@ -45,6 +45,11 @@ contract Exchange is Initializable, UUPSUpgradeable, OwnableUpgradeable { uint256 free_mint_balance; mapping(address=>bool) is_free_minted; + uint256 public test_dmc_balance; + uint256 public cycle_start_time; + uint256 public cycle_end_time; + uint256 public test_gwt_ratio; + event newCycle(uint256 cycle_number, uint256 dmc_balance, uint256 start_time); event gwtRateChanged(uint256 new_rate, uint256 old_rate); event DMCMinted(address user, uint256 amount, uint256 remain); @@ -77,13 +82,12 @@ contract Exchange is Initializable, UUPSUpgradeable, OwnableUpgradeable { initial_dmc_balance = 4817446 ether; test_mode = true; - //_newCycle(); } function getCircleBalance(uint256 circle) public view returns (uint256) { //return 210 ether; - + uint256 adjust_times = (circle-1) / adjust_period; uint256 balance = initial_dmc_balance; for (uint i = 0; i < adjust_times; i++) { @@ -217,18 +221,42 @@ contract Exchange is Initializable, UUPSUpgradeable, OwnableUpgradeable { } } - function addFreeDMCTestMintBalance(uint256 amount) public onlyOwner testEnabled { - require(amount > 0, "amount must be greater than 0"); + function GWTToDMCForTest(uint256 amount) public testEnabled { + require(test_gwt_ratio > 0, "not start test cycle"); + + uint256 dmc_count = amount / test_gwt_ratio; + uint256 real_dmc_amount = dmc_count; + if (test_dmc_balance <= dmc_count) { + real_dmc_amount = test_dmc_balance; + cycle_end_time = block.timestamp; + } + test_dmc_balance -= real_dmc_amount; + + GWT(gwtToken).transferFrom(msg.sender, address(this), real_dmc_amount * 210); + DMC(dmcToken).transfer(msg.sender, real_dmc_amount); + } + + function addDMCXForTest(uint256 amount) public testEnabled { DMC(dmcToken).transferFrom(msg.sender, address(this), amount); + startNewTestCycle(); } - function GWTToDMCForTest(uint256 amount) public testEnabled { - require(amount > 0, "ammount must be greater than 0"); - require(amount % 210 == 0, "ammount must be multiple of 210"); + function startNewTestCycle() public onlyOwner testEnabled { + if (test_gwt_ratio == 0) { // init + test_gwt_ratio = 210; + } else if (cycle_end_time > 0) { + uint256 gwt_ratio_change = (block.timestamp - cycle_end_time) * 100 / (block.timestamp - cycle_start_time); + if (gwt_ratio_change > 20) { + gwt_ratio_change = 20; + } - GWT(gwtToken).transferFrom(msg.sender, address(this), amount); - DMC(dmcToken).transfer(msg.sender, amount / 210); + test_gwt_ratio = test_gwt_ratio * (100 + gwt_ratio_change) / 100; + } + + cycle_start_time = block.timestamp; + cycle_end_time = 0; + test_dmc_balance = DMC(dmcToken).balanceOf(address(this)); } function GWTtoDMC(uint256 amount) public testDisabled { diff --git a/test/test_exchange.ts b/test/test_exchange.ts index 05c548c..6b2ccb9 100644 --- a/test/test_exchange.ts +++ b/test/test_exchange.ts @@ -46,12 +46,29 @@ describe("Exchange", function () { await expect(exchange.freeMintGWT()).to.changeTokenBalance(gwt, signers[0], ethers.parseEther("210")); await expect(exchange.freeMintGWT()).to.revertedWith("already free minted"); - await (await dmc.approve(await exchange.getAddress(), ethers.parseEther("1"))).wait(); - await (await exchange.addFreeDMCTestMintBalance(ethers.parseEther("1"))).wait(); + await (await dmc.transfer(await exchange.getAddress(), ethers.parseEther("1"))).wait(); + console.log("test change dmc 1"); + await expect(exchange.GWTToDMCForTest(ethers.parseEther("210"))).to.revertedWith("not start test cycle"); - expect(exchange.GWTToDMCForTest(ethers.parseEther("210"))).to.changeTokenBalance(dmc, signers[0], ethers.parseEther("1")); + await (await exchange.startNewTestCycle()).wait(); + expect(await exchange.test_gwt_ratio()).to.equal(210); + expect(await exchange.test_dmc_balance()).to.equal(ethers.parseEther("1")); + + console.log("test change dmc 2"); + await (await gwt.approve(await exchange.getAddress(), ethers.parseEther("210"))).wait(); + await expect(exchange.GWTToDMCForTest(ethers.parseEther("210"))).to.changeTokenBalance(dmc, signers[0], ethers.parseEther("1")); await expect(exchange.GWTtoDMC(ethers.parseEther("210"))).to.be.revertedWith("contract in test mode"); + + await (await dmc.approve(await exchange.getAddress(), ethers.parseEther("10"))).wait(); + await (await exchange.addDMCXForTest(ethers.parseEther("10"))).wait(); + + expect(await exchange.test_gwt_ratio()).to.equal(252); + expect(await exchange.test_dmc_balance()).to.equal(ethers.parseEther("10")); + + console.log("test change dmc 3"); + await (await gwt.approve(await exchange.getAddress(), ethers.parseEther("504"))).wait(); + await expect(exchange.GWTToDMCForTest(ethers.parseEther("504"))).to.changeTokenBalance(dmc, signers[0], ethers.parseEther("2")); }) it("enable prod mode", async () => { @@ -67,8 +84,8 @@ describe("Exchange", function () { // 兑换DMC到GWT await (await dmc.approve(await exchange.getAddress(), ethers.parseEther("1"))).wait(); - // 用1 DMC兑换GWT,能兑换1*210*1.2个 - await expect(exchange.DMCtoGWT(ethers.parseEther("1"))).to.changeTokenBalance(gwt, signers[0], ethers.parseEther((1*210*1.2).toString())); + // 用1 DMC兑换GWT,能兑换1*210*1.1个 + await expect(exchange.DMCtoGWT(ethers.parseEther("1"))).to.changeTokenBalance(gwt, signers[0], ethers.parseEther("231")); }); it("cycle 2", async () => {