Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions helix-contract/address/xtoken-product.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@
"darwinia-dvm(to ethereum)": "0x092e19c46c9daab7824393f1cd9c22f5bea13560",
"crab-dvm(to darwinia)": "0x004D0dE211BC148c3Ce696C51Cbc85BD421727E9"
},
"xtokenConvertor": {
"xRINGConvertor": {
"ethereum": "0xc29dCb1F12a1618262eF9FBA673b77140adc02D6"
},
"xKTONConvertor": {
"ethereum": "0x3217F36AE34aCA2CE60d218af8F47d29101204a8"
},
"guard": {
"ethereum": "0x4CA75992d2750BEC270731A72DfDedE6b9E71cC7",
"darwinia-dvm": "0x4ca75992d2750bec270731a72dfdede6b9e71cc7"
Expand All @@ -45,6 +48,7 @@
"xtoken": {
"darwinia-dvm(xWCRAB)": "0x656567Eb75b765FC320783cc6EDd86bD854b2305",
"crab-dvm(xWRING)": "0x273131F7CB50ac002BDd08cA721988731F7e1092",
"ethereum(xRING)": "0x81e32d4652Be82AE225DEdd1bD0bf3BCba8FEE07 "
"ethereum(xRING)": "0x81e32d4652Be82AE225DEdd1bD0bf3BCba8FEE07 ",
"ethereum(xKTON)": "0x35f15275041B53324dF461d5ccC952EE19D4a982"
}
}
91 changes: 91 additions & 0 deletions helix-contract/contracts/xtoken/v3/templates/XKTONConvertor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: Apache-2.0

pragma solidity >=0.8.17;

import "@zeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "@zeppelin-solidity/contracts/utils/introspection/ERC165.sol";
import "./interfaces/IXKTONLockBox.sol";
import "../interfaces/IXTokenIssuing.sol";
import "../interfaces/IXTokenCallback.sol";

contract XKTONConvertor is IXTokenCallback, IXTokenRollbackCallback, ERC165 {
IXKTONLockBox public lockBox;
IXTokenIssuing public xTokenIssuing;
address public immutable KTON;
address public immutable XKTON;

mapping(uint256=>address) public senders;

event IssueKTON(uint256 transferId, address recipient, uint256 amount);
event RollbackBurn(uint256 transferId, address originalSender, uint256 amount);
event BurnAndXUnlock(uint256 transferId, address sender, address recipient, uint256 amount);

function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165) returns (bool) {
return
interfaceId == type(IXTokenCallback).interfaceId ||
interfaceId == type(IXTokenRollbackCallback).interfaceId ||
super.supportsInterface(interfaceId);
}

receive() external payable {}

modifier onlyXTokenIssuing() {
require(address(xTokenIssuing) == msg.sender, "invalid sender");
_;
}

modifier onlyXTokenIssuingAuthorized() {
require(address(xTokenIssuing) == msg.sender || xTokenIssuing.guard() == msg.sender, "invalid sender");
_;
}

constructor(address _xKTON, address _kton, address _xTokenIssuing, address _lockBox) {
KTON = _kton;
XKTON = _xKTON;
lockBox = IXKTONLockBox(_lockBox);
xTokenIssuing = IXTokenIssuing(_xTokenIssuing);
IERC20(_kton).approve(_lockBox, type(uint256).max);
IERC20(_xKTON).approve(_lockBox, type(uint256).max);
IERC20(_xKTON).approve(_xTokenIssuing, type(uint256).max);
}

function xTokenCallback(
uint256 _transferId,
address _xToken,
uint256 _amount,
bytes calldata extData
) onlyXTokenIssuingAuthorized external {
address recipient = address(bytes20(extData));
require(_xToken == XKTON, "invalid xtoken");
lockBox.depositFor(recipient, _amount);
emit IssueKTON(_transferId, recipient, _amount);
}

function xTokenRollbackCallback(
uint256 _transferId,
address _xToken,
uint256 _amount
) onlyXTokenIssuing external {
require(_xToken == XKTON, "invalid xtoken");
address originalSender = senders[_transferId];
lockBox.depositFor(originalSender, _amount);
emit RollbackBurn(_transferId, originalSender, _amount);
}

function burnAndXUnlock(
address _recipient,
address _rollbackAccount,
uint256 _amount,
uint256 _nonce,
bytes calldata _extData,
bytes memory _extParams
) payable external {
IERC20(KTON).transferFrom(msg.sender, address(this), _amount);
lockBox.withdraw(_amount);
bytes32 transferId = xTokenIssuing.burnAndXUnlock{value: msg.value}(XKTON, _recipient, _rollbackAccount, _amount, _nonce, _extData, _extParams);
uint256 id = uint256(transferId);
senders[id] = msg.sender;
emit BurnAndXUnlock(id, msg.sender, _recipient, _amount);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.17;

interface IXKTONLockBox {
function depositFor(address to, uint256 amount) external;
function withdraw(uint256 amount) external;
}
Loading
Loading