Contract 0x98e119990E3653486d84Ba46b66BbC4d82f7f604

Txn Hash Method
Block
From
To
Value [Txn Fee]
0xcebea1efa03e87ce9011b0053fb0b7ea375fa48b33a89403f089338bf4f99d3bMint395569922022-06-01 17:42:5227 days 21 hrs ago0x2b0eb0848b9eecf6955c033bdcb243907da0362b IN  0x98e119990e3653486d84ba46b66bbc4d82f7f604330 FTM0.014505577073
0x979a2a9dbd75da7ee2b009f79160b09899b7a30666a94d85cfe1bd13bc759ba2Collect Redempti...371762952022-04-28 23:30:3961 days 16 hrs ago0x9f2f589c15b8f3fd705c78a272091fc66529417f IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6040 FTM0.064157689796
0x2762b9eafde92da6592641bda84cf91fab2aae1ce1b0677bbd76f78cff5a8a89Mint369630362022-04-26 0:04:2764 days 15 hrs ago0x1f381f47b9c7c5216f346bc22501a47a61ea3448 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6045 FTM0.049230616072
0x4ddfbdfb1d0ec65f8135b9589251b380f473165f11069ea2093570fb07c6a24dMint356121342022-04-09 7:23:5781 days 8 hrs ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.027130280511
0x2ba5b72c2738ea217caa437b00571c163588e8ccdf6fd357298d587e29ab0c9eMint356121032022-04-09 7:23:2781 days 8 hrs ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.029836201011
0xde462d3f58c8509a645c314089014c9715f194ff3ca4b55bbce53d02a647e336Mint354698462022-04-07 15:11:4283 days 22 mins ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.042858067577
0x05539af8abb8ed401ff15fe388285fc2ba3a2bfd7e381d4244dd66816908a010Mint353656092022-04-06 8:30:3084 days 7 hrs ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6042 FTM0.051017780567
0xf376e000bd19b0ac59de144b9c473a589e8f9e3e536d5986ce3a6a76eb1554bfMint353650132022-04-06 8:19:1684 days 7 hrs ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6042 FTM0.036760985892
0xdbd49acf2c16e9dd3a5e21143486dd44df2dab2d3f362b8444152c9fc60106f0Mint353645912022-04-06 8:12:3184 days 7 hrs ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.035752488847
0xc0dd1ff43d2595d21fc22f6e6cba410480cd5887a3949f98b952afaa43f95683Mint347472792022-03-29 23:47:1891 days 15 hrs ago0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6042 FTM0.029347135129
0xd12b7bf4595e01c11413805fbb24d6a1ef8a6b44f2f92508c781dc9b14ab5615Redeem331092952022-03-11 4:54:00110 days 10 hrs ago0x09852a0d95bace7857ceccaedb5b366c58e01fee IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6040 FTM0.45234
0x85d819dce1f83b111fc624985f8dd2304823bf8832601ea7ecee05ab846c91d4Mint311541442022-02-16 16:58:04132 days 22 hrs ago0x6ea8d23189ae68f1423c6fc8f93b602b5c0524a7 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.044433294349
0xb891395709e99bbf41230c987a38a45178273c36b3a5db47decfe7c63be2d0f1Mint308055162022-02-13 3:25:23136 days 12 hrs ago0xa9b7f513deca109d2271dff29aedd08934d5a84f IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.020784038537
0xfd552e029d5bbf5717b6e800bfc063b7ebb5a6d6482428745caed6d5cab84a0cMint307437162022-02-12 12:45:15137 days 2 hrs ago0x88151e6f02169457b8eeaa57a12817a0c80b4365 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6045 FTM0.023140636752
0xcc5c4a6337872a458bee6b5fa2ba7dacb8c160e0a36a4224debe1329c87508c7Mint293489412022-01-29 4:32:07151 days 11 hrs ago0x1d4e027c56f870c27c660c4a201735afb142b924 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6046.335123 FTM0.039854247907
0x14b5e6f589e201c6493acf858bd0563fa37dc62efab708fdf2135569427e84d1Mint284157862022-01-19 6:21:14161 days 9 hrs ago0x581cca9da44a6e353f2b35ae06df0108c9a162ca IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6044.991339 FTM0.065707066744
0xc458f6c04f4e1cec9632843c30f7a1c924e885e26a0cfba64e31f0bebdf3ef49Mint284049032022-01-19 3:54:14161 days 11 hrs ago0xa6d873a90cd349f9905d09019c6cc7e03140bbc4 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f60420 FTM0.064268684744
0xdcbf25c7dbfbe14ca51a231934eca68deceac5c1123bed8889c01f0551763848Mint283009102022-01-18 2:44:25162 days 12 hrs ago0x35869a674086d32251aa1836ee38faa663917c67 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f60422.3 FTM0.043008173414
0x350c54d4f55eb8e6da483462e0da35d7edcdbc6871a30a3df847dbf389778e16Mint282500682022-01-17 14:41:38163 days 52 mins ago0xa47807b8eaf211b7c553f83cb977314e7be3892a IN  0x98e119990e3653486d84ba46b66bbc4d82f7f604441 FTM0.046480907371
0xa1867f3113bd0534d363cf3f21a5fcb08ac1b0524a0275b26ebd8153e7096c77Mint281849842022-01-17 0:00:34163 days 15 hrs ago0xc109c45330576048b35686275372e8a84daed552 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f60465 FTM0.105222868908
0x1b94c69397ea72d17949a6584aa964818ddb32272910f45f02a57e18e7fc06d0Mint281800762022-01-16 22:42:52163 days 16 hrs ago0x5de493f86a0859810f8172177174c0f9c1c63b1e IN  0x98e119990e3653486d84ba46b66bbc4d82f7f604100 FTM0.099788154384
0x8b65a1dd1338a1fd7921199c38eb9f0ade1323fb2c57807088a5f024a164cb21Mint281712412022-01-16 20:41:01163 days 18 hrs ago0xbe12344e4dfac5afb79010ad934c0935d36e5910 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f60410 FTM0.082270151164
0xeefbc6f6fb2df7c05d71283f6118b3673cbe54735b608f0103725d813f7641c4Mint281612682022-01-16 18:14:43163 days 21 hrs ago0x225f6fe1893132da384ca17be176c94a2b884519 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f60410 FTM0.055127573376
0x9398231ee1726f82ab2ef56bc594921db9e9ea862ff0cf885126e1f07f4be410Mint281464672022-01-16 14:51:34164 days 42 mins ago0x210ade94de97e7a85de69ff68fe9cfb221357af1 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM0.076330805196
0x01e712af23227863f2519361b18fe6bcbc32c12591fe9f658803591c035334d9Mint281302292022-01-16 10:58:09164 days 4 hrs ago0xa219712cc2aaa5aa98ccf2a7ba055231f1752323 IN  0x98e119990e3653486d84ba46b66bbc4d82f7f6049 FTM0.084367082454
[ Download CSV Export 
Latest 25 internal transaction
Parent Txn Hash Block From To Value
0xcebea1efa03e87ce9011b0053fb0b7ea375fa48b33a89403f089338bf4f99d3b395569922022-06-01 17:42:5227 days 21 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token330 FTM
0x979a2a9dbd75da7ee2b009f79160b09899b7a30666a94d85cfe1bd13bc759ba2371762952022-04-28 23:30:3961 days 16 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f6040x9f2f589c15b8f3fd705c78a272091fc66529417f2.010045345879418053 FTM
0x979a2a9dbd75da7ee2b009f79160b09899b7a30666a94d85cfe1bd13bc759ba2371762952022-04-28 23:30:3961 days 16 hrs ago Fantom Finance: Wrapped Fantom Token 0x98e119990e3653486d84ba46b66bbc4d82f7f6042.010045345879418053 FTM
0x2762b9eafde92da6592641bda84cf91fab2aae1ce1b0677bbd76f78cff5a8a89369630362022-04-26 0:04:2764 days 15 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token5 FTM
0x4ddfbdfb1d0ec65f8135b9589251b380f473165f11069ea2093570fb07c6a24d356121342022-04-09 7:23:5781 days 8 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token1 FTM
0x4ddfbdfb1d0ec65f8135b9589251b380f473165f11069ea2093570fb07c6a24d356121342022-04-09 7:23:5781 days 8 hrs ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM
0x2ba5b72c2738ea217caa437b00571c163588e8ccdf6fd357298d587e29ab0c9e356121032022-04-09 7:23:2781 days 8 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token1 FTM
0x2ba5b72c2738ea217caa437b00571c163588e8ccdf6fd357298d587e29ab0c9e356121032022-04-09 7:23:2781 days 8 hrs ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM
0xde462d3f58c8509a645c314089014c9715f194ff3ca4b55bbce53d02a647e336354698462022-04-07 15:11:4283 days 22 mins ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token1 FTM
0xde462d3f58c8509a645c314089014c9715f194ff3ca4b55bbce53d02a647e336354698462022-04-07 15:11:4283 days 22 mins ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM
0x05539af8abb8ed401ff15fe388285fc2ba3a2bfd7e381d4244dd66816908a010353656092022-04-06 8:30:3084 days 7 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token2 FTM
0x05539af8abb8ed401ff15fe388285fc2ba3a2bfd7e381d4244dd66816908a010353656092022-04-06 8:30:3084 days 7 hrs ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6042 FTM
0xf376e000bd19b0ac59de144b9c473a589e8f9e3e536d5986ce3a6a76eb1554bf353650132022-04-06 8:19:1684 days 7 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token2 FTM
0xf376e000bd19b0ac59de144b9c473a589e8f9e3e536d5986ce3a6a76eb1554bf353650132022-04-06 8:19:1684 days 7 hrs ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6042 FTM
0xdbd49acf2c16e9dd3a5e21143486dd44df2dab2d3f362b8444152c9fc60106f0353645912022-04-06 8:12:3184 days 7 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token1 FTM
0xdbd49acf2c16e9dd3a5e21143486dd44df2dab2d3f362b8444152c9fc60106f0353645912022-04-06 8:12:3184 days 7 hrs ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM
0xc0dd1ff43d2595d21fc22f6e6cba410480cd5887a3949f98b952afaa43f95683347472792022-03-29 23:47:1891 days 15 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token2 FTM
0xc0dd1ff43d2595d21fc22f6e6cba410480cd5887a3949f98b952afaa43f95683347472792022-03-29 23:47:1891 days 15 hrs ago 0xcc93f2ebe4b9c4bd0fa5e579c55afc19e660e30a 0x98e119990e3653486d84ba46b66bbc4d82f7f6042 FTM
0x85d819dce1f83b111fc624985f8dd2304823bf8832601ea7ecee05ab846c91d4311541442022-02-16 16:58:04132 days 22 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token1 FTM
0x85d819dce1f83b111fc624985f8dd2304823bf8832601ea7ecee05ab846c91d4311541442022-02-16 16:58:04132 days 22 hrs ago 0x6ea8d23189ae68f1423c6fc8f93b602b5c0524a7 0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM
0xb891395709e99bbf41230c987a38a45178273c36b3a5db47decfe7c63be2d0f1308055162022-02-13 3:25:23136 days 12 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token1 FTM
0xb891395709e99bbf41230c987a38a45178273c36b3a5db47decfe7c63be2d0f1308055162022-02-13 3:25:23136 days 12 hrs ago 0xa9b7f513deca109d2271dff29aedd08934d5a84f 0x98e119990e3653486d84ba46b66bbc4d82f7f6041 FTM
0xfd552e029d5bbf5717b6e800bfc063b7ebb5a6d6482428745caed6d5cab84a0c307437162022-02-12 12:45:15137 days 2 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token5 FTM
0xfd552e029d5bbf5717b6e800bfc063b7ebb5a6d6482428745caed6d5cab84a0c307437162022-02-12 12:45:15137 days 2 hrs ago 0x88151e6f02169457b8eeaa57a12817a0c80b4365 0x98e119990e3653486d84ba46b66bbc4d82f7f6045 FTM
0xcc5c4a6337872a458bee6b5fa2ba7dacb8c160e0a36a4224debe1329c87508c7293489412022-01-29 4:32:07151 days 11 hrs ago 0x98e119990e3653486d84ba46b66bbc4d82f7f604 Fantom Finance: Wrapped Fantom Token6.335123 FTM
[ Download CSV Export 
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
Gate

Compiler Version
v0.8.7+commit.e28d00a7

Optimization Enabled:
Yes with 500 runs

Other Settings:
default evmVersion

Contract Source Code (Solidity Standard Json-Input format)

File 1 of 24 : Gate.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";

import "./interfaces/IPoolToken.sol";
import "./CoffinOracle.sol";
import "./interfaces/IGatePolicy.sol";

import "./interfaces/IGate.sol";
import "./interfaces/ICollateralReserve.sol";
import "./interfaces/IWETH.sol";

contract Gate is Ownable, Initializable, IGate, ReentrancyGuard {
    using SafeERC20 for ERC20;
    using SafeMath for uint256;
    using Address for address;

    address public oracle;
    address public collateral;
    address public dollar;
    address public policy;
    address public share;

    address public collateralReserve;

    mapping(address => uint256) public redeem_share_balances;
    mapping(address => uint256) public redeem_collateral_balances;

    uint256 public override unclaimed_pool_collateral;
    uint256 public unclaimed_pool_share;

    mapping(address => uint256) public last_redeemed;

    // Constants for various precisions
    uint256 private constant PRICE_PRECISION = 1e6;
    uint256 private constant RATIO_PRECISION = 1e6;

    uint256 private constant COLLATERAL_RATIO_PRECISION = 1e6;
    uint256 private constant COLLATERAL_RATIO_MAX = 1e6;
    uint256 private constant LIMIT_SWAP_TIME = 10 minutes;
    
    // 

    // wrapped ftm
    address private wftmAddress = 0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83;

    uint256 private missing_decimals;

    // AccessControl state variables
    bool public mint_paused = false;
    bool public redeem_paused = false;
    
    // router address. it's spooky router by default. 
    address routerAddress = 0xF491e7B69E4244ad4002BC14e878a34207E38c29;

        
    /* ========== MODIFIERS ========== */

    modifier notContract() {
        require(!msg.sender.isContract(), "Allow non-contract only");
        _;
    }

    /* ========== CONSTRUCTOR ========== */

    function init(
        address _oracle,
        address _dollar,
        address _share,
        address _collateral,
        address _collateralReserve,
        address _policy
    ) external initializer onlyOwner {
        oracle = _oracle;
        dollar = _dollar;
        share = _share;
        collateral = _collateral;
        missing_decimals = 18 - ERC20(_collateral).decimals();
        collateralReserve = _collateralReserve;
        policy = _policy;
    }

    /* ========== VIEWS ========== */

    function getCollateralPrice() public view override returns (uint256) {
        (uint256 __price, uint8 __d) = ICoffinOracle(oracle).getFTMUSD();
        return __price.mul(PRICE_PRECISION).div(10**__d);
    }

    function getDollarPrice() public view override returns (uint256) {
        (uint256 __price, uint8 __d) = ICoffinOracle(oracle).getCOUSDUSD();
        return __price.mul(PRICE_PRECISION).div(10**__d);
    }

    function getCoffinPrice() public view override returns (uint256) {
        (uint256 __price, uint8 __d) = ICoffinOracle(oracle).getCOFFINUSD();
        return __price.mul(PRICE_PRECISION).div(10**__d);
    }


    // function getCollateralTwap() public view override returns (uint256) {
    //     (uint256 __price, uint8 __d) = ICoffinOracle(oracle).getTwapFTMUSD();
    //     return __price.mul(PRICE_PRECISION).div(10**__d);
    // }

    function getDollarTwap() public view override returns (uint256) {
        (uint256 __price, uint8 __d) = ICoffinOracle(oracle).getTwapCOUSDUSD();
        return __price.mul(PRICE_PRECISION).div(10**__d);
    }

    function getCoffinTwap() public view override returns (uint256) {
        (uint256 __price, uint8 __d) = ICoffinOracle(oracle).getTwapCOFFINUSD();
        return __price.mul(PRICE_PRECISION).div(10**__d);
    }

    /* ========== PUBLIC FUNCTIONS ========== */

    function gateInfo()
        public
        view
        returns (
            uint256 _minting_fee,
            uint256 _redemption_fee,
            uint256 _ex_red_fee,
            uint256 _collateral_price,
            uint256 _share_price,
            uint256 _dollar_price,
            uint256 _share_twap,
            uint256 _dollar_twap,
            // uint256 _ecr,
            uint256 _tcr,
            bool _mint_paused,
            bool _redeem_paused,
            uint256 _unclaimed_pool_collateral,
            uint256 _unclaimed_pool_share
        )
    {
        _minting_fee = IGatePolicy(policy).minting_fee();
        _redemption_fee = IGatePolicy(policy).redemption_fee();
        _ex_red_fee = IGatePolicy(policy).extra_redemption_fee();
        _collateral_price = getCollateralPrice();
        _share_price = getCoffinPrice();
        _dollar_price = getDollarPrice();
        _share_twap = getCoffinTwap();
        _dollar_twap = getDollarTwap();

        _tcr = IGatePolicy(policy).target_collateral_ratio();
        // _ecr = IGatePolicy(policy).getEffectiveCollateralRatio();

        _mint_paused = mint_paused;
        _redeem_paused = redeem_paused;

        _unclaimed_pool_collateral = unclaimed_pool_collateral;
        _unclaimed_pool_share = unclaimed_pool_share;

    }

    // function setWFTMAddress(address adr) external onlyOwner {
    //     wftmAddress = adr;
    // }

    receive() external payable {}

    function rescueFund() external onlyOwner {
        uint256 amount = ERC20(collateral).balanceOf(collateralReserve);
        _requestTransferCollateralFTM(msg.sender, amount);
    }

    function redeem(
        uint256 _dollar_amount,
        uint256 _share_out_min,
        uint256 _collateral_out_min
    ) external nonReentrant {
        uint256 _share_price = 0;
        uint256 _dollar_price = 0;
        if (IGatePolicy(policy).using_twap_for_redeem()) { 
            _share_price = getCoffinTwap();
            _dollar_price = getDollarTwap();
        }
        if (_share_price==0 ) {
            _share_price = getCoffinPrice();
        }
        if (_dollar_price==0) {
            _dollar_price = getDollarPrice();
        }

        uint256 _redemption_fee = IGatePolicy(policy).redemption_fee();
        uint256 extra_redemption_fee = IGatePolicy(policy).extra_redemption_fee();
        uint256 price_target = IGatePolicy(policy).price_target();

        // uint256 _redemption_fee = redemption_fee;
        if (_dollar_price < price_target) {
            _redemption_fee += extra_redemption_fee;
        }

        uint256 _ecr = IGatePolicy(policy).getEffectiveCollateralRatio();

        uint256 _collateral_price = getCollateralPrice();
        require(_collateral_price > 0, "Invalid collateral price");
        require(_share_price > 0, "Invalid share price");
        uint256 _dollar_amount_post_fee = _dollar_amount - ((_dollar_amount * _redemption_fee) / PRICE_PRECISION);
        uint256 _collateral_output_amount = 0;
        uint256 _share_output_amount = 0;

        if (_ecr < COLLATERAL_RATIO_MAX) {
            uint256 _share_output_value 
                = _dollar_amount_post_fee - ((_dollar_amount_post_fee * _ecr) / PRICE_PRECISION);
            _share_output_amount 
                = (_share_output_value * PRICE_PRECISION) / _share_price;
        }

        if (_ecr > 0) {
            uint256 _collateral_output_value 
                = ((_dollar_amount_post_fee * _ecr) / PRICE_PRECISION) / (10**missing_decimals);
            _collateral_output_amount 
                = (_collateral_output_value * PRICE_PRECISION) / _collateral_price;
        }

        // Check if collateral balance meets and meet output expectation
        uint256 _totalCollateralBalance = globalCollateralBalance();
        require(_collateral_output_amount <= _totalCollateralBalance, "<collateralBalance");
        require(_collateral_out_min <= _collateral_output_amount , ">> slippage than expected...");
        require(_share_out_min <= _share_output_amount, ">> slippage than expected......");

        if (_collateral_output_amount > 0) {
            redeem_collateral_balances[msg.sender] = redeem_collateral_balances[msg.sender] + _collateral_output_amount;
            unclaimed_pool_collateral = unclaimed_pool_collateral + _collateral_output_amount;
        }
        
        if (_share_output_amount > 0) {
            redeem_share_balances[msg.sender] = redeem_share_balances[msg.sender] + _share_output_amount;
            unclaimed_pool_share = unclaimed_pool_share + _share_output_amount;
        }

        last_redeemed[msg.sender] = block.timestamp;

        uint256 dollar_amount = _dollar_amount;
        IPoolToken(dollar).pool_burn_from(msg.sender, dollar_amount);
        if (_share_output_amount > 0) {
            _mintShareToCollateralReserve(_share_output_amount);
        }
    }

    // mint CoUSD(dollar) by  COFFIN(share) & FTM(collateral)
    function mint(uint256 _share_amount, uint256 _dollar_out_min) external payable nonReentrant {
        require(mint_paused == false, "Minting is paused");
        require(msg.value > 0, "need FTM");

        uint256 _collateral_amount = msg.value;

        uint256 _minting_fee = IGatePolicy(policy).minting_fee();

        uint256 _share_price = 0;
        if (IGatePolicy(policy).using_twap_for_mint()) {
            _share_price = getCoffinTwap();
        }
        if (_share_price==0) {
            _share_price = getCoffinPrice();
        }
    
        uint256 _tcr = IGatePolicy(policy).target_collateral_ratio();
        uint256 _price_collateral = getCollateralPrice();
        uint256 _total_dollar_value = 0;
        uint256 _required_share_amount = 0;
        if (_tcr > 0) {
            uint256 _collateral_value 
                = ((_collateral_amount * (10**missing_decimals)) * _price_collateral) / PRICE_PRECISION;
            _total_dollar_value = (_collateral_value * COLLATERAL_RATIO_PRECISION) / _tcr;
            if (_tcr < COLLATERAL_RATIO_MAX) {
                // 0 < _tcr <100
                require(_share_price > 0, "Invalid share price");
                _required_share_amount = ((_total_dollar_value - _collateral_value) * PRICE_PRECISION) / _share_price;
            }
        } else {
            // _tcr == 0
            require(_share_price > 0, "Invalid share price");
            _total_dollar_value = (_share_amount * _share_price) / PRICE_PRECISION;
            _required_share_amount = _share_amount;
        }
        uint256 _actual_dollar_amount = _total_dollar_value - ((_total_dollar_value * _minting_fee) / PRICE_PRECISION);
        require(_dollar_out_min <= _actual_dollar_amount, 
            "_actual_dollar_amount is smaller than _dollar_out_min. slippage is bigger than you expected.  ");

        if (_required_share_amount > 0) {
            require(_required_share_amount <= _share_amount, "Not enough SHARE input");
            IPoolToken(share).pool_burn_from(msg.sender, _required_share_amount);
        }
        if (_collateral_amount > 0) {
            IWETH(wftmAddress).deposit{value: _collateral_amount}();
            _transferCollateralToReserve(_collateral_amount);
        }

        IPoolToken(dollar).pool_mint(msg.sender, _actual_dollar_amount);

    }

    function collectRedemption() external nonReentrant {
        uint256 redemption_delay = IGatePolicy(policy).redemption_delay();
        require((last_redeemed[msg.sender] + redemption_delay) <= block.timestamp, "<redemption_delay");
        
        // update 
        // IGatePolicy(policy).refreshCollateralRatio(true);

        bool _send_share = false;
        bool _send_collateral = false;
        uint256 _share_amount;
        uint256 _collateral_amount;

        // Use Checks-Effects-Interactions pattern
        if (redeem_share_balances[msg.sender] > 0) {
            _share_amount = redeem_share_balances[msg.sender];
            redeem_share_balances[msg.sender] = 0;
            unclaimed_pool_share = unclaimed_pool_share - _share_amount;
            _send_share = true;
        }

        if (redeem_collateral_balances[msg.sender] > 0) {
            _collateral_amount = redeem_collateral_balances[msg.sender];
            redeem_collateral_balances[msg.sender] = 0;
            unclaimed_pool_collateral = unclaimed_pool_collateral - _collateral_amount;
            _send_collateral = true;
        }

        if (_send_share) {
            _requestTransferShare(msg.sender, _share_amount);
        }

        if (_send_collateral) {
            _requestTransferCollateralFTM(msg.sender, _collateral_amount);
        }
    }

    /* ========== INTERNAL FUNCTIONS ========== */

    // transfer collateral(wftm) from a user to reserve directly by transferFrom.
    function _transferWFTMCollateralToReserve(address _sender, uint256 _amount) internal {
        require(collateralReserve != address(0), "Invalid reserve address");
        ERC20(collateral).safeTransferFrom(_sender, collateralReserve, _amount);
    }

    // transfer collateral(wftm) from the gate to reserve.
    function _transferCollateralToReserve(uint256 _amount) internal {
        require(collateralReserve != address(0), "Invalid reserve address");
        ERC20(collateral).safeTransfer(collateralReserve, _amount);
    }

    // mint share(ERC20) to reserve.
    function _mintShareToCollateralReserve(uint256 _amount) internal {
        require(collateralReserve != address(0), "Invalid reserve address");
        IPoolToken(share).pool_mint(collateralReserve, _amount);
    }

    // transfer collateral(wftm) from reserve to the gate.
    // then convert wftm to ftm, then transfer it to a user.
    function _requestTransferCollateralFTM(address to, uint256 amount) internal {
        require(to != address(0), "Invalid reserve address");
        ICollateralReserve(collateralReserve).transferTo(collateral, address(this), amount);
        IWETH(wftmAddress).withdraw(amount);
        payable(to).transfer(amount);
    }

    // transfer collateral(wftm) from reserve to a user.
    function _requestTransferCollateralWrappedFTM(address _receiver, uint256 _amount) internal {
        ICollateralReserve(collateralReserve).transferTo(collateral, _receiver, _amount);
    }

    // transfer share(ERC20) from reserve to users.
    function _requestTransferShare(address _receiver, uint256 _amount) internal {
        ICollateralReserve(collateralReserve).transferTo(share, _receiver, _amount);
    }

    /* ========== RESTRICTED FUNCTIONS ========== */

    function toggleMinting() external onlyOwner {
        mint_paused = !mint_paused;
    }

    function toggleRedeeming() external onlyOwner {
        redeem_paused = !redeem_paused;
    }


    function setOracle(address _oracle) external onlyOwner {
        require(_oracle != address(0), "Invalid address");
        oracle = _oracle;
    }


    function setPolicy(address _policy) external onlyOwner {
        require(_policy != address(0), "Invalid address");
        policy = _policy;
    }

    function getDollarSupply() public view override returns (uint256) {
        return IERC20(dollar).totalSupply();
    }

    function getCoffinSupply() public view override returns (uint256) {
        return IERC20(share).totalSupply();
    }

    function globalCollateralValue() public view override returns (uint256) {
        return (globalCollateralBalance() * getCollateralPrice() * (10**missing_decimals)) / PRICE_PRECISION;
    }

    function globalCollateralBalance() public view override returns (uint256) {
        uint256 _collateralReserveBalance = IERC20(collateral).balanceOf(collateralReserve);
        return _collateralReserveBalance - unclaimed_pool_collateral;
    }

    function setCollateralReserve(address _collateralReserve) public onlyOwner {
        require(_collateralReserve != address(0), "invalidAddress");
        collateralReserve = _collateralReserve;
    }

    function getCollateralBalance() public view override returns (uint256) {
        return IERC20(collateral).balanceOf(collateralReserve);
    }


    event ZapSwapped(uint256 indexed collateralAmount, uint256 indexed shareAmount);

}

File 2 of 24 : ERC20.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * We have followed general OpenZeppelin guidelines: functions revert instead
 * of returning `false` on failure. This behavior is nonetheless conventional
 * and does not conflict with the expectations of ERC20 applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20 is Context, IERC20, IERC20Metadata {
    mapping(address => uint256) private _balances;

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

    uint256 private _totalSupply;

    string private _name;
    string private _symbol;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * The default value of {decimals} is 18. To select a different value for
     * {decimals} you should overload it.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

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

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _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 this function is
     * overridden;
     *
     * 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 virtual override returns (uint8) {
        return 18;
    }

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

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

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

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

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

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * 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
    ) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][_msgSender()];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        unchecked {
            _approve(sender, _msgSender(), currentAllowance - amount);
        }

        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 {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + 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 {IERC20-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 virtual returns (bool) {
        uint256 currentAllowance = _allowances[_msgSender()][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        unchecked {
            _approve(_msgSender(), spender, currentAllowance - subtractedValue);
        }

        return true;
    }

    /**
     * @dev Moves `amount` of tokens from `sender` to `recipient`.
     *
     * This 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 virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");

        _beforeTokenTransfer(sender, recipient, amount);

        uint256 senderBalance = _balances[sender];
        require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
        unchecked {
            _balances[sender] = senderBalance - amount;
        }
        _balances[recipient] += amount;

        emit Transfer(sender, recipient, amount);

        _afterTokenTransfer(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:
     *
     * - `account` cannot be the zero address.
     */
    function _mint(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: mint to the zero address");

        _beforeTokenTransfer(address(0), account, amount);

        _totalSupply += amount;
        _balances[account] += amount;
        emit Transfer(address(0), account, amount);

        _afterTokenTransfer(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 virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        unchecked {
            _balances[account] = accountBalance - amount;
        }
        _totalSupply -= amount;

        emit Transfer(account, address(0), amount);

        _afterTokenTransfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This 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 virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

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

    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * has been transferred to `to`.
     * - when `from` is zero, `amount` tokens have been minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens have been burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 amount
    ) internal virtual {}
}

File 3 of 24 : SafeERC20.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../IERC20.sol";
import "../../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using Address for address;

    function safeTransfer(
        IERC20 token,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender) + value;
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            uint256 newAllowance = oldAllowance - value;
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
        }
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        if (returndata.length > 0) {
            // Return data is optional
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

File 4 of 24 : Ownable.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _setOwner(_msgSender());
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _setOwner(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _setOwner(newOwner);
    }

    function _setOwner(address newOwner) private {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

File 5 of 24 : ReentrancyGuard.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and make it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

File 6 of 24 : Initializable.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
 * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
 * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
 * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
 *
 * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
 * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
 *
 * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
 * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
 */
abstract contract Initializable {
    /**
     * @dev Indicates that the contract has been initialized.
     */
    bool private _initialized;

    /**
     * @dev Indicates that the contract is in the process of being initialized.
     */
    bool private _initializing;

    /**
     * @dev Modifier to protect an initializer function from being invoked twice.
     */
    modifier initializer() {
        require(_initializing || !_initialized, "Initializable: contract is already initialized");

        bool isTopLevelCall = !_initializing;
        if (isTopLevelCall) {
            _initializing = true;
            _initialized = true;
        }

        _;

        if (isTopLevelCall) {
            _initializing = false;
        }
    }
}

File 7 of 24 : SafeMath.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

// CAUTION
// This version of SafeMath should only be used with Solidity 0.8 or later,
// because it relies on the compiler's built in overflow checks.

/**
 * @dev Wrappers over Solidity's arithmetic operations.
 *
 * NOTE: `SafeMath` is no longer needed starting with Solidity 0.8. The compiler
 * now has built in overflow checking.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            uint256 c = a + b;
            if (c < a) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the substraction of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b > a) return (false, 0);
            return (true, a - b);
        }
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
     *
     * _Available since v3.4._
     */
    function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
            // benefit is lost if 'b' is also tested.
            // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
            if (a == 0) return (true, 0);
            uint256 c = a * b;
            if (c / a != b) return (false, 0);
            return (true, c);
        }
    }

    /**
     * @dev Returns the division of two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a / b);
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
     *
     * _Available since v3.4._
     */
    function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
        unchecked {
            if (b == 0) return (false, 0);
            return (true, a % b);
        }
    }

    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        return a + b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return a - b;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        return a * b;
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator.
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return a / b;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return a % b;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {trySub}.
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b <= a, errorMessage);
            return a - b;
        }
    }

    /**
     * @dev Returns the integer division of two unsigned integers, reverting with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a / b;
        }
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * reverting with custom message when dividing by zero.
     *
     * CAUTION: This function is deprecated because it requires allocating memory for the error
     * message unnecessarily. For custom revert reasons use {tryMod}.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(
        uint256 a,
        uint256 b,
        string memory errorMessage
    ) internal pure returns (uint256) {
        unchecked {
            require(b > 0, errorMessage);
            return a % b;
        }
    }
}

File 8 of 24 : Address.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        assembly {
            size := extcodesize(account)
        }
        return size > 0;
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }

    /**
     * @dev Performs a Solidity function call using a low level `call`. A
     * plain `call` is an unsafe replacement for a function call: use this
     * function instead.
     *
     * If `target` reverts with a revert reason, it is bubbled up by this
     * function (like regular Solidity function calls).
     *
     * Returns the raw returned data. To convert to the expected return value,
     * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
     *
     * Requirements:
     *
     * - `target` must be a contract.
     * - calling `target` with `data` must not revert.
     *
     * _Available since v3.1._
     */
    function functionCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionCall(target, data, "Address: low-level call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
     * `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, 0, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but also transferring `value` wei to `target`.
     *
     * Requirements:
     *
     * - the calling contract must have an ETH balance of at least `value`.
     * - the called Solidity function must be `payable`.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value
    ) internal returns (bytes memory) {
        return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    }

    /**
     * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
     * with `errorMessage` as a fallback revert reason when `target` reverts.
     *
     * _Available since v3.1._
     */
    function functionCallWithValue(
        address target,
        bytes memory data,
        uint256 value,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(address(this).balance >= value, "Address: insufficient balance for call");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
        return functionStaticCall(target, data, "Address: low-level static call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a static call.
     *
     * _Available since v3.3._
     */
    function functionStaticCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal view returns (bytes memory) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
        return functionDelegateCall(target, data, "Address: low-level delegate call failed");
    }

    /**
     * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
     * but performing a delegate call.
     *
     * _Available since v3.4._
     */
    function functionDelegateCall(
        address target,
        bytes memory data,
        string memory errorMessage
    ) internal returns (bytes memory) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return _verifyCallResult(success, returndata, errorMessage);
    }

    function _verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) private pure returns (bytes memory) {
        if (success) {
            return returndata;
        } else {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly

                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
}

File 9 of 24 : IPoolToken.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

interface IPoolToken {
    function pool_burn_from(address _address, uint256 _amount) external;
    function approve(address a, uint256 b) external returns (bool);
    function pool_mint(address _address, uint256 m_amount) external;
}

File 10 of 24 : CoffinOracle.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";

import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/IUniswapV2Router02.sol";
import "./interfaces/IUniswapV2Factory.sol";
import "./interfaces/IUniswapLP.sol";
import "./libs/FixedPoint.sol";
import "./interfaces/IBandStdReference.sol";


interface ICoffinOracle {
    function PERIOD() external view returns (uint32);
    function getCOFFINUSD() external view returns (uint256, uint8);
    function updateTwap(address token0, address token1) external ;
    function getCOUSDUSD() external view returns (uint256, uint8);
    function getTwapCOUSDUSD() external view returns (uint256, uint8);
    function getTwapCOFFINUSD() external view returns (uint256, uint8);
    function getTwapXCOFFINUSD() external view returns (uint256, uint8);
    function updateTwapDollar() external ;
    function updateTwapCoffin() external ;
    function updateTwapXCoffin() external ;
    function getXCOFFINUSD() external view returns (uint256, uint8);
    function getCOUSDFTM() external view returns (uint256, uint8);
    function getXCOFFINFTM() external view returns (uint256, uint8);
    function getCOFFINFTM() external view returns (uint256, uint8);
    function getFTMUSD() external view returns (uint256, uint8);

}

contract MockCoffinOracle is ICoffinOracle, Ownable {
    uint256 public xcoffinftm = (1 / 2) * 1 * 10**18;
    uint256 public coffinftm = 2 * 1 * 10**18;
    uint256 public ftmusd = (1 / 4) * 1 * 10**18;
    uint256 public cousdftm = (101 / 100) * 4 * 1 * 10**18;
    uint32 public override PERIOD = 600; // 10-minute TWAP 

    function updateTwap(address token0, address token1) external override {

    }
    function updateTwapDollar() external override{

    }
    function updateTwapCoffin() external override{

    }
    function updateTwapXCoffin() external override{

    }
    function setCOUSDFTM(uint256 val) external {
        cousdftm = val;
    }

    function getCOUSDFTM() public view override returns (uint256, uint8) {
        return (cousdftm, 18);
    }

    function setXCOFFINFTM(uint256 val) external {
        xcoffinftm = val;
    }

    function getXCOFFINFTM() public view override returns (uint256, uint8) {
        return (xcoffinftm, 18);
    }

    function setCOFFINFTM(uint256 val) external {
        coffinftm = val;
    }

    function getCOFFINFTM() public view override returns (uint256, uint8) {
        return (coffinftm, 18);
    }

 
    uint256 public cousdusd = 1030000000000000000;
    function setCOUSDUSD(uint256 val) external {
        cousdusd = val;// decimal 18 
    }
    function getCOUSDUSD() public view override returns(uint256,uint8){
        return (cousdusd,18);// decimal 18 
    }
    
 
    
    function setFTMUSD(uint256 val) external {
        ftmusd = val;
    }

    function getFTMUSD() public view override returns (uint256, uint8) {
        return (ftmusd, 18);
    }


    function getCOFFINUSD() public view override returns (uint256, uint8) {
        (uint256 v1, uint8 d1) = getCOFFINFTM();
        (uint256 v2, uint8 d2) = getFTMUSD();
        return ((v1 * v2) / (10**d1), d2);
    }

    function getXCOFFINUSD() public view override returns (uint256, uint8) {
        (uint256 v1, uint8 d1) = getXCOFFINFTM();
        (uint256 v2, uint8 d2) = getFTMUSD();
        return ((v1 * v2) / (10**d1), d2);
    }

    function getTwapCOUSDUSD() external view override returns (uint256, uint8){
        return getCOUSDUSD();
    }
    function getTwapCOFFINUSD() external view override returns (uint256, uint8){
        return getCOFFINUSD();
    }
    function getTwapXCOFFINUSD() external view override returns (uint256, uint8){
        return getXCOFFINUSD();
    }

}


contract CoffinOracle is ICoffinOracle, Initializable,Ownable {
    using SafeMath for uint256;
    using FixedPoint for *;

    IUniswapV2Router02 public uniswapv2router;
    address public coffin;
    address public dollar;
    address public xcoffin;
    address public wftm = 0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83;
    address public usdc = 0x04068DA6C83AFCFA0e13ba15A6696662335D5B75;
    address public dai = 0x8D11eC38a3EB5E956B052f67Da8Bdc9bef8Abf3E;
    address public boo = 0x841FAD6EAe12c286d1Fd18d1d525DFfA75C7EFFE;
    IBandStdReference bandRef;
    uint32 public override PERIOD = 600; // 10-minute TWAP

    struct Pair {
        uint256 price0CumulativeLast;
        uint256 price1CumulativeLast;
        uint32 blockTimestampLast;
        FixedPoint.uq112x112 price0Average;
        FixedPoint.uq112x112 price1Average;
        bool initialized;
    }

    mapping(address => Pair) public getPair;

    function setPeriod(uint32 _period) external onlyOwner {
        PERIOD = _period;
    }

    function init(
        address _coffinAddress,
        address _cousdAddress,
        address _xcoffinAddress
    ) external  initializer onlyOwner{
        // router address. it's spooky router by default. 
        address routerAddress = 0xF491e7B69E4244ad4002BC14e878a34207E38c29;
        setRouter(routerAddress);

        address fantomBandProtocol = 0x56E2898E0ceFF0D1222827759B56B28Ad812f92F;

        setBandOracle(fantomBandProtocol);
        setCOFFINAddress(_coffinAddress);
        setDollarAddress(_cousdAddress);
        setXCOFFINAddress(_xcoffinAddress);
    }

    function getBandRate(string memory token0, string memory token1)
        public
        view
        returns (uint256)
    {
        IBandStdReference.ReferenceData memory data = bandRef.getReferenceData(
            token0,
            token1
        );
        return data.rate;
    }


    function getFTMUSD() public view override returns (uint256, uint8) {
        return (getBandRate("FTM","USD"), 18);
    }

    



    function setCOFFINAddress(address _coffinAddress) public onlyOwner {
        coffin = _coffinAddress;
    }

    function setXCOFFINAddress(address _xcoffinAddress) public onlyOwner {
        xcoffin = _xcoffinAddress;
    }

    function setDollarAddress(address _cousdAddress) public onlyOwner {
        dollar = _cousdAddress;
    }

    function setRouter(address _uniswapv2routeraddress)
        public
        onlyOwner
    {
        uniswapv2router = IUniswapV2Router02(_uniswapv2routeraddress);
    }

    function setBandOracle(address _bandOracleAddress) public onlyOwner {
        bandRef = IBandStdReference(_bandOracleAddress);
    }


    function getCOFFINUSD() external view override returns (uint256, uint8) {
        (uint256 v1, uint8 d1) = getCOFFINFTM();
        (uint256 v2, uint8 d2) = getFTMUSD();
        return ((v1 * v2) / (10**d1), d2);
    }

    function getTwapCOFFINUSD() external view override returns (uint256, uint8) {
        (uint256 v1, uint8 d1) = getTwapCOFFINFTM();
        (uint256 v2, uint8 d2) = getFTMUSD();
        return ((v1 * v2) / (10**d1), d2);
    }
    
    function getUSDCUSD() public view  returns (uint256, uint8) {
        return (getBandRate("USDC","USD"), 18);
    }
    function getDAIUSD() public view  returns (uint256, uint8) {
        return (getBandRate("DAI","USD"), 18);
    }
    
    uint8 public oracleMode = 0; 

    function enableFTMOracle() external onlyOwner {
        oracleMode = 1;
    }
    function enableDAIOracle() external onlyOwner {
        oracleMode = 2 ;
    }
    function enableUSDCracle() external onlyOwner {
        oracleMode = 0 ;
    }

    function getCOUSDUSD() external view override returns (uint256, uint8) {
        if (oracleMode==1) {
            (uint256 v1, uint8 d1) = getCOUSDFTM();
            (uint256 v2, uint8 d2) = getFTMUSD();
            return ((v1 * v2) / (10**d1), d2);
        } else if (oracleMode==2) {
            (uint256 v1, uint8 d1) = getCOUSDDAI();
            (uint256 v2, uint8 d2) = getDAIUSD();
            return ((v1 * v2) / (10**d1), d2);
        } else {
            (uint256 v1, uint8 d1) = getCOUSDUSDC();
            (uint256 v2, uint8 d2) = getUSDCUSD();
            return ((v1 * v2) / (10**d1), d2);
        }
    }

    function getTwapCOUSDUSD() external view override returns (uint256, uint8) {
        if (oracleMode==1) {
            (uint256 v1, uint8 d1) = getTwapCOUSDFTM();
            (uint256 v2, uint8 d2) = getFTMUSD();
            return ((v1 * v2) / (10**d1), d2);
        } else if (oracleMode==2) {   
            (uint256 v1, uint8 d1) = getTwapCOUSDDAI();
            (uint256 v2, uint8 d2) = getDAIUSD();
            return ((v1 * v2) / (10**d1), d2);
        } else {
            (uint256 v1, uint8 d1) = getTwapCOUSDUSDC();
            (uint256 v2, uint8 d2) = getUSDCUSD();
            return ((v1 * v2) / (10**d1), d2);     
        }

    }

    
    


    function getXCOFFINUSD() external view override returns (uint256, uint8) {
        (uint256 v1, uint8 d1) = getXCOFFINFTM();
        (uint256 v2, uint8 d2) = getFTMUSD();
        return ((v1 * v2) / (10**d1), d2);
    }

    function getTwapXCOFFINUSD() external view override returns (uint256, uint8) {
        (uint256 v1, uint8 d1) = getTwapXCOFFINFTM();
        (uint256 v2, uint8 d2) = getFTMUSD();
        return ((v1 * v2) / (10**d1), d2);
    }

    function getTwapCOUSDFTM() public view  returns (uint256, uint8) {
        (uint256 a, uint8 b) = getTwapRate(dollar,wftm);
        if (a>0) {
            return (a,b);
        }
        return getRealtimeRate(dollar,wftm);
    }
    
    function getTwapCOUSDDAI() public view  returns (uint256, uint8) {
        (uint256 a, uint8 b) = getTwapRate(dollar,dai);
        if (a>0) {
            return (a,b);
        }
        return getRealtimeRate(dollar,usdc);
    }
    function getTwapCOUSDUSDC() public view  returns (uint256, uint8) {
        (uint256 a, uint8 b) = getTwapRate(dollar,usdc);
        if (a>0) {
            return (a,b);
        }
        return getRealtimeRate(dollar,usdc);
    }
    
    function getCOUSDFTM() public view override returns (uint256, uint8) {
        return getRealtimeRate(dollar,wftm);
    }
    function getCOUSDUSDC() public view returns (uint256, uint8) {
        return getRealtimeRate(dollar,usdc);
    }
    function getCOUSDDAI() public view returns (uint256, uint8) {
        return getRealtimeRate(dollar,dai);
    }
    

    function getTwapXCOFFINFTM() public view  returns (uint256, uint8) {
        (uint256 a, uint8 b) = getTwapRate(xcoffin,wftm);
        if (a>0) {
            return (a,b);
        }
        return getRealtimeRate(xcoffin,wftm);
    }

    function getXCOFFINFTM() public view override returns (uint256, uint8) {
        return getRealtimeRate(xcoffin,wftm);
    }

    function getTwapCOFFINFTM() public view returns (uint256, uint8) {
        (uint256 a, uint8 b) = getTwapRate(coffin,wftm);
        if (a>0) {
            return (a,b);
        }
        return getRealtimeRate(coffin,wftm);
    }
    function getCOFFINFTM() public view override returns (uint256, uint8) {
        return getRealtimeRate(coffin,wftm);
    }


    function currentBlockTimestamp() internal view returns (uint32) {
        return uint32(block.timestamp % 2**32);
    }

    function currentCumulativePrices(address uniswapV2Pair)
        internal
        view
        returns (
            uint256 price0Cumulative,
            uint256 price1Cumulative,
            uint32 blockTimestamp
        )
    {
        // Pair storage pairStorage = getPair[uniswapV2Pair];

        blockTimestamp = currentBlockTimestamp();
        IUniswapLP uniswapPair = IUniswapLP(uniswapV2Pair);
        price0Cumulative = uniswapPair.price0CumulativeLast();
        price1Cumulative = uniswapPair.price1CumulativeLast();

        // if time has elapsed since the last update on the pair, mock the accumulated price values
        (uint112 reserve0, uint112 reserve1, uint32 _blockTimestampLast) = uniswapPair.getReserves();
        if (_blockTimestampLast != blockTimestamp) {
            // subtraction overflow is desired
            uint32 timeElapsed = blockTimestamp - _blockTimestampLast;
            // addition overflow is desired
            // counterfactual
            price0Cumulative += uint256(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
            // counterfactual
            price1Cumulative += uint256(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
        }
    }


    function getTwapRate(address token0, address token1)
        public
        view
        returns (uint256 priceLatest, uint8 decimals)
    {
        address[] memory path = new address[](2);
        path[0] = token0;
        path[1] = token1;
        address factory = address(uniswapv2router.factory());
        address uniswapV2Pair = IUniswapV2Factory(factory).getPair(token0, token1);

        if (uniswapV2Pair== address(0)) {
            return (0,0);
        } 

        // Pair memory pair = getPair[uniswapV2Pair];
        Pair storage pairStorage = getPair[uniswapV2Pair];

        // require(pairStorage.initialized, "need to setup first");
        if (!pairStorage.initialized) {
            return getRealtimeRate(token0, token1);
            // return (0,0);
        }
        
        (uint256 price0Cumulative, uint256 price1Cumulative, uint32 blockTimestamp) = currentCumulativePrices(
            address(uniswapV2Pair)
        );
        uint32 timeElapsed = blockTimestamp - pairStorage.blockTimestampLast; // Overflow is desired

        FixedPoint.uq112x112 memory price0Average 
            = FixedPoint.uq112x112(uint224((price0Cumulative - pairStorage.price0CumulativeLast) / timeElapsed));
        FixedPoint.uq112x112 memory price1Average 
            = FixedPoint.uq112x112(uint224((price1Cumulative - pairStorage.price1CumulativeLast) / timeElapsed));

        uint256 amountIn = 1e18;
        if (IUniswapLP(uniswapV2Pair).token0() == token0) {
            priceLatest = uint256(price0Average.mul(amountIn).decode144());
            decimals = ERC20(token1).decimals();
        } else {
            require(IUniswapLP(uniswapV2Pair).token0() == token1, "TwapOracle: INVALID_TOKEN");
            priceLatest = uint256(price1Average.mul(amountIn).decode144());
            decimals = ERC20(token0).decimals();
        }
    }

    function getTwapRateWithUpdate(address token0, address token1)
        external
        returns (uint256 priceLatest, uint8 decimals)
    {
        updateTwap(token0,token1);
        return getTwapRate(token0,token1);
    }
    

    function updateTwapDollarFTM() public  {
        updateTwap(dollar, wftm);
    }
    function updateTwapDollar() public override {
        updateTwap(dollar, dai);
    }
    function updateTwapDollarUSDC() public  {
        updateTwap(dollar, usdc);
    }
    function updateTwapCoffin() public override {
        updateTwap(coffin, wftm);
    }
    function updateTwapXCoffin() public override {
        updateTwap(xcoffin, wftm);
    }

    function updateTwap(address token0, address token1) public override {

        address[] memory path = new address[](2);
        path[0] = token0;
        path[1] = token1;
        address factory = address(uniswapv2router.factory());
        address uniswapV2Pair = IUniswapV2Factory(factory).getPair(token0, token1);

        if (uniswapV2Pair== address(0)) {
            return;
        }
        Pair storage pairStorage = getPair[uniswapV2Pair];
        // require(pairStorage.initialized, "need to setup first");
        
        (uint256 price0Cumulative, uint256 price1Cumulative, uint32 blockTimestamp) = currentCumulativePrices(
            address(uniswapV2Pair)
        );

        if (!pairStorage.initialized) {
            // first time 
            pairStorage.price0CumulativeLast = price0Cumulative;
            pairStorage.price1CumulativeLast = price1Cumulative;
            pairStorage.blockTimestampLast = blockTimestamp;
            pairStorage.initialized = true;
            return;
        }

        // Overflow is desired
        uint32 timeElapsed = blockTimestamp - pairStorage.blockTimestampLast; 
        
        // Ensure that at least one full period has passed since the last update
        if (timeElapsed < PERIOD) {
            return ;
        }
        
        pairStorage.price0Average 
            = FixedPoint.uq112x112(uint224((price0Cumulative - pairStorage.price0CumulativeLast) / timeElapsed));
        pairStorage.price1Average 
            = FixedPoint.uq112x112(uint224((price1Cumulative - pairStorage.price1CumulativeLast) / timeElapsed));
        pairStorage.price0CumulativeLast = price0Cumulative;
        pairStorage.price1CumulativeLast = price1Cumulative;
        pairStorage.blockTimestampLast = blockTimestamp;
    }

    function getRealtimeRate(address tokenA, address tokenB)
        public
        view
        returns (uint256 priceLatest, uint8 decimals)
    {

        address factory = address(uniswapv2router.factory());
        address pair = IUniswapV2Factory(factory).getPair(tokenA, tokenB);
        if (pair== address(0)) {
            return (0,0);
        }
        
        (uint112 reserve0, uint112 reserve1,) =
            IUniswapLP(pair).getReserves();
        if (IUniswapLP(pair).token0()==address(tokenA)) {
            priceLatest = uint256(reserve1).mul(uint256(10**ERC20(tokenA).decimals())).div(uint256(reserve0));
            decimals = ERC20(tokenB).decimals();
        } else {
            priceLatest = uint256(reserve0).mul(uint256(10**ERC20(tokenA).decimals())).div(uint256(reserve1));
            decimals = ERC20(tokenB).decimals();
        }

        if ((18-decimals)>0) { 
            priceLatest = priceLatest.mul(10**(18-decimals));
            decimals = 18;
        }
    }
}

File 11 of 24 : IGatePolicy.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;
pragma experimental ABIEncoderV2;

interface IGatePolicy {
    function target_collateral_ratio() external view returns (uint256);
    function redemption_delay() external view returns (uint256);
    // function effective_collateral_ratio() external view returns (uint256);
    function getEffectiveCollateralRatio() external view returns (uint256); 
    function refreshCollateralRatio(bool noerror) external  ; 
    function redemption_fee() external view returns (uint256); 
    function extra_redemption_fee() external view returns (uint256); 
    function minting_fee() external view returns (uint256); 
    function price_target() external view returns (uint256); 
    function using_twap_for_redeem() external view returns (bool); 
    function using_twap_for_mint() external view returns (bool); 
    function using_twap_for_tcr() external view returns (bool); 
    
}

File 12 of 24 : IGate.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;


interface IGate {

    function unclaimed_pool_collateral() external view returns (uint256);


    function globalCollateralValue() external view returns (uint256) ;

    function getCollateralPrice() external view returns (uint256);
    function getDollarPrice() external view returns (uint256) ;
    function getCoffinPrice() external view returns (uint256) ;

    // function getCollateralTwap() external view returns (uint256);
    function getDollarTwap() external view returns (uint256) ;
    function getCoffinTwap() external view returns (uint256) ;

    
    function getDollarSupply() external view returns (uint256) ;

    function getCoffinSupply() external view returns (uint256) ;

    function globalCollateralBalance() external view returns (uint256);

    function getCollateralBalance() external view returns (uint256);

}

File 13 of 24 : ICollateralReserve.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

interface ICollateralReserve {
    function transferTo(
        address _token,
        address _receiver,
        uint256 _amount
    ) external;
    function fundBalance
    (
        address _token
    ) external view returns (uint256) ;

}

File 14 of 24 : IWETH.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

interface IWETH {
    function deposit() external payable;

    function transfer(address to, uint256 value) external returns (bool);

    function transferFrom(
        address src,
        address dst,
        uint256 wad
    ) external returns (bool);

    function withdraw(uint256) external;

    function balanceOf(address account) external view returns (uint256);
}

File 15 of 24 : IERC20.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

File 16 of 24 : IERC20Metadata.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "../IERC20.sol";

/**
 * @dev Interface for the optional metadata functions from the ERC20 standard.
 *
 * _Available since v4.1._
 */
interface IERC20Metadata is IERC20 {
    /**
     * @dev Returns the name of the token.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the symbol of the token.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the decimals places of the token.
     */
    function decimals() external view returns (uint8);
}

File 17 of 24 : Context.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/*
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

File 18 of 24 : IUniswapV2Router02.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.11;

import "./IUniswapV2Router01.sol";

interface IUniswapV2Router02 is IUniswapV2Router01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint256 liquidity,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline
    ) external returns (uint256 amountETH);

    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint256 liquidity,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline,
        bool approveMax,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external returns (uint256 amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;

    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external payable;

    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;

}

File 19 of 24 : IUniswapV2Factory.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
interface IUniswapV2Factory {

    function feeTo() external view returns (address);
    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);
    function allPairs(uint) external view returns (address pair);
    function allPairsLength() external view returns (uint);

    function createPair(address tokenA, address tokenB) external returns (address pair);

    function setFeeTo(address) external;
    function setFeeToSetter(address) external;
}

File 20 of 24 : IUniswapLP.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.8.7;
pragma experimental ABIEncoderV2;

interface IUniswapLP {
    function token0() external view returns (address);

    function token1() external view returns (address);

    function getReserves()
        external
        view
        returns (
            uint112 reserve0,
            uint112 reserve1,
            uint32 blockTimestampLast
        );

    function price0CumulativeLast() external view returns (uint256);

    function price1CumulativeLast() external view returns (uint256);

    function getTokenWeights() external view returns (uint32 tokenWeight0, uint32 tokenWeight1);
}

File 21 of 24 : FixedPoint.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

import "./Babylonian.sol";

// a library for handling binary fixed point numbers (https://en.wikipedia.org/wiki/Q_(number_format))
library FixedPoint {
    // range: [0, 2**112 - 1]
    // resolution: 1 / 2**112
    struct uq112x112 {
        uint224 _x;
    }

    // range: [0, 2**144 - 1]
    // resolution: 1 / 2**112
    struct uq144x112 {
        uint256 _x;
    }

    uint8 private constant RESOLUTION = 112;
    uint256 private constant Q112 = uint256(1) << RESOLUTION;
    uint256 private constant Q224 = Q112 << RESOLUTION;

    // encode a uint112 as a UQ112x112
    function encode(uint112 x) internal pure returns (uq112x112 memory) {
        return uq112x112(uint224(x) << RESOLUTION);
    }

    // encodes a uint144 as a UQ144x112
    function encode144(uint144 x) internal pure returns (uq144x112 memory) {
        return uq144x112(uint256(x) << RESOLUTION);
    }

    // divide a UQ112x112 by a uint112, returning a UQ112x112
    function div(uq112x112 memory self, uint112 x) internal pure returns (uq112x112 memory) {
        require(x != 0, "FixedPoint: DIV_BY_ZERO");
        return uq112x112(self._x / uint224(x));
    }

    // multiply a UQ112x112 by a uint, returning a UQ144x112
    // reverts on overflow
    function mul(uq112x112 memory self, uint256 y) internal pure returns (uq144x112 memory) {
        uint256 z;
        require(y == 0 || (z = uint256(self._x) * y) / y == uint256(self._x), "FixedPoint: MULTIPLICATION_OVERFLOW");
        return uq144x112(z);
    }

    // returns a UQ112x112 which represents the ratio of the numerator to the denominator
    // equivalent to encode(numerator).div(denominator)
    function fraction(uint112 numerator, uint112 denominator) internal pure returns (uq112x112 memory) {
        require(denominator > 0, "FixedPoint: DIV_BY_ZERO");
        return uq112x112((uint224(numerator) << RESOLUTION) / denominator);
    }

    // decode a UQ112x112 into a uint112 by truncating after the radix point
    function decode(uq112x112 memory self) internal pure returns (uint112) {
        return uint112(self._x >> RESOLUTION);
    }

    // decode a UQ144x112 into a uint144 by truncating after the radix point
    function decode144(uq144x112 memory self) internal pure returns (uint144) {
        return uint144(self._x >> RESOLUTION);
    }

    // take the reciprocal of a UQ112x112
    function reciprocal(uq112x112 memory self) internal pure returns (uq112x112 memory) {
        require(self._x != 0, "FixedPoint: ZERO_RECIPROCAL");
        return uq112x112(uint224(Q224 / self._x));
    }

    // square root of a UQ112x112
    function sqrt(uq112x112 memory self) internal pure returns (uq112x112 memory) {
        return uq112x112(uint224(Babylonian.sqrt(uint256(self._x)) << 56));
    }
}

File 22 of 24 : IBandStdReference.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

interface IBandStdReference {
    /// A structure returned whenever someone requests for standard reference data.
    struct ReferenceData {
        uint256 rate; // base/quote exchange rate, multiplied by 1e18.
        uint256 lastUpdatedBase; // UNIX epoch of the last time when base price gets updated.
        uint256 lastUpdatedQuote; // UNIX epoch of the last time when quote price gets updated.
    }

    /// Returns the price data for the given base/quote pair. Revert if not available.
    function getReferenceData(string memory _base, string memory _quote)
        external
        view
        returns (ReferenceData memory);
    
    /// Similar to getReferenceData, but with multiple base/quote pairs at once.
    function getReferenceDataBulk(
        string[] memory _bases,
        string[] memory _quotes
    ) external view returns (ReferenceData[] memory);
}

File 23 of 24 : IUniswapV2Router01.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

interface IUniswapV2Router01 {
    function factory() external view   returns (address);
    function WETH() external view  returns (address);

    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function removeLiquidity(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function removeLiquidityWithPermit(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountToken, uint amountETH);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapTokensForExactTokens(
        uint amountOut,
        uint amountInMax,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);

    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}

File 24 of 24 : Babylonian.sol
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

// computes square roots using the babylonian method
// https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
library Babylonian {
    function sqrt(uint256 y) internal pure returns (uint256 z) {
        if (y > 3) {
            z = y;
            uint256 x = y / 2 + 1;
            while (x < z) {
                z = x;
                x = (y / x + x) / 2;
            }
        } else if (y != 0) {
            z = 1;
        }
        // else z = 0
    }
}

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 500
  },
  "metadata": {
    "bytecodeHash": "none",
    "useLiteralContent": true
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "abi"
      ]
    }
  },
  "libraries": {}
}

Contract Security Audit

Contract ABI

[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"collateralAmount","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"shareAmount","type":"uint256"}],"name":"ZapSwapped","type":"event"},{"inputs":[],"name":"collateral","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"collateralReserve","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"collectRedemption","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"dollar","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gateInfo","outputs":[{"internalType":"uint256","name":"_minting_fee","type":"uint256"},{"internalType":"uint256","name":"_redemption_fee","type":"uint256"},{"internalType":"uint256","name":"_ex_red_fee","type":"uint256"},{"internalType":"uint256","name":"_collateral_price","type":"uint256"},{"internalType":"uint256","name":"_share_price","type":"uint256"},{"internalType":"uint256","name":"_dollar_price","type":"uint256"},{"internalType":"uint256","name":"_share_twap","type":"uint256"},{"internalType":"uint256","name":"_dollar_twap","type":"uint256"},{"internalType":"uint256","name":"_tcr","type":"uint256"},{"internalType":"bool","name":"_mint_paused","type":"bool"},{"internalType":"bool","name":"_redeem_paused","type":"bool"},{"internalType":"uint256","name":"_unclaimed_pool_collateral","type":"uint256"},{"internalType":"uint256","name":"_unclaimed_pool_share","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCoffinPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCoffinSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCoffinTwap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCollateralBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getCollateralPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDollarPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDollarSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getDollarTwap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"globalCollateralBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"globalCollateralValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_oracle","type":"address"},{"internalType":"address","name":"_dollar","type":"address"},{"internalType":"address","name":"_share","type":"address"},{"internalType":"address","name":"_collateral","type":"address"},{"internalType":"address","name":"_collateralReserve","type":"address"},{"internalType":"address","name":"_policy","type":"address"}],"name":"init","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"last_redeemed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_share_amount","type":"uint256"},{"internalType":"uint256","name":"_dollar_out_min","type":"uint256"}],"name":"mint","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"mint_paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"oracle","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"policy","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_dollar_amount","type":"uint256"},{"internalType":"uint256","name":"_share_out_min","type":"uint256"},{"internalType":"uint256","name":"_collateral_out_min","type":"uint256"}],"name":"redeem","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"redeem_collateral_balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"redeem_paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"redeem_share_balances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"rescueFund","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_collateralReserve","type":"address"}],"name":"setCollateralReserve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_oracle","type":"address"}],"name":"setOracle","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_policy","type":"address"}],"name":"setPolicy","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"share","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"toggleMinting","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"toggleRedeeming","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unclaimed_pool_collateral","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"unclaimed_pool_share","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]

6080604052600d80546001600160a01b0319167321be370d5312f44cb42ce377bc9b8a0cef1a4c83179055600f805475f491e7b69e4244ad4002bc14e878a34207e38c2900006001600160b01b031990911617905534801561006057600080fd5b5061006a33610073565b600180556100c3565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b612d5f80620000d36000396000f3fe60806040526004361061021e5760003560e01c80638da5cb5b11610123578063c82c4d0e116100ab578063dc0d2a981161006f578063dc0d2a981461062d578063e1f095aa1461065a578063f2fde38b1461066f578063f7683bbc1461068f578063ff626c5f146106a457600080fd5b8063c82c4d0e146105a0578063d0d132f3146105b6578063d2d97b06146105e3578063d8dfeb45146105f8578063daa504851461061857600080fd5b8063b12f0b6b116100f2578063b12f0b6b146104b6578063b819220514610531578063bfa3e19b14610551578063c0baeadb14610566578063c7d272281461058657600080fd5b80638da5cb5b1461044357806399bcc7751461046157806399e133f914610476578063a8d5fd651461049657600080fd5b806354367135116101a65780637adbf973116101755780637adbf9731461039f5780637d4163d3146103bf5780637d55094d146103df5780637dc0d1d0146103f457806380a66d051461041457600080fd5b80635436713514610333578063548f8939146103605780636912cb4314610375578063715018a61461038a57600080fd5b80631529a639116101ed5780631529a639146102b65780631b2ef1ca146102cb578063465b0c41146102de578063508f657d146102fe57806351adeb571461031357600080fd5b80630505c8c91461022a5780630ae2dbd81461026757806312ace5a21461028a57806314362530146102a157600080fd5b3661022557005b600080fd5b34801561023657600080fd5b5060055461024a906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b34801561027357600080fd5b5061027c6106ba565b60405190815260200161025e565b34801561029657600080fd5b5061029f610771565b005b3480156102ad57600080fd5b5061027c61096c565b3480156102c257600080fd5b5061027c6109be565b61029f6102d9366004612a7a565b610a47565b3480156102ea57600080fd5b5060075461024a906001600160a01b031681565b34801561030a57600080fd5b5061029f611088565b34801561031f57600080fd5b5060045461024a906001600160a01b031681565b34801561033f57600080fd5b5061027c61034e3660046129b0565b60086020526000908152604090205481565b34801561036c57600080fd5b5061027c611163565b34801561038157600080fd5b5061027c6111a8565b34801561039657600080fd5b5061029f611241565b3480156103ab57600080fd5b5061029f6103ba3660046129b0565b611295565b3480156103cb57600080fd5b5061029f6103da3660046129b0565b611347565b3480156103eb57600080fd5b5061029f6113f9565b34801561040057600080fd5b5060025461024a906001600160a01b031681565b34801561042057600080fd5b50600f5461043390610100900460ff1681565b604051901515815260200161025e565b34801561044f57600080fd5b506000546001600160a01b031661024a565b34801561046d57600080fd5b5061027c611455565b34801561048257600080fd5b5061029f6104913660046129cb565b6114a7565b3480156104a257600080fd5b5060065461024a906001600160a01b031681565b3480156104c257600080fd5b506104cb6116bf565b604080519d8e5260208e019c909c529a8c019990995260608b019790975260808a019590955260a089019390935260c088019190915260e0870152610100860152151561012085015215156101408401526101608301526101808201526101a00161025e565b34801561053d57600080fd5b5061029f61054c366004612a9c565b611962565b34801561055d57600080fd5b5061027c61206d565b34801561057257600080fd5b5061029f6105813660046129b0565b6120bd565b34801561059257600080fd5b50600f546104339060ff1681565b3480156105ac57600080fd5b5061027c600b5481565b3480156105c257600080fd5b5061027c6105d13660046129b0565b60096020526000908152604090205481565b3480156105ef57600080fd5b5061027c61217d565b34801561060457600080fd5b5060035461024a906001600160a01b031681565b34801561062457600080fd5b5061029f6121c0565b34801561063957600080fd5b5061027c6106483660046129b0565b600c6020526000908152604090205481565b34801561066657600080fd5b5061027c612225565b34801561067b57600080fd5b5061029f61068a3660046129b0565b612277565b34801561069b57600080fd5b5061027c61232d565b3480156106b057600080fd5b5061027c600a5481565b6000806000600260009054906101000a90046001600160a01b03166001600160a01b031663e926649b6040518163ffffffff1660e01b8152600401604080518083038186803b15801561070c57600080fd5b505afa158015610720573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906107449190612ac8565b909250905061076a61075782600a612be7565b61076484620f424061237f565b90612394565b9250505090565b600260015414156107c95760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064015b60405180910390fd5b600260015560055460408051631512842560e01b815290516000926001600160a01b0316916315128425916004808301926020929190829003018186803b15801561081357600080fd5b505afa158015610827573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061084b9190612a61565b336000908152600c6020526040902054909150429061086b908390612b5e565b11156108b95760405162461bcd60e51b815260206004820152601160248201527f3c726564656d7074696f6e5f64656c617900000000000000000000000000000060448201526064016107c0565b3360009081526008602052604081205481908190819015610901573360009081526008602052604081208054919055600b549092506108f9908390612cb2565b600b55600193505b336000908152600960205260409020541561094157503360009081526009602052604081208054919055600a54610939908290612cb2565b600a55600192505b83156109515761095133836123a0565b8215610961576109613382612414565b505060018055505050565b6000806000600260009054906101000a90046001600160a01b03166001600160a01b031663d34410ba6040518163ffffffff1660e01b8152600401604080518083038186803b15801561070c57600080fd5b6003546007546040516370a0823160e01b81526001600160a01b03918216600482015260009291909116906370a082319060240160206040518083038186803b158015610a0a57600080fd5b505afa158015610a1e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610a429190612a61565b905090565b60026001541415610a9a5760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064016107c0565b6002600155600f5460ff1615610af25760405162461bcd60e51b815260206004820152601160248201527f4d696e74696e672069732070617573656400000000000000000000000000000060448201526064016107c0565b60003411610b2d5760405162461bcd60e51b81526020600482015260086024820152676e6565642046544d60c01b60448201526064016107c0565b6005546040805163c3355b8d60e01b8152905134926000926001600160a01b039091169163c3355b8d91600480820192602092909190829003018186803b158015610b7757600080fd5b505afa158015610b8b573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610baf9190612a61565b90506000600560009054906101000a90046001600160a01b03166001600160a01b031663146a549d6040518163ffffffff1660e01b815260040160206040518083038186803b158015610c0157600080fd5b505afa158015610c15573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610c399190612a3f565b15610c4957610c466106ba565b90505b80610c5957610c56611455565b90505b60055460408051634006311b60e01b815290516000926001600160a01b031691634006311b916004808301926020929190829003018186803b158015610c9e57600080fd5b505afa158015610cb2573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610cd69190612a61565b90506000610ce261232d565b90506000808315610dba576000620f424084600e54600a610d039190612bdb565b610d0d908b612c93565b610d179190612c93565b610d219190612b76565b905084610d31620f424083612c93565b610d3b9190612b76565b9250620f4240851015610db45760008611610d8e5760405162461bcd60e51b8152602060048201526013602482015272496e76616c696420736861726520707269636560681b60448201526064016107c0565b85620f4240610d9d8386612cb2565b610da79190612c93565b610db19190612b76565b91505b50610e1e565b60008511610e005760405162461bcd60e51b8152602060048201526013602482015272496e76616c696420736861726520707269636560681b60448201526064016107c0565b620f4240610e0e868b612c93565b610e189190612b76565b91508890505b6000620f4240610e2e8885612c93565b610e389190612b76565b610e429084612cb2565b905080891115610ee05760405162461bcd60e51b815260206004820152605e60248201527f5f61637475616c5f646f6c6c61725f616d6f756e7420697320736d616c6c657260448201527f207468616e205f646f6c6c61725f6f75745f6d696e2e20736c6970706167652060648201527f697320626967676572207468616e20796f752065787065637465642e20200000608482015260a4016107c0565b8115610f9b5789821115610f365760405162461bcd60e51b815260206004820152601660248201527f4e6f7420656e6f75676820534841524520696e7075740000000000000000000060448201526064016107c0565b600654604051635453bc5760e11b8152336004820152602481018490526001600160a01b039091169063a8a778ae90604401600060405180830381600087803b158015610f8257600080fd5b505af1158015610f96573d6000803e3d6000fd5b505050505b871561101357600d60009054906101000a90046001600160a01b03166001600160a01b031663d0e30db0896040518263ffffffff1660e01b81526004016000604051808303818588803b158015610ff157600080fd5b505af1158015611005573d6000803e3d6000fd5b50505050506110138861256b565b60048054604051635a7ab59360e11b81523392810192909252602482018390526001600160a01b03169063b4f56b2690604401600060405180830381600087803b15801561106057600080fd5b505af1158015611074573d6000803e3d6000fd5b505060018055505050505050505050505050565b6000546001600160a01b031633146110d05760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b6003546007546040516370a0823160e01b81526001600160a01b03918216600482015260009291909116906370a082319060240160206040518083038186803b15801561111c57600080fd5b505afa158015611130573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906111549190612a61565b90506111603382612414565b50565b600654604080516318160ddd60e01b815290516000926001600160a01b0316916318160ddd916004808301926020929190829003018186803b158015610a0a57600080fd5b6003546007546040516370a0823160e01b81526001600160a01b039182166004820152600092839216906370a082319060240160206040518083038186803b1580156111f357600080fd5b505afa158015611207573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061122b9190612a61565b9050600a548161123b9190612cb2565b91505090565b6000546001600160a01b031633146112895760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b61129360006125da565b565b6000546001600160a01b031633146112dd5760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b6001600160a01b0381166113255760405162461bcd60e51b815260206004820152600f60248201526e496e76616c6964206164647265737360881b60448201526064016107c0565b600280546001600160a01b0319166001600160a01b0392909216919091179055565b6000546001600160a01b0316331461138f5760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b6001600160a01b0381166113d75760405162461bcd60e51b815260206004820152600f60248201526e496e76616c6964206164647265737360881b60448201526064016107c0565b600580546001600160a01b0319166001600160a01b0392909216919091179055565b6000546001600160a01b031633146114415760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b600f805460ff19811660ff90911615179055565b6000806000600260009054906101000a90046001600160a01b03166001600160a01b03166343be12306040518163ffffffff1660e01b8152600401604080518083038186803b15801561070c57600080fd5b600054600160a81b900460ff16806114c95750600054600160a01b900460ff16155b61153b5760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201527f647920696e697469616c697a656400000000000000000000000000000000000060648201526084016107c0565b600054600160a81b900460ff16158015611565576000805461ffff60a01b191661010160a01b1790555b6000546001600160a01b031633146115ad5760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b600280546001600160a01b03808a166001600160a01b0319928316179092556004805489841690831617815560068054898516908416179055600380549388169390921683179091556040805163313ce56760e01b8152905163313ce56792828101926020929190829003018186803b15801561162957600080fd5b505afa15801561163d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116619190612af4565b61166c906012612cc9565b60ff16600e55600780546001600160a01b038086166001600160a01b031992831617909255600580549285169290911691909117905580156116b6576000805460ff60a81b191690555b50505050505050565b6000806000806000806000806000806000806000600560009054906101000a90046001600160a01b03166001600160a01b031663c3355b8d6040518163ffffffff1660e01b815260040160206040518083038186803b15801561172157600080fd5b505afa158015611735573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117599190612a61565b9c50600560009054906101000a90046001600160a01b03166001600160a01b031663cb73999f6040518163ffffffff1660e01b815260040160206040518083038186803b1580156117a957600080fd5b505afa1580156117bd573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117e19190612a61565b9b50600560009054906101000a90046001600160a01b03166001600160a01b031663965ff4616040518163ffffffff1660e01b815260040160206040518083038186803b15801561183157600080fd5b505afa158015611845573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906118699190612a61565b9a5061187361232d565b995061187d611455565b9850611887612225565b97506118916106ba565b965061189b61096c565b9550600560009054906101000a90046001600160a01b03166001600160a01b0316634006311b6040518163ffffffff1660e01b815260040160206040518083038186803b1580156118eb57600080fd5b505afa1580156118ff573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906119239190612a61565b9450600f60009054906101000a900460ff169350600f60019054906101000a900460ff169250600a549150600b549050909192939495969798999a9b9c565b600260015414156119b55760405162461bcd60e51b815260206004820152601f60248201527f5265656e7472616e637947756172643a207265656e7472616e742063616c6c0060448201526064016107c0565b600260015560055460408051637013da0560e01b8152905160009283926001600160a01b0390911691637013da0591600480820192602092909190829003018186803b158015611a0457600080fd5b505afa158015611a18573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611a3c9190612a3f565b15611a5657611a496106ba565b9150611a5361096c565b90505b81611a6657611a63611455565b91505b80611a7657611a73612225565b90505b6005546040805163cb73999f60e01b815290516000926001600160a01b03169163cb73999f916004808301926020929190829003018186803b158015611abb57600080fd5b505afa158015611acf573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611af39190612a61565b90506000600560009054906101000a90046001600160a01b03166001600160a01b031663965ff4616040518163ffffffff1660e01b815260040160206040518083038186803b158015611b4557600080fd5b505afa158015611b59573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611b7d9190612a61565b90506000600560009054906101000a90046001600160a01b03166001600160a01b0316632cb4f63e6040518163ffffffff1660e01b815260040160206040518083038186803b158015611bcf57600080fd5b505afa158015611be3573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611c079190612a61565b905080841015611c1e57611c1b8284612b5e565b92505b60055460408051634ff33ead60e01b815290516000926001600160a01b031691634ff33ead916004808301926020929190829003018186803b158015611c6357600080fd5b505afa158015611c77573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190611c9b9190612a61565b90506000611ca761232d565b905060008111611cf95760405162461bcd60e51b815260206004820152601860248201527f496e76616c696420636f6c6c61746572616c207072696365000000000000000060448201526064016107c0565b60008711611d3f5760405162461bcd60e51b8152602060048201526013602482015272496e76616c696420736861726520707269636560681b60448201526064016107c0565b6000620f4240611d4f878d612c93565b611d599190612b76565b611d63908c612cb2565b9050600080620f4240851015611db5576000620f4240611d838786612c93565b611d8d9190612b76565b611d979085612cb2565b90508a611da7620f424083612c93565b611db19190612b76565b9150505b8415611e0c576000600e54600a611dcc9190612bdb565b620f4240611dda8887612c93565b611de49190612b76565b611dee9190612b76565b905084611dfe620f424083612c93565b611e089190612b76565b9250505b6000611e166111a8565b905080831115611e685760405162461bcd60e51b815260206004820152601260248201527f3c636f6c6c61746572616c42616c616e6365000000000000000000000000000060448201526064016107c0565b828c1115611eb85760405162461bcd60e51b815260206004820152601c60248201527f3e3e20736c697070616765207468616e2065787065637465642e2e2e0000000060448201526064016107c0565b818d1115611f085760405162461bcd60e51b815260206004820152601f60248201527f3e3e20736c697070616765207468616e2065787065637465642e2e2e2e2e2e0060448201526064016107c0565b8215611f4b5733600090815260096020526040902054611f29908490612b5e565b33600090815260096020526040902055600a54611f47908490612b5e565b600a555b8115611f8e5733600090815260086020526040902054611f6c908390612b5e565b33600090815260086020526040902055600b54611f8a908390612b5e565b600b555b42600c6000336001600160a01b03166001600160a01b031681526020019081526020016000208190555060008e9050600460009054906101000a90046001600160a01b03166001600160a01b031663a8a778ae33836040518363ffffffff1660e01b81526004016120149291906001600160a01b03929092168252602082015260400190565b600060405180830381600087803b15801561202e57600080fd5b505af1158015612042573d6000803e3d6000fd5b505050506000831115612058576120588361262a565b50506001805550505050505050505050505050565b6000600460009054906101000a90046001600160a01b03166001600160a01b03166318160ddd6040518163ffffffff1660e01b815260040160206040518083038186803b158015610a0a57600080fd5b6000546001600160a01b031633146121055760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b6001600160a01b03811661215b5760405162461bcd60e51b815260206004820152600e60248201527f696e76616c69644164647265737300000000000000000000000000000000000060448201526064016107c0565b600780546001600160a01b0319166001600160a01b0392909216919091179055565b6000620f4240600e54600a6121929190612bdb565b61219a61232d565b6121a26111a8565b6121ac9190612c93565b6121b69190612c93565b610a429190612b76565b6000546001600160a01b031633146122085760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b600f805461ff001981166101009182900460ff1615909102179055565b6000806000600260009054906101000a90046001600160a01b03166001600160a01b031663e61574b06040518163ffffffff1660e01b8152600401604080518083038186803b15801561070c57600080fd5b6000546001600160a01b031633146122bf5760405162461bcd60e51b81526020600482018190526024820152600080516020612d3383398151915260448201526064016107c0565b6001600160a01b0381166123245760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016107c0565b611160816125da565b6000806000600260009054906101000a90046001600160a01b03166001600160a01b03166312eb11196040518163ffffffff1660e01b8152600401604080518083038186803b15801561070c57600080fd5b600061238b8284612c93565b90505b92915050565b600061238b8284612b76565b6007546006546040516352f950a960e11b81526001600160a01b03918216600482015284821660248201526044810184905291169063a5f2a15290606401600060405180830381600087803b1580156123f857600080fd5b505af115801561240c573d6000803e3d6000fd5b505050505050565b6001600160a01b0382166124645760405162461bcd60e51b8152602060048201526017602482015276496e76616c69642072657365727665206164647265737360481b60448201526064016107c0565b6007546003546040516352f950a960e11b81526001600160a01b0391821660048201523060248201526044810184905291169063a5f2a15290606401600060405180830381600087803b1580156124ba57600080fd5b505af11580156124ce573d6000803e3d6000fd5b5050600d54604051632e1a7d4d60e01b8152600481018590526001600160a01b039091169250632e1a7d4d9150602401600060405180830381600087803b15801561251857600080fd5b505af115801561252c573d6000803e3d6000fd5b50506040516001600160a01b038516925083156108fc02915083906000818181858888f19350505050158015612566573d6000803e3d6000fd5b505050565b6007546001600160a01b03166125bd5760405162461bcd60e51b8152602060048201526017602482015276496e76616c69642072657365727665206164647265737360481b60448201526064016107c0565b600754600354611160916001600160a01b039182169116836126e7565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6007546001600160a01b031661267c5760405162461bcd60e51b8152602060048201526017602482015276496e76616c69642072657365727665206164647265737360481b60448201526064016107c0565b600654600754604051635a7ab59360e11b81526001600160a01b0391821660048201526024810184905291169063b4f56b2690604401600060405180830381600087803b1580156126cc57600080fd5b505af11580156126e0573d6000803e3d6000fd5b5050505050565b604080516001600160a01b03848116602483015260448083018590528351808403909101815260649092018352602080830180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1663a9059cbb60e01b17905283518085019094528084527f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564908401526125669286929160009161278c918516908490612809565b80519091501561256657808060200190518101906127aa9190612a3f565b6125665760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b60648201526084016107c0565b60606128188484600085612822565b90505b9392505050565b6060824710156128835760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b60648201526084016107c0565b843b6128d15760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016107c0565b600080866001600160a01b031685876040516128ed9190612b0f565b60006040518083038185875af1925050503d806000811461292a576040519150601f19603f3d011682016040523d82523d6000602084013e61292f565b606091505b509150915061293f82828661294a565b979650505050505050565b6060831561295957508161281b565b8251156129695782518084602001fd5b8160405162461bcd60e51b81526004016107c09190612b2b565b80356001600160a01b038116811461299a57600080fd5b919050565b805160ff8116811461299a57600080fd5b6000602082840312156129c257600080fd5b61238b82612983565b60008060008060008060c087890312156129e457600080fd5b6129ed87612983565b95506129fb60208801612983565b9450612a0960408801612983565b9350612a1760608801612983565b9250612a2560808801612983565b9150612a3360a08801612983565b90509295509295509295565b600060208284031215612a5157600080fd5b8151801515811461281b57600080fd5b600060208284031215612a7357600080fd5b5051919050565b60008060408385031215612a8d57600080fd5b50508035926020909101359150565b600080600060608486031215612ab157600080fd5b505081359360208301359350604090920135919050565b60008060408385031215612adb57600080fd5b82519150612aeb6020840161299f565b90509250929050565b600060208284031215612b0657600080fd5b61238b8261299f565b60008251612b21818460208701612cec565b9190910192915050565b6020815260008251806020840152612b4a816040850160208701612cec565b601f01601f19169190910160400192915050565b60008219821115612b7157612b71612d1c565b500190565b600082612b9357634e487b7160e01b600052601260045260246000fd5b500490565b600181815b80851115612bd3578160001904821115612bb957612bb9612d1c565b80851615612bc657918102915b93841c9390800290612b9d565b509250929050565b600061238b8383612bf2565b600061238b60ff8416835b600082612c015750600161238e565b81612c0e5750600061238e565b8160018114612c245760028114612c2e57612c4a565b600191505061238e565b60ff841115612c3f57612c3f612d1c565b50506001821b61238e565b5060208310610133831016604e8410600b8410161715612c6d575081810a61238e565b612c778383612b98565b8060001904821115612c8b57612c8b612d1c565b029392505050565b6000816000190483118215151615612cad57612cad612d1c565b500290565b600082821015612cc457612cc4612d1c565b500390565b600060ff821660ff841680821015612ce357612ce3612d1c565b90039392505050565b60005b83811015612d07578181015183820152602001612cef565b83811115612d16576000848401525b50505050565b634e487b7160e01b600052601160045260246000fdfe4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572a164736f6c6343000807000a

Block Transaction Gas Used Reward
Age Block Fee Address BC Fee Address Voting Power Jailed Incoming
Validator ID :
0 FTM

Amount Staked
0

Amount Delegated
0

Staking Total
0

Staking Start Epoch
0

Staking Start Time
0

Proof of Importance
0

Origination Score
0

Validation Score
0

Active
0

Online
0

Downtime
0 s
Address Amount claimed Rewards Created On Epoch Created On
Block Uncle Number Difficulty Gas Used Reward
Loading
Loading
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.