Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
shonentropy3 committed Mar 6, 2024
1 parent a1b3f95 commit 13dac97
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 9 deletions.
63 changes: 63 additions & 0 deletions contracts/merge/AdminRoleUpgrade.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts-upgradeable/utils/structs/EnumerableSetUpgradeable.sol";

contract AdminRoleUpgrade {
using EnumerableSetUpgradeable for EnumerableSetUpgradeable.AddressSet;

EnumerableSetUpgradeable.AddressSet private _admins;

event AdminAdded(address indexed account);
event AdminRemoved(address indexed account);

// constructor() {
// _addAdmin(msg.sender);
// }

modifier onlyAdmin() {
require(
isAdmin(msg.sender),
"AdminRole: caller does not have the Admin role"
);
_;
}

function isAdmin(address account) public view returns (bool) {
return _admins.contains(account);
}

function allAdmins() public view returns (address[] memory admins) {
admins = new address[](_admins.length());
for (uint256 i = 0; i < _admins.length(); i++) {
admins[i] = _admins.at(i);
}
}

function batchAddAdmin(address[] memory amounts) public onlyAdmin{
for(uint256 i=0; i < amounts.length; i++){
addAdmin(amounts[i]);
}
}

function addAdmin(address account) public onlyAdmin {
_addAdmin(account);
}

function removeAdmin(address account) public onlyAdmin {
_removeAdmin(account);
}

function renounceAdmin() public {
_removeAdmin(msg.sender);
}

function _addAdmin(address account) internal {
_admins.add(account);
emit AdminAdded(account);
}

function _removeAdmin(address account) internal {
_admins.remove(account);
emit AdminRemoved(account);
}
}
80 changes: 71 additions & 9 deletions contracts/merge/ERC20TokenMerge.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.20;
pragma solidity 0.8.24;

import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
//import {INTO} from "@openzeppelin/contracts-upgradeable/token/ERC20/INTO.sol";
import {SafeERC20Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "./AdminRoleUpgrade.sol";
import "./INTO.sol";

contract ERC20TokenMerge is Initializable {
using SafeERC20 for IERC20;
contract ERC20TokenMerge is Initializable, AdminRoleUpgrade {
using SafeERC20Upgradeable for INTO;

INTO mergeToken;

/// @dev A mapping source token address => merge token address
mapping(IERC20 sourceToken => IERC20 mergeToken) public mergeTokenAddress;
mapping(INTO sourceToken => INTO mergeToken) public mergeTokenAddress;
mapping(INTO sourceToken => bool) public sourceTokenMap;
mapping(INTO sourceToken => uint256) public sourceTokenBalanceMap;
mapping(INTO sourceToken => bool) public depositFlag;
mapping(INTO sourceToken => uint256) public depositLimit;

/// @dev Contract is expected to be used as proxy implementation.
/// @dev Disable the initialization to prevent Parity hack.
Expand All @@ -19,11 +27,65 @@ contract ERC20TokenMerge is Initializable {
}

/// @notice Initializes the bridge contract for later use. Expected to be used in the proxy.
function initialize() external initializer {}
function initialize() external initializer {
_addAdmin(msg.sender);
}

// Arbitrum
// Bridged USDC = 0xff970a61a04b1ca14834a43f5de4533ebddb5cc8
// Native USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831
// => L3
// L3_Arbitrum_Bridged_USDC = 0xff970a61a04b1ca14834a43f5de4533ebddb5cc9
// L3_Arbitrum_Native_USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5832

// Linea
// Bridged USDC = 0x176211869ca2b568f2a7d4ee941e073a821ee1ff
// => L3
// L3_Linea_Bridged_USDC = 0x176211869ca2b568f2a7d4ee941e073a821ee200

// L3_USDC = [L3_Arbitrum_Bridged_USDC, L3_Arbitrum_Native_USDC, L3_Linea_Bridged_USDC, ....]

/// @notice Deposit source token to mint merge token
function deposit(IERC20 _sourceToken, uint256 _amount, address _receiver) external {}
function deposit(INTO _sourceToken, uint256 _amount, address _receiver) external {
require(sourceTokenMap[_sourceToken] == true, "not the token");
require(depositLimit[_sourceToken] <= _amount, "exceed depositLimit");
require(depositFlag[_sourceToken] == true, "can not deposit");
INTO(_sourceToken).transferFrom(msg.sender, _receiver, _amount);
sourceTokenBalanceMap[_sourceToken] += _amount;
INTO(_sourceToken).mint(_receiver, _amount);
}

/// @notice Burn merge token and get source token back
function withdraw(IERC20 _sourceToken, uint256 _amount, address _receiver) external {}
function withdraw(INTO _sourceToken, uint256 _amount, address _receiver) external {
require(sourceTokenMap[_sourceToken] == true, "not the token");
sourceTokenBalanceMap[_sourceToken] -= _amount;
INTO(_sourceToken).burn(_amount);
INTO(_sourceToken).transfer(_receiver, _amount);
}

function addToken(INTO _sourceToken) external onlyAdmin {
sourceTokenMap[_sourceToken] = true;
mergeTokenAddress[_sourceToken] = mergeToken;
}

function removeToken(INTO _sourceToken) external onlyAdmin {
require(sourceTokenBalanceMap[_sourceToken] == 0, "can not remove");
sourceTokenMap[_sourceToken] = false;
}

function disableDeposit(INTO _sourceToken) external onlyAdmin {
require(sourceTokenMap[_sourceToken] == true, "already disable");
depositFlag[_sourceToken] = false;
}

function getTokenInfo(INTO _sourceToken) external view returns (bool,uint256,bool,uint256) {
return(sourceTokenMap[_sourceToken],
sourceTokenBalanceMap[_sourceToken],
depositFlag[_sourceToken],
depositLimit[_sourceToken]);
}

// Beacon模式
// 很多个MergeToken
// MergeToken = Proxy(MergeTokenBeaconProxy(MergeTokenImplement))
}
46 changes: 46 additions & 0 deletions contracts/merge/INTO.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "./AdminRole.sol";


contract INTO is ERC20Burnable, AdminRole {
using SafeMath for uint256;

constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_)
{

}

function decimals() public pure override returns (uint8) {
return 18;
}

function mint(address recipient_, uint256 amount_)
public
onlyAdmin
returns (bool)
{
uint256 balanceBefore = balanceOf(recipient_);
_mint(recipient_, amount_);
uint256 balanceAfter = balanceOf(recipient_);

return balanceAfter > balanceBefore;
}

function burn(uint256 amount) public override onlyAdmin {
super.burn(amount);
}

function burnFrom(address account, uint256 amount)
public
override
onlyAdmin
{
super.burnFrom(account, amount);
}

}

8 changes: 8 additions & 0 deletions contracts/merge/MyBeacon.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/proxy/beacon/UpgradeableBeacon.sol";

contract MyBeacon is UpgradeableBeacon {
constructor(address _implementation, address _admin) UpgradeableBeacon(_implementation, _admin) {}
}
11 changes: 11 additions & 0 deletions contracts/merge/MyProxy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/proxy/beacon/BeaconProxy.sol";

contract MyProxy is BeaconProxy {
bytes32 internal data;
constructor(address beaconAddress) BeaconProxy(beaconAddress, abi.encodePacked(data)) {
data = keccak256("initialize()");
}
}

0 comments on commit 13dac97

Please sign in to comment.