Skip to content

Commit

Permalink
Merge pull request #85 from binance-chain/mirror-sync
Browse files Browse the repository at this point in the history
R4R: add Mirror/Sync feature, implementation of BEP84
  • Loading branch information
unclezoro authored Jan 8, 2021
2 parents 4292247 + 31a6551 commit 66e29cb
Show file tree
Hide file tree
Showing 8 changed files with 1,605 additions and 65 deletions.
471 changes: 442 additions & 29 deletions abi/tokenmanager.abi

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/TokenHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
uint8 constant public TRANSFER_IN_FAILURE_UNKNOWN = 5;

uint256 constant public MAX_BEP2_TOTAL_SUPPLY = 9000000000000000000;
uint8 constant public MINIMUM_BEP20_SYMBOL_LEN = 3;
uint8 constant public MINIMUM_BEP20_SYMBOL_LEN = 2;
uint8 constant public MAXIMUM_BEP20_SYMBOL_LEN = 8;
uint8 constant public BEP2_TOKEN_DECIMALS = 8;
bytes32 constant public BEP2_TOKEN_SYMBOL_FOR_BNB = 0x424E420000000000000000000000000000000000000000000000000000000000; // "BNB"
Expand Down Expand Up @@ -333,7 +333,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
}
idx++;
}
return (transOutSynPkg, true);
return (transOutSynPkg, success);
}

function handleTransferOutFailAckPackage(bytes memory msgBytes) internal {
Expand Down
4 changes: 2 additions & 2 deletions contracts/TokenHub.template
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
uint8 constant public TRANSFER_IN_FAILURE_UNKNOWN = 5;

uint256 constant public MAX_BEP2_TOTAL_SUPPLY = 9000000000000000000;
uint8 constant public MINIMUM_BEP20_SYMBOL_LEN = 3;
uint8 constant public MINIMUM_BEP20_SYMBOL_LEN = 2;
uint8 constant public MAXIMUM_BEP20_SYMBOL_LEN = 8;
uint8 constant public BEP2_TOKEN_DECIMALS = 8;
bytes32 constant public BEP2_TOKEN_SYMBOL_FOR_BNB = 0x424E420000000000000000000000000000000000000000000000000000000000; // "BNB"
Expand Down Expand Up @@ -333,7 +333,7 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
}
idx++;
}
return (transOutSynPkg, true);
return (transOutSynPkg, success);
}

function handleTransferOutFailAckPackage(bytes memory msgBytes) internal {
Expand Down
364 changes: 355 additions & 9 deletions contracts/TokenManager.sol

Large diffs are not rendered by default.

298 changes: 298 additions & 0 deletions contracts/test/XYZToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
pragma solidity 0.6.4;

import "../interface/IBEP20.sol";
import "openzeppelin-solidity/contracts/GSN/Context.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/access/Ownable.sol";

contract XYZToken is Context, IBEP20, Ownable {
using SafeMath for uint256;

mapping (address => uint256) private _balances;

mapping (address => mapping (address => uint256)) private _allowances;

uint256 private _totalSupply;
uint8 private _decimals;
string private _symbol;
string private _name;

constructor() public {
_name = "XYZ token";
_symbol = "XYZ";
_decimals = 18;
_totalSupply = 100000000000000000000000000;
_balances[msg.sender] = _totalSupply;

emit Transfer(address(0), msg.sender, _totalSupply);
}

/**
* @dev Just for test
*
* - `newName` new name
*/
function setName(string calldata newName) external {
_name = newName;
}

/**
* @dev Just for test
*
* - `newDecimals` new decimals
*/
function setDecimals(uint8 newDecimals) external {
_decimals = newDecimals;
}

/**
* @dev Just for test
*
* - `newTotalSupply` new totalSupply
*/
function setTotalSupply(uint256 newTotalSupply) external {
_totalSupply = newTotalSupply;
}


/**
* @dev Just for test
*
* - `newSymbol` new symbol
*/
function setSymbol(string calldata newSymbol) external {
_symbol = newSymbol;
}

/**
* @dev Returns the bep token owner.
*/
function getOwner() external override view returns (address) {
return owner();
}

/**
* @dev Returns the token decimals.
*/
function decimals() external override view returns (uint8) {
return _decimals;
}

/**
* @dev Returns the token symbol.
*/
function symbol() external override view returns (string memory) {
return _symbol;
}

/**
* @dev Returns the token name.
*/
function name() external override view returns (string memory) {
return _name;
}

/**
* @dev See {BEP20-totalSupply}.
*/
function totalSupply() external override view returns (uint256) {
return _totalSupply;
}

/**
* @dev See {BEP20-balanceOf}.
*/
function balanceOf(address account) external override view returns (uint256) {
return _balances[account];
}

/**
* @dev See {BEP20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) external override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}

/**
* @dev See {BEP20-allowance}.
*/
function allowance(address owner, address spender) external override view returns (uint256) {
return _allowances[owner][spender];
}

/**
* @dev See {BEP20-approve}.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) external override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}

/**
* @dev See {BEP20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {BEP20};
*
* Requirements:
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for `sender`'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "BEP20: transfer amount exceeds allowance"));
return true;
}

/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {BEP20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}

/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {BEP20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "BEP20: decreased allowance below zero"));
return true;
}

/**
* @dev Creates `amount` tokens and assigns them to `msg.sender`, increasing
* the total supply.
*
* Requirements
*
* - `msg.sender` must be the token owner
*/
function mint(uint256 amount) public onlyOwner returns (bool) {
_mint(_msgSender(), amount);
return true;
}

/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer(address sender, address recipient, uint256 amount) internal {
require(sender != address(0), "BEP20: transfer from the zero address");
require(recipient != address(0), "BEP20: transfer to the zero address");

_balances[sender] = _balances[sender].sub(amount, "BEP20: transfer amount exceeds balance");
_balances[recipient] = _balances[recipient].add(amount);
emit Transfer(sender, recipient, amount);
}

/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal {
require(account != address(0), "BEP20: mint to the zero address");

_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}

/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal {
require(account != address(0), "BEP20: burn from the zero address");

_balances[account] = _balances[account].sub(amount, "BEP20: burn amount exceeds balance");
_totalSupply = _totalSupply.sub(amount);
emit Transfer(account, address(0), amount);
}

/**
* @dev Sets `amount` as the allowance of `spender` over the `owner`s tokens.
*
* This is internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 amount) internal {
require(owner != address(0), "BEP20: approve from the zero address");
require(spender != address(0), "BEP20: approve to the zero address");

_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}

/**
* @dev Destroys `amount` tokens from `account`.`amount` is then deducted
* from the caller's allowance.
*
* See {_burn} and {_approve}.
*/
function _burnFrom(address account, uint256 amount) internal {
_burn(account, amount);
_approve(account, _msgSender(), _allowances[account][_msgSender()].sub(amount, "BEP20: burn amount exceeds allowance"));
}
}
4 changes: 2 additions & 2 deletions genesis.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const TokenHub = artifacts.require("TokenHub");
const TokenManager = artifacts.require("TokenManager");
const ABCToken = artifacts.require("test/ABCToken");
const DEFToken = artifacts.require("test/DEFToken");
const XYZToken = artifacts.require("test/XYZToken");
const MiniToken = artifacts.require("test/MiniToken");
const MaliciousToken = artifacts.require("test/MaliciousToken");
const BSCValidatorSetTool = artifacts.require("tool/BSCValidatorSetTool");
Expand All @@ -48,6 +49,7 @@ module.exports = function(deployer, network, accounts) {
});
deployer.deploy(ABCToken);
deployer.deploy(DEFToken);
deployer.deploy(XYZToken);
deployer.deploy(MiniToken);
deployer.deploy(MaliciousToken);
deployer.deploy(MockRelayerHub);
Expand Down
Loading

0 comments on commit 66e29cb

Please sign in to comment.