Contract Name:
NativeDepositor
Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
/**
* @dev Interface for Arbitrum special l2 functions
*/
interface IArbSys {
function arbBlockNumber() external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
/**
* @dev Interface for errors potentially used in all libraries (general names)
*/
interface IGeneralErrors {
error InitError();
error InvalidAddresses();
error InvalidAddress();
error InvalidInputLength();
error InvalidCollateralIndex();
error WrongParams();
error WrongLength();
error WrongOrder();
error WrongIndex();
error BlockOrder();
error Overflow();
error ZeroAddress();
error ZeroValue();
error AlreadyExists();
error DoesntExist();
error Paused();
error BelowMin();
error AboveMax();
error NotAuthorized();
error WrongTradeType();
error WrongOrderType();
error InsufficientBalance();
error UnsupportedChain();
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
/**
* @dev Interface for GToken contract
*/
interface IGToken {
struct GnsPriceProvider {
address addr;
bytes signature;
}
struct LockedDeposit {
address owner;
uint256 shares; // collateralConfig.precision
uint256 assetsDeposited; // collateralConfig.precision
uint256 assetsDiscount; // collateralConfig.precision
uint256 atTimestamp; // timestamp
uint256 lockDuration; // timestamp
}
struct ContractAddresses {
address asset;
address owner; // 2-week timelock contract
address manager; // 3-day timelock contract
address admin; // bypasses timelock, access to emergency functions
address gnsToken;
address lockedDepositNft;
address pnlHandler;
address openTradesPnlFeed;
GnsPriceProvider gnsPriceProvider;
}
struct Meta {
string name;
string symbol;
}
function manager() external view returns (address);
function admin() external view returns (address);
function currentEpoch() external view returns (uint256);
function currentEpochStart() external view returns (uint256);
function currentEpochPositiveOpenPnl() external view returns (uint256);
function updateAccPnlPerTokenUsed(
uint256 prevPositiveOpenPnl,
uint256 newPositiveOpenPnl
) external returns (uint256);
function getLockedDeposit(uint256 depositId) external view returns (LockedDeposit memory);
function sendAssets(uint256 assets, address receiver) external;
function receiveAssets(uint256 assets, address user) external;
function distributeReward(uint256 assets) external;
function tvl() external view returns (uint256);
function marketCap() external view returns (uint256);
function shareToAssetsPrice() external view returns (uint256);
function collateralConfig() external view returns (uint128, uint128);
event ManagerUpdated(address newValue);
event AdminUpdated(address newValue);
event PnlHandlerUpdated(address newValue);
event OpenTradesPnlFeedUpdated(address newValue);
event GnsPriceProviderUpdated(GnsPriceProvider newValue);
event WithdrawLockThresholdsPUpdated(uint256[2] newValue);
event MaxAccOpenPnlDeltaUpdated(uint256 newValue);
event MaxDailyAccPnlDeltaUpdated(uint256 newValue);
event MaxSupplyIncreaseDailyPUpdated(uint256 newValue);
event LossesBurnPUpdated(uint256 newValue);
event MaxGnsSupplyMintDailyPUpdated(uint256 newValue);
event MaxDiscountPUpdated(uint256 newValue);
event MaxDiscountThresholdPUpdated(uint256 newValue);
event CurrentMaxSupplyUpdated(uint256 newValue);
event DailyAccPnlDeltaReset();
event ShareToAssetsPriceUpdated(uint256 newValue);
event OpenTradesPnlFeedCallFailed();
event WithdrawRequested(
address indexed sender,
address indexed owner,
uint256 shares,
uint256 currEpoch,
uint256 indexed unlockEpoch
);
event WithdrawCanceled(
address indexed sender,
address indexed owner,
uint256 shares,
uint256 currEpoch,
uint256 indexed unlockEpoch
);
event DepositLocked(address indexed sender, address indexed owner, uint256 depositId, LockedDeposit d);
event DepositUnlocked(
address indexed sender,
address indexed receiver,
address indexed owner,
uint256 depositId,
LockedDeposit d
);
event RewardDistributed(address indexed sender, uint256 assets);
event AssetsSent(address indexed sender, address indexed receiver, uint256 assets);
event AssetsReceived(address indexed sender, address indexed user, uint256 assets, uint256 assetsLessDeplete);
event Depleted(address indexed sender, uint256 assets, uint256 amountGns);
event Refilled(address indexed sender, uint256 assets, uint256 amountGns);
event AccPnlPerTokenUsedUpdated(
address indexed sender,
uint256 indexed newEpoch,
uint256 prevPositiveOpenPnl,
uint256 newPositiveOpenPnl,
uint256 newEpochPositiveOpenPnl,
int256 newAccPnlPerTokenUsed
);
error OnlyManager();
error OnlyTradingPnlHandler();
error OnlyPnlFeed();
error AddressZero();
error PriceZero();
error ValueZero();
error BytesZero();
error NoActiveDiscount();
error BelowMin();
error AboveMax();
error WrongValue();
error WrongValues();
error GnsPriceCallFailed();
error GnsTokenPriceZero();
error PendingWithdrawal();
error EndOfEpoch();
error NotAllowed();
error NoDiscount();
error NotUnlocked();
error NotEnoughAssets();
error MaxDailyPnl();
error NotUnderCollateralized();
error AboveInflationLimit();
// Ownable
error OwnableInvalidOwner(address owner);
// ERC4626
error ERC4626ExceededMaxDeposit();
error ERC4626ExceededMaxMint();
error ERC4626ExceededMaxWithdraw();
error ERC4626ExceededMaxRedeem();
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import "./IGToken.sol";
/**
* @dev Extended interface for GToken contract
*/
interface IGTokenExtended is IGToken {
function asset() external view returns (address);
function deposit(uint256 assets, address receiver) external returns (uint256);
function depositWithDiscountAndLock(
uint256 assets,
uint256 lockDuration,
address receiver
) external returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
/**
* @dev Interface for WETH9 token
*/
interface IWETH9 {
function approve(address spender, uint256 amount) external returns (bool);
function transfer(address to, uint256 amount) external returns (bool);
function deposit() external payable;
function withdraw(uint256) external;
function balanceOf(address account) external view returns (uint256);
event Approval(address indexed src, address indexed guy, uint256 wad);
event Transfer(address indexed src, address indexed dst, uint256 wad);
event Deposit(address indexed dst, uint256 wad);
event Withdrawal(address indexed src, uint256 wad);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
/**
* @dev Interface for BlockManager_Mock contract (test helper)
*/
interface IBlockManager_Mock {
function getBlockNumber() external view returns (uint256);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
import "../interfaces/IArbSys.sol";
import "../interfaces/IGeneralErrors.sol";
import "../interfaces/mock/IBlockManager_Mock.sol";
/**
* @dev Chain helpers internal library
*/
library ChainUtils {
// Supported chains
uint256 internal constant ARBITRUM_MAINNET = 42161;
uint256 internal constant ARBITRUM_SEPOLIA = 421614;
uint256 internal constant POLYGON_MAINNET = 137;
uint256 internal constant BASE_MAINNET = 8453;
uint256 internal constant APECHAIN_MAINNET = 33139;
uint256 internal constant TESTNET = 31337;
// Wrapped native tokens
address private constant ARBITRUM_MAINNET_WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;
address private constant ARBITRUM_SEPOLIA_WETH = 0x980B62Da83eFf3D4576C647993b0c1D7faf17c73;
address private constant POLYGON_MAINNET_WMATIC = 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270;
address private constant BASE_MAINNET_WETH = 0x4200000000000000000000000000000000000006;
address private constant APECHAIN_MAINNET_WAPE = 0x00000000000f7e000644657dC9417b185962645a; // Custom non-rebasing WAPE
IArbSys private constant ARB_SYS = IArbSys(address(100));
error Overflow();
/**
* @dev Returns the current block number (l2 block for arbitrum)
*/
function getBlockNumber() internal view returns (uint256) {
if (block.chainid == ARBITRUM_MAINNET || block.chainid == ARBITRUM_SEPOLIA) {
return ARB_SYS.arbBlockNumber();
}
if (block.chainid == TESTNET) {
return IBlockManager_Mock(address(420)).getBlockNumber();
}
return block.number;
}
/**
* @dev Returns blockNumber converted to uint48
* @param blockNumber block number to convert
*/
function getUint48BlockNumber(uint256 blockNumber) internal pure returns (uint48) {
if (blockNumber > type(uint48).max) revert Overflow();
return uint48(blockNumber);
}
/**
* @dev Returns the wrapped native token address for the current chain
*/
function getWrappedNativeToken() internal view returns (address) {
if (block.chainid == ARBITRUM_MAINNET) {
return ARBITRUM_MAINNET_WETH;
}
if (block.chainid == BASE_MAINNET) {
return BASE_MAINNET_WETH;
}
if (block.chainid == APECHAIN_MAINNET) {
return APECHAIN_MAINNET_WAPE;
}
if (block.chainid == POLYGON_MAINNET) {
return POLYGON_MAINNET_WMATIC;
}
if (block.chainid == ARBITRUM_SEPOLIA) {
return ARBITRUM_SEPOLIA_WETH;
}
if (block.chainid == TESTNET) {
return address(421);
}
return address(0);
}
/**
* @dev Returns whether a token is the wrapped native token for the current chain
* @param _token token address to check
*/
function isWrappedNativeToken(address _token) internal view returns (bool) {
return _token != address(0) && _token == getWrappedNativeToken();
}
/**
* @dev Converts blocks to seconds for the current chain.
* @dev Important: the result is an estimation and may not be accurate. Use with caution.
* @param _blocks block count to convert to seconds
*/
function convertBlocksToSeconds(uint256 _blocks) internal view returns (uint256) {
uint256 millisecondsPerBlock;
if (block.chainid == ARBITRUM_MAINNET || block.chainid == ARBITRUM_SEPOLIA) {
millisecondsPerBlock = 300; // 0.3 seconds per block
} else if (block.chainid == BASE_MAINNET) {
millisecondsPerBlock = 2000; // 2 seconds per block
} else if (block.chainid == POLYGON_MAINNET) {
millisecondsPerBlock = 2200; // 2.2 seconds per block
} else if (block.chainid == APECHAIN_MAINNET) {
millisecondsPerBlock = 12000; // for apescan we use L1 blocktime (12s)
} else if (block.chainid == TESTNET) {
millisecondsPerBlock = 1000; // 1 second per block
} else {
revert IGeneralErrors.UnsupportedChain();
}
return Math.mulDiv(_blocks, millisecondsPerBlock, 1000, Math.Rounding.Up);
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;
import "../interfaces/IGTokenExtended.sol";
import "../interfaces/IWETH9.sol";
import "../interfaces/IGeneralErrors.sol";
import "../libraries/ChainUtils.sol";
/**
* @dev GToken depositor helper. Accepts native tokens, wraps them and deposits them for msg.sender
*/
contract NativeDepositor {
receive() external payable {}
function validateRequest(
IGTokenExtended _gToken,
uint256 _value,
address _receiver
) public view returns (address asset) {
if (address(_gToken) == address(0) || _receiver == address(0)) revert IGeneralErrors.ZeroAddress();
if (_value == 0) revert IGeneralErrors.ZeroValue();
asset = _gToken.asset();
if (!ChainUtils.isWrappedNativeToken(asset)) revert IGeneralErrors.InvalidAddress();
}
/**
* @dev Accepts native payment, wraps native token and deposits value for `_receiver`
* @param _gToken the gToken address
* @param _receiver the address receiving the gTokens
*/
function deposit(IGTokenExtended _gToken, address _receiver) external payable returns (uint256 shares) {
IWETH9 asset = IWETH9(validateRequest(_gToken, msg.value, _receiver));
asset.deposit{value: msg.value}();
asset.approve(address(_gToken), msg.value);
return _gToken.deposit(msg.value, _receiver);
}
/**
* @dev Accepts native payment, wraps native token and deposits value for `_receiver` with discount and lock
* @param _gToken the gToken address
* @param _lockDuration the duration of the lock
* @param _receiver the address receiving the gTokens
*/
function depositWithDiscountAndLock(
IGTokenExtended _gToken,
uint256 _lockDuration,
address _receiver
) external payable returns (uint256 shares) {
IWETH9 asset = IWETH9(validateRequest(_gToken, msg.value, _receiver));
asset.deposit{value: msg.value}();
asset.approve(address(_gToken), msg.value);
return _gToken.depositWithDiscountAndLock(msg.value, _lockDuration, _receiver);
}
}