Cryo Explorer Ethereum Mainnet

Address Contract Partially Verified

Address 0x23445c63FeEf8D85956dc0f19aDe87606D0e19A9
Balance 0 ETH
Nonce 1
Code Size 8992 bytes
Indexed Transactions 4 (24,458,88524,458,941)
Gas Used (indexed) 674,961
External Etherscan · Sourcify

Contract Bytecode

8992 bytes
0x608060405234801561001057600080fd5b50600436106101da5760003560e01c8063715018a61161010457806398969e82116100a2578063c418441c11610071578063c418441c146104c5578063d82ada50146104d8578063f2fde38b1461051e578063f7c618c11461053157600080fd5b806398969e8214610413578063a6d6f4ed14610426578063a912616914610439578063b33494671461046c57600080fd5b80638ae39cac116100de5780638ae39cac146103915780638da5cb5b1461039a5780638dbb1e3a146103ab57806393f1a40b146103be57600080fd5b8063715018a61461036d5780637b0472f0146103755780638aa285501461038857600080fd5b8063441a3e701161017c578063525eef2f1161014b578063525eef2f146102e25780635312ea8e1461033f5780635df5f96f14610352578063630b5ba11461036557600080fd5b8063441a3e70146102a057806348cd4cb1146102b35780634bea3f03146102bc57806351eb05a6146102cf57600080fd5b80631ab06ee5116101b85780631ab06ee5146102445780631aed6553146102595780632b8bbbe8146102625780633a2c67771461027557600080fd5b8063081e3eda146101df5780631526fe27146101f657806317caf6f11461023b575b600080fd5b6067545b6040519081526020015b60405180910390f35b610209610204366004611e7a565b610544565b604080516001600160a01b0390961686526020860194909452928401919091526060830152608082015260a0016101ed565b6101e3606a5481565b610257610252366004611e93565b61058f565b005b6101e3606c5481565b610257610270366004611eca565b610692565b606e54610288906001600160a01b031681565b6040516001600160a01b0390911681526020016101ed565b6102576102ae366004611e93565b6108b6565b6101e3606b5481565b6101e36102ca366004611efa565b610a60565b6102576102dd366004611e7a565b610bda565b6103236102f0366004611f26565b606f6020908152600093845260408085208252928452828420905282529020805460019091015463ffffffff9091169082565b6040805163ffffffff90931683526020830191909152016101ed565b61025761034d366004611e7a565b610cbf565b610257610360366004611f71565b610dad565b6102576110b9565b6102576110e0565b610257610383366004611e93565b611116565b6101e3606d5481565b6101e360665481565b6033546001600160a01b0316610288565b6101e36103b9366004611e93565b61126a565b6103f86103cc366004611eca565b606860209081526000928352604080842090915290825290208054600182015460029092015490919083565b604080519384526020840192909252908201526060016101ed565b6101e3610421366004611eca565b61133d565b610257610434366004611e7a565b611465565b61045c610447366004611fcf565b60696020526000908152604090205460ff1681565b60405190151581526020016101ed565b6101e361047a366004611eca565b60009182526068602090815260408084206001600160a01b03939093168452918152918190208151606081018352815480825260018301549482019490945260029091015491015290565b6101e36104d3366004611fec565b611494565b6105096104e6366004611eca565b607060209081526000928352604080842090915290825290205463ffffffff1681565b60405163ffffffff90911681526020016101ed565b61025761052c366004611fcf565b61172e565b606554610288906001600160a01b031681565b6067818154811061055457600080fd5b6000918252602090912060059091020180546001820154600283015460038401546004909401546001600160a01b0390931694509092909185565b6033546001600160a01b031633146105c25760405162461bcd60e51b81526004016105b990612024565b60405180910390fd5b606754829081106105e55760405162461bcd60e51b81526004016105b990612059565b6105ed6110b9565b6106308261062a6067868154811061060757610607612086565b906000526020600020906005020160010154606a546117c990919063ffffffff16565b906117d5565b606a81905550816067848154811061064a5761064a612086565b90600052602060002090600502016001018190555081837f545b620a3000f6303b158b321f06b4e95e28a27d70aecac8c6bdac4f48a9f6b360405160405180910390a3505050565b6033546001600160a01b031633146106bc5760405162461bcd60e51b81526004016105b990612024565b6001600160a01b03811660009081526069602052604090205460ff16156107255760405162461bcd60e51b815260206004820152601960248201527f5374616b6520746f6b656e20616c72656164792065786973740000000000000060448201526064016105b9565b61072d6110b9565b6000606b54431161074057606b54610742565b435b606a5490915061075290846117d5565b606a556001600160a01b0382811660008181526069602090815260408083208054600160ff199091168117909155815160a0810183528581529283018981528383018881526060850186815260808601878152606780549586018155885295517f9787eeb91fe3101235e4a76063c7023ecb40f923f97916639c598592fa30d6ae600590950294850180546001600160a01b03191691909a161790985590517f9787eeb91fe3101235e4a76063c7023ecb40f923f97916639c598592fa30d6af830155517f9787eeb91fe3101235e4a76063c7023ecb40f923f97916639c598592fa30d6b082015594517f9787eeb91fe3101235e4a76063c7023ecb40f923f97916639c598592fa30d6b186015590517f9787eeb91fe3101235e4a76063c7023ecb40f923f97916639c598592fa30d6b290940193909355915185927f2728c9d3205d667bbc0eefdfeda366261b4d021949630c047f3e5834b30611ab91a3505050565b606754829081106108d95760405162461bcd60e51b81526004016105b990612059565b6000606784815481106108ee576108ee612086565b60009182526020808320878452606882526040808520338652909252922080546005909202909201925084111561095c5760405162461bcd60e51b81526020600482015260126024820152711dda5d1a191c985dce881b9bdd0819dbdbd960721b60448201526064016105b9565b61096585610bda565b600061099f826001015461099964e8d4a51000610993876003015487600001546117e190919063ffffffff16565b906117ed565b906117c9565b90508082600201546109b191906120b2565b600283015581546109c290866117c9565b825560048301546109d390866117c9565b6004840155600383015482546109f39164e8d4a5100091610993916117e1565b60018301558254610a0e906001600160a01b031633876117f9565b610a1b8633876000611861565b6040805186815260006020820152879133917f02f25270a4d87bea75db541cdfe559334a275b4a233520ed6c0a2429667cca94910160405180910390a3505050505050565b60675460009082908110610a865760405162461bcd60e51b81526004016105b990612059565b606e546001600160a01b03163314610ae05760405162461bcd60e51b815260206004820152601a60248201527f4f776e61626c653a206f6e6c7920726577617264207661756c7400000000000060448201526064016105b9565b600060678481548110610af557610af5612086565b600091825260208083208784526068825260408085206001600160a01b038b168652909252922060059091029091019150610b2f85610bda565b6000610b5d826001015461099964e8d4a51000610993876003015487600001546117e190919063ffffffff16565b9050816002015481610b6f91906120b2565b6000600284015560038401548354919250610b959164e8d4a510009161099391906117e1565b600183015560405186906001600160a01b038916907f926f6d060d7b34315435a6af02d8d2f0382328cd5821183eadda8a4742258d4690600090a39695505050505050565b60675481908110610bfd5760405162461bcd60e51b81526004016105b990612059565b600060678381548110610c1257610c12612086565b9060005260206000209060050201905080600201544311610c3257505050565b600481015480610c485750436002909101555050565b6000610c5883600201544361126a565b90506000610c85606a546109938660010154610c7f606654876117e190919063ffffffff16565b906117e1565b9050610ca8610c9d846109938464e8d4a510006117e1565b6003860154906117d5565b6003850155505043600290920191909155505b5050565b60675481908110610ce25760405162461bcd60e51b81526004016105b990612059565b600060678381548110610cf757610cf7612086565b600091825260208083208684526068825260408085203386529092529083208054848255600182018590556002820194909455600592909202016004810154909350909190610d4690826117c9565b60048401558254610d61906001600160a01b031633836117f9565b610d6e8533836000611861565b604051818152859033907fbb757047c2b5f3974fe26b7c10f732e7bce710b0952a71082702781e62ae0595906020015b60405180910390a35050505050565b600054610100900460ff16610dc85760005460ff1615610dcc565b303b155b610e2f5760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084016105b9565b600054610100900460ff16158015610e51576000805461ffff19166101011790555b6001600160a01b03821615801590610e7157506001600160a01b03871615155b610ebd5760405162461bcd60e51b815260206004820152601760248201527f5a65726f20616464726573732076616c69646174696f6e00000000000000000060448201526064016105b9565b838510610f1b5760405162461bcd60e51b815260206004820152602660248201527f537461727420626c6f636b206c6f776572207468616e20626f6e757320656e6460448201526520626c6f636b60d01b60648201526084016105b9565b866001600160a01b03166318160ddd6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610f59573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610f7d91906120ca565b8610610fea5760405162461bcd60e51b815260206004820152603660248201527f5265776172642070657220626c6f636b20626967676572207468616e2072657760448201527561726420746f6b656e20746f74616c20737570706c7960501b60648201526084016105b9565b6064606d54106110525760405162461bcd60e51b815260206004820152602d60248201527f426f6e7573206d756c7469706c657220626967676572207468616e203130307860448201526c2072657761726420626f6e757360981b60648201526084016105b9565b61105a611946565b606580546001600160a01b03808a166001600160a01b0319928316179092556066889055606b879055606c869055606d859055606e80549285169290911691909117905580156110b0576000805461ff00191690555b50505050505050565b60675460005b81811015610cbb576110d081610bda565b6110d9816120e3565b90506110bf565b6033546001600160a01b0316331461110a5760405162461bcd60e51b81526004016105b990612024565b6111146000611975565b565b606754829081106111395760405162461bcd60e51b81526004016105b990612059565b60006067848154811061114e5761114e612086565b6000918252602080832087845260688252604080852033865290925292206005909102909101915061117f85610bda565b8054156111cd5760006111b4826001015461099964e8d4a51000610993876003015487600001546117e190919063ffffffff16565b60028301549091506111c690826117d5565b6002830155505b60048201546111dc90856117d5565b600483015581546111f8906001600160a01b03163330876119c7565b805461120490856117d5565b80825560038301546112219164e8d4a510009161099391906117e1565b81600101819055506112368533866001611861565b604051848152859033907f5af417134f72a9d41143ace85b0a26dce6f550f894f2cbc1eeee8810603d91b690602001610d9e565b6000606b548310156112d05760405162461bcd60e51b815260206004820152602960248201527f66726f6d20626c6f636b206e756d62657220626967676572207468616e20737460448201526861727420626c6f636b60b81b60648201526084016105b9565b606c5482116112f157606d546112ea90610c7f84866117c9565b9050611337565b606c548310611304576112ea82846117c9565b6112ea61131c606c54846117c990919063ffffffff16565b61062a606d54610c7f87606c546117c990919063ffffffff16565b92915050565b606754600090839081106113635760405162461bcd60e51b81526004016105b990612059565b60006067858154811061137857611378612086565b600091825260208083208884526068825260408085206001600160a01b038a16865290925292206003600590920290920190810154600482015460028301549294509091431180156113c957508015155b156114295760006113de85600201544361126a565b90506000611405606a546109938860010154610c7f606654876117e190919063ffffffff16565b905061142461141d846109938464e8d4a510006117e1565b85906117d5565b935050505b6114598360010154610999856002015461062a64e8d4a51000610993888a600001546117e190919063ffffffff16565b98975050505050505050565b6033546001600160a01b0316331461148f5760405162461bcd60e51b81526004016105b990612024565b606655565b60004382106114f55760405162461bcd60e51b815260206004820152602760248201527f436f6d703a3a6765745072696f72566f7465733a206e6f742079657420646574604482015266195c9b5a5b995960ca1b60648201526084016105b9565b60008481526070602090815260408083206001600160a01b038716845290915290205463ffffffff168061152d576000915050611727565b6000858152606f602090815260408083206001600160a01b03881684529091528120849161155c6001856120fe565b63ffffffff908116825260208201929092526040016000205416116115cf576000858152606f602090815260408083206001600160a01b03881684529091528120906115a96001846120fe565b63ffffffff1663ffffffff16815260200190815260200160002060010154915050611727565b6000858152606f602090815260408083206001600160a01b0388168452825280832083805290915290205463ffffffff16831015611611576000915050611727565b60008061161f6001846120fe565b90505b8163ffffffff168163ffffffff1611156116ee576000600261164484846120fe565b61164e9190612139565b61165890836120fe565b6000898152606f602090815260408083206001600160a01b038c168452825280832063ffffffff80861685529083529281902081518083019092528054909316808252600190930154918101919091529192508714156116c2576020015194506117279350505050565b805163ffffffff168711156116d9578193506116e7565b6116e46001836120fe565b92505b5050611622565b506000868152606f602090815260408083206001600160a01b0389168452825280832063ffffffff909416835292905220600101549150505b9392505050565b6033546001600160a01b031633146117585760405162461bcd60e51b81526004016105b990612024565b6001600160a01b0381166117bd5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016105b9565b6117c681611975565b50565b6000611727828461215c565b600061172782846120b2565b60006117278284612173565b60006117278284612192565b6040516001600160a01b03831660248201526044810182905261185c90849063a9059cbb60e01b906064015b60408051601f198184030181529190526020810180516001600160e01b03166001600160e01b0319909316929092179091526119ff565b505050565b8115611940576001600160a01b038316156119405760008481526070602090815260408083206001600160a01b038716845290915281205463ffffffff1690816118ac5760006118f9565b6000868152606f602090815260408083206001600160a01b03891684529091528120906118da6001856120fe565b63ffffffff1663ffffffff168152602001908152602001600020600101545b9050821561192257600061190d82866117d5565b905061191c8787858585611ad1565b5061193d565b600061192e82866117c9565b90506110b08787858585611ad1565b50505b50505050565b600054610100900460ff1661196d5760405162461bcd60e51b81526004016105b9906121a6565b611114611c99565b603380546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b6040516001600160a01b03808516602483015283166044820152606481018290526119409085906323b872dd60e01b90608401611825565b6000611a54826040518060400160405280602081526020017f5361666545524332303a206c6f772d6c6576656c2063616c6c206661696c6564815250856001600160a01b0316611cc99092919063ffffffff16565b80519091501561185c5780806020019051810190611a7291906121f1565b61185c5760405162461bcd60e51b815260206004820152602a60248201527f5361666545524332303a204552433230206f7065726174696f6e20646964206e6044820152691bdd081cdd58d8d9595960b21b60648201526084016105b9565b6000611af5436040518060600160405280603481526020016122b760349139611ce0565b905060008463ffffffff16118015611b5957506000868152606f602090815260408083206001600160a01b0389168452909152812063ffffffff831691611b3d6001886120fe565b63ffffffff908116825260208201929092526040016000205416145b15611bac576000868152606f602090815260408083206001600160a01b038916845290915281208391611b8d6001886120fe565b63ffffffff168152602081019190915260400160002060010155611c4d565b60408051808201825263ffffffff8381168252602080830186815260008b8152606f83528581206001600160a01b038c16825283528581208a851682529092529390209151825463ffffffff191691161781559051600191820155611c12908590612213565b60008781526070602090815260408083206001600160a01b038a1684529091529020805463ffffffff191663ffffffff929092169190911790555b60408051848152602081018490526001600160a01b038716917fdec2bacdd2f05b59de34da9b523dff8be42e5e38e818c82fdb0bae774387a724910160405180910390a2505050505050565b600054610100900460ff16611cc05760405162461bcd60e51b81526004016105b9906121a6565b61111433611975565b6060611cd88484600085611d10565b949350505050565b6000816401000000008410611d085760405162461bcd60e51b81526004016105b99190612267565b509192915050565b606082471015611d715760405162461bcd60e51b815260206004820152602660248201527f416464726573733a20696e73756666696369656e742062616c616e636520666f6044820152651c8818d85b1b60d21b60648201526084016105b9565b6001600160a01b0385163b611dc85760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016105b9565b600080866001600160a01b03168587604051611de4919061229a565b60006040518083038185875af1925050503d8060008114611e21576040519150601f19603f3d011682016040523d82523d6000602084013e611e26565b606091505b5091509150611e36828286611e41565b979650505050505050565b60608315611e50575081611727565b825115611e605782518084602001fd5b8160405162461bcd60e51b81526004016105b99190612267565b600060208284031215611e8c57600080fd5b5035919050565b60008060408385031215611ea657600080fd5b50508035926020909101359150565b6001600160a01b03811681146117c657600080fd5b60008060408385031215611edd57600080fd5b823591506020830135611eef81611eb5565b809150509250929050565b60008060408385031215611f0d57600080fd5b8235611f1881611eb5565b946020939093013593505050565b600080600060608486031215611f3b57600080fd5b833592506020840135611f4d81611eb5565b9150604084013563ffffffff81168114611f6657600080fd5b809150509250925092565b60008060008060008060c08789031215611f8a57600080fd5b8635611f9581611eb5565b95506020870135945060408701359350606087013592506080870135915060a0870135611fc181611eb5565b809150509295509295509295565b600060208284031215611fe157600080fd5b813561172781611eb5565b60008060006060848603121561200157600080fd5b83359250602084013561201381611eb5565b929592945050506040919091013590565b6020808252818101527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604082015260600190565b602080825260139082015272141bdbdb08191bd95cc81b9bdd08195e1a5cdd606a1b604082015260600190565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b600082198211156120c5576120c561209c565b500190565b6000602082840312156120dc57600080fd5b5051919050565b60006000198214156120f7576120f761209c565b5060010190565b600063ffffffff8381169083168181101561211b5761211b61209c565b039392505050565b634e487b7160e01b600052601260045260246000fd5b600063ffffffff8084168061215057612150612123565b92169190910492915050565b60008282101561216e5761216e61209c565b500390565b600081600019048311821515161561218d5761218d61209c565b500290565b6000826121a1576121a1612123565b500490565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b60006020828403121561220357600080fd5b8151801515811461172757600080fd5b600063ffffffff8083168185168083038211156122325761223261209c565b01949350505050565b60005b8381101561225657818101518382015260200161223e565b838111156119405750506000910152565b602081526000825180602084015261228681604085016020870161223b565b601f01601f19169190910160400192915050565b600082516122ac81846020870161223b565b919091019291505056fe436f6d703a3a5f7772697465436865636b706f696e743a20626c6f636b206e756d62657220657863656564732033322062697473a2646970667358221220140472603efdbf4df863aa939776e18f31e9c45290876da4bc0519d8291be82564736f6c634300080c0033

Verified Source Code Partial Match

Compiler: v0.8.12+commit.f00d7308 EVM: london Optimization: Yes (200 runs)
CHNStaking.sol 1423 lines
// File: @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol


// OpenZeppelin Contracts (last updated v4.5.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @dev Collection of functions related to the address type
 */
library AddressUpgradeable {
    /**
     * @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
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 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 Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal 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: @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol


// OpenZeppelin Contracts (last updated v4.5.0) (proxy/utils/Initializable.sol)

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 proxied contracts do not make use of 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.
 *
 * [CAUTION]
 * ====
 * Avoid leaving a contract uninitialized.
 *
 * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
 * contract, which may impact the proxy. To initialize the implementation contract, you can either invoke the
 * initializer manually, or you can include a constructor to automatically mark it as initialized when it is deployed:
 *
 * [.hljs-theme-light.nopadding]
 * ```
 * /// @custom:oz-upgrades-unsafe-allow constructor
 * constructor() initializer {}
 * ```
 * ====
 */
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() {
        // If the contract is initializing we ignore whether _initialized is set in order to support multiple
        // inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
        // contract may have been reentered.
        require(_initializing ? _isConstructor() : !_initialized, "Initializable: contract is already initialized");

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

        _;

        if (isTopLevelCall) {
            _initializing = false;
        }
    }

    /**
     * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
     * {initializer} modifier, directly or indirectly.
     */
    modifier onlyInitializing() {
        require(_initializing, "Initializable: contract is not initializing");
        _;
    }

    function _isConstructor() private view returns (bool) {
        return !AddressUpgradeable.isContract(address(this));
    }
}

// File: @openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol


// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

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 ContextUpgradeable is Initializable {
    function __Context_init() internal onlyInitializing {
    }

    function __Context_init_unchained() internal onlyInitializing {
    }
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

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

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[50] private __gap;
}

// File: @openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol


// OpenZeppelin Contracts v4.4.1 (access/Ownable.sol)

pragma solidity ^0.8.0;



/**
 * @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 OwnableUpgradeable is Initializable, ContextUpgradeable {
    address private _owner;

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

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    function __Ownable_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

    function __Ownable_init_unchained() internal onlyInitializing {
        _transferOwnership(_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 {
        _transferOwnership(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");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }

    /**
     * @dev This empty reserved space is put in place to allow future versions to add new
     * variables without shifting down storage in the inheritance chain.
     * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
     */
    uint256[49] private __gap;
}

// File: @openzeppelin/contracts/utils/math/SafeMath.sol


// OpenZeppelin Contracts v4.4.1 (utils/math/SafeMath.sol)

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 generally not needed starting with Solidity 0.8, since 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: @openzeppelin/contracts/utils/Address.sol


// OpenZeppelin Contracts (last updated v4.5.0) (utils/Address.sol)

pragma solidity ^0.8.1;

/**
 * @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
     * ====
     *
     * [IMPORTANT]
     * ====
     * You shouldn't rely on `isContract` to protect against flash loan attacks!
     *
     * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
     * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
     * constructor.
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies on extcodesize/address.code.length, which returns 0
        // for contracts in construction, since the code is only stored at the end
        // of the constructor execution.

        return account.code.length > 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);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason using the provided one.
     *
     * _Available since v4.3._
     */
    function verifyCallResult(
        bool success,
        bytes memory returndata,
        string memory errorMessage
    ) internal 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: @openzeppelin/contracts/token/ERC20/IERC20.sol


// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/IERC20.sol)

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 `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, 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 `from` to `to` 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 from,
        address to,
        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: @openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol


// OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;



/**
 * @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: CHNStaking.sol



pragma solidity ^0.8.3;





contract CHNStaking is OwnableUpgradeable {
    using SafeMath for uint256;
    using SafeERC20 for IERC20;
    // Info of each user.
    struct UserInfo {
        uint256 amount;
        uint256 rewardDebt;
        uint256 pendingTokenReward;
    }
    // Info of each pool.
    struct PoolInfo {
        IERC20 stakeToken;
        uint256 allocPoint;
        uint256 lastRewardBlock;
        uint256 accCHNPerShare;
        uint256 totalAmountStake;
    }

    event Add(address indexed stakToken, uint256 indexed allocPoint);
    event Set(uint256 indexed pid, uint256 indexed allocPoint);
    event Stake(address indexed user, uint256 indexed pid, uint256 amount);
    event Withdraw(address indexed user, uint256 indexed pid, uint256 amount, uint256 reward);
    event EmergencyWithdraw(
        address indexed user,
        uint256 indexed pid,
        uint256 amount
    );
    event ClaimRewardFromVault(address indexed userAddress, uint256 indexed pid);
    
    /// @notice An event thats emitted when a delegate account's vote balance changes
    event DelegateVotesChanged(address indexed delegate, uint previousBalance, uint newBalance);
    
    IERC20 public rewardToken;
    uint256 public rewardPerBlock;
    PoolInfo[] public poolInfo;
    mapping(uint256 => mapping(address => UserInfo)) public userInfo;
    mapping(address => bool) public poolTokens;
    uint256 public totalAllocPoint = 0;
    uint256 public startBlock;
    uint256 public bonusEndBlock;
    uint256 public BONUS_MULTIPLIER;
    address public rewardVault;

    /// @notice A checkpoint for marking number of votes from a given block
    struct Checkpoint {
        uint32 fromBlock;
        uint256 votes;
    }

    /// @notice A record of votes checkpoints for each account, by index
    mapping (uint256 => mapping (address => mapping (uint32 => Checkpoint))) public checkpoints;

    /// @notice The number of checkpoints for each account
    mapping (uint256 => mapping (address => uint32)) public numCheckpoints;

    modifier validatePoolByPid(uint256 _pid) {
        require(_pid < poolInfo.length, "Pool does not exist");
        _;
    }

    function initialize(
        IERC20 _rewardToken,
        uint256 _rewardPerBlock,
        uint256 _startBlock,
        uint256 _bonusEndBlock,
        uint256 _multiplier,
        address _rewardVault
    ) public initializer {
        require(_rewardVault != address(0) && address(_rewardToken) != address(0), "Zero address validation");
        require(_startBlock < _bonusEndBlock, "Start block lower than bonus end block");
        require(_rewardPerBlock < _rewardToken.totalSupply(), "Reward per block bigger than reward token total supply");
        require(BONUS_MULTIPLIER < 100, "Bonus multipler bigger than 100x reward bonus");
        __Ownable_init();
        rewardToken = _rewardToken;
        rewardPerBlock = _rewardPerBlock;
        startBlock = _startBlock;
        bonusEndBlock = _bonusEndBlock;
        BONUS_MULTIPLIER = _multiplier;
        rewardVault = _rewardVault;
    }

    function poolLength() external view returns (uint256) {
        return poolInfo.length;
    }

    function getStakingAmount(uint256 pid, address user) public view returns (uint256) {
        UserInfo memory info = userInfo[pid][user];
        return info.amount;
    }

    // Add a new stake to the pool. Can only be called by the Timelock and DAO.
    // XXX DO NOT add the same stake token more than once. Rewards will be messed up if you do.
    // This function can be only called by Timelock and DAO with voting power
    function add(
        uint256 _allocPoint,
        IERC20 _stakeToken
    ) public onlyOwner {
        require(!poolTokens[address(_stakeToken)], "Stake token already exist");
        massUpdatePools();
        uint256 lastRewardBlock =
            block.number > startBlock ? block.number : startBlock;
        totalAllocPoint = totalAllocPoint.add(_allocPoint);
        poolTokens[address(_stakeToken)] = true;
        poolInfo.push(
            PoolInfo({
                stakeToken: _stakeToken,
                allocPoint: _allocPoint,
                lastRewardBlock: lastRewardBlock,
                accCHNPerShare: 0,
                totalAmountStake: 0
            })
        );
        emit Add(address(_stakeToken), _allocPoint);
    }

    // Update the given pool's XCN allocation point. Can only be called by the Timelock and DAO.
    // This function can be only called by Timelock and DAO with voting power
    function set(
        uint256 _pid,
        uint256 _allocPoint
    ) public onlyOwner validatePoolByPid(_pid) {
        massUpdatePools();
        totalAllocPoint = totalAllocPoint.sub(poolInfo[_pid].allocPoint).add(
            _allocPoint
        );
        poolInfo[_pid].allocPoint = _allocPoint;
        emit Set(_pid, _allocPoint);
    }

    // Update reward per block by the Timelock and DAO
    function setRewardPerblock(uint256 speed)
        public
        onlyOwner {
            rewardPerBlock = speed;
    }

    // Return reward multiplier over the given _from to _to block.
    function getMultiplier(uint256 _from, uint256 _to)
        public
        view
        returns (uint256)
    {
        require(_from >= startBlock, "from block number bigger than start block");
        if (_to <= bonusEndBlock) {
            return _to.sub(_from).mul(BONUS_MULTIPLIER);
        } else if (_from >= bonusEndBlock) {
            return _to.sub(_from);
        } else {
            return
                bonusEndBlock.sub(_from).mul(BONUS_MULTIPLIER).add(
                    _to.sub(bonusEndBlock)
                );
        }
    }

    function pendingReward(uint256 _pid, address _user)
        external
        view
        validatePoolByPid(_pid)
        returns (uint256)
    {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][_user];
        uint256 accCHNPerShare = pool.accCHNPerShare;
        uint256 supply = pool.totalAmountStake;
        if (block.number > pool.lastRewardBlock && supply != 0) {
            uint256 multiplier =
                getMultiplier(pool.lastRewardBlock, block.number);
            uint256 reward =
                multiplier.mul(rewardPerBlock).mul(pool.allocPoint).div(
                    totalAllocPoint
                );
            accCHNPerShare = accCHNPerShare.add(
                reward.mul(1e12).div(supply)
            );
        }
        return user.amount.mul(accCHNPerShare).div(1e12).add(user.pendingTokenReward).sub(user.rewardDebt);
    }

    // Update reward vairables for all pools. Be careful of gas spending!
    function massUpdatePools() public {
        uint256 length = poolInfo.length;
        for (uint256 pid = 0; pid < length; ++pid) {
            updatePool(pid);
        }
    }

    // Update reward variables of the given pool to be up-to-date.
    function updatePool(uint256 _pid) public validatePoolByPid(_pid) {
        PoolInfo storage pool = poolInfo[_pid];
        if (block.number <= pool.lastRewardBlock) {
            return;
        }
        uint256 supply = pool.totalAmountStake;
        if (supply == 0) {
            pool.lastRewardBlock = block.number;
            return;
        }
        uint256 multiplier = getMultiplier(pool.lastRewardBlock, block.number);
        uint256 reward =
            multiplier.mul(rewardPerBlock).mul(pool.allocPoint).div(
                totalAllocPoint
            );
        pool.accCHNPerShare = pool.accCHNPerShare.add(
            reward.mul(1e12).div(supply)
        );
        pool.lastRewardBlock = block.number;
    }

    function _moveDelegates(uint256 _pid, address dstRep, uint256 amount, bool stake) internal {
        if (amount > 0) {
            if (dstRep != address(0)) {
                uint32 dstRepNum = numCheckpoints[_pid][dstRep];
                uint256 dstRepOld = dstRepNum > 0 ? checkpoints[_pid][dstRep][dstRepNum - 1].votes : 0;
                if (stake) {
                    uint256 dstRepNew = dstRepOld.add(amount);
                    _writeCheckpoint(_pid, dstRep, dstRepNum, dstRepOld, dstRepNew);
                } else {
                    uint256 dstRepNew = dstRepOld.sub(amount);
                    _writeCheckpoint(_pid, dstRep, dstRepNum, dstRepOld, dstRepNew);
                }
            }
        }
    }

    // Only support non-deflationary tokens staking
    function stake(uint256 _pid, uint256 _amount) public validatePoolByPid(_pid) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        updatePool(_pid);
        if (user.amount > 0) {
            uint256 pending =
                user.amount.mul(pool.accCHNPerShare).div(1e12).sub(
                    user.rewardDebt
                );
            user.pendingTokenReward = user.pendingTokenReward.add(pending);
        }
        pool.totalAmountStake = pool.totalAmountStake.add(_amount);
        pool.stakeToken.safeTransferFrom(
            address(msg.sender),
            address(this),
            _amount
        );
        user.amount = user.amount.add(_amount);
        user.rewardDebt = user.amount.mul(pool.accCHNPerShare).div(1e12);

        _moveDelegates(_pid, msg.sender, _amount, true);
        emit Stake(msg.sender, _pid, _amount);
    }

    function withdraw(uint256 _pid, uint256 _amount) public validatePoolByPid(_pid) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        require(user.amount >= _amount, "withdraw: not good");
        updatePool(_pid);
        uint256 pending =
            user.amount.mul(pool.accCHNPerShare).div(1e12).sub(
                user.rewardDebt
            );
        // pending = pending.add(user.pendingTokenReward);
        // pool.stakeToken.safeTransfer(address(msg.sender), pending);
        user.pendingTokenReward = user.pendingTokenReward + pending;
        user.amount = user.amount.sub(_amount);
        pool.totalAmountStake = pool.totalAmountStake.sub(_amount);
        user.rewardDebt = user.amount.mul(pool.accCHNPerShare).div(1e12);
        pool.stakeToken.safeTransfer(address(msg.sender), _amount);

        // Remove delegates from staking user
        _moveDelegates(_pid, msg.sender, _amount, false);
        emit Withdraw(msg.sender, _pid, _amount, 0);
    }

    // Withdraw without caring about rewards. EMERGENCY ONLY.
    function emergencyWithdraw(uint256 _pid) public validatePoolByPid(_pid) {
        PoolInfo storage pool = poolInfo[_pid];
        UserInfo storage user = userInfo[_pid][msg.sender];
        uint256 userAmount = user.amount;
        user.amount = 0;
        user.rewardDebt = 0;
        user.pendingTokenReward = 0;
        pool.totalAmountStake = pool.totalAmountStake.sub(userAmount);
        pool.stakeToken.safeTransfer(address(msg.sender), userAmount);
        // Remove delegates from staking user
        _moveDelegates(_pid, msg.sender, userAmount, false);
        emit EmergencyWithdraw(msg.sender, _pid, userAmount);
    }

    function claimRewardFromVault(address userAddress, uint256 pid) public validatePoolByPid(pid) returns (uint256) {
        require(msg.sender == rewardVault, "Ownable: only reward vault");
        PoolInfo storage pool = poolInfo[pid];
        UserInfo storage user = userInfo[pid][userAddress];
        updatePool(pid);
        uint256 pending =
            user.amount.mul(pool.accCHNPerShare).div(1e12).sub(
                user.rewardDebt
            );
        pending = pending + user.pendingTokenReward;
        user.pendingTokenReward = 0;
        user.rewardDebt = user.amount.mul(pool.accCHNPerShare).div(1e12);
        emit ClaimRewardFromVault(userAddress, pid);
        return pending;
    }

    /**
     * @notice Determine the prior number of votes for an account as of a block number
     * @dev Block number must be a finalized block or else this function will revert to prevent misinformation.
     * ...

// [truncated — 52332 bytes total]

Read Contract

BONUS_MULTIPLIER 0x8aa28550 → uint256
bonusEndBlock 0x1aed6553 → uint256
checkpoints 0x525eef2f → uint32, uint256
getMultiplier 0x8dbb1e3a → uint256
getPriorVotes 0xc418441c → uint256
getStakingAmount 0xb3349467 → uint256
numCheckpoints 0xd82ada50 → uint32
owner 0x8da5cb5b → address
pendingReward 0x98969e82 → uint256
poolInfo 0x1526fe27 → address, uint256, uint256, uint256, uint256
poolLength 0x081e3eda → uint256
poolTokens 0xa9126169 → bool
rewardPerBlock 0x8ae39cac → uint256
rewardToken 0xf7c618c1 → address
rewardVault 0x3a2c6777 → address
startBlock 0x48cd4cb1 → uint256
totalAllocPoint 0x17caf6f1 → uint256
userInfo 0x93f1a40b → uint256, uint256, uint256

Write Contract 12 functions

These functions modify contract state and require a wallet transaction to execute.

add 0x2b8bbbe8
uint256 _allocPoint
address _stakeToken
claimRewardFromVault 0x4bea3f03
address userAddress
uint256 pid
returns: uint256
emergencyWithdraw 0x5312ea8e
uint256 _pid
initialize 0x5df5f96f
address _rewardToken
uint256 _rewardPerBlock
uint256 _startBlock
uint256 _bonusEndBlock
uint256 _multiplier
address _rewardVault
massUpdatePools 0x630b5ba1
No parameters
renounceOwnership 0x715018a6
No parameters
set 0x1ab06ee5
uint256 _pid
uint256 _allocPoint
setRewardPerblock 0xa6d6f4ed
uint256 speed
stake 0x7b0472f0
uint256 _pid
uint256 _amount
transferOwnership 0xf2fde38b
address newOwner
updatePool 0x51eb05a6
uint256 _pid
withdraw 0x441a3e70
uint256 _pid
uint256 _amount

Top Interactions

Recent Transactions

CSV
|
Hash Method Block Age From/To Value Txn Fee Type
0x15e445b2...bc514d 0x7b0472f0 24,458,941 IN 0xF5B4dFc1...F4C4 0 ETH EIP-1559
0x1aeeef42...e68a5e 0x7b0472f0 24,458,941 IN 0xcffe148D...89E5 0 ETH EIP-1559
0xfcf88dfd...8b8214 0x7b0472f0 24,458,928 IN 0xD700055f...060e 0 ETH EIP-1559
0x408d195e...eee60d 0x7b0472f0 24,458,885 IN 0x57D58d89...DA6b 0 ETH 0.000007196498 ETH Legacy