Skip to content

Commit

Permalink
Update all ver2 contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiushi committed May 12, 2024
1 parent de28e85 commit 77c4f69
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 709 deletions.
9 changes: 1 addition & 8 deletions contracts/dmc2.sol → contracts/dmc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract DMC2 is ERC20Burnable, Ownable {
contract DMC is ERC20Burnable, Ownable {
mapping (address => bool) allow_minter;
bool can_change_minter = true;

modifier onlyMinter() {
require(allow_minter[msg.sender], "mint not allowed");
Expand All @@ -21,20 +20,14 @@ contract DMC2 is ERC20Burnable, Ownable {
}
_mint(address(this), _totalSupply - totalInited);
}

function disableMinterChange() public onlyOwner {
can_change_minter = false;
}

function enableMinter(address[] calldata addresses) public onlyOwner {
require(can_change_minter, "mint change not allowed");
for (uint i = 0; i < addresses.length; i++) {
allow_minter[addresses[i]] = true;
}
}

function disableMinter(address[] calldata addresses) public onlyOwner {
require(can_change_minter, "mint change not allowed");
for (uint i = 0; i < addresses.length; i++) {
allow_minter[addresses[i]] = false;
}
Expand Down
10 changes: 5 additions & 5 deletions contracts/dmc2_bridge.sol → contracts/dmc_bridge.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./dmc2.sol";
import "./dmc.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract DMC2Bridge is Ownable {
DMC2 public dmc2;
contract DMCBridge is Ownable {
DMC public dmc2;

// DMC1到2的兑换
mapping(bytes32 => uint256) public dmc1_to_dmc2;

constructor(address _dmc2) Ownable(msg.sender) {
dmc2 = DMC2(_dmc2);
dmc2 = DMC(_dmc2);
}

function registerDMC1(address recvAddress, string calldata cookie, uint256 dmc1Amount) onlyOwner public {
Expand All @@ -22,9 +22,9 @@ contract DMC2Bridge is Ownable {
bytes32 key = keccak256(abi.encodePacked(msg.sender, cookie));
require(dmc1_to_dmc2[key] > 0, "no dmc1 amount");
uint256 dmc2Amount = dmc1_to_dmc2[key] * 4 / 5;
dmc1_to_dmc2[key] = 0;

dmc2.transfer(msg.sender, dmc2Amount);
dmc1_to_dmc2[key] = 0;
}

function claimRemainDMC2() public onlyOwner {
Expand Down
90 changes: 53 additions & 37 deletions contracts/exchange2.sol → contracts/exchange.sol
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./dmc2.sol";
import "./gwt2.sol";
import "./dmc.sol";
import "./gwt.sol";
import "./dividend.sol";

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
contract Exchange is Initializable, UUPSUpgradeable, OwnableUpgradeable {
address dmcToken;
address gwtToken;
address fundationIncome;

bool enable_dmc_mint = false;
bool test_mode;
// 当前周期,也可以看做周期总数
uint256 current_circle;
uint256 current_mine_circle_start;
Expand All @@ -41,17 +41,27 @@ contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
uint256 adjust_period;
uint256 initial_dmc_balance;

uint256 free_mint_balance;
mapping(address=>bool) is_free_minted;

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);

modifier testEnabled() {
require(test_mode, "contract not in test mode");
_;
}

modifier testDisabled() {
require(!test_mode, "contract in test mode");
_;
}

function initialize(address _dmcToken, address _gwtToken, address _fundationIncome, uint256 _min_circle_time) public initializer {
__UUPSUpgradeable_init();
__Ownable_init(msg.sender);
__Exchange2Upgradable_init(_dmcToken, _gwtToken, _fundationIncome, _min_circle_time);
free_mint_balance = 210*5000*10**18;
}

function __Exchange2Upgradable_init(address _dmcToken, address _gwtToken, address _fundationIncome, uint256 _min_circle_time) public onlyInitializing {
Expand All @@ -65,18 +75,20 @@ contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
adjust_period = 21;
initial_dmc_balance = 768242.27 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++) {
balance = balance * 4 / 5;
}
return balance;
//return 210 ether;
}

function _newCycle() internal {
Expand All @@ -95,7 +107,7 @@ contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {

current_circle_dmc_balance = remain_dmc_balance;
emit newCycle(current_circle, current_circle_dmc_balance, current_mine_circle_start);
//console.log("new cycle %d start at %d, dmc balance %d", current_circle, current_mine_circle_start, current_circle_dmc_balance);
console.log("new cycle %d start at %d, dmc balance %d", current_circle, current_mine_circle_start, current_circle_dmc_balance);
}

function adjustExchangeRate() internal {
Expand Down Expand Up @@ -129,10 +141,8 @@ contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
// 涨幅限制为20%
dmc2gwt_rate = old_rate * 6 / 5;
}
/*
// for test
dmc2gwt_rate = 210;
*/
// for test
//dmc2gwt_rate = 210;
console.log("increase dmc2gwt_rate to %d", dmc2gwt_rate);
}

Expand Down Expand Up @@ -162,48 +172,54 @@ contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
return (real_amount, is_empty);
}

function addFreeMintBalance(uint256 amount) public onlyOwner {
function addFreeMintBalance(uint256 amount) public {
require(amount > 0, "amount must be greater than 0");
GWTToken2(gwtToken).transferFrom(msg.sender, address(this), amount);
free_mint_balance += amount;
GWT(gwtToken).transferFrom(msg.sender, address(this), amount);
}

function freeMintGWT() public {
function freeMintGWT() public {
//每个地址只能free mint一次
//每次成功得到210个GWT
require(free_mint_balance > 0, "no free mint balance");
require(!is_free_minted[msg.sender], "already free minted");
require(free_mint_balance > 0, "no free mint balance");

GWTToken2(gwtToken).transferFrom(this, msg.sender, 210*10**18);
is_free_minted[msg.sender] = true;
free_mint_balance -= 210 ether;
GWT(gwtToken).mint(msg.sender, 210 ether);
}

function DMCtoGWT(uint256 amount) public {
DMC2(dmcToken).burnFrom(msg.sender, amount);
GWTToken2(gwtToken).mint(msg.sender, amount * dmc2gwt_rate * 6 / 5);
DMC(dmcToken).burnFrom(msg.sender, amount);
GWT(gwtToken).mint(msg.sender, amount * dmc2gwt_rate * 6 / 5);
}

function eanbleDMCMint() public onlyOwner {
enable_dmc_mint = true;
function enableTestMode() public onlyOwner testDisabled {
test_mode = true;
}

function addFreeDMCTestMintBalance(uint256 amount) public onlyOwner {
require(!enable_dmc_mint, "dmc mint is enabled,test end");
require(amount > 0, "amount must be greater than 0");
DMC2(dmcToken).transferFrom(msg.sender, address(this), amount);
function enableProdMode() public onlyOwner testEnabled {
test_mode = false;

// 将剩余的DMC还给owner
DMC(dmcToken).transfer(msg.sender, DMC(dmcToken).balanceOf(address(this)));
}

function GWTToDMCForTest(uint256 ammount) public {
require(!enable_dmc_mint, "dmc mint is enabled,test end");
require(ammount > 0, "ammount must be greater than 0");
require(ammount % 210 == 0, "ammount must be multiple of 210");
function addFreeDMCTestMintBalance(uint256 amount) public onlyOwner testEnabled {
require(amount > 0, "amount must be greater than 0");

dmc_ammount = ammount / 210;
DMC2(dmcToken).transferFrom(this, msg.sender, dmc_ammount);
DMC(dmcToken).transferFrom(msg.sender, address(this), amount);
}

function GWTtoDMC(uint256 amount) public {
require(enable_dmc_mint, "dmc mint not enabled");
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");

GWT(gwtToken).transferFrom(msg.sender, address(this), amount);
DMC(dmcToken).transfer(msg.sender, amount / 210);
}

function GWTtoDMC(uint256 amount) public testDisabled {
adjustExchangeRate();
uint256 dmc_count = amount / dmc2gwt_rate;
console.log("exchange dmc %d from amount %d, rate %d", dmc_count, amount, dmc2gwt_rate);
Expand All @@ -218,15 +234,15 @@ contract Exchange2 is Initializable, UUPSUpgradeable, OwnableUpgradeable {
}

//不用立刻转给分红合约,而是等积累一下
GWTToken2(gwtToken).transferFrom(msg.sender, address(this), real_gwt_amount);
DMC2(dmcToken).mint(msg.sender, real_dmc_amount);
GWT(gwtToken).transferFrom(msg.sender, address(this), real_gwt_amount);
DMC(dmcToken).mint(msg.sender, real_dmc_amount);
emit DMCMinted(msg.sender, real_dmc_amount, remain_dmc_balance);
}

// 手工将累积的收入打给分红合约
function transferIncome() public {
GWTToken2(gwtToken).approve(fundationIncome, GWTToken2(gwtToken).balanceOf(address(this)));
DividendContract(payable(fundationIncome)).deposit(GWTToken2(gwtToken).balanceOf(address(this)), address(gwtToken));
GWT(gwtToken).approve(fundationIncome, GWT(gwtToken).balanceOf(address(this)));
DividendContract(payable(fundationIncome)).deposit(GWT(gwtToken).balanceOf(address(this)), address(gwtToken));
}

function getCycleInfo() public view returns (uint256, uint256, uint256) {
Expand Down
16 changes: 5 additions & 11 deletions contracts/gwt2.sol → contracts/gwt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@ pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract GWTToken2 is ERC20, Ownable {
contract GWT is ERC20, Ownable {
mapping (address => bool) allow_minter;
bool can_change_minter = true;

constructor() ERC20("Gb storage per Week Token", "GWT") Ownable(msg.sender) {
//给owner分配5000万Token
_mint(msg.sender, 50000000 * 10 ** uint(decimals()));
constructor(address[] memory initAddress, uint[] memory initAmount) ERC20("Gb storage per Week Token", "GWT") Ownable(msg.sender) {
for (uint i = 0; i < initAddress.length; i++) {
_mint(initAddress[i], initAmount[i]);
}
}

modifier onlyMinter() {
require(allow_minter[msg.sender], "mint not allowed");
_;
}

function disableMinterChange() public onlyOwner {
can_change_minter = false;
}

function enableMinter(address[] calldata addresses) public onlyOwner {
require(can_change_minter, "mint change not allowed");
for (uint i = 0; i < addresses.length; i++) {
allow_minter[addresses[i]] = true;
}
}

function disableMinter(address[] calldata addresses) public onlyOwner {
require(can_change_minter, "mint change not allowed");
for (uint i = 0; i < addresses.length; i++) {
allow_minter[addresses[i]] = false;
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/public_data_storage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
uint256 lastShowBlock;
}

GWTToken public gwtToken;// Gb per Week Token
GWT public gwtToken;// Gb per Week Token
address public foundationAddress;

mapping(address => SupplierInfo) _supplierInfos;
Expand Down Expand Up @@ -137,7 +137,7 @@ contract PublicDataStorage is Initializable, UUPSUpgradeable, OwnableUpgradeable
__UUPSUpgradeable_init();
__Ownable_init(msg.sender);

gwtToken = GWTToken(_gwtToken);
gwtToken = GWT(_gwtToken);
_startBlock = block.number;
_currectCycle = 0;
foundationAddress = _Foundation;
Expand Down
Loading

0 comments on commit 77c4f69

Please sign in to comment.