Skip to content

Commit d7c12b2

Browse files
authored
Merge pull request #8 from QEDK/main
Fix for Mintable ERC-20 transfer
2 parents 61c308a + 2b064b1 commit d7c12b2

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

contracts/examples/mintable-erc20-transfer/FxMintableERC20ChildTunnel.sol

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { Create2 } from '../../lib/Create2.sol';
66
import { Ownable } from '../../lib/Ownable.sol';
77
import { FxERC20 } from '../../tokens/FxERC20.sol';
88

9+
910
/**
1011
* @title FxMintableERC20ChildTunnel
1112
*/
1213
contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
1314
bytes32 public constant DEPOSIT = keccak256("DEPOSIT");
14-
bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");
15+
//bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");
16+
1517

1618
// event for token maping
1719
event TokenMapped(address indexed rootToken, address indexed childToken);
@@ -42,10 +44,29 @@ contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
4244
// check if mapping is already there
4345
require(rootToChildToken[rootToken] == address(0x0), "FxMintableERC20ChildTunnel: ALREADY_MAPPED");
4446
rootToChildToken[rootToken] = childToken;
45-
47+
emit TokenMapped(rootToken,childToken);
48+
4649
// initialize child token with all parameters
4750
FxERC20(childToken).initialize(address(this), rootToken, name, symbol, decimals);
4851
}
52+
53+
//To mint tokens on child chain
54+
function mintToken(address childToken, uint256 amount) public onlyOwner {
55+
FxERC20 childTokenContract = FxERC20(childToken);
56+
// child token contract will have root token
57+
address rootToken = childTokenContract.connectedToken();
58+
59+
// validate root and child token mapping
60+
require(
61+
childToken != address(0x0) &&
62+
rootToken != address(0x0) &&
63+
childToken == rootToChildToken[rootToken],
64+
"FxERC20ChildTunnel: NO_MAPPED_TOKEN"
65+
);
66+
67+
//mint token
68+
childTokenContract.mint(msg.sender, amount);
69+
}
4970

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

6384
// withdraw tokens
6485
childTokenContract.burn(msg.sender, amount);
65-
86+
6687
// name, symbol and decimals
67-
FxERC20 rootTokenContract = FxERC20(rootToken);
88+
FxERC20 rootTokenContract = FxERC20(childToken);
6889
string memory name = rootTokenContract.name();
6990
string memory symbol = rootTokenContract.symbol();
7091
uint8 decimals = rootTokenContract.decimals();
@@ -86,12 +107,14 @@ contract FxMintableERC20ChildTunnel is Ownable, FxBaseChildTunnel, Create2 {
86107
// decode incoming data
87108
(bytes32 syncType, bytes memory syncData) = abi.decode(data, (bytes32, bytes));
88109

110+
89111
if (syncType == DEPOSIT) {
90112
_syncDeposit(syncData);
91-
} else {
113+
} else {
92114
revert("FxERC20ChildTunnel: INVALID_SYNC_TYPE");
93115
}
94116
}
117+
95118

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

contracts/examples/mintable-erc20-transfer/FxMintableERC20RootTunnel.sol

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@ contract FxMintableERC20RootTunnel is FxBaseRootTunnel, Create2 {
1414

1515
// maybe DEPOSIT and MAP_TOKEN can be reduced to bytes4
1616
bytes32 public constant DEPOSIT = keccak256("DEPOSIT");
17-
bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");
17+
//bytes32 public constant MAP_TOKEN = keccak256("MAP_TOKEN");
1818

1919
mapping(address => address) public rootToChildTokens;
2020
address public rootTokenTemplate;
21+
bytes32 public childTokenTemplateCodeHash;
2122

22-
constructor(address _checkpointManager, address _fxRoot, address _rootTokenTemplate) FxBaseRootTunnel(_checkpointManager, _fxRoot) {
23+
constructor(address _checkpointManager, address _fxRoot, address _rootTokenTemplate) FxBaseRootTunnel(_checkpointManager, _fxRoot) {
2324
rootTokenTemplate = _rootTokenTemplate;
2425
}
2526

26-
function deposit(address rootToken, address user, uint256 amount, bytes memory data) public {
27-
// map token if not mapped
28-
require(rootToChildTokens[rootToken] != address(0x0), "FxMintableERC20RootTunnel: NO_MAPING_FOUND");
27+
28+
function deposit(address rootToken, address user, uint256 amount, bytes memory data) public {
29+
// map token if not mapped
30+
require(rootToChildTokens[rootToken] != address(0x0), "FxMintableERC20RootTunnel: NO_MAPPING_FOUND");
2931

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

67+
//approve token transfer
68+
tokenObj.approve(address(this), amount);
69+
6570
// transfer from tokens
6671
tokenObj.transferFrom(
6772
address(this),
@@ -91,3 +96,4 @@ contract FxMintableERC20RootTunnel is FxBaseRootTunnel, Create2 {
9196
return (size > 0);
9297
}
9398
}
99+

0 commit comments

Comments
 (0)