|
| 1 | +pragma solidity ^0.8.10; |
| 2 | + |
| 3 | +import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol'; |
| 4 | +import {SafeERC20} from '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @title MockUSTBSubscription |
| 8 | + */ |
| 9 | +contract MockUSTBSubscription { |
| 10 | + using SafeERC20 for IERC20; |
| 11 | + |
| 12 | + uint256 constant USDC_PRECISION = 1e6; |
| 13 | + uint256 constant SUPERSTATE_TOKEN_PRECISION = 1e6; |
| 14 | + uint256 constant CHAINLINK_FEED_PRECISION = 1e8; |
| 15 | + |
| 16 | + address public immutable asset; |
| 17 | + address public immutable liquidity; |
| 18 | + uint256 public USTBPrice; // 1_100_000_000 is 1 USTB = 11 USDC, including chainlink precision |
| 19 | + |
| 20 | + /** |
| 21 | + * @param _asset Address of asset token, ie USTB |
| 22 | + * @param _liquidity Address of liquidity token, ie USDC |
| 23 | + */ |
| 24 | + constructor(address _asset, address _liquidity, uint256 _price) { |
| 25 | + asset = _asset; |
| 26 | + liquidity = _liquidity; |
| 27 | + setUSTBPrice(_price); |
| 28 | + } |
| 29 | + |
| 30 | + function test_coverage_ignore() public virtual { |
| 31 | + // Intentionally left blank. |
| 32 | + // Excludes contract from coverage. |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @notice The ```subscribe``` function takes in stablecoins and mints SuperstateToken in the proper amount for the msg.sender depending on the current Net Asset Value per Share. |
| 37 | + * @param inAmount The amount of the stablecoin in |
| 38 | + * @param stablecoin The address of the stablecoin to calculate with |
| 39 | + */ |
| 40 | + function subscribe(uint256 inAmount, address stablecoin) external { |
| 41 | + ( |
| 42 | + uint256 superstateTokenOutAmount, |
| 43 | + uint256 stablecoinInAmountAfterFee, |
| 44 | + |
| 45 | + ) = calculateSuperstateTokenOut({inAmount: inAmount, stablecoin: stablecoin}); |
| 46 | + |
| 47 | + IERC20(stablecoin).safeTransferFrom({from: msg.sender, to: address(this), value: inAmount}); |
| 48 | + IERC20(asset).safeTransfer(msg.sender, superstateTokenOutAmount); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @notice The ```calculateSuperstateTokenOut``` function calculates the total amount of Superstate tokens you'll receive for the inAmount of stablecoin. Treats all stablecoins as if they are always worth a dollar. |
| 53 | + * @param inAmount The amount of the stablecoin in |
| 54 | + * @param stablecoin The address of the stablecoin to calculate with |
| 55 | + * @return superstateTokenOutAmount The amount of Superstate tokens received for inAmount of stablecoin |
| 56 | + * @return stablecoinInAmountAfterFee The amount of the stablecoin in after any fees |
| 57 | + * @return feeOnStablecoinInAmount The amount of the stablecoin taken in fees |
| 58 | + */ |
| 59 | + function calculateSuperstateTokenOut( |
| 60 | + uint256 inAmount, |
| 61 | + address stablecoin |
| 62 | + ) |
| 63 | + public |
| 64 | + view |
| 65 | + returns ( |
| 66 | + uint256 superstateTokenOutAmount, |
| 67 | + uint256 stablecoinInAmountAfterFee, |
| 68 | + uint256 feeOnStablecoinInAmount |
| 69 | + ) |
| 70 | + { |
| 71 | + StablecoinConfig memory config = supportedStablecoins[stablecoin]; |
| 72 | + |
| 73 | + feeOnStablecoinInAmount = 0; |
| 74 | + stablecoinInAmountAfterFee = inAmount - feeOnStablecoinInAmount; |
| 75 | + |
| 76 | + usdPerSuperstateTokenChainlinkRaw = USTBPrice; // 9.5 USDC/SUPERSTATE_TOKEN |
| 77 | + |
| 78 | + uint256 stablecoinPrecision = 10 ** 6; |
| 79 | + uint256 chainlinkFeedPrecision = 10 ** 8; |
| 80 | + |
| 81 | + // converts from a USD amount to a SUPERSTATE_TOKEN amount |
| 82 | + superstateTokenOutAmount = |
| 83 | + (stablecoinInAmountAfterFee * chainlinkFeedPrecision * SUPERSTATE_TOKEN_PRECISION) / |
| 84 | + (usdPerSuperstateTokenChainlinkRaw * stablecoinPrecision); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @notice Set the price of USTB, amount of USDC for 1 USTB. USTB/USDC both have 6 decimals. |
| 89 | + * @param newPrice The new price of USTB |
| 90 | + */ |
| 91 | + function setUSTBPrice(uint256 newPrice) public { |
| 92 | + USTBPrice = newPrice; |
| 93 | + } |
| 94 | +} |
0 commit comments