|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.12; |
| 4 | + |
| 5 | +import "./OldOFTCore.sol"; |
| 6 | +import "../../interfaces/IAgTokenSideChainMultiBridge.sol"; |
| 7 | +import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol"; |
| 8 | +import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol"; |
| 9 | + |
| 10 | +/// @title LayerZeroBridgeToken |
| 11 | +/// @author Angle Labs, Inc., forked from https://github.com/LayerZero-Labs/solidity-examples/blob/main/contracts/token/oft/OFT.sol |
| 12 | +/// @notice Contract to be deployed on a L2/sidechain for bridging an AgToken using a bridge intermediate token and LayerZero |
| 13 | +contract OldLayerZeroBridgeToken is OldOFTCore, ERC20Upgradeable, PausableUpgradeable { |
| 14 | + /// @notice Address of the bridgeable token |
| 15 | + /// @dev Immutable |
| 16 | + IAgTokenSideChainMultiBridge public canonicalToken; |
| 17 | + |
| 18 | + // =============================== Errors ================================ |
| 19 | + |
| 20 | + error InvalidAllowance(); |
| 21 | + |
| 22 | + // ============================= Constructor =================================== |
| 23 | + |
| 24 | + /// @notice Initializes the contract |
| 25 | + /// @param _name Name of the token corresponding to this contract |
| 26 | + /// @param _symbol Symbol of the token corresponding to this contract |
| 27 | + /// @param _lzEndpoint Layer zero endpoint to pass messages |
| 28 | + /// @param _treasury Address of the treasury contract used for access control |
| 29 | + /// @param initialSupply Initial supply to mint to the canonical token address |
| 30 | + /// @dev The initial supply corresponds to the initial amount that could be bridged using this OFT |
| 31 | + function initialize( |
| 32 | + string memory _name, |
| 33 | + string memory _symbol, |
| 34 | + address _lzEndpoint, |
| 35 | + address _treasury, |
| 36 | + uint256 initialSupply |
| 37 | + ) external initializer { |
| 38 | + __ERC20_init_unchained(_name, _symbol); |
| 39 | + __LzAppUpgradeable_init(_lzEndpoint, _treasury); |
| 40 | + |
| 41 | + canonicalToken = IAgTokenSideChainMultiBridge(address(ITreasury(_treasury).stablecoin())); |
| 42 | + _approve(address(this), address(canonicalToken), type(uint256).max); |
| 43 | + _mint(address(canonicalToken), initialSupply); |
| 44 | + } |
| 45 | + |
| 46 | + /// @custom:oz-upgrades-unsafe-allow constructor |
| 47 | + constructor() initializer {} |
| 48 | + |
| 49 | + // ==================== External Permissionless Functions ====================== |
| 50 | + |
| 51 | + function sendWithPermit( |
| 52 | + uint16 _dstChainId, |
| 53 | + bytes memory _toAddress, |
| 54 | + uint256 _amount, |
| 55 | + address payable _refundAddress, |
| 56 | + address _zroPaymentAddress, |
| 57 | + bytes memory _adapterParams, |
| 58 | + uint256 deadline, |
| 59 | + uint8 v, |
| 60 | + bytes32 r, |
| 61 | + bytes32 s |
| 62 | + ) public payable override { |
| 63 | + canonicalToken.permit(msg.sender, address(this), _amount, deadline, v, r, s); |
| 64 | + send(_dstChainId, _toAddress, _amount, _refundAddress, _zroPaymentAddress, _adapterParams); |
| 65 | + } |
| 66 | + |
| 67 | + function withdraw(uint256 amount, address recipient) external override returns (uint256 amountMinted) { |
| 68 | + // Does not check allowances as transfers from `msg.sender` |
| 69 | + _transfer(msg.sender, address(this), amount); |
| 70 | + amountMinted = canonicalToken.swapIn(address(this), amount, recipient); |
| 71 | + uint256 leftover = balanceOf(address(this)); |
| 72 | + if (leftover != 0) { |
| 73 | + _transfer(address(this), msg.sender, leftover); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // ============================= Internal Functions =================================== |
| 78 | + |
| 79 | + function _debitFrom( |
| 80 | + uint16, |
| 81 | + bytes memory, |
| 82 | + uint256 _amount |
| 83 | + ) internal override whenNotPaused returns (uint256 amountSwapped) { |
| 84 | + // No need to use safeTransferFrom as we know this implementation reverts on failure |
| 85 | + canonicalToken.transferFrom(msg.sender, address(this), _amount); |
| 86 | + |
| 87 | + // Swap canonical for this bridge token. There may be some fees |
| 88 | + amountSwapped = canonicalToken.swapOut(address(this), _amount, address(this)); |
| 89 | + _burn(address(this), amountSwapped); |
| 90 | + } |
| 91 | + |
| 92 | + function _debitCreditFrom(uint16, bytes memory, uint256 _amount) internal override whenNotPaused returns (uint256) { |
| 93 | + _burn(msg.sender, _amount); |
| 94 | + return _amount; |
| 95 | + } |
| 96 | + |
| 97 | + function _creditTo( |
| 98 | + uint16, |
| 99 | + address _toAddress, |
| 100 | + uint256 _amount |
| 101 | + ) internal override whenNotPaused returns (uint256 amountMinted) { |
| 102 | + _mint(address(this), _amount); |
| 103 | + amountMinted = canonicalToken.swapIn(address(this), _amount, _toAddress); |
| 104 | + uint256 leftover = balanceOf(address(this)); |
| 105 | + if (leftover != 0) { |
| 106 | + _transfer(address(this), _toAddress, leftover); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // ======================= View Functions ================================ |
| 111 | + |
| 112 | + /// @inheritdoc ERC165Upgradeable |
| 113 | + function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { |
| 114 | + return |
| 115 | + interfaceId == type(IOFT).interfaceId || |
| 116 | + interfaceId == type(IERC20).interfaceId || |
| 117 | + super.supportsInterface(interfaceId); |
| 118 | + } |
| 119 | + |
| 120 | + // ======================= Governance Functions ================================ |
| 121 | + |
| 122 | + /// @notice Mints the intermediate contract to the `canonicalToken` |
| 123 | + /// @dev Used to increase the bridging capacity |
| 124 | + function mint(uint256 amount) external onlyGovernorOrGuardian { |
| 125 | + _mint(address(canonicalToken), amount); |
| 126 | + } |
| 127 | + |
| 128 | + /// @notice Burns the intermediate contract from the `canonicalToken` |
| 129 | + /// @dev Used to decrease the bridging capacity |
| 130 | + function burn(uint256 amount) external onlyGovernorOrGuardian { |
| 131 | + _burn(address(canonicalToken), amount); |
| 132 | + } |
| 133 | + |
| 134 | + /// @notice Increases allowance of the `canonicalToken` |
| 135 | + function setupAllowance() public onlyGovernorOrGuardian { |
| 136 | + _approve(address(this), address(canonicalToken), type(uint256).max); |
| 137 | + } |
| 138 | + |
| 139 | + /// @notice Pauses bridging through the contract |
| 140 | + /// @param pause Future pause status |
| 141 | + function pauseSendTokens(bool pause) external onlyGovernorOrGuardian { |
| 142 | + pause ? _pause() : _unpause(); |
| 143 | + } |
| 144 | + |
| 145 | + uint256[49] private __gap; |
| 146 | +} |
0 commit comments