Skip to content

Commit

Permalink
Merge pull request #8 from QEDK/main
Browse files Browse the repository at this point in the history
Fix for Mintable ERC-20 transfer
  • Loading branch information
jdkanani authored Jun 7, 2021
2 parents 61c308a + 2b064b1 commit d7c12b2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import { Create2 } from '../../lib/Create2.sol';
import { Ownable } from '../../lib/Ownable.sol';
import { FxERC20 } from '../../tokens/FxERC20.sol';


/**
* @title FxMintableERC20ChildTunnel
*/
contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
bytes32 public constant DEPOSIT = keccak256("DEPOSIT");
bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");
//bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");


// event for token maping
event TokenMapped(address indexed rootToken, address indexed childToken);
Expand Down Expand Up @@ -42,10 +44,29 @@ contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
// check if mapping is already there
require(rootToChildToken[rootToken] == address(0x0), "FxMintableERC20ChildTunnel: ALREADY_MAPPED");
rootToChildToken[rootToken] = childToken;

emit TokenMapped(rootToken,childToken);

// initialize child token with all parameters
FxERC20(childToken).initialize(address(this), rootToken, name, symbol, decimals);
}

//To mint tokens on child chain
function mintToken(address childToken, uint256 amount) public onlyOwner {
FxERC20 childTokenContract = FxERC20(childToken);
// child token contract will have root token
address rootToken = childTokenContract.connectedToken();

// validate root and child token mapping
require(
childToken != address(0x0) &&
rootToken != address(0x0) &&
childToken == rootToChildToken[rootToken],
"FxERC20ChildTunnel: NO_MAPPED_TOKEN"
);

//mint token
childTokenContract.mint(msg.sender, amount);
}

function withdraw(address childToken, uint256 amount) public {
FxERC20 childTokenContract = FxERC20(childToken);
Expand All @@ -62,9 +83,9 @@ contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {

// withdraw tokens
childTokenContract.burn(msg.sender, amount);

// name, symbol and decimals
FxERC20 rootTokenContract = FxERC20(rootToken);
FxERC20 rootTokenContract = FxERC20(childToken);
string memory name = rootTokenContract.name();
string memory symbol = rootTokenContract.symbol();
uint8 decimals = rootTokenContract.decimals();
Expand All @@ -86,12 +107,14 @@ contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
// decode incoming data
(bytes32 syncType, bytes memory syncData) = abi.decode(data, (bytes32, bytes));


if (syncType == DEPOSIT) {
_syncDeposit(syncData);
} else {
} else {
revert("FxERC20ChildTunnel: INVALID_SYNC_TYPE");
}
}


function _syncDeposit(bytes memory syncData) internal {
(address rootToken, address depositor, address to, uint256 amount, bytes memory depositData) = abi.decode(syncData, (address, address, address, uint256, bytes));
Expand Down Expand Up @@ -122,3 +145,4 @@ contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
return (size > 0);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ contract FxMintableERC20RootTunnel is FxBaseRootTunnel, Create2 {

// maybe DEPOSIT and MAP_TOKEN can be reduced to bytes4
bytes32 public constant DEPOSIT = keccak256("DEPOSIT");
bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");
//bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");

mapping(address => address) public rootToChildTokens;
address public rootTokenTemplate;
bytes32 public childTokenTemplateCodeHash;

constructor(address _checkpointManager, address _fxRoot, address _rootTokenTemplate) FxBaseRootTunnel(_checkpointManager, _fxRoot) {
constructor(address _checkpointManager, address _fxRoot, address _rootTokenTemplate) FxBaseRootTunnel(_checkpointManager, _fxRoot) {
rootTokenTemplate = _rootTokenTemplate;
}

function deposit(address rootToken, address user, uint256 amount, bytes memory data) public {
// map token if not mapped
require(rootToChildTokens[rootToken] != address(0x0), "FxMintableERC20RootTunnel: NO_MAPING_FOUND");

function deposit(address rootToken, address user, uint256 amount, bytes memory data) public {
// map token if not mapped
require(rootToChildTokens[rootToken] != address(0x0), "FxMintableERC20RootTunnel: NO_MAPPING_FOUND");

// transfer from depositor to this contract
FxERC20(rootToken).transferFrom(
Expand Down Expand Up @@ -62,6 +64,9 @@ contract FxMintableERC20RootTunnel is FxBaseRootTunnel, Create2 {
tokenObj.mint(address(this), amount.sub(balanceOf));
}

//approve token transfer
tokenObj.approve(address(this), amount);

// transfer from tokens
tokenObj.transferFrom(
address(this),
Expand Down Expand Up @@ -91,3 +96,4 @@ contract FxMintableERC20RootTunnel is FxBaseRootTunnel, Create2 {
return (size > 0);
}
}

0 comments on commit d7c12b2

Please sign in to comment.