Skip to content

Commit

Permalink
Add new test cycle gwt -> dmc logic
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiushi committed Aug 7, 2024
1 parent 4939642 commit 9106090
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
46 changes: 37 additions & 9 deletions contracts/exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 22 additions & 5 deletions test/test_exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 9106090

Please sign in to comment.