The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/solidity/optics-xapps/contracts/bridge/BridgeToken.sol

167 lines
5.8 KiB

// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
// ============ Internal Imports ============
import {IBridgeToken} from "../../interfaces/bridge/IBridgeToken.sol";
import {ERC20} from "./OZERC20.sol";
// ============ External Imports ============
test: migrate tests and deploy process to ts (#400) * refactor: migrate js lib to ts * refactor: rewrite extendEnvironment in ts * add: sample tests * add: extend hre * refactor: ts deploy and signer type * refactor: devDeploy for tests * document: isTestDeploy param, add common.test.ts * add: common tests * fix: common tests * clean: remove sample script * refactor: remove Signer type and use ethers.Signer * refactor: typescript directory * build: generate typechain * refactor: typescript code (#399) * refactor: optics-tests/optics-deploy * fix: install typechain * fix * fix: ts errors * add: merkle tests (#417) * add: merkle tests * clean: remove commented code * fix: merkle tests * test: add queue tests (#422) * Add queue tests * fix: add await to fix nonce bug * test: home ts (#401) * add: home tests * add: home tests * add: deploy home (broken) * refactor: add num confirmations to config * refactor: clean up around ts tests (#424) - add testChain.ts with utility functions for making test Chain and Deploy - refactor the Optics hardhat extension - improve typing of enums in Optics lib - remove most references to waffle (prefer ethers) - remove isTestDeploy from deployment args in favor of a test? on Deploys * refactor: clean test deploy, home tests passing * refactor: deployOptics * fix: white space * lint: fix white space * refactor: testCase vectors and imports * fix: rust test vector generation * fix: missing await Co-authored-by: James Prestwich <10149425+prestwich@users.noreply.github.com> * test: add message test (#423) * test: add message test * fix: replace require with import * fix: import for queue.test.ts * enhance: add type check * fix: import conflicts * test: add upgrade tests (#443) * test: add upgrade tests * fix: missing arg * refactor: clean up code * remove: unused code * refactor: deploy proxy * fix: unsaved code * fix: ts conflicts * add: utils ts (#453) * draft * refactor: remove waffle from home tests * fix: types, remove waffle Co-authored-by: yoduyodu <wang7ong@gmail.com> * test: add XAppConnectionManager tests (#456) * test: add XAppConnectionManager tests * test: initial setup for XAppConnectionManager tests * test: most XAppConnectionManager tests passing * fix: failing test * test: SimpleMessage ts (#468) * test: add cross-chain test utils * test: simpleMessage, mostly passing * fix: prove and process test * test: add initial state test, clean up * test: recoverymanager ts (#473) * test: add recoveryManager initial tests * fix: set recoveryManager in ts deploy * fix: set governor * test: replica ts (#483) * test: add replica tests * debug: fix failing tests * WIP: test: governance router/upgrade ts (#470) * test: add governance router tests * debug: some tests * debug: Gov Router tests * refactor: upgradeUtils * fix: missing await * test: clean up ts tests (#484) * clean: imports * refactor: utils * refactor: remove logs during testing * fix: weird bug, ethers.getSigners messes up describe blocks * delete: solidity/optics-core/js * update: pre-commit script for ts * update: pre-commit and scripts * fix: test, update with main * clean: types * fix: bad recipient handle * add: todo for gov router test * add: add back verify deploy stuff in js for now * bug: fix the governance upgrade test (#490) * fix: gov router upgrade test * feature: use TS in both solidity packages * bug: install deps in typescript dir in tests * feature: enhanced github action install * chore: disable automerge, and move lint before test Co-authored-by: Tong Wang <wang7ong@gmail.com> Co-authored-by: James Prestwich <10149425+prestwich@users.noreply.github.com> Co-authored-by: James Prestwich <james@prestwi.ch>
3 years ago
import {TypeCasts} from "@celo-org/optics-sol/contracts/XAppConnectionManager.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
contract BridgeToken is IBridgeToken, Ownable, ERC20 {
// Immutables used in EIP 712 structured data hashing & signing
// https://eips.ethereum.org/EIPS/eip-712
bytes32 public immutable _PERMIT_TYPEHASH =
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);
bytes32 private immutable _EIP712_STRUCTURED_DATA_VERSION = keccak256(bytes("1"));
uint16 private immutable _EIP712_PREFIX_AND_VERSION = uint16(0x1901);
mapping(address => uint256) public nonces;
/**
* @notice Destroys `_amnt` tokens from `_from`, reducing the
* total supply.
*
* @dev Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `_from` cannot be the zero address.
* - `_from` must have at least `_amnt` tokens.
*
* @param _from The address from which to destroy the tokens
* @param _amnt The amount of tokens to be destroyed
*/
function burn(address _from, uint256 _amnt) external override onlyOwner {
_burn(_from, _amnt);
}
/** @notice Creates `_amnt` tokens and assigns them to `_to`, increasing
* the total supply.
*
* @dev Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `to` cannot be the zero address.
*
* @param _to The destination address
* @param _amnt The amount of tokens to be minted
*/
function mint(address _to, uint256 _amnt) external override onlyOwner {
_mint(_to, _amnt);
}
/**
* @notice Set the details of a token
* @param _newName The new name
* @param _newSymbol The new symbol
* @param _newDecimals The new decimals
*/
function setDetails(
bytes32 _newName,
bytes32 _newSymbol,
uint8 _newDecimals
) external override onlyOwner {
// careful with naming convention change here
token.name = TypeCasts.coerceString(_newName);
token.symbol = TypeCasts.coerceString(_newSymbol);
token.decimals = _newDecimals;
}
/**
* @notice Sets approval from owner to spender to value
* as long as deadline has not passed
* by submitting a valid signature from owner
* Uses EIP 712 structured data hashing & signing
* https://eips.ethereum.org/EIPS/eip-712
* @param _owner The account setting approval & signing the message
* @param _spender The account receiving approval to spend owner's tokens
* @param _value The amount to set approval for
* @param _deadline The timestamp before which the signature must be submitted
* @param _v ECDSA signature v
* @param _r ECDSA signature r
* @param _s ECDSA signature s
*/
function permit(
address _owner,
address _spender,
uint256 _value,
uint256 _deadline,
uint8 _v,
bytes32 _r,
bytes32 _s
) external {
require(block.timestamp <= _deadline, "ERC20Permit: expired deadline");
require(_owner != address(0), "ERC20Permit: owner zero address");
uint256 _nonce = nonces[_owner];
bytes32 _hashStruct = keccak256(
abi.encode(_PERMIT_TYPEHASH, _owner, _spender, _value, _nonce, _deadline)
);
bytes32 _digest = keccak256(
abi.encodePacked(_EIP712_PREFIX_AND_VERSION, domainSeparator(), _hashStruct)
);
address _signer = ecrecover(_digest, _v, _r, _s);
require(_signer == _owner, "ERC20Permit: invalid signature");
nonces[_owner] = _nonce + 1;
_approve(_owner, _spender, _value);
}
/**
* @dev Returns the name of the token.
*/
function name() public view override returns (string memory) {
return token.name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view override returns (string memory) {
return token.symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
* called.
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view override returns (uint8) {
return token.decimals;
}
// ======= PERMIT =======
/// @dev This is ALWAYS calculated at runtime because the token name may
/// change.
function domainSeparator() public view returns (bytes32) {
uint256 _chainId;
assembly {
_chainId := chainid()
}
return
keccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
),
keccak256(bytes(token.name)),
_EIP712_STRUCTURED_DATA_VERSION,
_chainId,
address(this)
)
);
}
}