Repository: opynfinance/squeeth-monorepo Branch: main Commit: 10e70837705a Files: 886 Total size: 25.4 MB Directory structure: gitextract_ry7xy3jk/ ├── .circleci/ │ └── config.yml ├── .editorconfig ├── .github/ │ ├── ISSUE_TEMPLATE/ │ │ ├── bug_report.md │ │ └── feature_request.md │ └── pull_request_template.md ├── .gitignore ├── .gitmodules ├── .gitpod.yml ├── .husky/ │ └── pre-commit ├── .huskyrc ├── .lintstagedrc ├── README.md ├── package.json └── packages/ ├── crab-netting/ │ ├── LICENSE_BUSL │ ├── foundry.toml │ ├── remappings.txt │ ├── script/ │ │ └── Counter.s.sol │ ├── src/ │ │ ├── CrabNetting.sol │ │ └── interfaces/ │ │ ├── IController.sol │ │ ├── ICrabStrategyV2.sol │ │ ├── IOracle.sol │ │ └── IWETH.sol │ └── test/ │ ├── BaseForkSetup.t.sol │ ├── BaseSetup.t.sol │ ├── Deposit.t.sol │ ├── DepositAuction.t.sol │ ├── ForkTestNetAtPrice.sol │ ├── Netting.t.sol │ ├── PriceChecks.sol │ ├── QueuedBalances.t.sol │ ├── SkipDeposits.t.sol │ ├── WithdrawAuction.t.sol │ └── utils/ │ ├── SigUtils.sol │ └── UniswapQuote.sol ├── frontend/ │ ├── .babelrc │ ├── .eslintrc │ ├── .gitignore │ ├── .nvmrc │ ├── .prettierrc │ ├── CONTRIBUTING.md │ ├── LICENSE │ ├── README.md │ ├── custom.d.ts │ ├── cypress/ │ │ ├── TEST.md │ │ ├── fixtures/ │ │ │ └── example.json │ │ ├── integration/ │ │ │ ├── pages/ │ │ │ │ ├── header.js │ │ │ │ ├── notifications.js │ │ │ │ ├── onboard.js │ │ │ │ ├── page.js │ │ │ │ └── trade.js │ │ │ └── specs/ │ │ │ ├── 01-trade-long.spec.js │ │ │ ├── 02-trade-short.spec.js │ │ │ └── 06-manual-short.spec.js │ │ ├── plugins/ │ │ │ └── index.js │ │ └── support/ │ │ ├── commands.js │ │ └── index.js │ ├── cypress.json │ ├── env.example │ ├── jest.config.js │ ├── jest.setup.ts │ ├── middleware.ts │ ├── next-env.d.ts │ ├── next.config.js │ ├── package.json │ ├── pages/ │ │ ├── _app.tsx │ │ ├── _document.tsx │ │ ├── api/ │ │ │ ├── __tests__/ │ │ │ │ └── isValidAddress.test.ts │ │ │ ├── auction/ │ │ │ │ └── lastHedgeAuction.ts │ │ │ ├── charts/ │ │ │ │ └── longchart.ts │ │ │ ├── currentsqueethvol.ts │ │ │ ├── historicalprice.ts │ │ │ ├── isValidAddress.ts │ │ │ ├── pnl.tsx │ │ │ ├── strikes.ts │ │ │ ├── tvl.ts │ │ │ ├── twelvedata.ts │ │ │ └── updateBlockedAddress.ts │ │ ├── blocked.tsx │ │ ├── index.tsx │ │ ├── lp.tsx │ │ ├── mint.tsx │ │ ├── opt-out.tsx │ │ ├── pos-merge.tsx │ │ ├── positions.tsx │ │ ├── privacy-policy.tsx │ │ ├── share-pnl/ │ │ │ └── [...slug].tsx │ │ ├── squeeth.tsx │ │ ├── strategies/ │ │ │ ├── bull.tsx │ │ │ ├── crab.tsx │ │ │ └── index.tsx │ │ ├── terms-of-service-faq.tsx │ │ ├── terms-of-service.tsx │ │ └── vault/ │ │ └── [vid].tsx │ ├── sentry.client.config.ts │ ├── sentry.server.config.ts │ ├── src/ │ │ ├── abis/ │ │ │ ├── NFTpositionmanager.json │ │ │ ├── auctionBull.json │ │ │ ├── bullEmergencyWithdraw.json │ │ │ ├── bullShutdownEmergencyWithdraw.json │ │ │ ├── bullStrategy.json │ │ │ ├── controller.json │ │ │ ├── controllerHelper.json │ │ │ ├── crabHelper.json │ │ │ ├── crabMigration.json │ │ │ ├── crabNetting.json │ │ │ ├── crabStrategy.json │ │ │ ├── crabStrategyV2.json │ │ │ ├── erc20.json │ │ │ ├── erc721.json │ │ │ ├── eulerEToken.json │ │ │ ├── eulerSimpleLens.json │ │ │ ├── flashBullStrategy.json │ │ │ ├── oracle.json │ │ │ ├── quoter.json │ │ │ ├── shortHelper.json │ │ │ ├── squeeth.json │ │ │ ├── swapRouter.json │ │ │ ├── swapRouter2.json │ │ │ ├── uniswapPool.json │ │ │ ├── vaultManager.json │ │ │ └── weth.json │ │ ├── components/ │ │ │ ├── Alert.tsx │ │ │ ├── Alerts/ │ │ │ │ ├── ShutdownAlert.tsx │ │ │ │ └── ZenBullAlert.tsx │ │ │ ├── Announcement.tsx │ │ │ ├── Button/ │ │ │ │ ├── WalletButton.tsx │ │ │ │ └── index.tsx │ │ │ ├── Charts/ │ │ │ │ ├── BullStrategyPerformanceChart.tsx │ │ │ │ ├── CrabStrategyChart.tsx │ │ │ │ ├── CrabStrategyV2PnLChart.tsx │ │ │ │ ├── FundingChart.tsx │ │ │ │ ├── LPBuyChart.tsx │ │ │ │ ├── LPMintChart.tsx │ │ │ │ ├── LongChart.tsx │ │ │ │ ├── LongChartPayoff.tsx │ │ │ │ ├── LongSqueethPayoff.tsx │ │ │ │ ├── ShortChart.tsx │ │ │ │ ├── ShortFundingChart.tsx │ │ │ │ ├── ShortSqueethPayoff.tsx │ │ │ │ └── VaultChart.tsx │ │ │ ├── Checkbox.tsx │ │ │ ├── CollatRange.tsx │ │ │ ├── CollatRatioSlider.tsx │ │ │ ├── CustomProgress.tsx │ │ │ ├── CustomSlider.tsx │ │ │ ├── CustomSwitch.tsx │ │ │ ├── DefaultSiteSeo/ │ │ │ │ └── DefaultSiteSeo.tsx │ │ │ ├── Emoji.tsx │ │ │ ├── HidePnLText.tsx │ │ │ ├── IV.tsx │ │ │ ├── Input/ │ │ │ │ ├── NumberInput.tsx │ │ │ │ └── PrimaryInput.tsx │ │ │ ├── InputNew/ │ │ │ │ ├── InputBase.tsx │ │ │ │ ├── InputNumber.tsx │ │ │ │ ├── InputToken.tsx │ │ │ │ └── index.tsx │ │ │ ├── LabelWithTooltip.test.tsx │ │ │ ├── LabelWithTooltip.tsx │ │ │ ├── LandingPage/ │ │ │ │ ├── DesktopLandingPage.tsx │ │ │ │ └── MobileLandingPage.tsx │ │ │ ├── LegendBox.tsx │ │ │ ├── LinkWrapper.tsx │ │ │ ├── Lp/ │ │ │ │ ├── GetSqueeth.tsx │ │ │ │ ├── LPPosition.tsx │ │ │ │ ├── LPTable.tsx │ │ │ │ ├── ObtainSqueeth.tsx │ │ │ │ ├── ProvideLiquidity.tsx │ │ │ │ ├── SelectMethod.tsx │ │ │ │ ├── SqueethInfo.tsx │ │ │ │ └── Stepper.tsx │ │ │ ├── MarkdownPage.tsx │ │ │ ├── Metric.tsx │ │ │ ├── Modal/ │ │ │ │ ├── MobileModal.tsx │ │ │ │ ├── Modal.tsx │ │ │ │ └── UniswapIframe.tsx │ │ │ ├── Nav.tsx │ │ │ ├── PnLTooltip.tsx │ │ │ ├── Popup.tsx │ │ │ ├── PositionCard.tsx │ │ │ ├── RestrictionInfo.tsx │ │ │ ├── SettingsMenu.tsx │ │ │ ├── SharePnl/ │ │ │ │ └── PnlChart.tsx │ │ │ ├── SqueethCard.tsx │ │ │ ├── StepperBox.tsx │ │ │ ├── Strategies/ │ │ │ │ ├── Bull/ │ │ │ │ │ ├── About/ │ │ │ │ │ │ ├── AdvancedMetrics.tsx │ │ │ │ │ │ ├── NextRebalanceTimer.tsx │ │ │ │ │ │ ├── ProfitabilityChart.tsx │ │ │ │ │ │ └── index.tsx │ │ │ │ │ ├── BullTrade/ │ │ │ │ │ │ ├── Deposit.tsx │ │ │ │ │ │ ├── EmergencyWithdraw.tsx │ │ │ │ │ │ ├── ShutdownEmergencyWithdraw.tsx │ │ │ │ │ │ ├── Withdraw.tsx │ │ │ │ │ │ ├── index.tsx │ │ │ │ │ │ └── styles.ts │ │ │ │ │ ├── MyPosition/ │ │ │ │ │ │ ├── PnL.tsx │ │ │ │ │ │ └── index.tsx │ │ │ │ │ ├── StrategyPerformance.tsx │ │ │ │ │ └── index.tsx │ │ │ │ ├── Crab/ │ │ │ │ │ ├── About/ │ │ │ │ │ │ ├── AdvancedMetrics.tsx │ │ │ │ │ │ ├── NextRebalanceTimer.tsx │ │ │ │ │ │ ├── ProfitabilityChart.tsx │ │ │ │ │ │ └── index.tsx │ │ │ │ │ ├── CapDetails.tsx │ │ │ │ │ ├── CapDetailsV2.tsx │ │ │ │ │ ├── CrabMetricsV2.tsx │ │ │ │ │ ├── CrabMigrate.tsx │ │ │ │ │ ├── CrabPosition.tsx │ │ │ │ │ ├── CrabPositionV2.tsx │ │ │ │ │ ├── CrabTrade.tsx │ │ │ │ │ ├── CrabTradeV2/ │ │ │ │ │ │ ├── Deposit.tsx │ │ │ │ │ │ ├── Withdraw.tsx │ │ │ │ │ │ ├── index.tsx │ │ │ │ │ │ ├── styles.ts │ │ │ │ │ │ └── types.ts │ │ │ │ │ ├── MigrationNotice.tsx │ │ │ │ │ ├── MyPosition/ │ │ │ │ │ │ ├── CrabPosition.tsx │ │ │ │ │ │ ├── PnL.tsx │ │ │ │ │ │ ├── QueuedPosition.tsx │ │ │ │ │ │ └── index.tsx │ │ │ │ │ ├── StrategyChartsV2.tsx │ │ │ │ │ ├── StrategyHistory.tsx │ │ │ │ │ ├── StrategyHistoryV2.tsx │ │ │ │ │ ├── StrategyInfoV1.tsx │ │ │ │ │ ├── StrategyInfoV2.tsx │ │ │ │ │ ├── StrategyPerformance.tsx │ │ │ │ │ └── util.ts │ │ │ │ ├── SharePnl.tsx │ │ │ │ ├── StrategyInfoItem.tsx │ │ │ │ └── styles.ts │ │ │ ├── StrategyLayout/ │ │ │ │ └── StrategyLayout.tsx │ │ │ ├── StrikeWarning.tsx │ │ │ ├── Tabs.tsx │ │ │ ├── Trade/ │ │ │ │ ├── Cancelled.tsx │ │ │ │ ├── Confirmed.tsx │ │ │ │ ├── Long/ │ │ │ │ │ └── index.tsx │ │ │ │ ├── Mint.tsx │ │ │ │ ├── Short/ │ │ │ │ │ └── index.tsx │ │ │ │ ├── SqueethMetrics.tsx │ │ │ │ ├── WelcomeModal.tsx │ │ │ │ └── index.tsx │ │ │ ├── TradeOld/ │ │ │ │ ├── Cancelled.tsx │ │ │ │ ├── Confirmed.tsx │ │ │ │ ├── Long/ │ │ │ │ │ ├── BreakEven.tsx │ │ │ │ │ └── index.tsx │ │ │ │ ├── TradeDetails.tsx │ │ │ │ ├── TradeInfoItem.tsx │ │ │ │ └── UniswapData.tsx │ │ │ ├── TradeSettings.tsx │ │ │ └── WalletFailModal.tsx │ │ ├── constants/ │ │ │ ├── address.ts │ │ │ ├── diagram.ts │ │ │ ├── enums.ts │ │ │ └── index.ts │ │ ├── context/ │ │ │ ├── lp.tsx │ │ │ └── restrict-user.tsx │ │ ├── hooks/ │ │ │ ├── __tests__/ │ │ │ │ ├── useAppCallback.test.ts │ │ │ │ ├── useAppEffect.test.ts │ │ │ │ └── useAppMemo.test.ts │ │ │ ├── contracts/ │ │ │ │ ├── useAllowance.ts │ │ │ │ ├── useERC721.ts │ │ │ │ ├── useLiquidations.ts │ │ │ │ ├── useLongHelper.ts │ │ │ │ ├── useOracle.ts │ │ │ │ ├── useShortHelper.ts │ │ │ │ ├── useTokenBalance.ts │ │ │ │ ├── useVaultManager.ts │ │ │ │ └── useWeth.ts │ │ │ ├── payOffGraph/ │ │ │ │ ├── useCrabVaultPayoff.ts │ │ │ │ ├── useShortParams.ts │ │ │ │ └── useSqueethShortPayOffGraph.ts │ │ │ ├── useAmplitude.ts │ │ │ ├── useAppCallback.tsx │ │ │ ├── useAppEffect.tsx │ │ │ ├── useAppMemo.tsx │ │ │ ├── useAsyncMemo.tsx │ │ │ ├── useBullHedgeHistory.ts │ │ │ ├── useBullPosition/ │ │ │ │ ├── index.ts │ │ │ │ ├── mocks.ts │ │ │ │ └── useBullPosition.ts │ │ │ ├── useCopyClipboard.ts │ │ │ ├── useCrabAuctionHistory.ts │ │ │ ├── useCrabPosition/ │ │ │ │ ├── index.ts │ │ │ │ ├── mocks.ts │ │ │ │ ├── useCrabPosition.test.tsx │ │ │ │ └── useCrabPosition.ts │ │ │ ├── useCrabV2AuctionHistory.ts │ │ │ ├── useENS.ts │ │ │ ├── useETHPrice.ts │ │ │ ├── useExecuteOnce.ts │ │ │ ├── useInterval.ts │ │ │ ├── useIntervalAsync.ts │ │ │ ├── useNormHistory.ts │ │ │ ├── useNormHistoryFromTime.ts │ │ │ ├── useOSQTHPrice.ts │ │ │ ├── usePopup.tsx │ │ │ ├── useRenderCounter.js │ │ │ ├── useStateWithReset.ts │ │ │ ├── useTVL.ts │ │ │ ├── useTrackSiteReload.ts │ │ │ ├── useTrackTransactionFlow.ts │ │ │ ├── useTransactionHistory.ts │ │ │ ├── useUniswapQuoter.tsx │ │ │ ├── useUniswapTicks.ts │ │ │ ├── useUsdAmount.ts │ │ │ ├── useUserBullTxHistory.ts │ │ │ ├── useUserCrabTxHistory.ts │ │ │ ├── useUserCrabV2TxHistory.ts │ │ │ ├── useVault.ts │ │ │ ├── useVaultData.ts │ │ │ ├── useVaultHistory.ts │ │ │ └── useYourVaults.ts │ │ ├── lib/ │ │ │ └── pnl.ts │ │ ├── markdown/ │ │ │ ├── pos-merge.md │ │ │ ├── privacy-policy.md │ │ │ ├── terms-of-service-faq.md │ │ │ └── terms-of-service.md │ │ ├── pages/ │ │ │ └── positions/ │ │ │ ├── BullPosition.tsx │ │ │ ├── ConnectWallet.tsx │ │ │ ├── CrabPosition.tsx │ │ │ ├── CrabPositionV2.tsx │ │ │ ├── History.tsx │ │ │ ├── LPedSqueeth.tsx │ │ │ ├── LongSqueeth.tsx │ │ │ ├── MintedSqueeth.tsx │ │ │ ├── Positions.tsx │ │ │ ├── ShortSqueeth.tsx │ │ │ ├── ShortSqueethLiquidated.tsx │ │ │ ├── YourVaults.test.tsx │ │ │ ├── YourVaults.tsx │ │ │ └── useStyles.ts │ │ ├── queries/ │ │ │ ├── squeeth/ │ │ │ │ ├── __generated__/ │ │ │ │ │ ├── Vault.ts │ │ │ │ │ ├── VaultHistory.ts │ │ │ │ │ ├── Vaults.ts │ │ │ │ │ ├── YourVaults.ts │ │ │ │ │ ├── bullHedges.ts │ │ │ │ │ ├── crabAuctions.ts │ │ │ │ │ ├── crabV2Auctions.ts │ │ │ │ │ ├── liquidations.ts │ │ │ │ │ ├── normalizationFactorUpdates.ts │ │ │ │ │ ├── normalizationFactorUpdatesTime.ts │ │ │ │ │ ├── strategyQuery.ts │ │ │ │ │ ├── subscriptionVaultHistory.ts │ │ │ │ │ ├── subscriptionVaults.ts │ │ │ │ │ ├── userBullTxes.ts │ │ │ │ │ ├── userCrabTxes.ts │ │ │ │ │ └── userCrabV2Txes.ts │ │ │ │ ├── bullHedgeQuery.ts │ │ │ │ ├── crabAuctionQuery.ts │ │ │ │ ├── crabV2AuctionQuery.ts │ │ │ │ ├── liquidationsQuery.ts │ │ │ │ ├── normHistoryQuery.ts │ │ │ │ ├── strategyQuery.ts │ │ │ │ ├── userBullQuery.ts │ │ │ │ ├── userCrabTxQuery.ts │ │ │ │ ├── userCrabV2TxQuery.ts │ │ │ │ ├── vaultHistoryQuery.ts │ │ │ │ └── vaultsQuery.ts │ │ │ └── uniswap/ │ │ │ ├── __generated__/ │ │ │ │ ├── activePositions.ts │ │ │ │ ├── positions.ts │ │ │ │ ├── subscriptionSwaps.ts │ │ │ │ ├── subscriptionSwapsRopsten.ts │ │ │ │ ├── subscriptionpositions.ts │ │ │ │ ├── swaps.ts │ │ │ │ ├── swapsRopsten.ts │ │ │ │ ├── ticks.ts │ │ │ │ └── transactions.ts │ │ │ ├── positionsQuery.ts │ │ │ ├── swapsQuery.ts │ │ │ ├── swapsRopstenQuery.ts │ │ │ ├── ticksQuery.ts │ │ │ └── transactionsQuery.ts │ │ ├── server/ │ │ │ ├── firebase-admin.ts │ │ │ └── ipqs.ts │ │ ├── state/ │ │ │ ├── bull/ │ │ │ │ ├── atoms.ts │ │ │ │ ├── hooks.ts │ │ │ │ └── utils.ts │ │ │ ├── contracts/ │ │ │ │ └── atoms.ts │ │ │ ├── controller/ │ │ │ │ ├── atoms.ts │ │ │ │ ├── hooks.ts │ │ │ │ └── utils.ts │ │ │ ├── crab/ │ │ │ │ ├── atoms.ts │ │ │ │ ├── hooks.ts │ │ │ │ └── utils.ts │ │ │ ├── crabMigration/ │ │ │ │ ├── atom.ts │ │ │ │ └── hooks.ts │ │ │ ├── ethPriceCharts/ │ │ │ │ └── atoms.ts │ │ │ ├── lp/ │ │ │ │ └── hooks.ts │ │ │ ├── nftmanager/ │ │ │ │ └── hooks.ts │ │ │ ├── pnl/ │ │ │ │ ├── atoms.ts │ │ │ │ └── hooks.ts │ │ │ ├── positions/ │ │ │ │ ├── atoms.ts │ │ │ │ ├── hooks.ts │ │ │ │ └── providers.tsx │ │ │ ├── squeethPool/ │ │ │ │ ├── atoms.ts │ │ │ │ ├── hooks.ts │ │ │ │ └── price.ts │ │ │ ├── trade/ │ │ │ │ └── atoms.ts │ │ │ └── wallet/ │ │ │ ├── apis.ts │ │ │ ├── atoms.ts │ │ │ └── hooks.ts │ │ ├── styles/ │ │ │ ├── index.ts │ │ │ └── useTextStyles.ts │ │ ├── theme.ts │ │ ├── types/ │ │ │ └── index.ts │ │ └── utils/ │ │ ├── __tests__/ │ │ │ └── stringifyDeps.test.ts │ │ ├── amplitude.ts │ │ ├── apollo-client.ts │ │ ├── atomTester.tsx │ │ ├── calculations.ts │ │ ├── cookies.ts │ │ ├── crisp-chat.ts │ │ ├── csvToJson.ts │ │ ├── error.ts │ │ ├── ethPriceCharts.ts │ │ ├── firestore.ts │ │ ├── floatifyBigNums.ts │ │ ├── formatter.ts │ │ ├── gasProvider.ts │ │ ├── getContract.ts │ │ ├── index.tsx │ │ ├── markdown.ts │ │ ├── pricer.ts │ │ ├── quoter.ts │ │ └── stringifyDeps.ts │ ├── styles/ │ │ ├── Home.module.css │ │ └── globals.css │ ├── synpress.json │ ├── tsconfig.json │ └── types/ │ └── global_apollo.ts ├── hardhat/ │ ├── .eslintrc.js │ ├── .gitbook.yaml │ ├── .prettierignore │ ├── .prettierrc │ ├── .solcover.js │ ├── CHANGE_DATE │ ├── LICENSE_BUSL │ ├── README.md │ ├── SUMMARY.md │ ├── USE_GRANT │ ├── arguments/ │ │ ├── Controller-goerli.js │ │ ├── ControllerHelper-goerli.js │ │ ├── CrabHelper-goerli.js │ │ ├── CrabHelper-mainnet.js │ │ ├── CrabMigration-goerli.js │ │ ├── CrabMigration-mainnet.js │ │ ├── CrabMigration-ropsten.js │ │ ├── CrabStrategy-goerli.js │ │ ├── CrabStrategyV2-goerli.js │ │ ├── CrabStrategyV2-mainnet.js │ │ ├── CrabStrategyV2-ropsten.js │ │ ├── OpynUsdc-goerli.js │ │ ├── OpynWeth-goerli.js │ │ ├── Quoter-goerli.js │ │ ├── ShortHelper-goerli.js │ │ ├── ShortPowerPerp-goerli.js │ │ ├── Timelock-goerli.js │ │ ├── Timelock-mainnet.js │ │ ├── Timelock-ropsten.js │ │ └── WPowerPerp-goerli.js │ ├── arguments.js │ ├── ci/ │ │ └── e2e.sh │ ├── contracts/ │ │ ├── core/ │ │ │ ├── Controller.sol │ │ │ ├── LICENSE_GPL_3 │ │ │ ├── Oracle.sol │ │ │ ├── ShortPowerPerp.sol │ │ │ └── WPowerPerp.sol │ │ ├── external/ │ │ │ ├── LICENSE_GPL_3 │ │ │ └── WETH9.sol │ │ ├── import/ │ │ │ ├── LICENSE_GPL_3 │ │ │ └── Uni.sol │ │ ├── interfaces/ │ │ │ ├── IController.sol │ │ │ ├── ICrabStrategyV2.sol │ │ │ ├── IERC20Detailed.sol │ │ │ ├── IEuler.sol │ │ │ ├── IOracle.sol │ │ │ ├── IShortPowerPerp.sol │ │ │ ├── IWETH9.sol │ │ │ ├── IWPowerPerp.sol │ │ │ └── LICENSE_MIT │ │ ├── libs/ │ │ │ ├── ABDKMath64x64.sol │ │ │ ├── LICENSE_BSD_4_Clause │ │ │ ├── LICENSE_GPL_3 │ │ │ ├── LICENSE_MIT │ │ │ ├── OracleLibrary.sol │ │ │ ├── Power2Base.sol │ │ │ ├── SqrtPriceMathPartial.sol │ │ │ ├── TickMathExternal.sol │ │ │ ├── Uint256Casting.sol │ │ │ └── VaultLib.sol │ │ ├── mocks/ │ │ │ ├── LICENSE_GPL_3 │ │ │ ├── LICENSE_MIT │ │ │ ├── MockController.sol │ │ │ ├── MockCrab.sol │ │ │ ├── MockErc20.sol │ │ │ ├── MockEuler.sol │ │ │ ├── MockEulerDToken.sol │ │ │ ├── MockOracle.sol │ │ │ ├── MockShortPowerPerp.sol │ │ │ ├── MockTimelock.sol │ │ │ ├── MockUniPositionManager.sol │ │ │ ├── MockUniswapV3Pool.sol │ │ │ ├── MockWSqueeth.sol │ │ │ └── OpynWETH9.sol │ │ ├── periphery/ │ │ │ ├── ControllerHelper.sol │ │ │ ├── EulerControllerHelper.sol │ │ │ ├── LICENSE_GPL_3 │ │ │ ├── ShortHelper.sol │ │ │ ├── UniswapControllerHelper.sol │ │ │ └── lib/ │ │ │ ├── ControllerHelperDataType.sol │ │ │ ├── ControllerHelperUtil.sol │ │ │ └── LiquidityAmounts.sol │ │ ├── strategy/ │ │ │ ├── AGPL_3 │ │ │ ├── CrabHelper.sol │ │ │ ├── CrabMigration.sol │ │ │ ├── CrabStrategy.sol │ │ │ ├── CrabStrategyV2.sol │ │ │ ├── LICENSE_GPL_3 │ │ │ ├── base/ │ │ │ │ ├── StrategyBase.sol │ │ │ │ ├── StrategyFlashSwap.sol │ │ │ │ └── StrategyMath.sol │ │ │ ├── helper/ │ │ │ │ └── StrategySwap.sol │ │ │ └── timelock/ │ │ │ ├── SafeMath.sol │ │ │ └── Timelock.sol │ │ └── test/ │ │ ├── ABDKTester.sol │ │ ├── CastingTester.sol │ │ ├── ControllerAccessTester.sol │ │ ├── ControllerTester.sol │ │ ├── LICENSE_BSD_4_Clause │ │ ├── LICENSE_BUSL │ │ ├── LICENSE_GPL_3 │ │ ├── LiquidationHelper.sol │ │ ├── OracleTester.sol │ │ └── VaultTester.sol │ ├── crab-migration.js │ ├── crabmigration-arguments.js │ ├── crabv2-arguments.js │ ├── deploy/ │ │ ├── 00_deploy_basic_coins.ts │ │ ├── 01_deploy_uniswapv3.ts │ │ ├── 02_deploy_core.ts │ │ ├── 03_deploy_pools.ts │ │ ├── 04_deploy_controller.ts │ │ ├── 05_deploy_periphery.ts │ │ ├── 06_deploy_crab_strategy.ts │ │ ├── 07_deploy_controller_helper.ts │ │ ├── 08_deploy_crab_v2.ts │ │ └── 09_deploy_crab_helper.ts │ ├── deployments/ │ │ ├── goerli/ │ │ │ ├── .chainId │ │ │ ├── ABDKMath64x64.json │ │ │ ├── Controller.json │ │ │ ├── ControllerHelper.json │ │ │ ├── ControllerHelperUtil.json │ │ │ ├── CrabHelper.json │ │ │ ├── CrabStrategyDeployment.json │ │ │ ├── CrabStrategyV2.json │ │ │ ├── MockErc20.json │ │ │ ├── NonfungiblePositionManager.json │ │ │ ├── OpynWETH9.json │ │ │ ├── Oracle.json │ │ │ ├── Quoter.json │ │ │ ├── ShortHelper.json │ │ │ ├── ShortPowerPerp.json │ │ │ ├── SqrtPriceMathPartial.json │ │ │ ├── SwapRouter.json │ │ │ ├── TickMathExternal.json │ │ │ ├── UniswapV3Factory.json │ │ │ ├── WPowerPerp.json │ │ │ └── solcInputs/ │ │ │ ├── 0233939f192df1cbf8f7a345e29af693.json │ │ │ ├── 05b9c7d057ff8b2bf36168b053759720.json │ │ │ ├── 31d85619debc3954507258d16df1741b.json │ │ │ ├── 59fefefa6d402a64e50de797130b653c.json │ │ │ ├── 5f1800dd06bba4733ee96eb427b535c3.json │ │ │ └── c53867cdbf46138ab42de1c7a1689875.json │ │ ├── mainnet/ │ │ │ ├── .chainId │ │ │ ├── ABDKMath64x64.json │ │ │ ├── Controller.json │ │ │ ├── ControllerHelper.json │ │ │ ├── ControllerHelperUtil.json │ │ │ ├── CrabHelper.json │ │ │ ├── CrabMigration.json │ │ │ ├── CrabStrategyV2.json │ │ │ ├── Oracle.json │ │ │ ├── Quoter.json │ │ │ ├── ShortHelper.json │ │ │ ├── ShortPowerPerp.json │ │ │ ├── SqrtPriceMathPartial.json │ │ │ ├── TickMathExternal.json │ │ │ ├── Timelock.json │ │ │ ├── WPowerPerp.json │ │ │ └── solcInputs/ │ │ │ ├── 30afee76cfee2d4cc1c12451e0258499.json │ │ │ ├── 56b4ec4ab07157f3d2fb7e1de805d93e.json │ │ │ ├── c53867cdbf46138ab42de1c7a1689875.json │ │ │ ├── d97d3d4b09e0d70518330d405a7dd9ff.json │ │ │ ├── e85d9fda89ae8c3780a8ae01da3f423c.json │ │ │ └── fc32ed9e00a9017e491333926bef5473.json │ │ ├── rinkebyArbitrum/ │ │ │ ├── .chainId │ │ │ ├── Controller.json │ │ │ ├── MockErc20.json │ │ │ ├── Oracle.json │ │ │ ├── Quoter.json │ │ │ ├── ShortHelper.json │ │ │ ├── VaultNFTManager.json │ │ │ ├── WSqueeth.json │ │ │ └── solcInputs/ │ │ │ ├── 1eb2d8df0d50660ef74af8c7c5cd526b.json │ │ │ ├── 49c79f88c6e4ede6ca1f2ffd46fef4fe.json │ │ │ ├── 4ad189f955354b6f5c4f3b0e4a7b5326.json │ │ │ ├── 63325f69769d83fcd46161b0222ee3cc.json │ │ │ ├── 65b5636a064484e3748f101fc0e531a8.json │ │ │ └── 7ff94f89a93cd7aa904196251e75bdb3.json │ │ └── ropsten/ │ │ ├── .chainId │ │ ├── ABDKMath64x64.json │ │ ├── Controller.json │ │ ├── ControllerHelper.json │ │ ├── ControllerHelperUtil.json │ │ ├── CrabMigration.json │ │ ├── CrabStrategyDeployment.json │ │ ├── CrabStrategyV2.json │ │ ├── NonfungiblePositionManager.json │ │ ├── Oracle.json │ │ ├── Quoter.json │ │ ├── ShortHelper.json │ │ ├── ShortPowerPerp.json │ │ ├── SqrtPriceMathPartial.json │ │ ├── SwapRouter.json │ │ ├── TickMathExternal.json │ │ ├── Timelock.json │ │ ├── UniswapV3Factory.json │ │ ├── WPowerPerp.json │ │ └── solcInputs/ │ │ ├── 30afee76cfee2d4cc1c12451e0258499.json │ │ ├── 5f450d03d2b8109eaf06de68c57d8c2b.json │ │ ├── 6176622d73f0107e1e6916638dbba9b9.json │ │ ├── ab83bcba860cd7677bf7e9d807e32da5.json │ │ ├── cd8e568ab17078249f8bced32d618ecf.json │ │ ├── d91027c30078be5bfba0223abd5c6e60.json │ │ ├── dfd7860ebd97501ecbdb5935c168e58b.json │ │ ├── e2a303b98172c2b7a0e4fda5f2d8e859.json │ │ ├── e85d9fda89ae8c3780a8ae01da3f423c.json │ │ └── fc32ed9e00a9017e491333926bef5473.json │ ├── docs/ │ │ ├── contract.hbs │ │ └── contracts-documentation/ │ │ ├── core/ │ │ │ ├── Controller.md │ │ │ ├── Oracle.md │ │ │ ├── ShortPowerPerp.md │ │ │ ├── WPowerPerp.md │ │ │ └── WSqueeth.md │ │ ├── external/ │ │ │ └── WETH9.md │ │ ├── import/ │ │ │ └── Uni.md │ │ ├── interfaces/ │ │ │ ├── IController.md │ │ │ ├── IERC20Detailed.md │ │ │ ├── IOracle.md │ │ │ ├── IShortPowerPerp.md │ │ │ ├── IVaultManagerNFT.md │ │ │ ├── IWETH9.md │ │ │ └── IWPowerPerp.md │ │ ├── libs/ │ │ │ ├── OracleLibrary.md │ │ │ ├── Power2Base.md │ │ │ └── VaultLib.md │ │ ├── mocks/ │ │ │ ├── IUniswapV3FlashCallback.md │ │ │ ├── MockController.md │ │ │ ├── MockErc20.md │ │ │ ├── MockOracle.md │ │ │ ├── MockShortPowerPerp.md │ │ │ ├── MockUniPositionManager.md │ │ │ ├── MockUniswapV3Pool.md │ │ │ ├── MockVaultNFTManager.md │ │ │ ├── MockWPowerPerp.md │ │ │ └── MockWSqueeth.md │ │ ├── periphery/ │ │ │ └── ShortHelper.md │ │ ├── strategy/ │ │ │ ├── CrabStrategy.md │ │ │ └── base/ │ │ │ ├── StrategyBase.md │ │ │ ├── StrategyFlashSwap.md │ │ │ └── StrategyMath.md │ │ └── test/ │ │ ├── ControllerTester.md │ │ ├── OracleTester.md │ │ └── VaultLibTester.md │ ├── env.example │ ├── hardhat.config.ts │ ├── package.json │ ├── scripts/ │ │ ├── deploy.js │ │ ├── docs/ │ │ │ └── docify.js │ │ ├── etherscan_verify.sh │ │ ├── publish.js │ │ ├── start_hardhat_fork.sh │ │ ├── stop_hardhat_fork.sh │ │ └── watch.js │ ├── tasks/ │ │ ├── addSqueethLiquidity.ts │ │ ├── addWethLiquidity.ts │ │ ├── buySqueeth.ts │ │ ├── buyWeth.ts │ │ ├── default.ts │ │ ├── increaseSlot.ts │ │ ├── sellSqueeth.ts │ │ ├── sellWeth.ts │ │ └── utils.ts │ ├── test/ │ │ ├── calculator.ts │ │ ├── e2e/ │ │ │ ├── crab-Migration.ts │ │ │ └── periphery/ │ │ │ └── controller-helper.ts │ │ ├── integration-tests/ │ │ │ ├── crabv2/ │ │ │ │ ├── crab-hedging-otc.ts │ │ │ │ ├── crab-helper.ts │ │ │ │ ├── crab-liquidation-dust.ts │ │ │ │ ├── crab-liquidation-flashswap.ts │ │ │ │ ├── crab-liquidation-full.ts │ │ │ │ ├── crab-shutdown.ts │ │ │ │ ├── crab-timelock.ts │ │ │ │ └── strategy-flow.ts │ │ │ ├── liquidation.ts │ │ │ ├── oracle-attack.ts │ │ │ ├── oracle_integration.ts │ │ │ ├── periphery/ │ │ │ │ └── controller-helper.ts │ │ │ ├── short_helper_integration.ts │ │ │ ├── strategy/ │ │ │ │ ├── crab-flashswap-liquidation.ts │ │ │ │ ├── crab-hedge-uniswap-price-based.ts │ │ │ │ ├── crab-hedge-uniswap-time-based.ts │ │ │ │ ├── crab-hedging-price-based.ts │ │ │ │ ├── crab-hedging-time-based.ts │ │ │ │ ├── crab-liquidation-dust.ts │ │ │ │ ├── crab-liquidation-full.ts │ │ │ │ ├── crab-shutdown.ts │ │ │ │ └── strategy-flow.ts │ │ │ └── uni-position-collateral.ts │ │ ├── setup.ts │ │ ├── unit-tests/ │ │ │ ├── abdk-math.ts │ │ │ ├── casting.ts │ │ │ ├── controller-funding.ts │ │ │ ├── controller-lptoken-collateral.ts │ │ │ ├── controller-pause-time.ts │ │ │ ├── controller-shutdown.ts │ │ │ ├── controller-vaults.ts │ │ │ ├── controller.ts │ │ │ ├── liquidation.ts │ │ │ ├── oracle-token-decimals.ts │ │ │ ├── oracle.ts │ │ │ ├── short-power-perp.ts │ │ │ ├── squeeth.ts │ │ │ ├── strategy/ │ │ │ │ ├── crab-migration.ts │ │ │ │ ├── crab-strategy-v2.ts │ │ │ │ ├── crab-strategy.ts │ │ │ │ └── timelock.ts │ │ │ └── vault-lib.ts │ │ ├── utils.ts │ │ └── vault-utils.ts │ └── tsconfig.json ├── services/ │ ├── graph-node/ │ │ ├── Dockerfile │ │ ├── README.md │ │ ├── bin/ │ │ │ ├── create │ │ │ ├── debug │ │ │ ├── deploy │ │ │ ├── reassign │ │ │ └── remove │ │ ├── build.sh │ │ ├── cloudbuild.yaml │ │ ├── docker-compose.yml │ │ ├── hooks/ │ │ │ └── post_checkout │ │ ├── setup.sh │ │ ├── start │ │ ├── tag.sh │ │ └── wait_for │ └── package.json ├── subgraph/ │ ├── .gitignore │ ├── README.md │ ├── config/ │ │ ├── goerli-config.json │ │ ├── localhost-config.json │ │ ├── mainnet-config.json │ │ ├── rinkebyArbitrum-config.json │ │ └── ropsten-config.json │ ├── package.json │ ├── schema.graphql │ ├── src/ │ │ ├── bullStrategy.ts │ │ ├── constants.ts │ │ ├── controller.ts │ │ ├── crabV2.ts │ │ ├── crabstrategy.ts │ │ └── util.ts │ └── subgraph.template.yaml ├── zen-bull-netting/ │ ├── .gitignore │ ├── README.md │ ├── docs/ │ │ ├── .gitignore │ │ ├── book.css │ │ ├── book.toml │ │ └── src/ │ │ ├── README.md │ │ ├── SUMMARY.md │ │ └── src/ │ │ ├── FlashSwap.sol/ │ │ │ ├── contract.CallbackValidation.md │ │ │ ├── contract.FlashSwap.md │ │ │ └── contract.PoolAddress.md │ │ ├── NettingLib.sol/ │ │ │ └── contract.NettingLib.md │ │ ├── README.md │ │ ├── ZenBullNetting.sol/ │ │ │ └── contract.ZenBullNetting.md │ │ └── interface/ │ │ ├── IController.sol/ │ │ │ └── contract.IController.md │ │ ├── ICrabStrategyV2.sol/ │ │ │ └── contract.ICrabStrategyV2.md │ │ ├── IEulerSimpleLens.sol/ │ │ │ └── contract.IEulerSimpleLens.md │ │ ├── IFlashZen.sol/ │ │ │ └── contract.IFlashZen.md │ │ ├── IOracle.sol/ │ │ │ └── contract.IOracle.md │ │ ├── IWETH.sol/ │ │ │ └── contract.IWETH.md │ │ ├── IZenBullStrategy.sol/ │ │ │ └── contract.IZenBullStrategy.md │ │ └── README.md │ ├── foundry.toml │ ├── remappings.txt │ ├── script/ │ │ ├── DeployScript.s.sol │ │ ├── GoerliDeployScript.s.sol │ │ └── MainnetDeployScript.s.sol │ ├── src/ │ │ ├── FlashSwap.sol │ │ ├── NettingLib.sol │ │ ├── ZenBullNetting.sol │ │ └── interface/ │ │ ├── IController.sol │ │ ├── ICrabStrategyV2.sol │ │ ├── IEulerSimpleLens.sol │ │ ├── IFlashZen.sol │ │ ├── IOracle.sol │ │ ├── IWETH.sol │ │ └── IZenBullStrategy.sol │ └── test/ │ ├── ZenBullNettingBaseSetup.t.sol │ ├── fuzzing/ │ │ └── ZenBullNetting/ │ │ └── DepositAuctionFuzzing.t.sol │ ├── integration-test/ │ │ └── ZenBullNetting/ │ │ ├── DepositAuction.t.sol │ │ ├── DequeueEth.t.sol │ │ ├── DequeueZenBull.t.sol │ │ ├── NetAtPrice.t.sol │ │ └── WithdrawAuction.t.sol │ ├── unit-test/ │ │ ├── CancelNonce.t.sol │ │ ├── QueueEth.t.sol │ │ ├── QueueZenBull.t.sol │ │ ├── SetAuctionTwapPeriod.t.sol │ │ ├── SetDepositsIndex.t.sol │ │ ├── SetMinEthAmount.sol │ │ ├── SetMinZenBullAmount.t.sol │ │ ├── SetOTCPriceTolerance.t.sol │ │ ├── SetWithdrawsIndex.t.sol │ │ └── ToggleAuctionLive.t.sol │ └── util/ │ └── SigUtil.sol └── zen-bull-vault/ ├── .gitignore ├── LICENSE_BUSL ├── README.md ├── doc/ │ └── report/ │ ├── report_auction_bull │ ├── report_auction_bull.md │ ├── report_bull_strategy │ ├── report_bull_strategy.md │ ├── report_emergency_shutdown │ ├── report_emergency_shutdown.md │ ├── report_flash_bull │ └── report_flash_bull.md ├── foundry.toml ├── remappings.txt ├── script/ │ ├── AddLiquidityEuler.s.sol │ ├── Deploy.s.sol │ ├── GoerliAddUsdcLiquidityEuler.s.sol │ ├── GoerliAddWethLiquidityEuler.s.sol │ ├── GoerliDeploy.s.sol │ ├── GoerliDeployEmergencyWithdraw.s.sol │ ├── MainnetDeploy.s.sol │ ├── MainnetDeployEmergencyWithdraw.s.sol │ └── MainnetDeployShutdownEmergencyWithdraw.s.sol ├── src/ │ ├── EmergencyWithdraw.sol │ ├── FlashZen.sol │ ├── LeverageZen.sol │ ├── ShutdownEmergencyWithdraw.sol │ ├── UniFlash.sol │ ├── UniOracle.sol │ ├── ZenAuction.sol │ ├── ZenBullStrategy.sol │ ├── ZenEmergencyShutdown.sol │ └── interface/ │ ├── ICrabStrategyV2.sol │ ├── IEulerDToken.sol │ ├── IEulerEToken.sol │ ├── IEulerMarkets.sol │ ├── ILeverageZen.sol │ ├── IZenBullStrategy.sol │ └── IZenEmergencyWithdraw.sol └── test/ ├── e2e/ │ ├── EmergencyWithdrawScenarios.t.sol │ └── ShutdownEmergencyWithdrawTest.t.sol ├── fuzz-test/ │ ├── FlashZenFuzzTest.t.sol │ ├── LeverageZenFuzzTest.t.sol │ ├── ZenAuctionFuzzTest.t.sol │ └── ZenBullStrategyFuzzTest.t.sol ├── integration-test/ │ ├── EmergencyWithdraw/ │ │ ├── EmergencyRepayEulerDebtTest.t.sol │ │ ├── EmergencyWithdrawEthFromCrabTest.t.sol │ │ └── WithdrawEthTest.t.sol │ ├── FlashZenTestFork.t.sol │ ├── ZenAuctionTestFork.t.sol │ ├── ZenBullStrategyTestFork.t.sol │ └── ZenEmergencyShutdownTestFork.t.sol ├── unit-test/ │ ├── ZenBullStrategyTest.t.sol │ ├── mock/ │ │ ├── EulerDtokenMock.t.sol │ │ ├── EulerEtokenMock.t.sol │ │ ├── EulerMarketsMock.t.sol │ │ ├── EulerMock.t.sol │ │ └── Weth9Mock.t.sol │ └── util/ │ └── UnitSetupUtil.t.sol └── util/ ├── SigUtil.sol └── TestUtil.t.sol ================================================ FILE CONTENTS ================================================ ================================================ FILE: .circleci/config.yml ================================================ version: 2.1 orbs: coveralls: coveralls/coveralls@1.0.6 node: circleci/node@1.1.6 workflows: hardhat: jobs: - checkout-and-install-hardhat - compile-hardhat: requires: - checkout-and-install-hardhat - lint-hardhat: requires: - compile-hardhat - unit-test-hardhat: requires: - compile-hardhat - integration-test-hardhat: requires: - compile-hardhat - contract-size-hardhat: requires: - compile-hardhat - e2e-test-hardhat: requires: - compile-hardhat frontend: jobs: - checkout-and-install-frontend - unit-test-frontend: requires: - checkout-and-install-frontend crab-netting: jobs: - lint-crab-netting - compile-crab-netting: requires: - lint-crab-netting - test-crab-netting: requires: - compile-crab-netting zen-bull-vault: jobs: - compile-zen-bull-vault - test-zen-bull-vault: requires: - compile-zen-bull-vault jobs: ################ hardhat jobs checkout-and-install-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: dependency-cache-{{ checksum "package.json" }} - run: name: Install packages command: yarn install - save_cache: key: dependency-cache-{{ checksum "package.json" }} paths: - node_modules - save_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} paths: - ~/squeeth compile-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} - run: name: Compile Contracts command: npx hardhat compile - save_cache: key: typechain-cache-{{ .Environment.CIRCLE_SHA1 }} paths: - typechain - save_cache: key: artifacts-cache-{{ .Environment.CIRCLE_SHA1 }} paths: - artifacts lint-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} - restore_cache: key: typechain-cache-{{ .Environment.CIRCLE_SHA1 }} - run: name: Lint command: yarn lint:check unit-test-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} - restore_cache: key: typechain-cache-{{ .Environment.CIRCLE_SHA1 }} - run: name: Unit tests command: yarn test:crab-unit integration-test-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} - restore_cache: key: typechain-cache-{{ .Environment.CIRCLE_SHA1 }} - run: name: Integration tests command: yarn test:crab-integration contract-size-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} - restore_cache: key: artifacts-cache-{{ .Environment.CIRCLE_SHA1 }} - run: name: Check Contracts Size command: npx hardhat size-contracts e2e-test-hardhat: working_directory: ~/squeeth/packages/hardhat docker: - image: cimg/node:18.15.0 steps: - restore_cache: key: squeeth-{{ .Environment.CIRCLE_SHA1 }} - restore_cache: key: typechain-cache-{{ .Environment.CIRCLE_SHA1 }} - run: name: Set Env Variables command: echo "export ALCHEMY_KEY=${ALCHEMY_KEY}" >> $BASH_ENV - run: name: Mainnet E2E Test command: yarn test:e2e ################ frontend jobs checkout-and-install-frontend: working_directory: ~/squeeth/packages/frontend docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: dependency-frontend-cache-{{ checksum "package.json" }} - run: name: Install packages command: yarn install - save_cache: key: dependency-frontend-cache-{{ checksum "package.json" }} paths: - node_modules - save_cache: key: squeeth-frontend-{{ .Environment.CIRCLE_SHA1 }} paths: - ~/squeeth unit-test-frontend: working_directory: ~/squeeth/packages/frontend docker: - image: cimg/node:18.15.0 steps: - checkout: path: ~/squeeth - restore_cache: key: squeeth-frontend-{{ .Environment.CIRCLE_SHA1 }} - run: name: Unit tests command: yarn test:ci ################ crab-netting jobs lint-crab-netting: working_directory: ~/squeeth/packages/crab-netting docker: - image: ghcr.io/foundry-rs/foundry:latest steps: - checkout - run: name: Lint crab-netting command: cd packages/crab-netting && FOUNDRY_PROFILE=fmt forge fmt --check compile-crab-netting: working_directory: ~/squeeth/packages/crab-netting docker: - image: ghcr.io/foundry-rs/foundry:latest steps: - checkout - run: name: Build crab-netting command: cd packages/crab-netting && forge build --force test-crab-netting: working_directory: ~/squeeth/packages/crab-netting docker: - image: ghcr.io/foundry-rs/foundry:latest steps: - checkout - run: cd packages/crab-netting && forge test -vv --gas-report ################ zen-bull-vault jobs compile-zen-bull-vault: working_directory: ~/squeeth/packages/zen-bull-vault docker: - image: ghcr.io/foundry-rs/foundry:latest steps: - checkout - run: name: Build zen-bull-vault command: cd packages/zen-bull-vault && forge build --force test-zen-bull-vault: working_directory: ~/squeeth/packages/zen-bull-vault docker: - image: ghcr.io/foundry-rs/foundry:latest steps: - checkout - run: cd packages/zen-bull-vault && forge test -vv --gas-report ================================================ FILE: .editorconfig ================================================ root = true [packages/**.js{,x}] indent_style = space indent_size = 2 [*.{sol,yul}] indent_style = space indent_size = 4 ================================================ FILE: .github/ISSUE_TEMPLATE/bug_report.md ================================================ --- name: Bug report about: Create a report to help us improve title: "" labels: "" assignees: "" --- **Describe the bug** A clear and concise description of what the bug is. **To Reproduce** Steps to reproduce the behavior: 1. Go to '...' 2. Click on '....' 3. Scroll down to '....' 4. See error **Expected behavior** A clear and concise description of what you expected to happen. **Screenshots** If applicable, add screenshots to help explain your problem. **Additional context** Add any other context about the problem here. ================================================ FILE: .github/ISSUE_TEMPLATE/feature_request.md ================================================ --- name: Feature request about: Suggest an idea for this project title: "" labels: "" assignees: "" --- **Please describe the problem you're having.** A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] **Screenshots** If applicable, add screenshots to help explain your problem. **Additional context** Add any other context or screenshots about the feature request here. ================================================ FILE: .github/pull_request_template.md ================================================ # Task: ## Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. Fixes (linear-task) ## Type of change - [ ] New feature - [ ] Bug fix - [ ] Testing code - [ ] Document update or config files ================================================ FILE: .gitignore ================================================ packages/subgraph/subgraph.yaml packages/subgraph/generated packages/subgraph/abis/* packages/hardhat/*.txt **/aws.json # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. **/node_modules packages/hardhat/artifacts* packages/hardhat/typechain* # ignore localhost, but keep other deployment record on git packages/hardhat/deployments/localhost packages/react-app/src/contracts/* !packages/react-app/src/contracts/external_contracts.js packages/hardhat/cache* packages/hardhat/coverage.json packages/hardhat/coverage/ packages/**/data packages/subgraph/config/config.json tenderly.yaml # dependencies /node_modules /.pnp .pnp.js # testing coverage # production build # misc .DS_Store .env packages/hardhat/.env* # debug npm-debug.log* yarn-debug.log* yarn-error.log* .idea #Local vscode folder .vscode # Local Netlify folder .netlify # asdf config **/.tool-versions packages/bull-vault/out packages/bull-vault/cache packages/crab-netting/out packages/crab-netting/cache soljson* lcov.info ================================================ FILE: .gitmodules ================================================ [submodule "packages/services/arbitrum"] path = packages/services/arbitrum url = https://github.com/OffchainLabs/arbitrum branch = master [submodule "packages/services/optimism"] path = packages/services/optimism url = https://github.com/ethereum-optimism/optimism branch = regenesis/0.4.0 [submodule "packages/zen-bull-vault/lib/squeeth-monorepo"] path = packages/zen-bull-vault/lib/squeeth-monorepo url = https://github.com/opynfinance/squeeth-monorepo [submodule "packages/zen-bull-vault/lib/openzeppelin-contracts"] path = packages/zen-bull-vault/lib/openzeppelin-contracts url = https://github.com/openzeppelin/openzeppelin-contracts [submodule "packages/zen-bull-vault/lib/v3-core"] path = packages/zen-bull-vault/lib/v3-core url = https://github.com/Uniswap/v3-core [submodule "packages/zen-bull-vault/lib/v3-periphery"] path = packages/zen-bull-vault/lib/v3-periphery url = https://github.com/Uniswap/v3-periphery [submodule "packages/crab-netting/lib/forge-std"] path = packages/crab-netting/lib/forge-std url = https://github.com/foundry-rs/forge-std [submodule "packages/crab-netting/lib/squeeth-monorepo"] path = packages/crab-netting/lib/squeeth-monorepo url = https://github.com/opynfinance/squeeth-monorepo/ [submodule "packages/crab-netting/lib/openzeppelin-contracts"] path = packages/crab-netting/lib/openzeppelin-contracts url = https://github.com/OpenZeppelin/openzeppelin-contracts [submodule "packages/crab-netting/lib/v3-periphery"] path = packages/crab-netting/lib/v3-periphery url = https://github.com/uniswap/v3-periphery [submodule "packages/crab-netting/lib/v3-core"] path = packages/crab-netting/lib/v3-core url = https://github.com/uniswap/v3-core [submodule "packages/zen-bull-vault/lib/forge-std"] path = packages/zen-bull-vault/lib/forge-std url = https://github.com/foundry-rs/forge-std [submodule "packages/zen-bull-netting/lib/forge-std"] path = packages/zen-bull-netting/lib/forge-std url = https://github.com/foundry-rs/forge-std [submodule "packages/zen-bull-netting/lib/v3-core"] path = packages/zen-bull-netting/lib/v3-core url = https://github.com/Uniswap/v3-core branch = 0.8 [submodule "packages/zen-bull-netting/lib/v3-periphery"] path = packages/zen-bull-netting/lib/v3-periphery url = https://github.com/Uniswap/v3-periphery branch = 0.8 [submodule "packages/zen-bull-netting/lib/openzeppelin-contracts"] path = packages/zen-bull-netting/lib/openzeppelin-contracts url = https://github.com/OpenZeppelin/openzeppelin-contracts ================================================ FILE: .gitpod.yml ================================================ tasks: - name: App init: > yarn && gp sync-done install command: REACT_APP_PROVIDER=$(gp url 8545) yarn start - name: Chain init: gp sync-await install command: yarn chain openMode: split-right - name: Deployment init: gp sync-await install command: yarn deploy openMode: split-right ports: - port: 3000 onOpen: open-preview - port: 8545 onOpen: ignore github: prebuilds: pullRequestsFromForks: true addComment: true vscode: extensions: - dbaeumer.vscode-eslint - esbenp.prettier-vscode - juanblanco.solidity ================================================ FILE: .husky/pre-commit ================================================ #!/bin/sh . "$(dirname "$0")/_/husky.sh" npx lint-staged ================================================ FILE: .huskyrc ================================================ { "hooks": { "pre-commit": "lint-staged", "pre-push": "yarn pre-push" } } ================================================ FILE: .lintstagedrc ================================================ { "packages/hardhat/**/*.+(js|ts|json|sol)": [ "yarn lint-contracts" ] } ================================================ FILE: README.md ================================================ # Squeeth Monorepo

The Squeethiest 🐱

Discord Twitter Follow

## 🤔 What is Squeeth The squeeth contract is designed for users to long or short a special index: Eth², as an implementation of a Power Perpetual.

This monorepo contains the source code for the frontend app as well as the contracts, you can spin up the environment locally, run tests, or play around with the code. For more details about how to use the contracts and frontend, go to `packages/` and choose `hardhat` for the contracts or `frontend`, we have more detailed explanation in each sub-folder. ## 📚 Learn more - Read our [GitBook](https://opyn.gitbook.io/squeeth/) Documentation - Visit our official [Medium page](https://medium.com/opyn) where we have tons of great posts - Original paper on [Power Perpetual](https://www.paradigm.xyz/2021/08/power-perpetuals/) - Join our [Discord](https://tiny.cc/opyndiscord) to chat with all the derivative big brains ## 🔒 Security And Bug Bounty Program Security is our one of our highest priorities. Our team has created a protocol that we believe is safe and dependable, and is audited by Trail of Bits and Akira, and is insured by Sherlock. All smart contract code is publicly verifiable and we have a bug bounty for undiscovered vulnerabilities. We encourage our users to be mindful of risk and only use funds they can afford to lose. Smart contracts are still new and experimental technology. We want to remind our users to be optimistic about innovation while remaining cautious about where they put their money. Please see here for details on our [security audit](https://opyn.gitbook.io/squeeth/security/audits-and-insurance) and [bug bounty program](https://opyn.gitbook.io/squeeth/security/bug-bounty). ## 🏄‍♂️ Quick Start ### Prerequisites 1. Install [Node](https://nodejs.org/en/download/) LTS 1. Install [Yarn](https://classic.yarnpkg.com/en/docs/install/) ### Steps > install and start your 👷‍ Hardhat chain: ```bash cd packages/hardhat yarn install yarn chain ``` > in a second terminal window, start your 📱 frontend: ```bash cd packages/frontend yarn install yarn dev ``` > in a third terminal window, 🛰 deploy your contract: ```bash cd packages/hardhat yarn deploy ``` Open http://localhost:3000 to see the app ================================================ FILE: package.json ================================================ { "name": "@squeeth/monorepo", "version": "1.0.0", "keywords": [ "ethereum", "react", "uniswap", "workspaces", "yarn" ], "private": true, "scripts": { "frontend:build": "NODE_OPTIONS=--max-old-space-size=12288 yarn workspace @squeeth/frontend build", "frontend:eject": "yarn workspace @squeeth/frontend eject", "frontend:start": "yarn workspace @squeeth/frontend dev", "frontend:test": "yarn workspace @squeeth/frontend test", "build": "NODE_OPTIONS=--max-old-space-size=12288 yarn workspace @squeeth/frontend build ", "chain": "yarn workspace @squeeth/hardhat chain", "node": "yarn workspace @squeeth/hardhat chain", "test": "yarn workspace @squeeth/hardhat test", "start": "yarn workspace @squeeth/frontend start", "compile": "yarn workspace @squeeth/hardhat compile", "deploy": "yarn workspace @squeeth/hardhat deploy", "watch": "yarn workspace @squeeth/hardhat watch", "accounts": "yarn workspace @squeeth/hardhat accounts", "balance": "yarn workspace @squeeth/hardhat balance", "send": "yarn workspace @squeeth/hardhat send", "ipfs": "yarn workspace @squeeth/frontend ipfs", "surge": "yarn workspace @squeeth/frontend surge", "s3": "yarn workspace @squeeth/frontend s3", "ship": "yarn workspace @squeeth/frontend ship", "generate": "yarn workspace @squeeth/hardhat generate", "account": "yarn workspace @squeeth/hardhat account", "mineContractAddress": "cd packages/hardhat && npx hardhat mineContractAddress", "wallet": "cd packages/hardhat && npx hardhat wallet", "fundedwallet": "cd packages/hardhat && npx hardhat fundedwallet", "flatten": "cd packages/hardhat && npx hardhat flatten", "clean": "cd packages/hardhat && npx hardhat clean", "run-graph-node": "yarn workspace @squeeth/services run-graph-node", "remove-graph-node": "yarn workspace @squeeth/services remove-graph-node", "clean-graph-node": "yarn workspace @squeeth/services clean-graph-node", "graph-prepare": "mustache packages/subgraph/config/config.json packages/subgraph/src/subgraph.template.yaml > packages/subgraph/subgraph.yaml", "graph-codegen": "yarn workspace @squeeth/subgraph graph codegen", "graph-build": "yarn workspace @squeeth/subgraph graph build", "graph-create-local": "yarn workspace @squeeth/subgraph graph create --node http://localhost:8020/ scaffold-eth/your-contract", "graph-remove-local": "yarn workspace @squeeth/subgraph graph remove --node http://localhost:8020/ scaffold-eth/your-contract", "graph-deploy-local": "yarn workspace @squeeth/subgraph graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 scaffold-eth/your-contract", "graph-ship-local": "yarn graph-prepare && yarn graph-codegen && yarn graph-deploy-local", "deploy-and-graph": "yarn deploy && yarn graph-ship-local", "theme": "yarn workspace @squeeth/frontend theme", "watch-theme": "yarn workspace @squeeth/frontend watch", "lint-contracts": "yarn workspace @squeeth/hardhat lint", "pre-push": "cd packages/bull-vault && forge fmt && git commit -a -m 'auto foundry lint commit'" }, "workspaces": { "packages": [ "packages/*" ], "nohoist": [ "**/@graphprotocol/graph-ts", "**/@graphprotocol/graph-ts/**", "**/hardhat", "**/hardhat/**", "**/ip-range-check", "**/ip-range-check/**" ] }, "dependencies": {}, "resolutions": { "ipfs-http-client@34.0.0/concat-stream": "2.0.0" }, "devDependencies": { "husky": "^7.0.1", "lint-staged": "^11.1.2" } } ================================================ FILE: packages/crab-netting/LICENSE_BUSL ================================================ Business Source License 1.1 License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab. ----------------------------------------------------------------------------- Parameters Licensor: Opyn Licensed Work: Opyn Crab Netting v1.0 The Licensed Work is (c) 2022 Opyn Additional Use Grant: Any uses listed and defined at https://github.com/opynfinance/squeeth-monorepo/tree/main/packages/crab-netting/USE_GRANT Change Date: The earlier of 2023-12-14 or a date specified at https://github.com/opynfinance/squeeth-monorepo/tree/main/packages/crab-netting/CHANGE_DATE Change License: GNU General Public License v2.0 or later ----------------------------------------------------------------------------- Terms The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use. Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate. If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work. All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor. You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work. Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work. This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. MariaDB hereby grants you permission to use this License’s text to license your works, and to refer to it using the trademark "Business Source License", as long as you comply with the Covenants of Licensor below. ----------------------------------------------------------------------------- Covenants of Licensor In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor: 1. To specify as the Change License the GPL Version 2.0 or any later version, or a license that is compatible with GPL Version 2.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 2.0 or a later version. Licensor may specify additional Change Licenses without limitation. 2. To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None". 3. To specify a Change Date. 4. Not to modify this License in any other way. ----------------------------------------------------------------------------- Notice The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License. ================================================ FILE: packages/crab-netting/foundry.toml ================================================ [profile.default] src = 'src' out = 'out' libs = ['lib'] # See more config options https://github.com/foundry-rs/foundry/tree/master/config ================================================ FILE: packages/crab-netting/remappings.txt ================================================ ds-test/=lib/forge-std/lib/ds-test/src/ forge-std/=lib/forge-std/src/ squeeth-monorepo/=lib/squeeth-monorepo/packages/hardhat/contracts/ openzeppelin/=lib/openzeppelin-contracts/contracts/ @uniswap/v3-periphery/=lib/v3-periphery/ @uniswap/v3-core/=lib/v3-core/ ================================================ FILE: packages/crab-netting/script/Counter.s.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "forge-std/Script.sol"; contract CounterScript is Script { function setUp() public {} function run() public { vm.broadcast(); } } ================================================ FILE: packages/crab-netting/src/CrabNetting.sol ================================================ // SPDX-License-Identifier: BUSL-1.1 pragma solidity ^0.8.13; // interface import {IERC20} from "openzeppelin/token/ERC20/IERC20.sol"; import {IWETH} from "../src/interfaces/IWETH.sol"; import {IOracle} from "../src/interfaces/IOracle.sol"; import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; import {ICrabStrategyV2} from "../src/interfaces/ICrabStrategyV2.sol"; import {IController} from "../src/interfaces/IController.sol"; // contract import {Ownable} from "openzeppelin/access/Ownable.sol"; import {EIP712} from "openzeppelin/utils/cryptography/draft-EIP712.sol"; import {ECDSA} from "openzeppelin/utils/cryptography/ECDSA.sol"; /// @dev order struct for a signed order from market maker struct Order { uint256 bidId; address trader; uint256 quantity; uint256 price; bool isBuying; uint256 expiry; uint256 nonce; uint8 v; bytes32 r; bytes32 s; } /// @dev struct to store proportional amounts of erc20s (received or to send) struct Portion { uint256 crab; uint256 eth; uint256 sqth; } /// @dev params for deposit auction struct DepositAuctionParams { /// @dev USDC to deposit uint256 depositsQueued; /// @dev minETH equivalent to get from uniswap of the USDC to deposit uint256 minEth; /// @dev total ETH to deposit after selling the minted SQTH uint256 totalDeposit; /// @dev orders to buy sqth Order[] orders; /// @dev price from the auction to sell sqth uint256 clearingPrice; /// @dev remaining ETH to flashDeposit uint256 ethToFlashDeposit; /// @dev fee to pay uniswap for ethUSD swap uint24 ethUSDFee; /// @dev fee to pay uniswap for sqthETH swap uint24 flashDepositFee; } /// @dev params for withdraw auction struct WithdrawAuctionParams { /// @dev amont of crab to queue for withdrawal uint256 crabToWithdraw; /// @dev orders that sell sqth to the auction Order[] orders; /// @dev price that the auction pays for the purchased sqth uint256 clearingPrice; /// @dev minUSDC to receive from swapping the ETH obtained by withdrawing uint256 minUSDC; /// @dev uniswap fee for swapping eth to USD; uint24 ethUSDFee; } /// @dev receipt used to store deposits and withdraws struct Receipt { /// @dev address of the depositor or withdrawer address sender; /// @dev usdc amount to queue for deposit or crab amount to queue for withdrawal uint256 amount; /// @dev time of deposit uint256 timestamp; } /** * Crab netting error codes * N1: deposit amount smaller than minimum OTC amount * N2: auction is live * N3: remaining amount smaller than minimum, consider removing full balance * N4: force withdraw after 1 week from deposit * N5: withdraw amount smaller than minimum OTC amount * N6: remaining amount smaller than minimum, consider removing full balance * N7: Not enough deposits to net * N8: Not enough withdrawals to net * N9: signature incorrect * N10: order expired * N11: Min ETH out too low * N12: auction order not buying sqth * N13: buy order price less than clearing * N14: not enough buy orders for sqth * N15: auction order is not selling * N16: sell order price greater than clearing * N17: min USDC out too low * N18: twap period cannot be less than 180 * N19: Price tolerance has to be less than 20% * N20: Nonce already used * N21: Price too high relative to Uniswap twap. * N22: Price too low relative to Uniswap twap. * N23: Crab Price too high * N24: Crab Price too low * N25: only weth and crab can send me monies */ /** * @dev CrabNetting contract * @notice Contract for Netting Deposits and Withdrawals * @author Opyn team */ contract CrabNetting is Ownable, EIP712 { /// @dev typehash for signed orders bytes32 private constant _CRAB_NETTING_TYPEHASH = keccak256( "Order(uint256 bidId,address trader,uint256 quantity,uint256 price,bool isBuying,uint256 expiry,uint256 nonce)" ); /// @dev owner sets to true when starting auction bool public isAuctionLive; /// @dev sqth twap period uint32 public immutable sqthTwapPeriod; /// @dev twap period to use for auction calculations uint32 public auctionTwapPeriod = 420 seconds; /// @dev min USDC amounts to withdraw or deposit via netting uint256 public minUSDCAmount; /// @dev min CRAB amounts to withdraw or deposit via netting uint256 public minCrabAmount; // @dev OTC price must be within this distance of the uniswap twap price uint256 public otcPriceTolerance = 5e16; // 5% // @dev OTC price tolerance cannot exceed 20% uint256 public constant MAX_OTC_PRICE_TOLERANCE = 2e17; // 20% /// @dev address for ERC20 tokens address public immutable usdc; address public immutable crab; address public immutable weth; address public immutable sqth; /// @dev address for uniswap router ISwapRouter public immutable swapRouter; /// @dev address for uniswap oracle address public immutable oracle; /// @dev address for sqth eth pool address public immutable ethSqueethPool; /// @dev address for usdc eth pool address public immutable ethUsdcPool; /// @dev address for sqth controller address public immutable sqthController; /// @dev array index of last processed deposits uint256 public depositsIndex; /// @dev array index of last processed withdraws uint256 public withdrawsIndex; /// @dev array of deposit receipts Receipt[] public deposits; /// @dev array of withdrawal receipts Receipt[] public withdraws; /// @dev usd amount to deposit for an address mapping(address => uint256) public usdBalance; /// @dev crab amount to withdraw for an address mapping(address => uint256) public crabBalance; /// @dev indexes of deposit receipts of an address mapping(address => uint256[]) public userDepositsIndex; /// @dev indexes of withdraw receipts of an address mapping(address => uint256[]) public userWithdrawsIndex; /// @dev store the used flag for a nonce for each address mapping(address => mapping(uint256 => bool)) public nonces; event USDCQueued( address indexed depositor, uint256 amount, uint256 depositorsBalance, uint256 indexed receiptIndex ); event USDCDeQueued(address indexed depositor, uint256 amount, uint256 depositorsBalance); event CrabQueued( address indexed withdrawer, uint256 amount, uint256 withdrawersBalance, uint256 indexed receiptIndex ); event CrabDeQueued(address indexed withdrawer, uint256 amount, uint256 withdrawersBalance); event USDCDeposited( address indexed depositor, uint256 usdcAmount, uint256 crabAmount, uint256 indexed receiptIndex, uint256 refundedETH ); event CrabWithdrawn( address indexed withdrawer, uint256 crabAmount, uint256 usdcAmount, uint256 indexed receiptIndex ); event WithdrawRejected(address indexed withdrawer, uint256 crabAmount, uint256 index); event BidTraded(uint256 indexed bidId, address indexed trader, uint256 quantity, uint256 price, bool isBuying); event SetAuctionTwapPeriod(uint32 previousTwap, uint32 newTwap); event SetOTCPriceTolerance(uint256 previousTolerance, uint256 newOtcPriceTolerance); event SetMinCrab(uint256 amount); event SetMinUSDC(uint256 amount); event SetDepositsIndex(uint256 newDepositsIndex); event SetWithdrawsIndex(uint256 newWithdrawsIndex); event NonceTrue(address sender, uint256 nonce); event ToggledAuctionLive(bool isAuctionLive); /** * @notice netting contract constructor * @dev initializes the erc20 address, uniswap router and approves them * @param _crab address of crab contract token * @param _swapRouter address of uniswap swap router */ constructor(address _crab, address _swapRouter) EIP712("CRABNetting", "1") { crab = _crab; swapRouter = ISwapRouter(_swapRouter); sqthController = ICrabStrategyV2(_crab).powerTokenController(); usdc = IController(sqthController).quoteCurrency(); weth = ICrabStrategyV2(_crab).weth(); sqth = ICrabStrategyV2(_crab).wPowerPerp(); oracle = ICrabStrategyV2(_crab).oracle(); ethSqueethPool = ICrabStrategyV2(_crab).ethWSqueethPool(); ethUsdcPool = IController(sqthController).ethQuoteCurrencyPool(); sqthTwapPeriod = IController(sqthController).TWAP_PERIOD(); // approve crab and sqth so withdraw can happen IERC20(sqth).approve(crab, type(uint256).max); IERC20(weth).approve(address(swapRouter), type(uint256).max); IERC20(usdc).approve(address(swapRouter), type(uint256).max); } /** * @dev view function to get the domain seperator used in signing */ function DOMAIN_SEPARATOR() external view returns (bytes32) { return _domainSeparatorV4(); } /** * @dev toggles the value of isAuctionLive */ function toggleAuctionLive() external onlyOwner { isAuctionLive = !isAuctionLive; emit ToggledAuctionLive(isAuctionLive); } /** * @notice set nonce to true * @param _nonce the number to be set true */ function setNonceTrue(uint256 _nonce) external { nonces[msg.sender][_nonce] = true; emit NonceTrue(msg.sender, _nonce); } /** * @notice set minUSDCAmount * @param _amount the number to be set as minUSDC */ function setMinUSDC(uint256 _amount) external onlyOwner { minUSDCAmount = _amount; emit SetMinUSDC(_amount); } /** * @notice set minCrabAmount * @param _amount the number to be set as minCrab */ function setMinCrab(uint256 _amount) external onlyOwner { minCrabAmount = _amount; emit SetMinCrab(_amount); } /** * @notice set the depositIndex so that we want to skip processing some deposits * @param _newDepositsIndex the new deposits index */ function setDepositsIndex(uint256 _newDepositsIndex) external onlyOwner { depositsIndex = _newDepositsIndex; emit SetDepositsIndex(_newDepositsIndex); } /** * @notice set the withdraw index so that we want to skip processing some withdraws * @param _newWithdrawsIndex the new withdraw index */ function setWithdrawsIndex(uint256 _newWithdrawsIndex) external onlyOwner { withdrawsIndex = _newWithdrawsIndex; emit SetWithdrawsIndex(_newWithdrawsIndex); } /** * @notice queue USDC for deposit into crab strategy * @param _amount USDC amount to deposit */ function depositUSDC(uint256 _amount) external { require(_amount >= minUSDCAmount, "N1"); IERC20(usdc).transferFrom(msg.sender, address(this), _amount); // update usd balance of user, add their receipt, and receipt index to user deposits index usdBalance[msg.sender] = usdBalance[msg.sender] + _amount; deposits.push(Receipt(msg.sender, _amount, block.timestamp)); userDepositsIndex[msg.sender].push(deposits.length - 1); emit USDCQueued(msg.sender, _amount, usdBalance[msg.sender], deposits.length - 1); } /** * @notice withdraw USDC from queue * @param _amount USDC amount to dequeue * @param _force forceWithdraw if deposited more than a week ago */ function withdrawUSDC(uint256 _amount, bool _force) external { require(!isAuctionLive || _force, "N2"); usdBalance[msg.sender] = usdBalance[msg.sender] - _amount; require(usdBalance[msg.sender] >= minUSDCAmount || usdBalance[msg.sender] == 0, "N3"); // start withdrawing from the users last deposit uint256 toRemove = _amount; uint256 lastIndexP1 = userDepositsIndex[msg.sender].length; for (uint256 i = lastIndexP1; i > 0; i--) { Receipt storage r = deposits[userDepositsIndex[msg.sender][i - 1]]; if (_force) { require(block.timestamp > r.timestamp + 1 weeks, "N4"); } if (r.amount > toRemove) { r.amount -= toRemove; toRemove = 0; break; } else { toRemove -= r.amount; delete deposits[userDepositsIndex[msg.sender][i - 1]]; userDepositsIndex[msg.sender].pop(); } } IERC20(usdc).transfer(msg.sender, _amount); emit USDCDeQueued(msg.sender, _amount, usdBalance[msg.sender]); } /** * @notice queue Crab for withdraw from crab strategy * @param _amount crab amount to withdraw */ function queueCrabForWithdrawal(uint256 _amount) external { require(_amount >= minCrabAmount, "N5"); IERC20(crab).transferFrom(msg.sender, address(this), _amount); crabBalance[msg.sender] = crabBalance[msg.sender] + _amount; withdraws.push(Receipt(msg.sender, _amount, block.timestamp)); userWithdrawsIndex[msg.sender].push(withdraws.length - 1); emit CrabQueued(msg.sender, _amount, crabBalance[msg.sender], withdraws.length - 1); } /** * @notice withdraw Crab from queue * @param _amount Crab amount to dequeue * @param _force forceWithdraw if deposited more than a week ago */ function dequeueCrab(uint256 _amount, bool _force) external { require(!isAuctionLive || _force, "N2"); crabBalance[msg.sender] = crabBalance[msg.sender] - _amount; require(crabBalance[msg.sender] >= minCrabAmount || crabBalance[msg.sender] == 0, "N6"); // deQueue crab from the last, last in first out uint256 toRemove = _amount; uint256 lastIndexP1 = userWithdrawsIndex[msg.sender].length; for (uint256 i = lastIndexP1; i > 0; i--) { Receipt storage r = withdraws[userWithdrawsIndex[msg.sender][i - 1]]; if (_force) { require(block.timestamp > r.timestamp + 1 weeks, "N4"); } if (r.amount > toRemove) { r.amount -= toRemove; toRemove = 0; break; } else { toRemove -= r.amount; delete withdraws[userWithdrawsIndex[msg.sender][i - 1]]; userWithdrawsIndex[msg.sender].pop(); } } IERC20(crab).transfer(msg.sender, _amount); emit CrabDeQueued(msg.sender, _amount, crabBalance[msg.sender]); } /** * @dev swaps _quantity amount of usdc for crab at _price * @param _price price of crab in usdc * @param _quantity amount of USDC to net */ function netAtPrice(uint256 _price, uint256 _quantity) external onlyOwner { _checkCrabPrice(_price); uint256 crabQuantity = (_quantity * 1e18) / _price; require(_quantity <= IERC20(usdc).balanceOf(address(this)), "N7"); require(crabQuantity <= IERC20(crab).balanceOf(address(this)), "N8"); // process deposits and send crab uint256 i = depositsIndex; uint256 amountToSend; while (_quantity > 0) { Receipt memory deposit = deposits[i]; if (deposit.amount == 0) { i++; continue; } if (deposit.amount <= _quantity) { // deposit amount is lesser than quantity use it fully _quantity = _quantity - deposit.amount; usdBalance[deposit.sender] -= deposit.amount; amountToSend = (deposit.amount * 1e18) / _price; IERC20(crab).transfer(deposit.sender, amountToSend); emit USDCDeposited(deposit.sender, deposit.amount, amountToSend, i, 0); delete deposits[i]; i++; } else { // deposit amount is greater than quantity; use it partially deposits[i].amount = deposit.amount - _quantity; usdBalance[deposit.sender] -= _quantity; amountToSend = (_quantity * 1e18) / _price; IERC20(crab).transfer(deposit.sender, amountToSend); emit USDCDeposited(deposit.sender, _quantity, amountToSend, i, 0); _quantity = 0; } } depositsIndex = i; // process withdraws and send usdc i = withdrawsIndex; while (crabQuantity > 0) { Receipt memory withdraw = withdraws[i]; if (withdraw.amount == 0) { i++; continue; } if (withdraw.amount <= crabQuantity) { crabQuantity = crabQuantity - withdraw.amount; crabBalance[withdraw.sender] -= withdraw.amount; amountToSend = (withdraw.amount * _price) / 1e18; IERC20(usdc).transfer(withdraw.sender, amountToSend); emit CrabWithdrawn(withdraw.sender, withdraw.amount, amountToSend, i); delete withdraws[i]; i++; } else { withdraws[i].amount = withdraw.amount - crabQuantity; crabBalance[withdraw.sender] -= crabQuantity; amountToSend = (crabQuantity * _price) / 1e18; IERC20(usdc).transfer(withdraw.sender, amountToSend); emit CrabWithdrawn(withdraw.sender, withdraw.amount, amountToSend, i); crabQuantity = 0; } } withdrawsIndex = i; } /** * @return sum usdc amount in queue */ function depositsQueued() external view returns (uint256) { uint256 j = depositsIndex; uint256 sum; while (j < deposits.length) { sum = sum + deposits[j].amount; j++; } return sum; } /** * @return sum crab amount in queue */ function withdrawsQueued() external view returns (uint256) { uint256 j = withdrawsIndex; uint256 sum; while (j < withdraws.length) { sum = sum + withdraws[j].amount; j++; } return sum; } function checkOrder(Order memory _order) external view { return _checkOrder(_order); } /** * @dev checks the expiry nonce and signer of an order * @param _order is the Order struct */ function _checkOrder(Order memory _order) internal view { bytes32 structHash = keccak256( abi.encode( _CRAB_NETTING_TYPEHASH, _order.bidId, _order.trader, _order.quantity, _order.price, _order.isBuying, _order.expiry, _order.nonce ) ); bytes32 hash = _hashTypedDataV4(structHash); address offerSigner = ECDSA.recover(hash, _order.v, _order.r, _order.s); require(offerSigner == _order.trader, "N9"); require(_order.expiry >= block.timestamp, "N10"); } /** * @dev calculates wSqueeth minted when amount is deposited * @param _amount to deposit into crab */ function _debtToMint(uint256 _amount) internal view returns (uint256) { uint256 feeAdjustment = _calcFeeAdjustment(); (,, uint256 collateral, uint256 debt) = ICrabStrategyV2(crab).getVaultDetails(); uint256 wSqueethToMint = (_amount * debt) / (collateral + (debt * feeAdjustment) / 1e18); return wSqueethToMint; } /** * @dev takes in orders from mm's to buy sqth and deposits the usd amount from the depositQueue into crab along with the eth from selling sqth * @param _p DepositAuction Params that contain orders, usdToDeposit, uniswap min amount and fee */ function depositAuction(DepositAuctionParams calldata _p) external onlyOwner { _checkOTCPrice(_p.clearingPrice, false); uint256 ethUSDCPrice = IOracle(oracle).getTwap(ethUsdcPool, weth, usdc, auctionTwapPeriod, true); require((_p.depositsQueued * (1e18 - otcPriceTolerance) * 1e12 / ethUSDCPrice) < _p.minEth, "N11"); /** * step 1: get eth from mm * step 2: get eth from deposit usdc * step 3: crab deposit * step 4: flash deposit * step 5: send sqth to mms * step 6: send crab to depositors */ uint256 initCrabBalance = IERC20(crab).balanceOf(address(this)); uint256 initEthBalance = address(this).balance; uint256 sqthToSell = _debtToMint(_p.totalDeposit); // step 1 get all the eth in uint256 remainingToSell = sqthToSell; for (uint256 i = 0; i < _p.orders.length; i++) { require(_p.orders[i].isBuying, "N12"); require(_p.orders[i].price >= _p.clearingPrice, "N13"); _checkOrder(_p.orders[i]); _useNonce(_p.orders[i].trader, _p.orders[i].nonce); if (_p.orders[i].quantity >= remainingToSell) { IWETH(weth).transferFrom( _p.orders[i].trader, address(this), (remainingToSell * _p.clearingPrice) / 1e18 ); remainingToSell = 0; break; } else { IWETH(weth).transferFrom( _p.orders[i].trader, address(this), (_p.orders[i].quantity * _p.clearingPrice) / 1e18 ); remainingToSell -= _p.orders[i].quantity; } } require(remainingToSell == 0, "N14"); // step 2 ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({ tokenIn: usdc, tokenOut: weth, fee: _p.ethUSDFee, recipient: address(this), deadline: block.timestamp, amountIn: _p.depositsQueued, amountOutMinimum: _p.minEth, sqrtPriceLimitX96: 0 }); swapRouter.exactInputSingle(params); // step 3 IWETH(weth).withdraw(IWETH(weth).balanceOf(address(this))); ICrabStrategyV2(crab).deposit{value: _p.totalDeposit}(); // step 4 Portion memory to_send; to_send.eth = address(this).balance - initEthBalance; if (to_send.eth > 0 && _p.ethToFlashDeposit > 0) { if (to_send.eth <= _p.ethToFlashDeposit) { // we cant send more than the flashDeposit ICrabStrategyV2(crab).flashDeposit{value: to_send.eth}(_p.ethToFlashDeposit, _p.flashDepositFee); } } // step 5 to_send.sqth = IERC20(sqth).balanceOf(address(this)); remainingToSell = to_send.sqth; for (uint256 j = 0; j < _p.orders.length; j++) { if (_p.orders[j].quantity < remainingToSell) { IERC20(sqth).transfer(_p.orders[j].trader, _p.orders[j].quantity); remainingToSell -= _p.orders[j].quantity; emit BidTraded(_p.orders[j].bidId, _p.orders[j].trader, _p.orders[j].quantity, _p.clearingPrice, true); } else { IERC20(sqth).transfer(_p.orders[j].trader, remainingToSell); emit BidTraded(_p.orders[j].bidId, _p.orders[j].trader, remainingToSell, _p.clearingPrice, true); break; } } // step 6 send crab to depositors uint256 remainingDeposits = _p.depositsQueued; uint256 k = depositsIndex; to_send.crab = IERC20(crab).balanceOf(address(this)) - initCrabBalance; // get the balance between start and now to_send.eth = address(this).balance - initEthBalance; IWETH(weth).deposit{value: to_send.eth}(); while (remainingDeposits > 0) { uint256 queuedAmount = deposits[k].amount; Portion memory portion; if (queuedAmount == 0) { k++; continue; } if (queuedAmount <= remainingDeposits) { remainingDeposits = remainingDeposits - queuedAmount; usdBalance[deposits[k].sender] -= queuedAmount; portion.crab = queuedAmount * to_send.crab / _p.depositsQueued; IERC20(crab).transfer(deposits[k].sender, portion.crab); portion.eth = queuedAmount * to_send.eth / _p.depositsQueued; if (portion.eth > 1e12) { IWETH(weth).transfer(deposits[k].sender, portion.eth); } else { portion.eth = 0; } emit USDCDeposited(deposits[k].sender, queuedAmount, portion.crab, k, portion.eth); delete deposits[k]; k++; } else { usdBalance[deposits[k].sender] -= remainingDeposits; portion.crab = remainingDeposits * to_send.crab / _p.depositsQueued; IERC20(crab).transfer(deposits[k].sender, portion.crab); portion.eth = remainingDeposits * to_send.eth / _p.depositsQueued; if (portion.eth > 1e12) { IWETH(weth).transfer(deposits[k].sender, portion.eth); } else { portion.eth = 0; } emit USDCDeposited(deposits[k].sender, remainingDeposits, portion.crab, k, portion.eth); deposits[k].amount -= remainingDeposits; remainingDeposits = 0; } } depositsIndex = k; isAuctionLive = false; } /** * @dev takes in orders from mm's to sell sqth and withdraws the crab amount in q * @param _p Withdraw Params that contain orders, crabToWithdraw, uniswap min amount and fee */ function withdrawAuction(WithdrawAuctionParams calldata _p) public onlyOwner { _checkOTCPrice(_p.clearingPrice, true); uint256 initWethBalance = IERC20(weth).balanceOf(address(this)); uint256 initEthBalance = address(this).balance; /** * step 1: get sqth from mms * step 2: withdraw from crab * step 3: send eth to mms * step 4: convert eth to usdc * step 5: send usdc to withdrawers */ // step 1 get sqth from mms uint256 sqthRequired = ICrabStrategyV2(crab).getWsqueethFromCrabAmount(_p.crabToWithdraw); uint256 toPull = sqthRequired; for (uint256 i = 0; i < _p.orders.length && toPull > 0; i++) { _checkOrder(_p.orders[i]); _useNonce(_p.orders[i].trader, _p.orders[i].nonce); require(!_p.orders[i].isBuying, "N15"); require(_p.orders[i].price <= _p.clearingPrice, "N16"); if (_p.orders[i].quantity < toPull) { toPull -= _p.orders[i].quantity; IERC20(sqth).transferFrom(_p.orders[i].trader, address(this), _p.orders[i].quantity); } else { IERC20(sqth).transferFrom(_p.orders[i].trader, address(this), toPull); toPull = 0; } } // step 2 withdraw from crab ICrabStrategyV2(crab).withdraw(_p.crabToWithdraw); // step 3 pay all mms IWETH(weth).deposit{value: address(this).balance - initEthBalance}(); toPull = sqthRequired; uint256 sqthQuantity; for (uint256 i = 0; i < _p.orders.length && toPull > 0; i++) { if (_p.orders[i].quantity < toPull) { sqthQuantity = _p.orders[i].quantity; } else { sqthQuantity = toPull; } IERC20(weth).transfer(_p.orders[i].trader, (sqthQuantity * _p.clearingPrice) / 1e18); toPull -= sqthQuantity; emit BidTraded(_p.orders[i].bidId, _p.orders[i].trader, sqthQuantity, _p.clearingPrice, false); } // step 4 convert to USDC uint256 ethUSDCPrice = IOracle(oracle).getTwap(ethUsdcPool, weth, usdc, auctionTwapPeriod, true); uint256 amountIn = (IERC20(weth).balanceOf(address(this)) - initWethBalance); require((amountIn * ethUSDCPrice * (1e18 - otcPriceTolerance) / 1e36 / 1e12) < _p.minUSDC, "N17"); ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({ tokenIn: address(weth), tokenOut: address(usdc), fee: _p.ethUSDFee, recipient: address(this), deadline: block.timestamp, amountIn: amountIn, amountOutMinimum: _p.minUSDC, sqrtPriceLimitX96: 0 }); uint256 usdcReceived = swapRouter.exactInputSingle(params); // step 5 pay all withdrawers and mark their withdraws as done uint256 remainingWithdraws = _p.crabToWithdraw; uint256 j = withdrawsIndex; uint256 usdcAmount; while (remainingWithdraws > 0) { Receipt memory withdraw = withdraws[j]; if (withdraw.amount == 0) { j++; continue; } if (withdraw.amount <= remainingWithdraws) { // full usage remainingWithdraws -= withdraw.amount; crabBalance[withdraw.sender] -= withdraw.amount; // send proportional usdc usdcAmount = withdraw.amount * usdcReceived / _p.crabToWithdraw; IERC20(usdc).transfer(withdraw.sender, usdcAmount); emit CrabWithdrawn(withdraw.sender, withdraw.amount, usdcAmount, j); delete withdraws[j]; j++; } else { withdraws[j].amount -= remainingWithdraws; crabBalance[withdraw.sender] -= remainingWithdraws; // send proportional usdc usdcAmount = remainingWithdraws * usdcReceived / _p.crabToWithdraw; IERC20(usdc).transfer(withdraw.sender, usdcAmount); emit CrabWithdrawn(withdraw.sender, remainingWithdraws, usdcAmount, j); remainingWithdraws = 0; } } withdrawsIndex = j; isAuctionLive = false; } /** * @dev owner rejects the withdraw at index i thereby sending the withdrawer their crab back * @param i index of the Withdraw receipt to reject */ function rejectWithdraw(uint256 i) external onlyOwner { Receipt memory withdraw = withdraws[i]; crabBalance[withdraw.sender] -= withdraw.amount; ICrabStrategyV2(crab).transfer(withdraw.sender, withdraw.amount); delete withdraws[i]; emit WithdrawRejected(withdraw.sender, withdraw.amount, i); } /** * @notice owner can set the twap period in seconds that is used for obtaining TWAP prices * @param _auctionTwapPeriod the twap period, in seconds */ function setAuctionTwapPeriod(uint32 _auctionTwapPeriod) external onlyOwner { require(_auctionTwapPeriod >= 180, "N18"); uint32 previousTwap = auctionTwapPeriod; auctionTwapPeriod = _auctionTwapPeriod; emit SetAuctionTwapPeriod(previousTwap, _auctionTwapPeriod); } /** * @notice owner can set a threshold, scaled by 1e18 that determines the maximum discount of a clearing sale price to the current uniswap twap price * @param _otcPriceTolerance the OTC price tolerance, in percent, scaled by 1e18 */ function setOTCPriceTolerance(uint256 _otcPriceTolerance) external onlyOwner { // Tolerance cannot be more than 20% require(_otcPriceTolerance <= MAX_OTC_PRICE_TOLERANCE, "N19"); uint256 previousOtcTolerance = otcPriceTolerance; otcPriceTolerance = _otcPriceTolerance; emit SetOTCPriceTolerance(previousOtcTolerance, _otcPriceTolerance); } /** * @dev set nonce flag of the trader to true * @param _trader address of the signer * @param _nonce number that is to be traded only once */ function _useNonce(address _trader, uint256 _nonce) internal { require(!nonces[_trader][_nonce], "N20"); nonces[_trader][_nonce] = true; } /** * @notice check that the proposed sale price is within a tolerance of the current Uniswap twap * @param _price clearing price provided by manager * @param _isAuctionBuying is crab buying or selling oSQTH */ function _checkOTCPrice(uint256 _price, bool _isAuctionBuying) internal view { // Get twap uint256 squeethEthPrice = IOracle(oracle).getTwap(ethSqueethPool, sqth, weth, auctionTwapPeriod, true); if (_isAuctionBuying) { require(_price <= (squeethEthPrice * (1e18 + otcPriceTolerance)) / 1e18, "N21"); } else { require(_price >= (squeethEthPrice * (1e18 - otcPriceTolerance)) / 1e18, "N22"); } } function _checkCrabPrice(uint256 _price) internal view { // Get twap uint256 squeethEthPrice = IOracle(oracle).getTwap(ethSqueethPool, sqth, weth, auctionTwapPeriod, true); uint256 usdcEthPrice = IOracle(oracle).getTwap(ethUsdcPool, weth, usdc, auctionTwapPeriod, true); (,, uint256 collateral, uint256 debt) = ICrabStrategyV2(crab).getVaultDetails(); uint256 crabFairPrice = ((collateral - ((debt * squeethEthPrice) / 1e18)) * usdcEthPrice) / ICrabStrategyV2(crab).totalSupply(); crabFairPrice = crabFairPrice / 1e12; //converting from units of 18 to 6 require(_price <= (crabFairPrice * (1e18 + otcPriceTolerance)) / 1e18, "N23"); require(_price >= (crabFairPrice * (1e18 - otcPriceTolerance)) / 1e18, "N24"); } function _calcFeeAdjustment() internal view returns (uint256) { uint256 feeRate = IController(sqthController).feeRate(); if (feeRate == 0) return 0; uint256 squeethEthPrice = IOracle(oracle).getTwap(ethSqueethPool, sqth, weth, sqthTwapPeriod, true); return (squeethEthPrice * feeRate) / 10000; } receive() external payable { require(msg.sender == weth || msg.sender == crab, "N25"); } } ================================================ FILE: packages/crab-netting/src/interfaces/IController.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; interface IController { function feeRate() external view returns (uint256); function TWAP_PERIOD() external view returns (uint32); function quoteCurrency() external view returns (address); function ethQuoteCurrencyPool() external view returns (address); function setFeeRate(uint256 _newFeeRate) external; function setFeeRecipient(address _newFeeRecipient) external; } ================================================ FILE: packages/crab-netting/src/interfaces/ICrabStrategyV2.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import {IERC20} from "openzeppelin/interfaces/IERC20.sol"; interface ICrabStrategyV2 is IERC20 { function getVaultDetails() external view returns (address, uint256, uint256, uint256); function deposit() external payable; function withdraw(uint256 _crabAmount) external; function flashDeposit(uint256 _ethToDeposit, uint24 _poolFee) external payable; function getWsqueethFromCrabAmount(uint256 _crabAmount) external view returns (uint256); function powerTokenController() external view returns (address); function weth() external view returns (address); function wPowerPerp() external view returns (address); function oracle() external view returns (address); function ethWSqueethPool() external view returns (address); } ================================================ FILE: packages/crab-netting/src/interfaces/IOracle.sol ================================================ // SPDX-License-Identifier: MIT pragma solidity ^0.8.13; interface IOracle { function getTwap(address _pool, address _base, address _quote, uint32 _period, bool _checkPeriod) external view returns (uint256); } ================================================ FILE: packages/crab-netting/src/interfaces/IWETH.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import {IERC20} from "openzeppelin/interfaces/IERC20.sol"; interface IWETH is IERC20 { function deposit() external payable; function withdraw(uint256 wad) external; } ================================================ FILE: packages/crab-netting/test/BaseForkSetup.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; import "forge-std/Test.sol"; import {ERC20} from "openzeppelin/token/ERC20/ERC20.sol"; import {IWETH} from "../src/interfaces/IWETH.sol"; import {IOracle} from "../src/interfaces/IOracle.sol"; import {ICrabStrategyV2} from "../src/interfaces/ICrabStrategyV2.sol"; import {CrabNetting, Order} from "../src/CrabNetting.sol"; import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; import {IQuoter} from "@uniswap/v3-periphery/contracts/interfaces/IQuoter.sol"; import {IController} from "../src/interfaces/IController.sol"; contract BaseForkSetup is Test { ICrabStrategyV2 crab; ERC20 usdc; IWETH weth; ERC20 sqth; CrabNetting netting; ISwapRouter swapRouter; IController sqthController; IQuoter quoter; IOracle oracle; uint256 activeFork; uint256 internal ownerPrivateKey; address internal owner; uint256 internal depositorPk; address internal depositor; uint256 internal withdrawerPk; address internal withdrawer; uint256 internal mm1Pk; address internal mm1; uint256 internal mm2Pk; address internal mm2; Order[] orders; function setUp() public virtual { string memory FORK_URL = vm.envString("FORK_URL"); activeFork = vm.createSelectFork(FORK_URL, 15819213); crab = ICrabStrategyV2(0x3B960E47784150F5a63777201ee2B15253D713e8); weth = IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); usdc = ERC20(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48); sqth = ERC20(0xf1B99e3E573A1a9C5E6B2Ce818b617F0E664E86B); swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); quoter = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6); oracle = IOracle(0x65D66c76447ccB45dAf1e8044e918fA786A483A1); sqthController = IController(0x64187ae08781B09368e6253F9E94951243A493D5); netting = new CrabNetting(address(crab), address(swapRouter)); vm.prank(address(netting)); payable(depositor).transfer(address(netting).balance); ownerPrivateKey = 0xA11CE; owner = vm.addr(ownerPrivateKey); depositorPk = 0xA11CA; depositor = vm.addr(depositorPk); vm.label(depositor, "depositor"); withdrawerPk = 0xA11CB; withdrawer = vm.addr(withdrawerPk); vm.label(withdrawer, "withdrawer"); mm1Pk = 0xA11CC; mm1 = vm.addr(mm1Pk); vm.label(mm1, "market maker 1"); mm2Pk = 0xA11CA; mm2 = vm.addr(mm2Pk); vm.label(mm2, "market maker 2"); } } ================================================ FILE: packages/crab-netting/test/BaseSetup.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; import "forge-std/Test.sol"; import {ERC20} from "openzeppelin/token/ERC20/ERC20.sol"; import {CrabNetting} from "../src/CrabNetting.sol"; import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; import {IOracle} from "../src/interfaces/IOracle.sol"; contract FixedERC20 is ERC20 { constructor(uint256 initialSupply) ERC20("USDC", "USDC") { _mint(msg.sender, initialSupply); } } contract BaseSetup is Test { FixedERC20 usdc; FixedERC20 crab; FixedERC20 weth; FixedERC20 sqth; CrabNetting netting; ISwapRouter public immutable swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); IOracle public immutable oracle = IOracle(0x65D66c76447ccB45dAf1e8044e918fA786A483A1); uint256 internal ownerPrivateKey; address internal owner; uint256 internal depositorPk; address internal depositor; uint256 internal withdrawerPk; address internal withdrawer; function setUp() public virtual { usdc = new FixedERC20(10000 * 1e6); crab = new FixedERC20(10000 * 1e18); weth = new FixedERC20(10000 * 1e18); sqth = new FixedERC20(10000 * 1e18); netting = new CrabNetting(address(crab), address(swapRouter)); ownerPrivateKey = 0xA11CE; owner = vm.addr(ownerPrivateKey); depositorPk = 0xA11CA; depositor = vm.addr(depositorPk); vm.label(depositor, "depositor"); withdrawerPk = 0xA11CB; withdrawer = vm.addr(withdrawerPk); vm.label(withdrawer, "withdrawer"); } } ================================================ FILE: packages/crab-netting/test/Deposit.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; contract DepositTest is BaseForkSetup { function setUp() public override { BaseForkSetup.setUp(); // gives you netting, depositor, withdrawer, usdc, crab vm.startPrank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); usdc.transfer(depositor, 20e6); vm.stopPrank(); vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, 20e18); } function testDepositMin() public { netting.setMinUSDC(1e6); vm.startPrank(depositor); usdc.approve(address(netting), 2 * 1e6); netting.depositUSDC(1e6); assertEq(netting.usdBalance(depositor), 1e6); } function testDepositLessThanMin() public { netting.setMinUSDC(1e6); vm.startPrank(depositor); usdc.approve(address(netting), 5 * 1e5); vm.expectRevert(); netting.depositUSDC(5e5); } function testDepositAndWithdrawPartialUSDC() public { vm.startPrank(depositor); usdc.approve(address(netting), 2 * 1e6); netting.depositUSDC(2 * 1e6); assertEq(netting.usdBalance(depositor), 2e6); netting.withdrawUSDC(1 * 1e6, false); assertEq(netting.usdBalance(depositor), 1e6); assertEq(netting.depositsQueued(), 1e6); } function testDepositAndWithdrawFullUSDC() public { vm.startPrank(depositor); usdc.approve(address(netting), 2 * 1e6); netting.depositUSDC(2 * 1e6); assertEq(netting.usdBalance(depositor), 2e6); netting.withdrawUSDC(2 * 1e6, false); assertEq(netting.usdBalance(depositor), 0); assertEq(netting.depositsQueued(), 0); } function testLargeWithdraw() public { vm.startPrank(depositor); usdc.approve(address(netting), 4 * 1e6); netting.depositUSDC(2 * 1e6); netting.depositUSDC(2 * 1e6); assertEq(netting.usdBalance(depositor), 4e6); netting.withdrawUSDC(3 * 1e6, false); assertEq(netting.usdBalance(depositor), 1e6); assertEq(netting.depositsQueued(), 1e6); } function testDepositAndWithdrawCrabPartial() public { vm.startPrank(withdrawer); crab.approve(address(netting), 2 * 1e6); netting.queueCrabForWithdrawal(2 * 1e6); assertEq(netting.crabBalance(withdrawer), 2e6); netting.dequeueCrab(1 * 1e6, false); assertEq(netting.crabBalance(withdrawer), 1e6); assertEq(netting.withdrawsQueued(), 1e6); } function testDepositAndWithdrawCrabFull() public { vm.startPrank(withdrawer); crab.approve(address(netting), 2 * 1e6); netting.queueCrabForWithdrawal(2 * 1e6); assertEq(netting.crabBalance(withdrawer), 2e6); netting.dequeueCrab(2 * 1e6, false); assertEq(netting.crabBalance(withdrawer), 0); assertEq(netting.withdrawsQueued(), 0); } function testCrabDepositLargeWithdraw() public { vm.startPrank(withdrawer); crab.approve(address(netting), 4 * 1e6); netting.queueCrabForWithdrawal(2 * 1e6); netting.queueCrabForWithdrawal(2 * 1e6); assertEq(netting.crabBalance(withdrawer), 4e6); netting.dequeueCrab(3 * 1e6, false); assertEq(netting.crabBalance(withdrawer), 1e6, "withdrawer balance incorrect"); assertEq(netting.withdrawsQueued(), 1e6, "withdraws queued balance incorrect"); } function testCannotWithdrawCrabWhenAuctionLive() public { netting.toggleAuctionLive(); vm.startPrank(withdrawer); crab.approve(address(netting), 2 * 1e18); netting.queueCrabForWithdrawal(2 * 1e18); vm.expectRevert(bytes("N2")); netting.dequeueCrab(2 * 1e18, false); vm.stopPrank(); } function testCannotWithdrawUSDCWhenAuctionLive() public { netting.toggleAuctionLive(); vm.startPrank(depositor); usdc.approve(address(netting), 2 * 1e6); netting.depositUSDC(2 * 1e6); vm.expectRevert(bytes("N2")); netting.withdrawUSDC(2 * 1e6, false); vm.stopPrank(); } function testDepositAndWithdrawFail_POC() public { netting.setMinUSDC(10e6); vm.startPrank(depositor); usdc.approve(address(netting), 10e9); netting.depositUSDC(10e6); for (uint256 i = 0; i < 100; i++) { netting.depositUSDC(10e6); netting.withdrawUSDC{gas: 200_000}(10e6, false); } } function testWithdrawAndDeQueueFail_POC() public { netting.setMinCrab(1); vm.startPrank(withdrawer); crab.approve(address(netting), 20e18); netting.queueCrabForWithdrawal(10e18); for (uint256 i = 0; i < 100; i++) { netting.queueCrabForWithdrawal(10); netting.dequeueCrab{gas: 200_000}(10e6, false); } } function testForceWithdraw() public { uint256 startBalance = usdc.balanceOf(depositor); vm.startPrank(depositor); usdc.approve(address(netting), 2 * 1e6); netting.depositUSDC(2 * 1e6); vm.stopPrank(); netting.toggleAuctionLive(); vm.startPrank(depositor); vm.expectRevert(bytes("N2")); netting.withdrawUSDC(2e6, false); skip(8 * 24 * 60 * 60); netting.withdrawUSDC(2e6, true); assertEq(startBalance, usdc.balanceOf(depositor)); } function testForceWithdrawCrab() public { uint256 startBalance = crab.balanceOf(withdrawer); vm.startPrank(withdrawer); crab.approve(address(netting), 2e18); netting.queueCrabForWithdrawal(2e18); vm.stopPrank(); netting.toggleAuctionLive(); vm.startPrank(withdrawer); vm.expectRevert(bytes("N2")); netting.dequeueCrab(2e18, false); skip(8 * 24 * 60 * 60); netting.dequeueCrab(2e18, true); assertEq(startBalance, crab.balanceOf(withdrawer)); } } ================================================ FILE: packages/crab-netting/test/DepositAuction.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "forge-std/Test.sol"; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; import {Order, DepositAuctionParams} from "../src/CrabNetting.sol"; import {ICrabStrategyV2} from "../src/interfaces/ICrabStrategyV2.sol"; import {SigUtils} from "./utils/SigUtils.sol"; struct Sign { uint8 v; bytes32 r; bytes32 s; } contract DepositAuctionTest is BaseForkSetup { SigUtils sig; function setUp() public override { BaseForkSetup.setUp(); sig = new SigUtils(netting.DOMAIN_SEPARATOR()); vm.deal(depositor, 100000000e18); // this is a crab whale, get some crab token from //vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); // crab.tranfer(depositor, 100e18); // some WETH and USDC rich address vm.startPrank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); weth.transfer(depositor, 10000e18); weth.transfer(mm1, 1000e18); weth.transfer(mm2, 1000e18); usdc.transfer(depositor, 500000e6); vm.stopPrank(); vm.startPrank(depositor); usdc.approve(address(netting), 1500000 * 1e6); netting.depositUSDC(200000 * 1e6); vm.stopPrank(); // depositor has queued in 200k USDC } function _findTotalDepositAndToMint(uint256 _eth, uint256 _collateral, uint256 _debt, uint256 _price) internal pure returns (uint256, uint256) { uint256 totalDeposit = (_eth * 1e18) / (1e18 - ((_debt * _price) / _collateral)); return (totalDeposit, (totalDeposit * _debt) / _collateral); } function _findTotalDepositFromAuctioned(uint256 _collateral, uint256 _debt, uint256 _auctionedSqth) internal pure returns (uint256) { return (_collateral * _auctionedSqth) / _debt; } function testDepositAuctionPartialFill() public { DepositAuctionParams memory p; uint256 sqthPriceLimit = (_getSqthPrice(1e18) * 988) / 1000; (,, uint256 collateral, uint256 debt) = crab.getVaultDetails(); // Large first deposit. 10 & 40 as the deposit. 20 is the amount to net vm.prank(depositor); netting.depositUSDC(300000 * 1e6); //200+300 500k usdc deposited p.depositsQueued = 300000 * 1e6; p.minEth = (_convertUSDToETH(p.depositsQueued) * 9975) / 10000; uint256 toMint; (p.totalDeposit, toMint) = _findTotalDepositAndToMint(p.minEth, collateral, debt, sqthPriceLimit); bool trade_works = _isEnough(p.minEth, toMint, sqthPriceLimit, p.totalDeposit); require(trade_works, "depositing more than we have from sellling"); Order memory order = Order(0, mm1, toMint, (sqthPriceLimit * 1005) / 1000, true, block.timestamp, 0, 1, 0x00, 0x00); bytes32 digest = sig.getTypedDataHash(order); (uint8 v, bytes32 r, bytes32 s) = vm.sign(mm1Pk, digest); order.v = v; order.r = r; order.s = s; orders.push(order); p.orders = orders; vm.prank(mm1); weth.approve(address(netting), 1e30); p.clearingPrice = (sqthPriceLimit * 1005) / 1000; uint256 excessEth = (toMint * (p.clearingPrice - sqthPriceLimit)) / 1e18; p.ethUSDFee = 500; p.flashDepositFee = 3000; // Find the borrow ration for toFlash uint256 mid = _findBorrow(excessEth, debt, collateral); p.ethToFlashDeposit = (excessEth * mid) / 10 ** 7; // ------------- // uint256 depositorBalance = weth.balanceOf(depositor); netting.depositAuction(p); assertApproxEqAbs(ICrabStrategyV2(crab).balanceOf(depositor), 221e18, 1e18); assertEq(netting.usdBalance(depositor), 200000e6); assertEq(sqth.balanceOf(mm1), toMint); assertEq(weth.balanceOf(address(netting)), 1); assertApproxEqAbs(weth.balanceOf(depositor) - depositorBalance, 5e17, 1e17); } function testDepositAuctionAfterFullWithdrawal() public { vm.startPrank(depositor); console.log(netting.usdBalance(depositor), "depositor balance"); netting.withdrawUSDC(netting.usdBalance(depositor), false); assertEq(netting.usdBalance(depositor), 0, "depositor balancez ero"); netting.depositUSDC(200000e6); vm.stopPrank(); DepositAuctionParams memory p; uint256 sqthPriceLimit = (_getSqthPrice(1e18) * 988) / 1000; (,, uint256 collateral, uint256 debt) = crab.getVaultDetails(); // Large first deposit. 10 & 40 as the deposit. 20 is the amount to net vm.prank(depositor); netting.depositUSDC(300000 * 1e6); //200+300 500k usdc deposited p.depositsQueued = 300000 * 1e6; p.minEth = (_convertUSDToETH(p.depositsQueued) * 9975) / 10000; uint256 toMint; (p.totalDeposit, toMint) = _findTotalDepositAndToMint(p.minEth, collateral, debt, sqthPriceLimit); bool trade_works = _isEnough(p.minEth, toMint, sqthPriceLimit, p.totalDeposit); require(trade_works, "depositing more than we have from sellling"); Order memory order = Order(0, mm1, toMint, (sqthPriceLimit * 1005) / 1000, true, block.timestamp, 0, 1, 0x00, 0x00); bytes32 digest = sig.getTypedDataHash(order); (uint8 v, bytes32 r, bytes32 s) = vm.sign(mm1Pk, digest); order.v = v; order.r = r; order.s = s; orders.push(order); p.orders = orders; vm.prank(mm1); weth.approve(address(netting), 1e30); p.clearingPrice = (sqthPriceLimit * 1005) / 1000; uint256 excessEth = (toMint * (p.clearingPrice - sqthPriceLimit)) / 1e18; p.ethUSDFee = 500; p.flashDepositFee = 3000; // Find the borrow ration for toFlash uint256 mid = _findBorrow(excessEth, debt, collateral); p.ethToFlashDeposit = (excessEth * mid) / 10 ** 7; // ------------- // uint256 depositorBalance = weth.balanceOf(depositor); console.log(depositorBalance, "balance bfore"); netting.depositAuction(p); console.log(ICrabStrategyV2(crab).balanceOf(depositor), "crab balance"); assertGt(ICrabStrategyV2(crab).balanceOf(depositor), 221e18); assertEq(netting.usdBalance(depositor), 200000e6); assertEq(sqth.balanceOf(mm1), toMint); assertLe(weth.balanceOf(address(netting)), 1e16); assertGt(weth.balanceOf(depositor) - depositorBalance, 5e17, "0.5 eth not remaining"); assertEq(netting.depositsIndex(), 2); } function testSqthPriceTooLow() public { DepositAuctionParams memory p; uint256 sqthPriceLimit = (_getSqthPrice(1e18) * 99) / 100; (,, uint256 collateral, uint256 debt) = crab.getVaultDetails(); // Large first deposit. 10 & 40 as the deposit. 20 is the amount to net vm.prank(depositor); netting.depositUSDC(300000 * 1e6); //200+300 500k usdc deposited p.depositsQueued = 300000 * 1e6; p.minEth = (_convertUSDToETH(p.depositsQueued) * 9975) / 10000; uint256 toMint; (p.totalDeposit, toMint) = _findTotalDepositAndToMint(p.minEth, collateral, debt, sqthPriceLimit); Order memory order = Order(0, mm1, toMint, sqthPriceLimit, true, block.timestamp, 0, 1, 0x00, 0x00); orders.push(order); p.orders = orders; vm.prank(mm1); weth.approve(address(netting), 1e30); p.clearingPrice = (sqthPriceLimit * 94) / 100; p.ethUSDFee = 500; p.flashDepositFee = 3000; p.ethToFlashDeposit = (p.ethToFlashDeposit * 1) / 10 ** 7; vm.expectRevert(bytes("N22")); netting.depositAuction(p); } function testFirstDepositAuction() public { DepositAuctionParams memory p; // get the usd to deposit remaining p.depositsQueued = netting.depositsQueued(); // find the eth value of it p.minEth = (_convertUSDToETH(p.depositsQueued) * 9975) / 10000; // lets get the uniswap price, you can get this from uniswap function in crabstratgegy itself uint256 sqthPrice = (_getSqthPrice(1e18) * 988) / 1000; // get the vault details (,, uint256 collateral, uint256 debt) = crab.getVaultDetails(); // get the total deposit uint256 toMint; (p.totalDeposit, toMint) = _findTotalDepositAndToMint(p.minEth, collateral, debt, sqthPrice); // -------- // then write a test suite with a high eth value where it fails bool trade_works = _isEnough(p.minEth, toMint, sqthPrice, p.totalDeposit); require(trade_works, "depositing more than we have from sellling"); // if i sell the sqth and get eth add to user eth, will it be > total deposit // then reduce the total value to get more trade value like in crab otc looping // find out the root cause of this rounding issue // turns out the issue did not occur, // so we go ahead as though the auction closed for 0.993 osqth price Order memory order = Order(0, mm1, toMint - 1e18, (sqthPrice * 1005) / 1000, true, block.timestamp, 0, 1, 0x00, 0x00); Sign memory s; (s.v, s.r, s.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order)); order.v = s.v; order.r = s.r; order.s = s.s; Order memory order0 = Order(0, mm1, 1e18, (sqthPrice * 1005) / 1000, true, block.timestamp, 1, 1, 0x00, 0x00); Sign memory s0; (s0.v, s0.r, s0.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order0)); order0.v = s0.v; order0.r = s0.r; order0.s = s0.s; orders.push(order0); orders.push(order); vm.prank(mm1); weth.approve(address(netting), 1e30); p.orders = orders; p.clearingPrice = (sqthPrice * 1005) / 1000; uint256 excessEth = (toMint * (p.clearingPrice - sqthPrice)) / 1e18; console.log(excessEth, "excess eth is"); console.log(ICrabStrategyV2(crab).balanceOf(depositor), "balance start crab"); // Find the borrow ration for toFlash uint256 mid = _findBorrow(excessEth, debt, collateral); console.log(mid, "borrow percentage is"); p.ethToFlashDeposit = (excessEth * mid) / 10 ** 7; console.log("after multiplying", p.ethToFlashDeposit); p.ethUSDFee = 500; p.flashDepositFee = 3000; // ------------- // console.log(p.depositsQueued, p.minEth, p.totalDeposit, toMint); console.log(p.clearingPrice); uint256 initEthBalance = weth.balanceOf(depositor); netting.depositAuction(p); assertApproxEqAbs(ICrabStrategyV2(crab).balanceOf(depositor), 147e18, 1e18); assertEq(sqth.balanceOf(mm1), toMint); assertApproxEqAbs(weth.balanceOf(depositor) - initEthBalance, 3e17, 1e17); } function testFirstDepositAuctionWithFee() public { vm.startPrank(0x609FFF64429e2A275a879e5C50e415cec842c629); sqthController.setFeeRecipient(depositor); sqthController.setFeeRate(10); vm.stopPrank(); DepositAuctionParams memory p; p.depositsQueued = netting.depositsQueued(); p.minEth = (_convertUSDToETH(p.depositsQueued) * 9975) / 10000; uint256 sqthPrice = (_getSqthPrice(1e18) * 988) / 1000; (,, uint256 collateral, uint256 debt) = crab.getVaultDetails(); uint256 toMint; (p.totalDeposit, toMint) = _findTotalDepositAndToMint(p.minEth, collateral, debt, sqthPrice); bool trade_works = _isEnough(p.minEth, toMint, sqthPrice, p.totalDeposit); require(trade_works, "depositing more than we have from sellling"); Order memory order = Order(0, mm1, toMint - 1e18, (sqthPrice * 1005) / 1000, true, block.timestamp, 0, 1, 0x00, 0x00); Sign memory s; (s.v, s.r, s.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order)); order.v = s.v; order.r = s.r; order.s = s.s; Order memory order0 = Order(0, mm1, 1e18, (sqthPrice * 1005) / 1000, true, block.timestamp, 1, 1, 0x00, 0x00); Sign memory s0; (s0.v, s0.r, s0.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order0)); order0.v = s0.v; order0.r = s0.r; order0.s = s0.s; orders.push(order0); orders.push(order); vm.prank(mm1); weth.approve(address(netting), 1e30); p.orders = orders; p.clearingPrice = (sqthPrice * 1005) / 1000; uint256 excessEth = (toMint * (p.clearingPrice - sqthPrice)) / 1e18; // Find the borrow ration for toFlash uint256 mid = _findBorrow(excessEth, debt, collateral); p.ethToFlashDeposit = (excessEth * mid) / 10 ** 7; p.ethUSDFee = 500; p.flashDepositFee = 3000; // ------------- // uint256 initEthBalance = weth.balanceOf(depositor); netting.depositAuction(p); assertApproxEqAbs(ICrabStrategyV2(crab).balanceOf(depositor), 147e18, 1e18); assertApproxEqAbs(sqth.balanceOf(mm1), toMint, 2e18); assertApproxEqAbs(weth.balanceOf(depositor) - initEthBalance, 3e17, 1e17); } // TODO find a way to make this reusable and test easily // for multiple ETH movements and external events like partial fills // eth going down function testDepositAuctionEthUp() public { DepositAuctionParams memory p; // get the usd to deposit remaining p.depositsQueued = netting.depositsQueued(); // find the eth value of it p.minEth = _convertUSDToETH(p.depositsQueued); console.log("Starting ETH", p.minEth / 10 ** 18); // lets get the uniswap price, you can get this from uniswap function in crabstratgegy itself uint256 sqthPrice = (_getSqthPrice(1e18) * 988) / 1000; // get the vault details (,, uint256 collateral, uint256 debt) = crab.getVaultDetails(); // get the total deposit uint256 toMint; (p.totalDeposit, toMint) = _findTotalDepositAndToMint(p.minEth, collateral, debt, sqthPrice); console.log("Auctioning for ", toMint / 10 ** 18, "sqth"); // -------- // then write a test suite with a high eth value where it fails require(_isEnough(p.minEth, toMint, sqthPrice, p.totalDeposit), "depositing more than we have from sellling"); // if i sell the sqth and get eth add to user eth, will it be > total deposit // then reduce the total value to get more trade value like in crab otc looping // find out the root cause of this rounding issue // turns out the issue did not occur, // so we go ahead as though the auction closed for 0.993 osqth price Order memory order = Order( 0, mm1, toMint, 63974748984830990, // sqth price in the future true, block.timestamp + 26000000, 0, 1, 0x00, 0x00 ); (uint8 v, bytes32 r, bytes32 s) = vm.sign(mm1Pk, sig.getTypedDataHash(order)); order.v = v; order.r = r; order.s = s; orders.push(order); vm.prank(mm1); weth.approve(address(netting), 1e30); p.orders = orders; p.clearingPrice = (sqthPrice * 1005) / 1000; uint256 excessEth = (toMint * (p.clearingPrice - sqthPrice)) / 1e18; console.log(ICrabStrategyV2(crab).balanceOf(depositor), "balance start crab"); // Find the borrow ration for toFlash uint256 mid = _findBorrow(excessEth, debt, collateral); p.ethToFlashDeposit = (excessEth * mid) / 10 ** 7; p.ethUSDFee = 500; p.flashDepositFee = 3000; // ------------- // // vm.stopPrank(); assertEq(activeFork, vm.activeFork()); vm.makePersistent(address(netting)); vm.makePersistent(address(weth)); vm.makePersistent(address(usdc)); vm.rollFork(activeFork, 15829113); console.log(address(depositor).balance, "starting"); p.minEth = _convertUSDToETH(p.depositsQueued); p.clearingPrice = _getSqthPrice(1e18); console.log("Ending ETH", p.minEth / 10 ** 18); (,, collateral, debt) = ICrabStrategyV2(crab).getVaultDetails(); p.totalDeposit = _findTotalDepositFromAuctioned(collateral, debt, toMint); console.log("Using only", toMint, "sqth"); console.log(p.totalDeposit); uint256 mm1Balance = weth.balanceOf(mm1); uint256 initDepositorBalance = weth.balanceOf(depositor); netting.depositAuction(p); assertLe(((toMint * p.clearingPrice) / 10 ** 18) - (mm1Balance - weth.balanceOf(mm1)), 180); assertApproxEqAbs(ICrabStrategyV2(crab).balanceOf(depositor), 147e18, 5e18); assertApproxEqAbs( sqth.balanceOf(mm1), toMint, 0.001e18, "All minted not sold, check if we sold only what we took for" ); assertApproxEqAbs( weth.balanceOf(depositor) - initDepositorBalance, 23e17, 1e17, "deposit not refunded enough eth" ); } function _findBorrow(uint256 toFlash, uint256 debt, uint256 collateral) internal returns (uint256) { // we want a precision of six decimals // TODo fix the inifinte loop uint8 decimals = 6; uint256 start = 5 * 10 ** decimals; uint256 end = 30 * 10 ** decimals; uint256 mid; uint256 ethToBorrow; uint256 totDep; uint256 debtMinted; uint256 ethReceived; while (true) { mid = (start + end) / 2; ethToBorrow = (toFlash * mid) / 10 ** (decimals + 1); totDep = toFlash + ethToBorrow; debtMinted = (totDep * debt) / collateral; // get quote for debt minted and check if eth value is > borrowed but within deviation // if eth value is lesser, then we borrow less so end = mid; else start = mid ethReceived = _getSqthPrice(debtMinted); if (ethReceived >= ethToBorrow && ethReceived <= (ethToBorrow * 10100) / 10000) { break; } // mid is the multiple else { if (ethReceived > ethToBorrow) { start = mid; } else { end = mid; } } } // why is all the eth not being take in return mid + 1e7; } function _isEnough(uint256 _userETh, uint256 oSqthQuantity, uint256 oSqthPrice, uint256 _totalDep) internal pure returns (bool) { uint256 totalAfterSelling = (_userETh + ((oSqthQuantity * oSqthPrice)) / 1e18); return totalAfterSelling > _totalDep; } function _convertUSDToETH(uint256 _usdc) internal returns (uint256) { // get the uniswap quoter contract code and address and initiate it return quoter.quoteExactInputSingle( address(usdc), address(weth), 500, //3000 is 0.3 _usdc, 0 ); } function _getSqthPrice(uint256 _quantity) internal returns (uint256) { return quoter.quoteExactInputSingle(address(sqth), address(weth), 3000, _quantity, 0); } } ================================================ FILE: packages/crab-netting/test/ForkTestNetAtPrice.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; import "forge-std/Test.sol"; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; contract ForkTestNetAtPrice is BaseForkSetup { function setUp() public override { BaseForkSetup.setUp(); // this is a crab whale, get some crab token from vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, 1e18); // some WETH and USDC rich address vm.prank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); usdc.transfer(depositor, 20e6); } function testForkTestNetAtPrice() public { vm.startPrank(depositor); usdc.approve(address(netting), 17e6); netting.depositUSDC(17e6); vm.stopPrank(); vm.startPrank(withdrawer); crab.approve(address(netting), 1e18); netting.queueCrabForWithdrawal(1e18); vm.stopPrank(); assertEq(usdc.balanceOf(withdrawer), 0); assertEq(crab.balanceOf(depositor), 0); uint256 priceToNet = 1336290000; uint256 quantityToNet = 16840842; netting.netAtPrice(priceToNet, quantityToNet); // $1336.29 per crab and nets $16.84 assertApproxEqAbs(usdc.balanceOf(withdrawer), quantityToNet, 1); // withdrawer gets that amount uint256 crabReceived = (quantityToNet * 1e18) / priceToNet; assertEq(crab.balanceOf(depositor), crabReceived); // depositor gets 0.01265755 crab assertEq(netting.crabBalance(withdrawer), 1e18 - crabReceived); // ensure crab remains } } ================================================ FILE: packages/crab-netting/test/Netting.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "forge-std/Test.sol"; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; import {console} from "forge-std/console.sol"; contract NettingTest is BaseForkSetup { function setUp() public override { BaseForkSetup.setUp(); // gives you netting, depositor, withdrawer, usdc, crab vm.startPrank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); usdc.transfer(depositor, 400e6); vm.stopPrank(); vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, 40e18); vm.startPrank(depositor); // makes some USDC deposits usdc.approve(address(netting), 280 * 1e6); netting.depositUSDC(20 * 1e6); netting.depositUSDC(100 * 1e6); netting.depositUSDC(80 * 1e6); assertEq(netting.usdBalance(depositor), 200e6); vm.stopPrank(); vm.startPrank(withdrawer); // queue some crab crab.approve(address(netting), 200 * 1e18); netting.queueCrabForWithdrawal(5 * 1e18); netting.queueCrabForWithdrawal(4 * 1e18); netting.queueCrabForWithdrawal(11 * 1e18); assertEq(netting.crabBalance(withdrawer), 20e18); vm.stopPrank(); // withdrawer has 20 queued and depositor 200 } function testNettingAmountEqlsDeposit() public { uint256 price = 1330e6; uint256 quantity = 20e6; netting.netAtPrice(price, quantity); assertEq(netting.usdBalance(depositor), 180e6); uint256 crabReceived = ((quantity * 1e18) / price); assertEq(crab.balanceOf(depositor), crabReceived); } function testNettingAmountEqlsZero() public { uint256 price = 1330e6; uint256 quantity = 0; netting.netAtPrice(price, quantity); assertEq(netting.usdBalance(depositor), 200e6); } function testNettingAmountGreaterThanBalance() public { uint256 price = 1330e6; uint256 quantity = 30e10; vm.expectRevert(); netting.netAtPrice(price, quantity); } function testNetting() public { // TODO turn this into a fuzzing test assertEq(usdc.balanceOf(withdrawer), 0, "starting balance"); assertEq(crab.balanceOf(depositor), 0, "depositor got their crab"); uint256 price = 1330e6; uint256 quantity = 100e6; netting.netAtPrice(price, quantity); // net for 100 USD where 1 crab is 10 USD, so 10 crab assertApproxEqAbs(usdc.balanceOf(withdrawer), quantity, 1, "withdrawer did not get their usdc"); assertEq(crab.balanceOf(depositor), (quantity * 1e18) / price, "depositor did not get their crab"); } function testNettingWithMultipleDeposits() public { assertEq(usdc.balanceOf(withdrawer), 0, "withdrawer starting balance"); assertEq(crab.balanceOf(depositor), 0, "depositor starting balance"); uint256 price = 1330e6; uint256 quantity = 200e6; netting.netAtPrice(price, quantity); // net for 200 USD where 1 crab is 10 USD, so 20 crab assertApproxEqAbs(usdc.balanceOf(withdrawer), quantity, 1, "withdrawer did not get their usdc"); assertEq(crab.balanceOf(depositor), (quantity * 1e18) / price, "depositor did not get their crab"); } function testNettingWithPartialReceipt() public { assertEq(usdc.balanceOf(withdrawer), 0, "withdrawer starting balance"); assertEq(crab.balanceOf(depositor), 0, "depositor starting balance"); uint256 price = 1330e6; uint256 quantity = 30e6; netting.netAtPrice(price, quantity); // 20 from first desposit and 10 from second (partial) assertEq(netting.depositsQueued(), 170e6, "receipts were not updated correctly"); netting.netAtPrice(price, 170e6); assertEq(crab.balanceOf(depositor), (200e6 * 1e18) / price, "depositor got their crab"); } function testNettingAfterWithdraw() public { assertEq(usdc.balanceOf(withdrawer), 0, "withdrawer starting balance"); assertEq(crab.balanceOf(depositor), 0, "depositor starting balance"); vm.prank(depositor); uint256 withdrawQuantity = 50e6; netting.withdrawUSDC(withdrawQuantity, false); uint256 price = 1330e6; uint256 quantity = 200e6 - withdrawQuantity; netting.netAtPrice(price, quantity); assertEq(crab.balanceOf(depositor), (quantity * 1e18) / price, "depositor got their crab"); } function testNettingAfterARun() public { uint256 price = 1330e6; uint256 quantity = 200e6; vm.startPrank(depositor); netting.withdrawUSDC(80e6, false); netting.depositUSDC(80e6); vm.stopPrank(); vm.prank(withdrawer); netting.dequeueCrab(20e18 - (quantity * 1e18) / price, false); netting.netAtPrice(price, 200e6); // net for 100 USD where 1 crab is 10 USD, so 10 crab assertEq(netting.crabBalance(withdrawer), 0, "crab balance not zero after first netting"); // queue more vm.startPrank(depositor); usdc.approve(address(netting), 200 * 1e6); netting.depositUSDC(20 * 1e6); netting.depositUSDC(100 * 1e6); netting.depositUSDC(80 * 1e6); assertEq(netting.usdBalance(depositor), 200e6, "usd balance not reflecting correctly"); vm.stopPrank(); console.log("no issues till here 2"); vm.startPrank(withdrawer); crab.approve(address(netting), 200 * 1e18); netting.queueCrabForWithdrawal(5 * 1e18); netting.queueCrabForWithdrawal(4 * 1e18); netting.queueCrabForWithdrawal(11 * 1e18); assertEq(netting.crabBalance(withdrawer), 20e18, "crab balance not reflecting correctly"); vm.stopPrank(); console.log("no issues till here 3"); netting.netAtPrice(price, 200e6); // net for 100 USD where 1 crab is 10 USD, so 10 crab console.log("no issues till here 4"); assertApproxEqAbs(usdc.balanceOf(withdrawer), 400e6, 2, "witadrawer got their usdc"); assertEq(crab.balanceOf(depositor), (400e6 * 1e18) / price, "depositor got their crab"); } function testCannotWithdrawMoreThanDeposited() public { vm.startPrank(depositor); vm.expectRevert(stdError.arithmeticError); netting.withdrawUSDC(210e6, false); vm.stopPrank(); } function testSkipsUSDCBannedAddress() public { // remove the withdrawers crab so that we dont net them vm.prank(withdrawer); netting.dequeueCrab(20e18, false); // get bob the blacklisted address some crab address bob = address(0xAa05F7C7eb9AF63D6cC03C36c4f4Ef6c37431EE0); vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(bob, 10e18); // bob now deposits vm.startPrank(bob); crab.approve(address(netting), 10e18); netting.queueCrabForWithdrawal(10e18); vm.stopPrank(); vm.expectRevert(bytes("Blacklistable: account is blacklisted")); netting.netAtPrice(1330e6, 200e6); netting.rejectWithdraw(3); vm.prank(withdrawer); netting.queueCrabForWithdrawal(20e18); netting.netAtPrice(1330e6, 200e6); assertEq(crab.balanceOf(bob), 10e18); assertEq(netting.crabBalance(bob), 0); } } ================================================ FILE: packages/crab-netting/test/PriceChecks.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import "forge-std/Test.sol"; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; contract PriceChecks is BaseForkSetup { uint256 crabsToWithdraw = 40e18; uint256 price = 1269e6; // 1335 bounds are 1267 and 1401 uint256 totalUSDCRequired = (crabsToWithdraw * price) / 1e18; function setUp() public override { BaseForkSetup.setUp(); // gives you netting, depositor, withdrawer, usdc, crab vm.prank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); usdc.transfer(depositor, totalUSDCRequired); vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, crabsToWithdraw); // make multiple deposits from depositor vm.startPrank(depositor); usdc.approve(address(netting), totalUSDCRequired); netting.depositUSDC(totalUSDCRequired); vm.stopPrank(); // queue multiple crabs from withdrawer vm.startPrank(withdrawer); crab.approve(address(netting), crabsToWithdraw); netting.queueCrabForWithdrawal(crabsToWithdraw); vm.stopPrank(); } function testCrabPriceHigh() public { console.log("expecting a high crdab price"); vm.expectRevert(bytes("N23")); netting.netAtPrice(1500e6, totalUSDCRequired / 2); } function testCrabPriceLow() public { vm.expectRevert(bytes("N24")); netting.netAtPrice(1100e6, totalUSDCRequired / 2); } } ================================================ FILE: packages/crab-netting/test/QueuedBalances.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; contract QueuedBalancesTest is BaseForkSetup { uint256 crabsToWithdraw = 40e18; uint256 price = 1279e6; // 1338 bounds are 1271 and 1404 uint256 totalUSDCRequired = (crabsToWithdraw * price) / 1e18; function setUp() public override { BaseForkSetup.setUp(); // gives you netting, depositor, withdrawer, usdc, crab vm.startPrank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); usdc.transfer(depositor, totalUSDCRequired); vm.stopPrank(); vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, crabsToWithdraw); // make multiple deposits from depositor vm.startPrank(depositor); usdc.approve(address(netting), totalUSDCRequired); netting.depositUSDC((totalUSDCRequired * 10) / 100); netting.depositUSDC((totalUSDCRequired * 50) / 100); netting.depositUSDC((totalUSDCRequired * 40) / 100); assertEq(netting.usdBalance(depositor), totalUSDCRequired); vm.stopPrank(); // queue multiple crabs from withdrawer vm.startPrank(withdrawer); crab.approve(address(netting), crabsToWithdraw); netting.queueCrabForWithdrawal((crabsToWithdraw * 25) / 100); netting.queueCrabForWithdrawal((crabsToWithdraw * 20) / 100); netting.queueCrabForWithdrawal((crabsToWithdraw * 55) / 100); assertEq(netting.crabBalance(withdrawer), crabsToWithdraw); vm.stopPrank(); netting.netAtPrice(price, totalUSDCRequired / 2); // net for 100 USD where 1 crab is 10 USD, so 10 crab } function testcrabBalanceQueued() public { assertEq(netting.depositsQueued(), totalUSDCRequired / 2); } function testWithdrawsQueued() public { assertEq(netting.withdrawsQueued(), crabsToWithdraw / 2); } } ================================================ FILE: packages/crab-netting/test/SkipDeposits.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.13; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; contract SkipDeposits is BaseForkSetup { function setUp() public override { BaseForkSetup.setUp(); // gives you netting, depositor, withdrawer, usdc, crab vm.startPrank(0x57757E3D981446D585Af0D9Ae4d7DF6D64647806); usdc.transfer(depositor, 20e6); vm.stopPrank(); vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, 20e18); } function testSkipDeposits() public { netting.setMinUSDC(1e6); vm.startPrank(depositor); usdc.approve(address(netting), 2 * 1e6); netting.depositUSDC(1e6); netting.depositUSDC(1e6); vm.stopPrank(); assertEq(netting.depositsQueued(), 2e6); netting.setDepositsIndex(1); assertEq(netting.depositsQueued(), 1e6); } function testSkipWithdraws() public { netting.setMinCrab(1e18); vm.startPrank(withdrawer); crab.approve(address(netting), 2 * 1e18); netting.queueCrabForWithdrawal(1e18); netting.queueCrabForWithdrawal(1e18); vm.stopPrank(); assertEq(netting.withdrawsQueued(), 2e18); netting.setWithdrawsIndex(1); assertEq(netting.withdrawsQueued(), 1e18); } } ================================================ FILE: packages/crab-netting/test/WithdrawAuction.t.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; import "forge-std/Test.sol"; import {Order, WithdrawAuctionParams} from "../src/CrabNetting.sol"; import {ICrabStrategyV2} from "../src/interfaces/ICrabStrategyV2.sol"; import {UniswapQuote} from "./utils/UniswapQuote.sol"; import {BaseForkSetup} from "./BaseForkSetup.t.sol"; import {ERC20} from "openzeppelin/token/ERC20/ERC20.sol"; import {IWETH} from "../src/interfaces/IWETH.sol"; import {SigUtils} from "./utils/SigUtils.sol"; struct TimeBalances { uint256 start; uint256 end; } struct Portion { uint256 collateral; uint256 debt; } struct Sign { uint8 v; bytes32 r; bytes32 s; } contract TestWithdrawAuction is BaseForkSetup { SigUtils sig; function setUp() public override { BaseForkSetup.setUp(); sig = new SigUtils(netting.DOMAIN_SEPARATOR()); // this is a crab whale, get some crab token from vm.prank(0x06CECFbac34101aE41C88EbC2450f8602b3d164b); crab.transfer(withdrawer, 20e18); // send sqth to market makers todo vm.startPrank(0x56178a0d5F301bAf6CF3e1Cd53d9863437345Bf9); sqth.transfer(mm1, 1000e18); sqth.transfer(mm2, 1000e18); vm.stopPrank(); // deposit crab for withdrawing vm.startPrank(withdrawer); crab.approve(address(netting), 19 * 1e18); netting.queueCrabForWithdrawal(2 * 1e18); netting.queueCrabForWithdrawal(3 * 1e18); netting.queueCrabForWithdrawal(6 * 1e18); vm.stopPrank(); // 11 crabs queued for withdrawal } function testWithdrawAuction() public { WithdrawAuctionParams memory params; // find the sqth to buy to make the trade params.crabToWithdraw = 10e18; uint256 sqthToBuy = crab.getWsqueethFromCrabAmount(params.crabToWithdraw); UniswapQuote quote = new UniswapQuote(); uint256 sqthPrice = quote.getSqthPrice(1e18); params.clearingPrice = (sqthPrice * 1001) / 1000; // get the orders for that sqth vm.prank(mm1); sqth.approve(address(netting), 1000000e18); Order memory order0 = Order(0, mm1, 1e18, params.clearingPrice, false, block.timestamp, 2, 1, 0x00, 0x00); Sign memory s0; (s0.v, s0.r, s0.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order0)); order0.v = s0.v; order0.r = s0.r; order0.s = s0.s; orders.push(order0); Order memory order = Order(0, mm1, sqthToBuy - 1e18, params.clearingPrice, false, block.timestamp, 0, 1, 0x00, 0x00); Sign memory s1; (s1.v, s1.r, s1.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order)); order.v = s1.v; order.r = s1.r; order.s = s1.s; orders.push(order); params.orders = orders; // find the minUSDC to receive // get col and wsqth from crab amount, find the equity value in eth (,, uint256 collateral,) = crab.getVaultDetails(); Portion memory p; p.collateral = (params.crabToWithdraw * collateral) / crab.totalSupply(); p.debt = crab.getWsqueethFromCrabAmount(params.crabToWithdraw); uint256 equityInEth = p.collateral - (p.debt * params.clearingPrice) / 1e18; params.minUSDC = (quote.convertWETHToUSDC(equityInEth) * 999) / 1000; params.ethUSDFee = 500; // get equivalent usdc quote with slippage and send // call withdrawAuction on netting contract TimeBalances memory timeUSDC; TimeBalances memory timeWETH; timeUSDC.start = ERC20(usdc).balanceOf(withdrawer); timeWETH.start = IWETH(weth).balanceOf(mm1); netting.withdrawAuction(params); timeUSDC.end = ERC20(usdc).balanceOf(withdrawer); timeWETH.end = IWETH(weth).balanceOf(mm1); assertGe(timeUSDC.end - timeUSDC.start, params.minUSDC); assertGe(timeWETH.end - timeWETH.start, (sqthToBuy * sqthPrice) / 1e18); // and eth recevied for mm assertEq(address(netting).balance, 0); assertEq(ERC20(sqth).balanceOf(address(netting)), 0, "sqth balance"); assertLe(ERC20(usdc).balanceOf(address(netting)), 1, "usdc balance"); assertEq(ICrabStrategyV2(crab).balanceOf(address(netting)), 1e18, "crab balance"); assertEq(netting.crabBalance(address(withdrawer)), 11e18 - params.crabToWithdraw); assertEq(IWETH(weth).balanceOf(address(netting)), 0, "weth balance"); } function testWithdrawAuctionAfterFullWithdraw() public { vm.startPrank(withdrawer); netting.dequeueCrab(6e18, false); netting.queueCrabForWithdrawal(6e18); vm.stopPrank(); WithdrawAuctionParams memory params; // find the sqth to buy to make the trade params.crabToWithdraw = 10e18; uint256 sqthToBuy = crab.getWsqueethFromCrabAmount(params.crabToWithdraw); UniswapQuote quote = new UniswapQuote(); uint256 sqthPrice = quote.getSqthPrice(1e18); params.clearingPrice = (sqthPrice * 1001) / 1000; // get the orders for that sqth vm.prank(mm1); sqth.approve(address(netting), 1000000e18); Order memory order0 = Order(0, mm1, 1e18, params.clearingPrice, false, block.timestamp, 2, 1, 0x00, 0x00); Sign memory s0; (s0.v, s0.r, s0.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order0)); order0.v = s0.v; order0.r = s0.r; order0.s = s0.s; orders.push(order0); Order memory order = Order(0, mm1, sqthToBuy - 1e18, params.clearingPrice, false, block.timestamp, 0, 1, 0x00, 0x00); Sign memory s1; (s1.v, s1.r, s1.s) = vm.sign(mm1Pk, sig.getTypedDataHash(order)); order.v = s1.v; order.r = s1.r; order.s = s1.s; orders.push(order); params.orders = orders; // find the minUSDC to receive // get col and wsqth from crab amount, find the equity value in eth (,, uint256 collateral,) = crab.getVaultDetails(); Portion memory p; p.collateral = (params.crabToWithdraw * collateral) / crab.totalSupply(); p.debt = crab.getWsqueethFromCrabAmount(params.crabToWithdraw); uint256 equityInEth = p.collateral - (p.debt * params.clearingPrice) / 1e18; params.minUSDC = (quote.convertWETHToUSDC(equityInEth) * 999) / 1000; params.ethUSDFee = 500; // get equivalent usdc quote with slippage and send // call withdrawAuction on netting contract TimeBalances memory timeUSDC; TimeBalances memory timeWETH; timeUSDC.start = ERC20(usdc).balanceOf(withdrawer); timeWETH.start = IWETH(weth).balanceOf(mm1); netting.withdrawAuction(params); timeUSDC.end = ERC20(usdc).balanceOf(withdrawer); timeWETH.end = IWETH(weth).balanceOf(mm1); assertGe(timeUSDC.end - timeUSDC.start, params.minUSDC); assertGe(timeWETH.end - timeWETH.start, (sqthToBuy * sqthPrice) / 1e18); // and eth recevied for mm assertEq(address(netting).balance, 0); assertEq(ERC20(sqth).balanceOf(address(netting)), 0, "sqth balance"); assertLe(ERC20(usdc).balanceOf(address(netting)), 1, "usdc balance"); assertEq(ICrabStrategyV2(crab).balanceOf(address(netting)), 1e18, "crab balance"); assertEq(netting.crabBalance(address(withdrawer)), 11e18 - params.crabToWithdraw); assertEq(IWETH(weth).balanceOf(address(netting)), 0, "weth balance"); } function testSqthPriceAboveThreshold() public { WithdrawAuctionParams memory params; // find the sqth to buy to make the trade params.crabToWithdraw = 10e18; uint256 sqthToBuy = 1e6; UniswapQuote quote = new UniswapQuote(); uint256 sqthPrice = quote.getSqthPrice(1e18); params.clearingPrice = (sqthPrice * 106) / 100; // get the orders for that sqth Order memory order = Order(0, mm1, sqthToBuy, params.clearingPrice, false, block.timestamp, 0, 1, 0x00, 0x00); (uint8 v, bytes32 r, bytes32 s) = vm.sign(mm1Pk, sig.getTypedDataHash(order)); order.v = v; order.r = r; order.s = s; orders.push(order); params.orders = orders; params.minUSDC = 1e6; params.ethUSDFee = 500; // get equivalent usdc quote with slippage and send vm.expectRevert(bytes("N21")); netting.withdrawAuction(params); } } ================================================ FILE: packages/crab-netting/test/utils/SigUtils.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; import {console} from "forge-std/console.sol"; import {Order} from "../../src/CrabNetting.sol"; contract SigUtils { bytes32 internal DOMAIN_SEPARATOR; constructor(bytes32 _DOMAIN_SEPARATOR) { DOMAIN_SEPARATOR = _DOMAIN_SEPARATOR; } // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 public constant _CRAB_NETTING_TYPEHASH = keccak256( "Order(uint256 bidId,address trader,uint256 quantity,uint256 price,bool isBuying,uint256 expiry,uint256 nonce)" ); // computes the hash of a permit function getStructHash(Order memory _order) internal pure returns (bytes32) { return keccak256( abi.encode( _CRAB_NETTING_TYPEHASH, _order.bidId, _order.trader, _order.quantity, _order.price, _order.isBuying, _order.expiry, _order.nonce ) ); } // computes the hash of the fully encoded EIP-712 message for the domain, which can be used to recover the signer function getTypedDataHash(Order memory _order) public view returns (bytes32) { return keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, getStructHash(_order))); } } ================================================ FILE: packages/crab-netting/test/utils/UniswapQuote.sol ================================================ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.17; import {IQuoter} from "@uniswap/v3-periphery/contracts/interfaces/IQuoter.sol"; contract UniswapQuote { address usdc = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; address sqth = 0xf1B99e3E573A1a9C5E6B2Ce818b617F0E664E86B; address weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; IQuoter public immutable quoter = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6); function convertUSDToETH(uint256 _usdc) external returns (uint256) { // get the uniswap quoter contract code and address and initiate it return quoter.quoteExactInputSingle( (usdc), (weth), 500, //3000 is 0.3 _usdc, 0 ); } function convertWETHToUSDC(uint256 _weth) external returns (uint256) { // get the uniswap quoter contract code and address and initiate it return quoter.quoteExactInputSingle( (weth), (usdc), 500, //3000 is 0.3 _weth, 0 ); } function getSqthPrice(uint256 _quantity) external returns (uint256) { return quoter.quoteExactInputSingle((sqth), (weth), 3000, _quantity, 0); } } ================================================ FILE: packages/frontend/.babelrc ================================================ { "presets": ["next/babel"] } ================================================ FILE: packages/frontend/.eslintrc ================================================ { "parser": "@typescript-eslint/parser", "plugins": ["prettier", "@typescript-eslint", "react", "react-hooks", "simple-import-sort"], "extends": [ "plugin:react/recommended", "plugin:@typescript-eslint/recommended", "plugin:prettier/recommended", "prettier", "next", "next/core-web-vitals" ], "env": { "browser": true, "jest": true }, "parserOptions": { "ecmaVersion": 2020, "sourceType": "module", "ecmaFeatures": { "jsx": true } }, "settings": { "react": { "version": "detect" } }, "rules": { "@typescript-eslint/no-unused-vars": [ "warn", { "argsIgnorePattern": "^_", "ignoreRestSiblings": true } ], "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/explicit-module-boundary-types": "off", "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/member-delimiter-style": "off", "@typescript-eslint/no-non-null-assertion": "off", "@typescript-eslint/no-use-before-define": "off", "@typescript-eslint/no-empty-interface": "off", "@typescript-eslint/ban-ts-ignore": "off", "@typescript-eslint/ban-ts-comment": "off", "@typescript-eslint/camelcase": "off", "react-hooks/rules-of-hooks": "error", "react-hooks/exhaustive-deps": [ "warn", { "additionalHooks": "(useAppEffect|useAppMemo|useAppCallback)" } ], "react/prop-types": "off", "import/order": "off", "sort-imports": "off", "simple-import-sort/exports": "error", "@typescript-eslint/ban-types": "off", "@typescript-eslint/no-empty-function": "off", // This rule will help with preventing to commit code that is not yet finished // I removed TODO as a term so that we can still use that for future work // and use FIXME during the development process "no-warning-comments": [ "warn", { "terms": ["fixme"], "location": "start" } ] } } ================================================ FILE: packages/frontend/.gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # next.js /.next/ /out/ # production /build # misc .DS_Store *.pem # debug npm-debug.log* yarn-debug.log* yarn-error.log* # local env files .env.local .env.development.local .env.test.local .env.production.local # vercel .vercel # tests cypress/screenshots cypress/videos cypress/downloads ================================================ FILE: packages/frontend/.nvmrc ================================================ v18.15.0 ================================================ FILE: packages/frontend/.prettierrc ================================================ { "semi": false, "trailingComma": "all", "singleQuote": true, "useTabs": false, "bracketSpacing": true, "printWidth": 120, "tabWidth": 2 } ================================================ FILE: packages/frontend/CONTRIBUTING.md ================================================ # Squeeth Frontend Contribution Guidelines ## Issues - Check to see if your issue has been previously brought up, and if so add a comment to the existing issue - Follow the issue templates for bugs or feature requests - As much as possible, tie the issue back to the problem users are facing ## Branches - Please create a feature / fix / documentation branch for what you’re working on - Make a PR from your branch into the either the _master_ branch or a _staging_ branch. - Make PRs into _master_ for fixes and single features - Make PRs into the _staging_ branch for a bigger feature or set of fixes that need to be tested and then released all at once. Different features might have different staging branches. eg. "staging/strategies" and "staging/lp-nft-collateral" ## Commits - Follow the [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) format ``` build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) docs: Documentation only changes feat: A new feature fix: A bug fix perf: A code change that improves performance refactor: A code change that neither fixes a bug nor adds a feature style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) test: Adding missing tests or correcting existing tests ``` ## PRs - Each PR requires at least one review before it can be merged to master - Follow the PR template when making PRs - Link to the issue you are working on ================================================ FILE: packages/frontend/LICENSE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: packages/frontend/README.md ================================================ # Squeeth frontend [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) ## Getting started As the first step, install the node dependencies ``` yarn install ``` ### Set up the environment Copy the contents of `.env.example` to a new file `.env`. `NEXT_PUBLIC_INFURA_API_KEY` - Sign up in [Infura](https://infura.io/dashboard/ethereum) and create an Ethereum project to get infura key. `NEXT_PUBLIC_ALCHEMY_API_KEY` - Sign up at [Alchemy] (https://www.alchemy.com/) and create an Etheruem project to get an alchemy key `NEXT_PUBLIC_BLOCKNATIVE_DAPP_ID` - Sign up in [Blocknative](https://www.blocknative.com/) and get the api key. The backtests use Tardis and Firebase and we use Fathom for analytics. All of those fields are optional (note that the backtests will not show up without them though). ### Run the app Once everything is set run the following command. ``` yarn dev ``` Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. ## Deployment The easiest way to deploy this Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. Check out the [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. ## Contributions We welcome contributions to the Squeeth Frontend! You can contribute by resolving existing issues, taking on feature requests, and refactoring code. Please feel free to open new issues / feature requests as well. You can find our [contribution guidelines here.](CONTRIBUTING.md) If you have questions about contributing, ping us on #dev in the [Opyn discord](http://tiny.cc/opyndiscord) :) ## Branding Don't use the Opyn or Squeeth logo or name in anything dishonest or fraudulent. If you deploy another version of the interface, please make it clear that it is an interface to the Squeeth Protocol, but not affiliated with Opyn Inc. ================================================ FILE: packages/frontend/custom.d.ts ================================================ declare module 'fzero' ================================================ FILE: packages/frontend/cypress/TEST.md ================================================ - Setup / Preparation 1. Test Environment 1. Test locally 2. Network: Ropsten 3. Wallet: Metamask 4. Browser: Default is Chrome 1. Can test with firefox or chromium-based browsers as well, but will need to change the command in `package.json` , 5. Test websites 1. squeeth, at `localhost:3000` 2. uniswap lp page to lp oSQTH 2. Test setup 1. Make sure `synpress` and `dotenv-cli` packages are installed successfully with `package.json` 1. if there is no `dotenv-cli` installed, run `yarn global add dotenv-cli` 2. Put SECRET_WORDS or PRIVATE_KEY and NETWORK in `.env` file, i.e. ```jsx SECRET_WORDS = 'word1, word2, word3...' NETWORK_NAME = ropsten OR PRIVATE_KEY = 'your PRIVATE_KEY' NETWORK_NAME = ropsten ``` 3. If you want to test with squeeth website, set `baseUrl` in `cypress.json` as `"baseUrl": "http://localhost:3000",` 4. if you want to test lp, set `baseUrl` in `cypress.json` as `"baseUrl": "https://squeeth-uniswap.netlify.app/#/add/ETH/0xa4222f78d23593e82Aa74742d25D06720DCa4ab7/3000",` 5. Recommended to clear your existing position before you run the tests, otherwise it may be hard to tell if the test fails because of the existing position or the new position 6. Run `yarn run build` and `yarn start` , dont use `yarn dev` , cuz it will have warning pop-ups hiding some DOM elements 7. **Recommended**: if you only want to test one page or related functionalities or the functionalities you changed, plz specify the tests file you would like to include, it will run only provided spec files, i.e. ```jsx yarn run cypress -s cypress/integration/specs/01-trade-long.spec.js yarn run cypress -s cypress/integration/specs/02-trade-short.spec.js ``` 1. Tests you should run 1. Test long: `01-trade-long.spec` 2. Test short: `02-trade-short.spec` 3. Test vault with short or mint position: `03-vault.spec` 4. Test lp: 1. buy to lp: `04-buy-lp` + `05-lp-uniswap` + `05-lp-position` 2. mint to lp: `04-mint-lp` + `05-lp-uniswap` + `05-lp-position` + `05-lp-vault` 5. Test manual short : `06-manual-short` 6. Test strategy: `07-strategy` 7. Test on position page : `08-position` 8. Test on lp nft as collateral : `09-lp-token` 8. **Not recommended**: Run `yarn run cypress` to start all testing, it will start with `01-trade-long.spec` by default, it will take forever to run through all the tests ================================================ FILE: packages/frontend/cypress/fixtures/example.json ================================================ { "name": "Using fixtures to represent data", "email": "hello@cypress.io", "body": "Fixtures are a great way to mock data for responses to routes" } ================================================ FILE: packages/frontend/cypress/integration/pages/header.js ================================================ import Page from './page' export default class Header extends Page { getConnectWalletBtn() { return cy.get('#connect-wallet') } getWalletAddress() { return cy.get('#wallet-address') } } ================================================ FILE: packages/frontend/cypress/integration/pages/notifications.js ================================================ import Page from './page' export default class Notifications extends Page { getTransactionSuccessNotification() { return cy.contains('has succeeded', { timeout: 600000 }).should(`exist`) } getTransactionSuccessNotificationLink() { return cy.get('.bn-notify-notification-success a', { timeout: 60000 }) } } ================================================ FILE: packages/frontend/cypress/integration/pages/onboard.js ================================================ import Page from './page' export default class Onboard extends Page { getBrowserWalletBtn() { return cy.get('[alt="MetaMask"]') } } ================================================ FILE: packages/frontend/cypress/integration/pages/page.js ================================================ export default class Page { getTitle() { return cy.title() } getMetamaskWalletAddress() { return cy.fetchMetamaskWalletAddress() } acceptMetamaskAccessRequest() { cy.acceptMetamaskAccess() } confirmMetamaskTransaction() { // Currently without supplying a gas configuration results in failing transactions // Possibly caused by wrong default behaviour within Synpress cy.confirmMetamaskTransaction({ gasFee: 30, gasLimit: 5000000 }) } confirmMetamaskPermissionToSpend() { cy.confirmMetamaskPermissionToSpend().then((approved) => { expect(approved).to.be.true }) } } ================================================ FILE: packages/frontend/cypress/integration/pages/trade.js ================================================ import Page from './page' import Header from './header' import Onboard from './onboard' import Notifications from './notifications.js' export default class TradePage extends Page { constructor() { super() this.header = new Header() this.onboard = new Onboard() this.notifications = new Notifications() } connectBrowserWallet() { const connectWalletButton = this.header.getConnectWalletBtn() connectWalletButton.click() const onboardBrowserWalletButton = this.onboard.getBrowserWalletBtn() onboardBrowserWalletButton.click() } getLoggedInWalletAddress() { const addr = this.header.getWalletAddress() return addr.invoke('text') } waitForTransactionSuccess() { cy.waitUntil( () => { const txSuccessNotification = this.notifications.getTransactionSuccessNotification() return txSuccessNotification.should('exist') }, { // tx takes a bit longer so extend the timeout duration, wait for 200000 ms timeout: 200000, }, ) } getTransactionUrl() { const txUrl = this.notifications.getTransactionSuccessNotificationLink() return txUrl.invoke('attr', 'href') } } ================================================ FILE: packages/frontend/cypress/integration/specs/01-trade-long.spec.js ================================================ /// import TradePage from '../pages/trade' import BigNumber from 'bignumber.js' const trade = new TradePage() describe('Trade on trade page', () => { context('Before tests', () => { it(`Before tests`, () => { cy.disconnectMetamaskWalletFromAllDapps() cy.visit('/') }) }) context('Connect metamask wallet', () => { it(`should login with success`, () => { trade.connectBrowserWallet() trade.acceptMetamaskAccessRequest() cy.get('#wallet-address').should(`contain.text`, '0x' || '.eth') }) }) context('Trade switch checks', () => { it('check if there is open long card opened by default', () => { cy.get('#open-long-header-box').should('contain.text', 'Pay ETH to buy squeeth ERC20') }) it('switch between long & short, open & close trade cards with default 0 value', () => { cy.get('#long-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-long-header-box').should('contain.text', 'Pay ETH to buy squeeth ERC20') cy.get('#open-long-eth-input').should('have.value', '0') cy.get('#open-long-osqth-input').should('have.value', '0') // to make sure the cards are function independently after switching cy.get('#open-long-eth-input').type('2', { delay: 200 }).should('have.value', '2') cy.get('#long-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) cy.get('#close-long-header-box').should('contain.text', 'Sell squeeth ERC20 to get ETH') cy.get('#close-long-eth-input').should('have.value', '0') cy.get('#close-long-osqth-input').should('have.value', '0') cy.get('#close-long-eth-input').type('2', { delay: 200 }).should('have.value', '2') cy.get('#short-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-short-header-box').should('contain.text', 'Mint & sell squeeth for premium') cy.get('#open-short-eth-input').should('have.value', '0') cy.get('#open-short-trade-details .trade-details-amount').should('contain.text', '0') cy.get('#open-short-eth-input').type('2', { delay: 200 }).should('have.value', '2') cy.get('#short-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) cy.get('#close-short-header-box').should('contain.text', 'Buy back oSQTH & close position') cy.get('#close-short-osqth-input').should('have.value', '0') cy.get('#close-short-trade-details .trade-details-amount').should('contain.text', '0') }) }) let openLongoSQTHInput let openLongETHInput let closeLongoSQTHInput let closeLongETHInput let openLongOsqthBeforeTradeBal let posCardBeforeLongTradeBal let closeLongBeforeTradeBal context(`open long position`, () => { before('jump to open long trade card', () => { cy.get('#long-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) }) before(() => { it('eth balance should be greater than 0', () => { cy.get('#user-eth-wallet-balance').invoke('text').then(parseFloat).should('be.greaterThan', 0) }) }) context('open long trade condition checks', () => { it('eth balance from wallet should be the same as eth input box', () => { cy.get('#user-eth-wallet-balance').then((bal) => { cy.get('#open-long-eth-before-trade-balance').should('contain.text', Number(bal.text()).toFixed(4)) }) }) it('it is on open long card', () => { cy.get('#open-long-header-box').should('contain.text', 'Pay ETH to buy squeeth ERC20') }) }) context('input checks', () => { // issue 277 it.skip('inputs should be zero by default and tx button is disabled', () => { cy.get('#open-long-header-box').should('contain.text', 'Pay ETH to buy squeeth ERC20') cy.get('#open-long-eth-input').should('have.value', '0') cy.get('#open-long-osqth-input').should('have.value', '0') cy.get('#open-long-submit-tx-btn').should('be.disabled') }) it('zero input amount', () => { cy.get('#open-long-eth-input').clear().type('0', { delay: 200 }).should('have.value', '0') cy.get('#open-long-osqth-input').clear().type('0', { delay: 200 }).should('have.value', '0') }) it('invalid input amount', () => { cy.get('#open-long-eth-input').clear().type('\\', { delay: 200 }).should('have.value', '0') cy.get('#open-long-osqth-input').clear().type('\\', { delay: 200 }).should('have.value', '0') }) }) context('can enter an amount into eth input, check position card & input box balances', () => { it('can enter an amount into eth input', () => { cy.get('#open-long-eth-input').should('be.visible') cy.get('#open-long-eth-input').clear().type('1.', { force: true, delay: 200 }).should('have.value', '1.0') cy.get('#open-long-osqth-input').should('not.equal', '0') cy.get('#open-long-osqth-input').then((v) => { openLongoSQTHInput = new BigNumber(v.val().toString()) }) }) // a-post = a + input it('input box oSQTH post trade balance should be the same as before-trade + input when input changes', () => { cy.get('#open-long-osqth-before-trade-balance').then((bal) => { cy.get('#open-long-osqth-post-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(4))) .should('be.approximately', Number(openLongoSQTHInput.plus(Number(bal.text()))), 0.0002) }) }) // b-post = b + input it('position card oSQTH post trade balance should be the same as before-trade + input when input changes', () => { cy.get('#position-card-before-trade-balance').then((bal) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(4))) .should('be.approximately', Number(openLongoSQTHInput.plus(Number(bal.text()))), 0.0002) }) }) // a = b it('position card oSQTH before trade balance should be the same as input box before oSQTH trade balance', () => { cy.get('#open-long-osqth-before-trade-balance').then((bal) => { cy.get('#position-card-before-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) }) // a + input = b + input != 0 it('position card oSQTH post trade balance should be the same as input box post oSQTH trade balance and not equal 0', () => { cy.get('#open-long-osqth-post-trade-balance').then((bal) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) cy.get('#open-long-osqth-post-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) cy.get('#position-card-post-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) // eth-post = eth-before - openLongETHInput it('input box eth post trade balance should be the same as before-trade - input when input changes', () => { cy.get('#open-long-eth-before-trade-balance').then((bal) => { cy.get('#open-long-eth-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', (Number(bal.text()) - 1).toFixed(4)) }) }) }) context('can enter an amount into osqth input, check position card & input box balances', () => { it('can enter an amount into osqth input', () => { cy.get('#open-long-osqth-input').should('be.visible') cy.get('#open-long-osqth-input').clear().type('1.', { force: true, delay: 200 }).should('have.value', '1.0') openLongoSQTHInput = new BigNumber(1) }) // a-post = a + input it('input box oSQTH post trade balance should be the same as before-trade + input when input changes', () => { cy.get('#open-long-osqth-before-trade-balance').then((bal) => { cy.get('#open-long-osqth-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', openLongoSQTHInput.plus(Number(bal.text())).toFixed(4)) }) }) // b-post = b + input it('position card oSQTH post trade balance should be the same as before-trade + input when input changes', () => { cy.get('#position-card-before-trade-balance').then((bal) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', openLongoSQTHInput.plus(Number(bal.text())).toFixed(4)) }) }) // a = b it('position card oSQTH before trade balance should be the same as input box before oSQTH trade balance', () => { cy.get('#open-long-osqth-before-trade-balance').then((bal) => { cy.get('#position-card-before-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) }) // a + input = b + input != 0 it('position card oSQTH post trade balance should be the same as input box post oSQTH trade balance and not equal 0', () => { cy.get('#open-long-eth-input').then((bal) => { openLongETHInput = new BigNumber(bal.text().toString()) }) cy.get('#open-long-osqth-post-trade-balance').then((bal) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) cy.get('#open-long-osqth-post-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) cy.get('#position-card-post-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) // eth-post = eth-before - openLongETHInput // issue #280 it.skip('input box eth post trade balance should be the same as before-trade - input when input changes', () => { cy.get('#open-long-eth-before-trade-balance').then((bal) => { cy.get('#open-long-eth-post-trade-balance') .wait(15000) .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text()).minus(openLongETHInput).toFixed(4)) }) }) }) context('can open long position', () => { it('can open long position for osqth, and tx succeeds', () => { cy.get('#open-long-eth-input').clear().type('1.', { force: true, delay: 200 }).should('have.value', '1.0') cy.get('#open-long-osqth-input').should('not.equal', '0') cy.get('#open-long-osqth-input').then((v) => { openLongoSQTHInput = new BigNumber(v.val().toString()) }) cy.get('#open-long-submit-tx-btn').should('contain.text', 'Buy') cy.get('#open-long-submit-tx-btn').should('not.be.disabled') cy.get('#open-long-osqth-before-trade-balance').then((val) => { openLongOsqthBeforeTradeBal = new BigNumber(val.text()) }) cy.get('#position-card-before-trade-balance').then((val) => { posCardBeforeLongTradeBal = new BigNumber(val.text()) }) cy.get('#open-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() }) it('there is open loong tx finished card after tx succeeds with correct closing value', () => { cy.get('#open-long-card').should('contain.text', 'Close').should('contain.text', 'Bought') cy.get('#conf-msg').should('contain.text', openLongoSQTHInput.toFixed(6)) cy.get('#open-long-close-btn').click({ force: true }) }) it('return to open long card successfully with all values update to 0', () => { cy.get('#open-long-header-box').should('contain.text', 'Pay ETH to buy squeeth ERC20') cy.get('#open-long-eth-input').should('have.value', '0') cy.get('#open-long-osqth-input').should('have.value', '0') cy.get('#open-long-submit-tx-btn').should('be.disabled') }) it('position card should update to new osqth balance', () => { // wait for 20 sec to update positon cy.get('#position-card-before-trade-balance') .wait(30000) .then((v) => Number(parseFloat(v.text()).toFixed(4))) .should('be.approximately', Number(posCardBeforeLongTradeBal.plus(openLongoSQTHInput)), 0.0002) }) it('input box before trade update to new osqth balance', () => { cy.get('#open-long-osqth-before-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(4))) .should('be.approximately', Number(openLongOsqthBeforeTradeBal.plus(openLongoSQTHInput)), 0.0002) }) it('position card update to the same value as input box before trade balance and not equal 0', () => { cy.get('#open-long-osqth-before-trade-balance').then((bal) => { cy.get('#position-card-before-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) cy.get('#open-long-osqth-before-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) cy.get('#position-card-before-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) // issue #282 it.skip('unrealized PnL display', () => { cy.get('#unrealized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) it('should have "close your long position to open a long" error in short oSQTH input when user have short oSQTH', () => { cy.get('#short-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-short-eth-input-box').should('contain.text', 'Close your long position to open a short') }) }) }) context(`close long position`, () => { before(() => { cy.get('#long-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) }) context('close long trade condition checks', () => { it('it is on close long card', () => { cy.get('#close-long-header-box').should('contain.text', 'Sell squeeth ERC20 to get ETH') }) it('inputs should be zero by default and tx button is disabled', () => { cy.get('#close-long-eth-input').should('have.value', '0') cy.get('#close-long-osqth-input').should('have.value', '0') cy.get('#close-long-submit-tx-btn').should('be.disabled') }) it('should have oSQTH long balance in position card', () => { cy.get('#position-card-before-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) }) context('input checks', () => { it('zero input amount', () => { cy.get('#close-long-eth-input').clear().type('0', { delay: 200 }).should('have.value', '0') cy.get('#close-long-osqth-input').clear().type('0', { delay: 200 }).should('have.value', '0') }) it('invalid input amount', () => { cy.get('#close-long-eth-input').clear().type('\\', { delay: 200 }).should('have.value', '0') cy.get('#close-long-osqth-input').clear().type('\\', { delay: 200 }).should('have.value', '0') }) it('submit tx button should be disabled when input is zero', () => { cy.get('#close-long-osqth-input').clear().type('0', { delay: 200 }).should('have.value', '0') cy.get('#close-long-submit-tx-btn').should('be.disabled') }) }) //1 input status check -> trade status check -> send tx -> post trade status check context('can close long with manual osqth inputs and tx succeeds', () => { it('can enter an amount into osqth input', () => { cy.get('#close-long-osqth-input').clear().type('0.1', { force: true, delay: 800 }).should('have.value', '0.1') cy.get('#close-long-eth-input').should('not.equal', '0') cy.get('#close-long-submit-tx-btn').should('not.be.disabled') }) it('position card before trade balance should be the same as input box before trade balance', () => { cy.get('#position-card-before-trade-balance').then((val) => { cy.get('#close-long-osqth-before-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', Number(val.text()).toFixed(6)) }) }) it('position card post trade balance should become before-trade - input when input changes', () => { cy.get('#position-card-before-trade-balance').then((val) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(val.text()) - 0.1, 0.000002) }) }) it('input box before trade balance should become before-trade - input when input changes', () => { cy.get('#close-long-osqth-before-trade-balance').then((val) => { cy.get('#close-long-osqth-post-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(val.text()) - 0.1, 0.000002) }) }) it('send tx', () => { cy.get('#close-long-osqth-before-trade-balance').then((bal) => { closeLongBeforeTradeBal = bal.text() }) cy.get('#position-card-before-trade-balance').then((bal) => { posCardBeforeLongTradeBal = bal.text() }) cy.get('#close-long-submit-tx-btn').then((btn) => { if (btn.text().includes('Approve oSQTH')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskPermissionToSpend() trade.waitForTransactionSuccess() cy.wait(15000).get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } else if (btn.text().includes('Sell')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) it('there is close long tx finished card after tx succeeds with correct closing value', () => { cy.get('#close-long-card').should('contain.text', 'Close').should('contain.text', 'Sold') cy.get('#conf-msg').should('contain.text', (0.1).toFixed(6)) cy.get('#close-long-close-btn').click({ force: true }) }) it('new position card value should be the same as prev position card value', () => { // wait for 30 sec to update positon cy.get('#position-card-before-trade-balance') .wait(30000) .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(posCardBeforeLongTradeBal) - 0.1, 0.000002) }) // issue #280 it.skip('new input box before trade value should be the same as the one before trade', () => { cy.get('#close-long-osqth-before-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(closeLongBeforeTradeBal) - 0.1, 0.000002) }) it('return to close long card successfully', () => { cy.get('#close-long-header-box').should('contain.text', 'Sell squeeth ERC20 to get ETH') cy.get('#close-long-eth-input').should('have.value', '0') cy.get('#close-long-osqth-input').should('have.value', '0') cy.get('#close-long-submit-tx-btn').should('be.disabled') }) it('should have "close your long position" first error in short oSQTH input when user have long oSQTH', () => { cy.get('#short-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-short-eth-input-box').should('contain.text', 'Close your long position to open a short') }) // issue #282 it.skip('there should be unrealized PnL value', () => { cy.get('#unrealized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) it('there should be realized PnL value', () => { cy.get('#realized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) }) context('close long with manual eth inputs and tx succeeds', () => { before(() => { cy.get('#long-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) }) it('can enter an amount into eth input and tx succeeds', () => { cy.get('#close-long-eth-input').clear().type('0.1', { force: true, delay: 800 }).should('have.value', '0.1') cy.get('#close-long-osqth-input').should('not.equal', '0') cy.get('#close-long-submit-tx-btn').should('not.be.disabled') // wait for 15 secs for trade amount to load cy.get('#close-long-osqth-input') .wait(15000) .then((v) => { closeLongoSQTHInput = new BigNumber(v.val().toString()) }) }) it('position card before trade balance should be the same as input box before trade balance', () => { cy.get('#position-card-before-trade-balance').then((val) => { cy.get('#close-long-osqth-before-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', Number(val.text()).toFixed(6)) }) }) it('position card post trade balance should become before-trade - input when input changes', () => { cy.get('#position-card-before-trade-balance').then((val) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(new BigNumber(val.text()).minus(closeLongoSQTHInput)), 0.0002) }) }) it('input box before trade balance should become before-trade - input when input changes', () => { cy.get('#close-long-osqth-before-trade-balance').then((val) => { cy.get('#close-long-osqth-post-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(new BigNumber(val.text()).minus(closeLongoSQTHInput)), 0.0002) }) }) it('send tx', () => { cy.get('#close-long-osqth-before-trade-balance').then((bal) => { closeLongBeforeTradeBal = new BigNumber(bal.text().toString()) }) cy.get('#position-card-before-trade-balance').then((bal) => { posCardBeforeLongTradeBal = new BigNumber(bal.text().toString()) }) cy.get('#close-long-submit-tx-btn').then((btn) => { if (btn.text().includes('Approve oSQTH')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskPermissionToSpend() trade.waitForTransactionSuccess() cy.wait(15000).get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } else if (btn.text().includes('Sell')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) it('there is close long tx finished card after tx succeeds with correct closing value', () => { cy.get('#close-long-card').should('contain.text', 'Close').should('contain.text', 'Sold') cy.get('#conf-msg').should('contain.text', closeLongoSQTHInput.toFixed(6)) cy.get('#close-long-close-btn').click({ force: true }) }) it('new position card value should be the same as prev position card value', () => { // wait for 30 sec to update positon cy.get('#position-card-before-trade-balance') .wait(30000) .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(posCardBeforeLongTradeBal.minus(closeLongoSQTHInput)), 0.000002) }) it.skip('new input box before trade value should be the same as the one before trade', () => { // issue #280 cy.get('#close-long-osqth-before-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(6))) .should('be.approximately', Number(closeLongBeforeTradeBal.minus(closeLongoSQTHInput)), 0.000002) }) it('return to close long card successfully', () => { cy.get('#close-long-header-box').should('contain.text', 'Sell squeeth ERC20 to get ETH') cy.get('#close-long-eth-input').should('have.value', '0') cy.get('#close-long-osqth-input').should('have.value', '0') cy.get('#close-long-submit-tx-btn').should('be.disabled') }) it('should have "close your long position" first error in short oSQTH input when user have long oSQTH', () => { cy.get('#short-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-short-eth-input-box').should('contain.text', 'Close your long position to open a short') }) // issue #282 it.skip('there should be unrealized PnL value', () => { cy.get('#unrealized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) it('there should be realized PnL value', () => { cy.get('#realized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) }) context('close long position with max button and tx succeeds', () => { before(() => { cy.get('#long-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) }) // issue #280, sometimes get incorrect amount to close due to loading issue it('can use max button for close long osqth input and tx succeeds', () => { cy.get('#close-long-osqth-input-action').click() cy.get('#close-long-osqth-input').should('not.equal', '0') cy.get('#close-long-eth-input').should('not.equal', '0') cy.get('#close-long-submit-tx-btn').should('not.be.disabled') cy.get('#close-long-osqth-input') .wait(15000) .then((v) => { closeLongoSQTHInput = new BigNumber(v.val().toString()) }) }) it('send tx', () => { cy.get('#close-long-submit-tx-btn').then((btn) => { if (btn.text().includes('Approve oSQTH')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskPermissionToSpend() trade.waitForTransactionSuccess() cy.wait(15000).get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } else if (btn.text().includes('Sell')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) // issue #286 it.skip('there is close long tx finished card after tx succeeds with correct closing value', () => { cy.get('#close-long-card').should('contain.text', 'Close').should('contain.text', 'Sold') cy.get('#conf-msg').should('contain.text', closeLongoSQTHInput.toFixed(6)) cy.get('#close-long-close-btn').click({ force: true }) }) it('return to close long card successfully with all values update to 0', () => { cy.get('#close-long-close-btn').click({ force: true }) cy.get('#close-long-header-box').should('contain.text', 'Sell squeeth ERC20 to get ETH') cy.get('#close-long-eth-input').should('have.value', '0') cy.get('#close-long-osqth-input').should('have.value', '0') cy.get('#close-long-submit-tx-btn').should('be.disabled') }) it('position card should update to 0', () => { cy.get('#position-card-before-trade-balance').should('contain.text', '0') }) // issue 280 it.skip('input box before trade balance should update to 0', () => { cy.get('#close-long-osqth-before-trade-balance').should('contain.text', '0') }) it('unrealized PnL should be --', () => { cy.get('#unrealized-pnl-value').should('contain.text', '--') }) it('realized PnL should be --', () => { cy.get('#realized-pnl-value').should('contain.text', '--') }) }) }) }) ================================================ FILE: packages/frontend/cypress/integration/specs/02-trade-short.spec.js ================================================ /// import TradePage from '../pages/trade' import BigNumber from 'bignumber.js' import { MIN_COLLATERAL_AMOUNT } from '../../../src/constants/index' const trade = new TradePage() describe('Trade on trade page', () => { context('Before tests', () => { it(`Before tests`, () => { cy.disconnectMetamaskWalletFromAllDapps() cy.visit('/') }) }) context('Connect metamask wallet', () => { it(`should login with success`, () => { trade.connectBrowserWallet() trade.acceptMetamaskAccessRequest() cy.get('#wallet-address').should(`contain.text`, '0x' || '.eth') }) }) let openShortOsqthInput let openShortOsqthBeforeTradeBal let posCardBeforeShortTradeBal context(`open short position`, () => { before('jump to open short trade card', () => { cy.get('#short-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) }) before(() => { it('eth balance should be greater than minimum collateral amount', () => { cy.get('#user-eth-wallet-balance').invoke('text').then(parseFloat).should('be.at.least', MIN_COLLATERAL_AMOUNT) }) }) context('open short trade condition checks', () => { it('eth balance from wallet should be the same as balance of eth input box', () => { cy.get('#user-eth-wallet-balance').then((bal) => { cy.get('#open-short-eth-before-trade-balance').should('contain.text', Number(bal.text())) }) }) it('it is on open short card', () => { cy.get('#open-short-header-box').should('contain.text', 'Mint & sell squeeth for premium') }) }) context('input checks', () => { it('inputs should be zero by default and tx button is disabled', () => { cy.get('#open-short-eth-input').should('have.value', '0') cy.get('#open-short-trade-details .trade-details-amount').should('contain.text', '0') cy.get('#open-short-submit-tx-btn').should('be.disabled') }) it('open short input should be more than minimum collateral amount', () => { cy.get('#open-short-eth-input').clear().type('6.9', { delay: 200, force: true }).should('have.value', '6.90') cy.get('#open-short-eth-input').invoke('val').then(parseFloat).should('be.at.least', MIN_COLLATERAL_AMOUNT) }) it('zero input amount', () => { cy.get('#trade-card').parent().scrollTo('top') cy.get('#open-short-eth-input').should('be.visible') cy.get('#open-short-eth-input').clear().type('0', { delay: 200, force: true }).should('have.value', '0') cy.get('#open-short-trade-details .trade-details-amount').should('contain.text', '0') }) it('invalid input amount', () => { cy.get('#trade-card').parent().scrollTo('top') cy.get('#open-short-eth-input').should('be.visible') cy.get('#open-short-eth-input').clear().type('\\', { delay: 200, force: true }).should('have.value', '0') cy.get('#open-short-trade-details .trade-details-amount').should('contain.text', '0') }) }) context('can enter an amount into osqth input, check position card & input box balances', () => { it('can enter an amount into eth input', () => { cy.get('#trade-card').parent().scrollTo('top') cy.get('#open-short-eth-input').should('be.visible') cy.get('#open-short-eth-input').clear().type('8.', { force: true, delay: 200 }).should('have.value', '8.0') cy.get('#open-short-trade-details .trade-details-amount').invoke('text').then(parseFloat).should('not.equal', 0) cy.get('#open-short-trade-details .trade-details-amount').then((val) => { openShortOsqthInput = new BigNumber(val.text()) }) }) // post = before + input // a = input box oSQTH before trade balance // a-post = a + input it('input box oSQTH post trade balance should be the same as before-trade + input when input changes', () => { cy.get('#open-short-osqth-before-trade-balance').then((bal) => { cy.get('#open-short-osqth-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', openShortOsqthInput.plus(Number(bal.text())).toFixed(4)) }) }) // b = position card oSQTH before trade balance // b-post = b + input it('position card oSQTH post trade balance should be the same as before-trade + input when input changes', () => { cy.get('#position-card-before-trade-balance').then((bal) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', openShortOsqthInput.plus(Number(bal.text())).toFixed(4)) }) }) // a = b it('position card oSQTH before trade balance should be the same as input box before oSQTH trade balance', () => { cy.get('#open-short-osqth-before-trade-balance').then((bal) => { cy.get('#position-card-before-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) }) // a + input = b + input != 0 it('position card oSQTH post trade balance should be the same as input box post oSQTH trade balance and not equal 0', () => { cy.get('#open-short-osqth-post-trade-balance').then((bal) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) cy.get('#open-short-osqth-post-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) cy.get('#position-card-post-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) // eth-post = eth-before - 8 it('input box eth post trade balance should be the same as before-trade - input when input changes', () => { cy.get('#open-short-eth-before-trade-balance').then((bal) => { cy.get('#open-short-eth-post-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', (Number(bal.text()) - 8).toFixed(4)) }) }) it('can adjust collateral ratio', () => { cy.get('.open-short-collat-ratio-input-box input') .clear() .type('250.', { delay: 200, force: true }) .should('have.value', '250.0') }) }) context('open short position', () => { it('can open short position for osqth, and tx succeeds', () => { cy.get('#trade-card').parent().scrollTo('top') cy.get('#open-short-eth-input').should('be.visible') cy.get('#open-short-eth-input').clear().type('8.', { force: true, delay: 200 }).should('have.value', '8.0') cy.get('#open-short-trade-details .trade-details-amount').then((val) => { openShortOsqthInput = new BigNumber(val.text()) }) cy.get('#open-short-osqth-before-trade-balance').then((val) => { openShortOsqthBeforeTradeBal = new BigNumber(val.text()) }) cy.get('#position-card-before-trade-balance').then((val) => { posCardBeforeShortTradeBal = new BigNumber(val.text()) }) cy.get('#open-short-submit-tx-btn').then((btn) => { if (btn.text().includes('Allow wrapper')) { cy.get('#open-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() cy.get('#open-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } else if (btn.text().includes('Deposit and sell')) { cy.get('#open-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) it('there is open short tx finished card after tx succeeds with correct closing value', () => { cy.get('#open-short-card').should('contain.text', 'Close').should('contain.text', 'Opened') cy.get('#conf-msg').should('contain.text', openShortOsqthInput.toFixed(6)) cy.get('#open-short-close-btn').click({ force: true }) }) it('return to open short card successfully with all values update to 0', () => { cy.get('#open-short-header-box').should('contain.text', 'Mint & sell squeeth for premium') cy.get('#open-short-eth-input').should('have.value', '0') cy.get('#open-short-trade-details .trade-details-amount').should('contain.text', '0') cy.get('#open-short-submit-tx-btn').should('be.disabled') }) it('position card should update to new osqth balance', () => { // wait for 20 sec to update positon cy.get('#position-card-before-trade-balance') .wait(30000) .then((v) => Number(parseFloat(v.text()).toFixed(4))) .should('be.approximately', Number(posCardBeforeShortTradeBal.plus(openShortOsqthInput)), 0.0002) }) it('input box before trade update to new osqth balance', () => { cy.get('#open-short-osqth-before-trade-balance') .then((v) => Number(parseFloat(v.text()).toFixed(4))) .should('be.approximately', Number(openShortOsqthBeforeTradeBal.plus(openShortOsqthInput)), 0.0002) }) it('position card update to the same value as input box before trade balance and not equal 0', () => { cy.get('#open-short-osqth-before-trade-balance').then((bal) => { cy.get('#position-card-before-trade-balance') .then((v) => Number(v.text()).toFixed(4)) .should('eq', new BigNumber(bal.text().toString()).toFixed(4)) }) cy.get('#open-short-osqth-before-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) cy.get('#position-card-before-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) // issue #282 it.skip('unrealized PnL display', () => { cy.get('#unrealized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) it('should have "close your short position" first error in long oSQTH input when user have short oSQTH', () => { cy.get('#long-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-long-eth-input-box').should('contain.text', 'Close your short position to open a long') }) }) }) context(`when have short oSQTH balance, the default trade card would be short`, () => { // issue #278 it.skip('reload to see if by default is short & open trade cards', () => { cy.reload() trade.connectBrowserWallet() trade.acceptMetamaskAccessRequest() cy.get('#wallet-address').should(`contain.text`, '0x' || '.eth') cy.get('#open-short-header-box').should('contain.text', 'Mint & sell squeeth for premium') }) }) let closeShortBeforeTradeBal let maxBtnShortCloseInput let fullShortCloseInput context(`close short position`, () => { before('jump to close short card', () => { cy.get('#short-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) }) context('close short position partially', () => { context('close short trade condition checks', () => { it('it is on close short card', () => { cy.get('#close-short-header-box').should('contain.text', 'Buy back oSQTH & close position') }) // loading issues it.skip('should select full close by default and there should be oSQTH short balance in input, input shoulde be disabled and tx button is not disabled', () => { cy.get('#close-short-type-select').should('contain.text', 'Full Close') cy.get('#close-short-osqth-input').should('not.equal', '0') cy.get('#close-short-trade-details .trade-details-amount') .invoke('text') .then(parseFloat) .should('not.equal', 0) cy.get('#close-short-osqth-input').should('be.disabled') cy.get('#close-short-submit-tx-btn').should('not.be.disabled') }) it('should have oSQTH short balance in position card', () => { cy.get('#position-card-before-trade-balance').invoke('text').then(parseFloat).should('not.equal', 0) }) }) context('input checks', () => { it('zero input amount when partial close is selected', () => { cy.get('#close-short-type-select .MuiSelect-select').wait(10000).click({ force: true }) cy.get('#close-short-partial-close').click({ force: true }) cy.get('#close-short-type-select').should('contain.text', 'Partial Close') cy.get('#close-short-osqth-input').clear().type('0', { delay: 200 }).should('have.value', '0') cy.get('#close-short-trade-details .trade-details-amount').should('contain.text', '0') }) it('invalid input amount when partial close is selected', () => { cy.get('#close-short-type-select .MuiSelect-select').click({ force: true }) cy.get('#close-short-partial-close').click({ force: true }) cy.get('#close-short-type-select').should('contain.text', 'Partial Close') cy.get('#close-short-osqth-input').clear().type('\\', { delay: 200 }).should('have.value', '0') cy.get('#close-short-trade-details .trade-details-amount').should('contain.text', '0') }) it('submit tx button should be disabled when input is zero', () => { cy.get('#close-short-osqth-input').clear().type('0', { delay: 200 }).should('have.value', '0') cy.get('#close-short-submit-tx-btn').should('be.disabled') }) }) context('can enter an amount into osqth input', () => { it('select partial close and have manual input', () => { cy.get('#close-short-type-select .MuiSelect-select').click({ force: true }) cy.get('#close-short-partial-close').click({ force: true }) cy.get('#close-short-type-select').wait(2000).should('contain.text', 'Partial Close') cy.get('#close-short-osqth-input') .clear() .type('0.1', { force: true, delay: 800 }) .should('have.value', '0.1') // make sure it's able to close short partially cy.get('.close-short-collat-ratio-input-box input') .clear() .type('250.', { delay: 200, force: true }) .should('have.value', '250.0') cy.get('#close-short-trade-details .trade-details-amount') .invoke('text') .then(parseFloat) .should('not.equal', 0) cy.get('#close-short-submit-tx-btn').should('not.be.disabled') }) it('position card before trade balance should be the same as input box before trade balance', () => { cy.get('#position-card-before-trade-balance').then((val) => { cy.get('#close-short-osqth-before-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', Number(val.text()).toFixed(6)) }) }) it('position card post trade balance should become before-trade - input when input changes', () => { cy.get('#position-card-before-trade-balance').then((val) => { cy.get('#position-card-post-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', (Number(val.text()) - 0.1).toFixed(6)) }) }) it('input box before trade balance should become before-trade - input when input changes', () => { cy.get('#close-short-osqth-before-trade-balance').then((val) => { cy.get('#close-short-osqth-post-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', (Number(val.text()) - 0.1).toFixed(6)) }) }) }) context('can use max button for osqth input when partially close first selected', () => { it('can select partial close first then full close', () => { cy.get('#close-short-type-select .MuiSelect-select').click({ force: true }) cy.get('#close-short-partial-close').click({ force: true }) cy.get('#close-short-type-select').should('contain.text', 'Partial Close') cy.get('#close-short-osqth-input-action').click() cy.get('#close-short-type-select').should('contain.text', 'Full Close') cy.get('#close-short-osqth-input').should('not.equal', '0') cy.get('#close-short-trade-details .trade-details-amount') .invoke('text') .then(parseFloat) .should('not.equal', 0) cy.get('#close-short-osqth-input').should('be.disabled') cy.get('#close-short-submit-tx-btn').should('not.be.disabled') cy.get('#close-short-osqth-input').then((val) => { maxBtnShortCloseInput = new BigNumber(val.val().toString()).toFixed(6) }) }) it('position card before trade balance should be the same as input when input changes', () => { cy.get('#position-card-before-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', maxBtnShortCloseInput) }) it('position card post trade balance should become 0 when input changes', () => { cy.get('#position-card-post-trade-balance').should('contain.text', '0') }) it('input box before trade balance should be the same as input when input changes', () => { cy.get('#close-short-osqth-before-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', maxBtnShortCloseInput) }) it('position card post trade balance should become 0 when input changes', () => { cy.get('#close-short-osqth-post-trade-balance').should('contain.text', (0).toFixed(6)) }) }) context('close short position partially tx', () => { it('can close short position partially, and tx succeeds', () => { cy.get('#close-short-type-select .MuiSelect-select').click({ force: true }) // issue #279 cy.get('#close-short-partial-close').wait(2000).click({ force: true }) cy.get('#close-short-type-select').should('contain.text', 'Partial Close') cy.get('#close-short-osqth-input').should('not.be.disabled') cy.get('#close-short-osqth-input') .clear() .type('0.01', { force: true, delay: 800 }) .should('have.value', '0.01') // make sure it's able to close short partially cy.get('.close-short-collat-ratio-input-box input') .clear() .type('250.', { delay: 200, force: true }) .should('have.value', '250.0') cy.get('#close-short-osqth-before-trade-balance').then((bal) => { closeShortBeforeTradeBal = bal.text() }) cy.get('#position-card-before-trade-balance').then((bal) => { posCardBeforeShortTradeBal = bal.text() }) cy.get('#close-short-submit-tx-btn').then((btn) => { if (btn.text().includes('Allow wrapper')) { cy.get('#close-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() cy.get('#close-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } if (btn.text().includes('Buy back')) { cy.get('#close-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) it('there is close short tx finished card after tx succeeds with correct closing value', () => { cy.get('#close-short-card').should('contain.text', 'Close').should('contain.text', 'Closed') cy.get('#conf-msg').should('contain.text', (0.01).toFixed(6)) cy.get('#close-short-close-btn').click({ force: true }) }) it('new position card value should be the same as prev position card value', () => { // wait for 30 sec to update positon cy.get('#position-card-before-trade-balance') .wait(30000) .then((v) => Number(v.text()).toFixed(6)) .should('eq', (Number(posCardBeforeShortTradeBal) - 0.01).toFixed(6)) }) it.skip('new input box before trade value should be the same as the one before trade', () => { // new input box before trade value should be the same as the one before trade // issue #280 cy.get('#close-short-osqth-before-trade-balance') .then((v) => Number(v.text()).toFixed(6)) .should('eq', (Number(closeShortBeforeTradeBal) - 0.01).toFixed(6)) }) it('return to close short card successfully', () => { cy.get('#close-short-header-box').should('contain.text', 'Buy back oSQTH & close position') cy.get('#close-short-type-select').should('contain.text', 'Full Close') cy.get('#close-short-trade-details .trade-details-amount') .invoke('text') .then(parseFloat) .should('not.equal', 0) cy.get('#close-short-osqth-input').should('not.equal', '0').should('be.disabled') cy.get('#close-short-submit-tx-btn').should('not.be.disabled') }) it('should have "close your short position" first error in long oSQTH input when user have short oSQTH', () => { cy.get('#long-card-btn').click({ force: true }) cy.get('#open-btn').click({ force: true }) cy.get('#open-long-eth-input-box').should('contain.text', 'Close your short position to open a long') }) // issue #282 it.skip('there should be unrealized PnL value', () => { cy.get('#unrealized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) it('there should be realized PnL value', () => { cy.get('#realized-pnl-value').should('not.contain.text', 'Loading').should('not.contain.text', '--') }) }) }) context('close short position fully tx', () => { before('jump to short close card', () => { cy.get('#short-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) }) it(`can close short position fully and tx succeeds`, () => { cy.get('#close-short-type-select .MuiSelect-select').click({ force: true }) cy.get('#close-short-full-close').click({ force: true }) cy.get('#close-short-type-select').should('contain.text', 'Full Close') cy.get('#close-short-osqth-input').should('not.equal', '0') cy.get('#close-short-osqth-input').then((bal) => { fullShortCloseInput = new BigNumber(bal.val().toString()).toFixed(6) }) cy.get('#close-short-submit-tx-btn').then((btn) => { if (btn.text().includes('Allow wrapper')) { cy.get('#close-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() cy.get('#close-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } else if (btn.text().includes('Buy back')) { cy.get('#close-short-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) it('there is close short tx finished card after tx succeeds with correct closing value', () => { cy.get('#close-short-card').should('contain.text', 'Close').should('contain.text', 'Closed') cy.get('#conf-msg').should('contain.text', fullShortCloseInput) cy.get('#close-short-close-btn').click({ force: true }) }) it('return to close short card successfully with all values update to 0', () => { cy.get('#close-short-header-box').should('contain.text', 'Buy back oSQTH & close position') cy.get('#close-short-type-select').should('contain.text', 'Full Close') cy.get('#close-short-osqth-input').should('have.value', '0') cy.get('#close-short-trade-details .trade-details-amount').should('contain.text', '0') cy.get('#close-short-submit-tx-btn').should('be.disabled') }) it('position card should update to 0', () => { cy.get('#position-card-before-trade-balance').should('contain.text', '0') }) it('input box before trade balance should update to 0', () => { cy.get('#close-short-osqth-before-trade-balance').should('contain.text', '0') }) // issue #281 it.skip('Current CR in close short trade card is not updated to 0 after close out all of the short position', () => { cy.get('#close-short-collateral-ratio .trade-info-item-value').should('contain.text', '0') }) it('unrealized PnL should be --', () => { cy.get('#unrealized-pnl-value').should('contain.text', '--') }) it('realized PnL should be --', () => { cy.get('#realized-pnl-value').should('contain.text', '--') }) }) }) }) ================================================ FILE: packages/frontend/cypress/integration/specs/06-manual-short.spec.js ================================================ /// import BigNumber from 'bignumber.js' import TradePage from '../pages/trade' const trade = new TradePage() // need to change close long component to manually sell osqth, add these two lines: // const mintedDebt = useMintedDebt() // const longSqthBal = mintedDebt describe('Trade on trade page', () => { context('Before tests', () => { it(`Before tests`, () => { cy.disconnectMetamaskWalletFromAllDapps() cy.visit('/lp') }) }) context('Connect metamask wallet', () => { it(`should login with success`, () => { trade.connectBrowserWallet() trade.acceptMetamaskAccessRequest() cy.get('#wallet-address').should(`contain.text`, '0x' || '.eth') }) }) context('can create short position in 2 sep txs', () => { it('can mint first on LP page', () => { cy.get('#lp-prev-step-btn').click({ force: true }).click({ force: true }) cy.get('#mint-sqth-to-lp-btn').click({ force: true }) cy.get('#lp-page-mint-eth-input').clear().type('8.', { force: true, delay: 200 }).should('have.value', '8.0') cy.get('#mint-to-lp-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() }) context(`sell minted squth, which is create short position`, () => { before(() => { cy.visit('/') cy.get('#long-card-btn').click({ force: true }) cy.get('#close-btn').click({ force: true }) }) it('can create short position with manual selling with custom input', () => { cy.get('#close-long-osqth-input').clear().type('0.1', { force: true, delay: 200 }).should('have.value', '0.1') cy.get('#close-long-submit-tx-btn').then((btn) => { if (btn.text().includes('Approve oSQTH')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskPermissionToSpend() trade.waitForTransactionSuccess() cy.wait(15000).get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } if (btn.text().includes('Sell')) { cy.get('#close-long-submit-tx-btn').click({ force: true }) trade.confirmMetamaskTransaction() trade.waitForTransactionSuccess() } }) }) it('there is tx finished card with correct trade amount', () => { cy.get('#close-long-card').should('contain.text', 'Close').should('contain.text', 'Sold') cy.get('#conf-msg').should('contain.text', Number(0.1).toFixed(6)) cy.get('#close-long-close-btn').click() }) it('check position card with correct short position', () => { cy.get('#position-card-before-trade-balance').wait(15000).should('contain.text', Number(0.1).toFixed(6)) }) }) }) }) ================================================ FILE: packages/frontend/cypress/plugins/index.js ================================================ import synpressPlugins from '@synthetixio/synpress/plugins' /** @type {Cypress.PluginConfig} */ // eslint-disable-next-line no-unused-vars module.exports = (on, config) => { // `on` is used to hook into various events Cypress emits // `config` is the resolved Cypress config synpressPlugins(on, config) } ================================================ FILE: packages/frontend/cypress/support/commands.js ================================================ // *********************************************** // This example commands.js shows you how to // create various custom commands and overwrite // existing commands. // // For more comprehensive examples of custom // commands please read more here: // https://on.cypress.io/custom-commands // *********************************************** // // // -- This is a parent command -- // Cypress.Commands.add('login', (email, password) => { ... }) // // // -- This is a child command -- // Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) // // // -- This is a dual command -- // Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) // // // -- This will overwrite an existing command -- // Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) // Cypress.Commands.add('connectWallet', () => { // cy.get('body').then(($body) => { // if ($body.find('.chakra-modal__content').length <= 0) { // cy.findByText('Connect to a wallet').click() // } // }) // cy.findByText('MetaMask').click() // cy.task('acceptMetamaskAccess') // }) ================================================ FILE: packages/frontend/cypress/support/index.js ================================================ // *********************************************************** // This example support/index.js is processed and // loaded automatically before your test files. // // This is a great place to put global configuration and // behavior that modifies Cypress. // // You can change the location of this file or turn off // automatically serving support files with the // 'supportFile' configuration option. // // You can read more here: // https://on.cypress.io/configuration // *********************************************************** // Import commands.js using ES2015 syntax: import './commands' import '@synthetixio/synpress/support' // Alternatively you can use CommonJS syntax: // require('./commands') ================================================ FILE: packages/frontend/cypress.json ================================================ { "baseUrl": "http://localhost:3000", "userAgent": "synpress", "retries": { "runMode": 0, "openMode": 0 }, "integrationFolder": "cypress/integration/specs", "screenshotsFolder": "cypress/screenshots", "videosFolder": "cypress/videos", "supportFile": "cypress/support/index.js", "pluginsFile": "cypress/plugins/index.js", "chromeWebSecurity": true, "viewportWidth": 1920, "viewportHeight": 1080, "scrollBehavior": false, "component": { "componentFolder": ".", "testFiles": "**/*.spec.{js,jsx,ts,tsx}" }, "env": { "coverage": true }, "defaultCommandTimeout": 30000, "pageLoadTimeout": 30000, "requestTimeout": 30000 } ================================================ FILE: packages/frontend/env.example ================================================ NEXT_PUBLIC_INFURA_API_KEY=" " NEXT_PUBLIC_BLOCKNATIVE_DAPP_ID=" " NEXT_PUBLIC_UPDATE_DB="false" NEXT_PUBLIC_TARDIS_API_KEY=" " NEXT_PUBLIC_FIREBASE_API_KEY=" " NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=" " NEXT_PUBLIC_FIREBASE_PROJECT_ID=" " NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=" " NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=" " NEXT_PUBLIC_FIREBASE_APP_ID=" " NEXT_PUBLIC_FATHOM_CODE=" " ================================================ FILE: packages/frontend/jest.config.js ================================================ module.exports = { testEnvironment: 'jsdom', setupFilesAfterEnv: ['/jest.setup.ts'], testPathIgnorePatterns: ['/.next/', '/node_modules/', '/cypress/'], moduleNameMapper: { '^src(.*)$': '/src$1', '^@utils(.*)$': '/src/utils$1', '^@hooks(.*)$': '/src/hooks$1', '^@components(.*)$': '/src/components$1', }, } ================================================ FILE: packages/frontend/jest.setup.ts ================================================ import '@testing-library/jest-dom' import 'cross-fetch/polyfill' import dotenv from "dotenv" dotenv.config() ================================================ FILE: packages/frontend/middleware.ts ================================================ import { NextRequest, NextResponse } from 'next/server' import { Redis } from '@upstash/redis' import { isVPN } from 'src/server/ipqs' import { BLOCKED_IP_VALUE } from 'src/constants' const redis = new Redis({ url: process.env.UPSTASH_REDIS_REST_URL!, token: process.env.UPSTASH_REDIS_REST_TOKEN!, }) const THIRTY_DAYS_IN_MS = 30 * 24 * 60 * 60 * 1000 interface RedisResponse { value: string timestamp: number } async function isIPBlockedInRedis(ip: string, currentTime: number) { let redisData: RedisResponse | null = null try { redisData = await redis.get(ip) } catch (error) { console.error('Failed to get data from Redis:', error) } let isIPBlocked = false if (redisData) { try { const { value, timestamp } = redisData // check if entry is valid and is not more than 30 days old if (value === BLOCKED_IP_VALUE && currentTime - timestamp <= THIRTY_DAYS_IN_MS) { isIPBlocked = true } } catch (error) { console.error('Failed to parse data from Redis:', error) } } return isIPBlocked } export async function middleware(request: NextRequest) { const cloudflareCountry = request.headers.get('cf-ipcountry') const country = cloudflareCountry ?? request.geo?.country const url = request.nextUrl const ip = request.headers.get('cf-connecting-ip') || request.headers.get('x-forwarded-for') || request.ip const allowedIPs = (process.env.WHITELISTED_IPS || '').split(',') const isIPWhitelisted = ip && allowedIPs.includes(ip) if (ip && !isIPWhitelisted) { const currentTime = Date.now() // check if IP is blocked const isIPBlocked = await isIPBlockedInRedis(ip, currentTime) if (isIPBlocked && url.pathname !== '/blocked') { return NextResponse.redirect(`${url.protocol}//${url.host}/blocked`) } // check if IP is from VPN const isIPFromVPN = await isVPN(ip) if (isIPFromVPN && url.pathname !== '/blocked') { try { await redis.set(ip, { value: BLOCKED_IP_VALUE, timestamp: currentTime }) } catch (error) { console.error('Failed to set data in Redis:', error) } return NextResponse.redirect(`${url.protocol}//${url.host}/blocked`) } } if (url.searchParams.has('ct') && url.searchParams.get('ct') === String(country)) { return NextResponse.next() } url.searchParams.set('ct', country!) return NextResponse.redirect(url) } /* matcher for excluding public assets/api routes/_next link: https://github.com/vercel/next.js/discussions/36308#discussioncomment-3758041 */ export const config = { matcher: '/((?!api|static|.*\\..*|_next|blocked).*)', } ================================================ FILE: packages/frontend/next-env.d.ts ================================================ /// /// // NOTE: This file should not be edited // see https://nextjs.org/docs/basic-features/typescript for more information. ================================================ FILE: packages/frontend/next.config.js ================================================ // eslint-disable-next-line @typescript-eslint/no-var-requires const { withSentryConfig } = require('@sentry/nextjs') const securityHeaders = [ { key: 'X-Frame-Options', value: 'SAMEORIGIN', }, ] const moduleExports = { reactStrictMode: true, images: { domains: ['media.giphy.com'], }, async rewrites() { return [ { source: '/api/amplitude', destination: 'https://api.eu.amplitude.com/2/httpapi', }, ] }, async headers() { return [ { // Apply these headers to all routes in your application. source: '/:path*', headers: securityHeaders, }, ] }, eslint: { dirs: ['pages', 'src'], }, } const sentryWebpackPluginOptions = { org: 'opyn', project: 'squeeth', // authToken: process.env.SENTRY_AUTH_TOKEN, silent: true, } module.exports = withSentryConfig(moduleExports, sentryWebpackPluginOptions) ================================================ FILE: packages/frontend/package.json ================================================ { "name": "@squeeth/frontend", "version": "0.1.0", "private": true, "engines": { "node": "^18" }, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "build-start": "npm run build && npm run start", "lint": "next lint", "test": "jest --watch", "test:ci": "jest --no-watch", "cypress": "dotenv -e .env -- synpress run --configFile cypress.json --config supportFile='cypress/support/index.js',pluginsFile='cypress/plugins/index.js' -b chrome", "cypress:mobile": "dotenv -e .env -- synpress run --configFile cypress.json --config supportFile='cypress/support/index.js',pluginsFile='cypress/plugins/index.js,viewportWidth=375,viewportHeight=667' --env guildName='Cypress Mobile Gang',guildUrlName='cypress-mobile-gang',dcInvite='https://discord.gg/G6VWkWJrzp',tgId='-1001689086791' -b chromium", "cypress:record": "dotenv -e .env -e .env.development.local -- synpress run --record --configFile cypress.json --config supportFile='cypress/support/index.js',pluginsFile='cypress/plugins/index.js' -b chromium", "cypress:mobile:record": "dotenv -e .env -e .env.development.local -- synpress run --record --configFile cypress.json --config supportFile='cypress/support/index.js',pluginsFile='cypress/plugins/index.js,viewportWidth=375,viewportHeight=667' -b chromium", "cypress:open": "dotenv -e .env -- synpress open --configFile cypress.json", "cypress:gh": "synpress run --record --configFile cypress.json --config supportFile='cypress/support/index.js',pluginsFile='cypress/plugins/index.js' --group Desktop -b chromium", "cypress:mobile:gh": "synpress run --record --configFile cypress.json --config supportFile='cypress/support/index.js',pluginsFile='cypress/plugins/index.js,viewportWidth=375,viewportHeight=667' --group Mobile -b chromium", "start:server": "serve tests/test-dapp -p 3000", "test:e2e:ci": "start-server-and-test 'yarn start:server' http-get://localhost:3000 'yarn run cypress'", "codegen:uniswap": "apollo client:codegen --target typescript --globalTypesFile=types/global_apollo.ts --includes=./src/queries/uniswap/**/*.ts --endpoint 'https://api.thegraph.com/subgraphs/name/kmkoushik/uniswap-v3-ropsten'", "codegen:squeeth": "apollo client:codegen --target typescript --globalTypesFile=types/global_apollo.ts --includes=./src/queries/squeeth/**/*.ts --endpoint 'https://api.thegraph.com/subgraphs/name/haythem96/squeeth-temp-subgraph'" }, "dependencies": { "@amplitude/analytics-browser": "^1.3.0", "@apollo/client": "^3.4.10", "@cypress/react": "^5.12.4", "@date-io/date-fns": "^1.3.13", "@davatar/react": "^1.8.1", "@ethersproject/providers": "^5.5.2", "@material-ui/core": "^4.12.3", "@material-ui/icons": "^4.11.2", "@material-ui/lab": "^4.0.0-alpha.60", "@material-ui/pickers": "^3.3.10", "@testing-library/react-hooks": "^8.0.0", "@thanpolas/univ3prices": "^3.0.2", "@uniswap/sdk-core": "^3.0.1", "@uniswap/smart-order-router": "^2.5.30", "@uniswap/v3-sdk": "^3.3.2", "@upstash/redis": "^1.22.0", "@vercel/og": "^0.0.25", "axios": "^0.24.0", "bignumber.js": "^9.0.1", "bnc-notify": "^1.9.1", "bnc-onboard": "1.27.0", "chart.js": "^2.9.4", "clsx": "^1.1.1", "copy-to-clipboard": "^3.3.1", "crisp-sdk-web": "^1.0.11", "date-fns": "^2.29.3", "fathom-client": "^3.2.0", "firebase": "^8.8.0", "firebase-admin": "^11.10.1", "framer-motion": "^5.3.3", "fzero": "^0.2.4", "graphql": "^16.1.0", "gray-matter": "^4.0.3", "hamburger-react": "^2.5.0", "highcharts": "^10.2.1", "highcharts-react-official": "^3.1.0", "javascript-time-ago": "^2.5.7", "jotai": "^1.5.3", "kaktana-react-lightweight-charts": "^2.0.0", "lodash": "^4.17.21", "lodash.isequal": "^4.5.0", "markdown-to-jsx": "^7.1.5", "next": "^12.2.3", "next-seo": "^5.15.0", "react": "17.0.2", "react-chartjs-2": "^2.11.1", "react-cookie-consent": "^8.0.1", "react-dom": "17.0.2", "react-query": "^3.34.0", "react-use": "^17.3.2", "recharts": "^2.2.0", "set-interval-async": "^2.0.3", "subscriptions-transport-ws": "^0.11.0", "use-debounce": "^9.0.2", "web3": "^1.5.2" }, "devDependencies": { "@babel/preset-react": "^7.16.7", "@sentry/nextjs": "^7.10.0", "@synthetixio/synpress": "^1.1.1", "@testing-library/dom": "^8.11.3", "@testing-library/jest-dom": "^5.16.2", "@testing-library/react": "^12.1.3", "@testing-library/user-event": "^13.5.0", "@types/firebase": "^3.2.1", "@types/jest": "^27.4.1", "@types/lodash.isequal": "^4.5.5", "@types/react": "17.0.17", "@types/set-interval-async": "^1.0.0", "@typescript-eslint/eslint-plugin": "^4.14.2", "@typescript-eslint/parser": "^4.14.2", "babel-jest": "^27.5.1", "dotenv-cli": "^5.0.0", "eslint": "7.32.0", "eslint-config-next": "11.0.1", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.3.1", "eslint-plugin-react": "^7.22.0", "eslint-plugin-react-hooks": "^4.2.0", "eslint-plugin-simple-import-sort": "^7.0.0", "jest": "^27.5.1", "mock-apollo-client": "^1.2.0", "node-mocks-http": "^1.11.0", "prettier": "^2.3.1", "typescript": "4.3.5" }, "resolutions": { "assemblyscript": "git+https://github.com/AssemblyScript/assemblyscript.git#v0.6" }, "workspaces": { "nohoist": [ "ip-range-check", "ip-range-check/**" ] } } ================================================ FILE: packages/frontend/pages/_app.tsx ================================================ import useRenderCounter from '../src/hooks/useRenderCounter' import '../styles/globals.css' import { ApolloProvider } from '@apollo/client' import CssBaseline from '@material-ui/core/CssBaseline' import { ThemeProvider } from '@material-ui/core/styles' import * as Fathom from 'fathom-client' import Head from 'next/head' import { useRouter } from 'next/router' import { Crisp } from 'crisp-sdk-web' import React, { memo, useEffect, useMemo, useRef } from 'react' import { QueryClient, QueryClientProvider } from 'react-query' import { ReactQueryDevtools } from 'react-query/devtools' import { useAtomValue } from 'jotai' import { RestrictUserProvider, useRestrictUser } from '@context/restrict-user' import getTheme, { Mode } from '../src/theme' import { uniswapClient } from '@utils/apollo-client' import { useOnboard, useConnectWallet } from 'src/state/wallet/hooks' import { networkIdAtom, onboardAddressAtom, walletFailVisibleAtom } from 'src/state/wallet/atoms' import { useUpdateSqueethPrices, useUpdateSqueethPoolData } from 'src/state/squeethPool/hooks' import { useInitController } from 'src/state/controller/hooks' import { ComputeSwapsProvider } from 'src/state/positions/providers' import { useSwaps } from 'src/state/positions/hooks' import { useUpdateAtom } from 'jotai/utils' import useAppEffect from '@hooks/useAppEffect' import WalletFailModal from '@components/WalletFailModal' import { checkIsValidAddress, updateBlockedAddress } from 'src/state/wallet/apis' import TimeAgo from 'javascript-time-ago' import en from 'javascript-time-ago/locale/en' import '@utils/amplitude' import { initializeAmplitude } from '@utils/amplitude' import useAmplitude from '@hooks/useAmplitude' import StrategyLayout from '@components/StrategyLayout/StrategyLayout' import useTrackSiteReload from '@hooks/useTrackSiteReload' import { hideCrispChat, showCrispChat } from '@utils/crisp-chat' import { TOS_UPDATE_DATE, LEGAL_PAGES } from '@constants/index' import StrikeWarning from '@components/StrikeWarning' import { getAddressStrikeCount } from '@state/wallet/apis' import { addressStrikeCountAtom, isStrikeCountModalOpenAtom } from '@state/wallet/atoms' initializeAmplitude() TimeAgo.addDefaultLocale(en) const queryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false } } }) function MyApp({ Component, pageProps }: any) { useRenderCounter('9', '0') const router = useRouter() const { track } = useAmplitude() const networkId = useAtomValue(networkIdAtom) const client = useMemo(() => uniswapClient[networkId] || uniswapClient[1], [networkId]) React.useEffect(() => { // Remove the server-side injected CSS. const jssStyles = document.querySelector('#jss-server-side') if (jssStyles) { jssStyles.parentElement!.removeChild(jssStyles) } }, []) const siteID = process.env.NEXT_PUBLIC_FATHOM_CODE ? process.env.NEXT_PUBLIC_FATHOM_CODE : '' useEffect(() => { // Initialize Fathom when the app loads // Example: yourdomain.com // - Do not include https:// // - This must be an exact match of your domain. // - If you're using www. for your domain, make sure you include that here. Fathom.load(siteID, { includedDomains: ['opyn.co', 'www.opyn.co'], }) function onRouteChangeComplete() { Fathom.trackPageview() } // Record a pageview when route changes router.events.on('routeChangeComplete', onRouteChangeComplete) // Unassign event listener return () => { router.events.off('routeChangeComplete', onRouteChangeComplete) } }, [router.events, siteID]) useEffect(() => { function onRouteChangeComplete(url: string) { const e: string = url.split('?')[0].substring(1).toUpperCase() track('NAV_' + e) } router.events.on('routeChangeComplete', onRouteChangeComplete) return () => { router.events.off('routeChangeComplete', onRouteChangeComplete) } // eslint-disable-next-line react-hooks/exhaustive-deps }, [track]) useEffect(() => { Crisp.configure(process.env.NEXT_PUBLIC_CRISP_WEBSITE_ID as string) }, []) useEffect(() => { if (router.pathname !== '/') { showCrispChat() } else hideCrispChat() }, [router]) return ( ) } const Init = () => { const onboardAddress = useAtomValue(onboardAddressAtom) const setWalletFailVisible = useUpdateAtom(walletFailVisibleAtom) const setAddressStrikeCount = useUpdateAtom(addressStrikeCountAtom) const setIsStrikeCountModalOpen = useUpdateAtom(isStrikeCountModalOpenAtom) const firstAddressCheck = useRef(true) const router = useRouter() const connectWallet = useConnectWallet() const { isRestricted, blockUser } = useRestrictUser() const isZenbullPage = router.pathname === '/strategies/bull' useAppEffect(() => { if (!onboardAddress) { return } checkIsValidAddress(onboardAddress).then((valid) => { if (valid) { const hasConnectedAfterTosUpdate = window.localStorage.getItem(TOS_UPDATE_DATE) === 'true' if (!hasConnectedAfterTosUpdate) { return } else if (isZenbullPage) { // connect automatically (to zenbull) only if user has specifically connected to zenbull page const hasConnectedToZenbullBefore = window.localStorage.getItem('walletConnectedToZenbull') === 'true' if (hasConnectedToZenbullBefore) { connectWallet(onboardAddress) } } else { connectWallet(onboardAddress) } } else { if (firstAddressCheck.current) { firstAddressCheck.current = false } else { setWalletFailVisible(true) } } }) }, [onboardAddress, setWalletFailVisible, isZenbullPage, connectWallet]) // Check if the current page is exempt from restriction (e.g., legal pages) const isLegalPage = LEGAL_PAGES.includes(router.pathname) // increment strike count if user is restricted useEffect(() => { if (!isRestricted || !onboardAddress) { return } if (isRestricted && !isLegalPage) { // increment strike count updateBlockedAddress(onboardAddress).then((visitCount) => { setAddressStrikeCount(visitCount) if (visitCount >= 3) { blockUser() } // show strike count modal after updating strike count setIsStrikeCountModalOpen(true) }) } }, [isRestricted, onboardAddress, setAddressStrikeCount, blockUser, setIsStrikeCountModalOpen, isLegalPage]) // update user's strike count and block user if strike count >= 3 useEffect(() => { if (!onboardAddress) { return } getAddressStrikeCount(onboardAddress).then((visitCount) => { setAddressStrikeCount(visitCount) if (visitCount >= 3) { blockUser() } }) }, [blockUser, onboardAddress, setAddressStrikeCount]) useOnboard() useTrackSiteReload() useUpdateSqueethPrices() useUpdateSqueethPoolData() useInitController() useSwaps() return null } const MemoizedInit = memo(Init) const TradeApp = ({ Component, pageProps }: any) => { return ( {/* CssBaseline kickstart an elegant, consistent, and simple baseline to build upon. */} ) } export default MyApp ================================================ FILE: packages/frontend/pages/_document.tsx ================================================ import { ServerStyleSheets } from '@material-ui/core/styles' import Document, { Head, Html, Main, NextScript } from 'next/document' import React from 'react' export default class MyDocument extends Document { render() { return ( {/* preload fonts */}
) } } // `getInitialProps` belongs to `_document` (instead of `_app`), // it's compatible with server-side generation (SSG). MyDocument.getInitialProps = async (ctx) => { // Render app and page and get the context of the page with collected side effects. const sheets = new ServerStyleSheets() const originalRenderPage = ctx.renderPage ctx.renderPage = () => originalRenderPage({ enhanceApp: (App) => (props) => sheets.collect(), }) const initialProps = await Document.getInitialProps(ctx) return { ...initialProps, // Styles fragment is rendered after the app and page rendering finish. styles: [...React.Children.toArray(initialProps.styles), sheets.getStyleElement()], } } ================================================ FILE: packages/frontend/pages/api/__tests__/isValidAddress.test.ts ================================================ /** * @jest-environment node */ import { createMocks, RequestMethod, createRequest, createResponse } from 'node-mocks-http'; import type { NextApiResponse } from 'next'; import requestHandler from '../../../pages/api/isValidAddress'; import axios from 'axios'; jest.mock("axios"); const mockedAxios = axios as jest.Mocked; type APIRequest = NextApiResponse & ReturnType; type APIResponse = NextApiResponse & ReturnType; export interface isValidAddressResponse { valid: boolean, madeThirdPartyConnection: boolean } describe('/api/isValidAddress/[address] API Endpoint', () => { const safeAddress = '0xa3cb04d8bd927eec8826bd77b7c71abe3d29c081' const highRiskAddress = '0x098B716B8Aaf21512996dC57EB0615e2383E2f96' const invalidAddress = 'jksd' function mockRequestResponse(method: RequestMethod = 'GET', address: string) { const { req, res, }: { req: APIRequest; res: APIResponse } = createMocks({ method }); req.query = { address: address }; return { req, res }; } it('should return TRUE for a safe address supplied', async () => { const { req, res } = mockRequestResponse('GET',safeAddress); mockedAxios.post.mockResolvedValue({ data: [ { "asset": "ETH", "address": "0xa3cb04d8bd927eec8826bd77b7c71abe3d29c081", "cluster": null, "rating": "unknown", "customAddress": null, "chainalysisIdentification": null } ], }); await requestHandler(req, res); const responseData = res._getJSONData() as isValidAddressResponse expect(responseData.valid).toEqual(true); expect(responseData.madeThirdPartyConnection).toEqual(true); }); it('should return FALSE for a risky address supplied', async () => { const { req, res } = mockRequestResponse('GET',highRiskAddress); mockedAxios.post.mockResolvedValue({ data: [ { "asset": "ETH", "address": "0x098B716B8Aaf21512996dC57EB0615e2383E2f96", "cluster": { "name": "OFAC SDN Lazarus Group 2022-04-14", "category": "sanctions" }, "rating": "highRisk", "customAddress": null, "chainalysisIdentification": { "addressName": "Some info", "description": "Some info", "categoryName": "Some info" } } ], }); await requestHandler(req, res); const responseData = res._getJSONData() as isValidAddressResponse expect(responseData.valid).toEqual(false); expect(responseData.madeThirdPartyConnection).toEqual(true); }); it('should return TRUE if response body does not match what we expect', async () => { const { req, res } = mockRequestResponse('GET',highRiskAddress); mockedAxios.post.mockResolvedValue({ data: null, }); await requestHandler(req, res); const responseData = res._getJSONData() as isValidAddressResponse expect(responseData.valid).toEqual(true); expect(responseData.madeThirdPartyConnection).toEqual(true); }); it('should return TRUE for all errors encountered when querying third-party service', async () => { const { req, res } = mockRequestResponse('GET',invalidAddress); mockedAxios.post.mockRejectedValue(new Error('💣')) await requestHandler(req, res); const responseData = res._getJSONData() as isValidAddressResponse expect(responseData.valid).toEqual(true); expect(responseData.madeThirdPartyConnection).toEqual(false); }); }); ================================================ FILE: packages/frontend/pages/api/auction/lastHedgeAuction.ts ================================================ import axios from 'axios' import type { NextApiRequest, NextApiResponse } from 'next' const SQUEETH_PORTAL_API = process.env.NEXT_PUBLIC_SQUEETH_PORTAL_BASE_URL const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { if (!SQUEETH_PORTAL_API) { res.status(400).json({ status: 'error', message: 'Error fetching information' }) return } const jsonResponse = await axios.get(`${SQUEETH_PORTAL_API}/api/auction/getLastHedge?type=${req.query.type}`) res.status(200).json(jsonResponse.data) } export default handleRequest ================================================ FILE: packages/frontend/pages/api/charts/longchart.ts ================================================ import type { NextApiRequest, NextApiResponse } from 'next' import { getCoingeckoETHPricesBetweenTimestamps as getETHPricesBetweenTs } from '@utils/ethPriceCharts' import { getLiveVolMap, getSqueethChartWithFunding, getVolForTimestampOrDefault, getVolMap } from '@utils/pricer' export default async function handler(req: NextApiRequest, res: NextApiResponse) { const collatRatio = Number(req.query.collatRatio) const fromTs = Number(req.query.fromTs) const toTs = Number(req.query.toTs) const volMultiplier = Number(req.query.volMultiplier) const oneDay = 24 * 60 * 60 * 1000 const days = (toTs - fromTs) / oneDay try { const ethPrices = await getETHPricesBetweenTs(fromTs, toTs) const ethSqueethPNLSeriesPromise = getETHSqueethPNLCompounding(ethPrices, volMultiplier, days) const squeethSeriesPromise = getSqueethChartWithFunding(ethPrices, volMultiplier, collatRatio) const [ethSqueethPNLSeries, squeethSeries] = await Promise.all([ethSqueethPNLSeriesPromise, squeethSeriesPromise]) const longEthPNL = ethSqueethPNLSeries.ethPNL.map(({ time, longPNL }) => { return { time, value: longPNL } }) const longSeries = ethSqueethPNLSeries.squeethPNL.map(({ time, longPNL }) => { return { time, value: longPNL } }) const squeethIsLive = ethSqueethPNLSeries.squeethPNL.map(({ isLive }) => { return isLive }) const positionSizeSeries = squeethSeries.series.map(({ time, positionSize }) => { return { time, value: positionSize * 100 } }) const response = { longEthPNL, longSeries, positionSizeSeries, squeethIsLive } res.status(200).send(response) } catch (error: any) { console.log({ error: error.message }) res.status(400).json({ error: 'There was an error fetching the data' }) } } async function getETHSqueethPNLCompounding( ethPrices: { time: number; value: number }[], volMultiplier = 1.2, days = 365, ) { const timestamps = ethPrices.map(({ time }) => time) let cumulativeSqueethLongReturn = 0 let cumulativeSqueethCrabReturn = 1 const volsMap = await getVolMap() const liveVolsMap = await getLiveVolMap() const annualVolData = await Promise.all( timestamps.map(async (timestamp, index) => { const { value: price, time } = ethPrices[index > 0 ? index : 0] const utcDate = new Date(timestamp * 1000).toISOString().split('T')[0] let annualVol = liveVolsMap[utcDate] let isLive = true if (!annualVol) { annualVol = await getVolForTimestampOrDefault(volsMap, time, price) isLive = false } return { annualVol, isLive } }), ) let cumulativeEthLongShortReturn = 0 const ethChartData = ethPrices.map((ethItem, index) => { const { value: price, time } = ethItem const preEthPrice = ethPrices[index > 0 ? index - 1 : 0].value cumulativeEthLongShortReturn += Math.log(price / preEthPrice) const longPNL = Math.round((Math.exp(cumulativeEthLongShortReturn) - 1) * 10000) / 100 const shortPNL = Math.round((Math.exp(-cumulativeEthLongShortReturn) - 1) * 10000) / 100 return { shortPNL, longPNL, time } }) const squeethChartData = annualVolData.map((item, index) => { const { annualVol, isLive } = item const { value: price, time } = ethPrices[index > 0 ? index : 0] const fundingPeriodMultiplier = days > 90 ? 365 : days > 1 ? 365 * 24 : 356 * 24 * 12 let vol = annualVol * volMultiplier const preEthPrice = ethPrices[index > 0 ? index - 1 : 0].value let fundingCost = index === 0 ? 0 : (vol / Math.sqrt(fundingPeriodMultiplier)) ** 2 cumulativeSqueethLongReturn += 2 * Math.log(price / preEthPrice) + Math.log(price / preEthPrice) ** 2 - fundingCost // crab return const crabVolMultiplier = 0.9 vol = annualVol * crabVolMultiplier fundingCost = index === 0 ? 0 : (vol / Math.sqrt(fundingPeriodMultiplier)) ** 2 const simR = price / preEthPrice - 1 cumulativeSqueethCrabReturn *= 1 + -(simR ** 2) + fundingCost const longPNL = Math.round((Math.exp(cumulativeSqueethLongReturn) - 1) * 10000) / 100 const shortPNL = Math.round(Math.log(cumulativeSqueethCrabReturn) * 10000) / 100 return { shortPNL, longPNL, time, isLive } }) return { ethPNL: ethChartData, squeethPNL: squeethChartData } } ================================================ FILE: packages/frontend/pages/api/currentsqueethvol.ts ================================================ import axios from 'axios' import type { NextApiRequest, NextApiResponse } from 'next' const SQUEETH_VOL_API = process.env.SQUEETH_VOL_API_BASE_URL const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { if (!SQUEETH_VOL_API) { res.status(400).json({ status: 'error', message: 'Error fetching information' }) return } const jsonResponse = await axios.get(`${SQUEETH_VOL_API}/get_squeeth_iv`) res.status(200).json(jsonResponse.data['squeethVol']) } export default handleRequest ================================================ FILE: packages/frontend/pages/api/historicalprice.ts ================================================ import { format } from 'date-fns' import type { NextApiRequest, NextApiResponse } from 'next' const TWELVE_DATA_API = 'https://api.twelvedata.com' /** * Get Historical price using https://twelvedata.com/docs#complex-data complex data API */ const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { const { timestamps, interval, pair, tz } = req.query if (timestamps instanceof Array) { res.status(400).json({ status: 'error', message: 'Array params is not supported' }) return } if (!interval) { res.status(400).json({ status: 'error', message: 'Interval param is required' }) return } const timestampArr = timestamps?.split(',') ?? [] const methods = generateMethodData(timestampArr, interval) const resp = await fetch(`${TWELVE_DATA_API}/complex_data?apikey=${process.env.NEXT_PUBLIC_TWELVEDATA_APIKEY}`, { method: 'POST', body: JSON.stringify({ symbols: [pair], timezone: tz, intervals: [interval], outputsize: 1, methods, }), }) const respJson = await resp.json() // return res.status(200).json(respJson) const [current, ...rest] = respJson.data as Array // complex_data API returns current price, historical price for each timestamp // So rest value will be in the format [1st historical price, current price, 2nd historical price, current price, ...] // So filtering historical price const priceData = rest.filter((_, index) => index % 2 === 0) if (timestampArr.length !== priceData.length) { res.status(400).json({ status: 'error', message: 'Timestamps size and price data length mis match' }) return } const resultJson = timestampArr.reduce((acc, timestamp, index) => { const priceResp = priceData[index] if (priceResp.status === 'error') { acc[timestamp] = current.values[0].close } else { acc[timestamp] = priceResp.values[0].close } return acc }, {} as { [key: string]: string }) res.status(200).json(resultJson) } // Generate method param from timestamps that's needed to sent to twelvedata API const generateMethodData = (timestampArr: string[], interval: string | string[]) => { const methods = [] for (const timestamp of timestampArr) { const date = format(new Date(Number(timestamp)).setUTCSeconds(0, 0), 'yyyy-MM-dd HH:mm:ss') const params = { interval, start_date: date, end_date: date, symbols: ['ETH/USD'], } methods.push('time_series', params) } return methods } export default handleRequest ================================================ FILE: packages/frontend/pages/api/isValidAddress.ts ================================================ import type { NextApiRequest, NextApiResponse } from 'next' import axios from 'axios' import * as Sentry from '@sentry/nextjs' const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { const { address } = req.query let isValidAddress = true try { const { data } = await axios.post( `https://api.chainalysis.com/api/kyt/v1/users/${address}/withdrawaladdresses`, [ { network: 'Ethereum', asset: 'ETH', address, }, ], { headers: { Token: process.env.AML_API_KEY ?? '' } }, ) if (data && data?.[0]?.rating) isValidAddress = (data?.[0].rating === 'highRisk') ? false : true res.status(200).json({ valid: isValidAddress, madeThirdPartyConnection: true }) } catch (error) { // catches all reponses not 2XX from source if (error instanceof Error) { Sentry.captureMessage(`AML Check: Error occured when checking for address:${address}. Error: ${error.message} `) } else { Sentry.captureMessage(`AML Check: Error occured when checking for address:${address}. Error: ${JSON.stringify(error)}`) } res.status(200).json({ valid: true, madeThirdPartyConnection: false }) } } export default handleRequest ================================================ FILE: packages/frontend/pages/api/pnl.tsx ================================================ /* eslint-disable @next/next/no-img-element */ import { ImageResponse } from '@vercel/og' import { NextRequest } from 'next/server' import React from 'react' import { Duration, isFuture, intervalToDuration, format, isBefore } from 'date-fns' import { SQUEETH_BASE_URL, CRABV2_START_DATE, BULL_START_DATE } from '@constants/index' const OMDB_BASE_URL = process.env.NEXT_PUBLIC_OMDB_BASE_URL as string export const config = { runtime: 'experimental-edge', } const formatDuration = (duration: Duration) => { const { years, months, days, hours } = duration const formattedDuration = [] if (years) { formattedDuration.push(`${years}y`) } if (months) { formattedDuration.push(`${months}m`) } if (days) { formattedDuration.push(`${days}d`) } if (hours) { formattedDuration.push(`${hours}h`) } return formattedDuration.join(' ') } const CHART_WIDTH = 1000 const CHART_HEIGHT = 280 const X_AXIS_WIDTH = 5 const Y_AXIS_WIDTH = 5 const PADDING_X = 3 const PADDING_Y = 36 type StrategyType = 'crab' | 'zenbull' type PnLDataPoint = [number, number] interface UserPnlProps { strategy: StrategyType depositTimestamp: number pnl: number pnlData: PnLDataPoint[] } const UserPnl: React.FC = ({ strategy, depositTimestamp, pnl, pnlData }) => { const date = new Date(depositTimestamp * 1000) const strategyDuration = intervalToDuration({ start: new Date(), end: date }) const formattedDuration = formatDuration(strategyDuration) const pnlColor = pnl >= 0 ? '#67fabf' : '#FA7B67' const xMax = Math.max(...pnlData.map(([x]) => x)) const xMin = Math.min(...pnlData.map(([x]) => x)) const yMax = Math.max(...pnlData.map(([, y]) => y)) const yMin = Math.min(...pnlData.map(([, y]) => y)) const yRange = yMax - yMin const xRange = xMax - xMin const offsetX = xMin const offsetY = yMin const chartXPadding = Y_AXIS_WIDTH + PADDING_X const chartYPadding = X_AXIS_WIDTH + PADDING_Y const availableChartWidth = CHART_WIDTH - 2 * chartXPadding const availableChartHeight = CHART_HEIGHT - 2 * chartYPadding const points = pnlData .map(([x, y]) => { const pointX = chartXPadding + ((x - offsetX) / xRange) * availableChartWidth const pointY = CHART_HEIGHT - chartYPadding - ((y - offsetY) / yRange) * availableChartHeight return `${pointX},${pointY}` }) .join(' ') return (
{strategy === 'crab' && ( opyn crab logo )} {strategy === 'zenbull' && ( opyn zenbull logo )}
{strategy === 'crab' && 'Crabber - Stacking USDC'} {strategy === 'zenbull' && 'Zen Bull - Stacking ETH'}
opyn logo
{strategy === 'crab' && 'My Crab Position'} {strategy === 'zenbull' && 'My Zen Bull Position'}
{pnl > 0 && '+'} {pnl.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) + '%'}
{strategy === 'crab' && 'USD return'} {strategy === 'zenbull' && 'ETH return'}
{strategy === 'crab' && 'Crab Strategy'} {strategy === 'zenbull' && 'Zen Bull Strategy'}
{/* y-axis */}
{format(date, 'MM/dd/yy')} (deposited {formattedDuration} ago)
) } const font = fetch(new URL('../../public/fonts/DMSans-Regular.ttf', import.meta.url).toString()).then((res) => res.arrayBuffer(), ) const fontMedium = fetch(new URL('../../public/fonts/DMSans-Medium.ttf', import.meta.url).toString()).then((res) => res.arrayBuffer(), ) const fetchPnlData = async (strategy: StrategyType, startTimestamp: number, endTimestamp: number) => { if (strategy === 'crab') { const response = await fetch( `${OMDB_BASE_URL}/metrics/crabv2?start_timestamp=${startTimestamp}&end_timestamp=${endTimestamp}`, ).then((res) => res.json()) return response.data.map((x: Record) => [x.timestamp * 1000, x.crabPnL * 100]) } else if (strategy === 'zenbull') { const response = await fetch(`${OMDB_BASE_URL}/metrics/zenbull/pnl/${startTimestamp}/${endTimestamp}`).then((res) => res.json(), ) return response.data.map((x: Record) => [x.timestamp * 1000, x.bullEthPnl]) } throw new Error('Invalid strategy') } export default async function handler(req: NextRequest) { try { const [fontData, fontMediumData] = await Promise.all([font, fontMedium]) const { searchParams } = new URL(req.url) const strategy = searchParams.get('strategy') as StrategyType const depositedAt = searchParams.get('depositedAt') const pnl = searchParams.get('pnl') if (!strategy || !depositedAt || !pnl) { return new Response(`Missing "strategy", "depositedAt" or "pnl" query param`, { status: 400, }) } const depositDate = new Date(Number(depositedAt) * 1000) if (isFuture(depositDate)) { return new Response(`Deposit date is in future`, { status: 400, }) } const crabV2LaunchDate = new Date(CRABV2_START_DATE) const zenBullLaunchDate = new Date(BULL_START_DATE) let startDate = depositDate if (strategy === 'crab' && isBefore(depositDate, crabV2LaunchDate)) { startDate = crabV2LaunchDate } else if (strategy === 'zenbull' && isBefore(depositDate, zenBullLaunchDate)) { startDate = zenBullLaunchDate } const startTimestamp = startDate.getTime() / 1000 const endTimestamp = Math.round(new Date().getTime() / 1000) const pnlData = await fetchPnlData(strategy, startTimestamp, endTimestamp) return new ImageResponse( , { width: 1200, height: 630, fonts: [ { name: 'DMSans', data: fontData, style: 'normal', weight: 400, }, { name: 'DMSans', data: fontMediumData, style: 'normal', weight: 500, }, ], }, ) } catch (e: any) { console.log(`${e.message}`) return new Response(`Failed to generate the image`, { status: 500, }) } } ================================================ FILE: packages/frontend/pages/api/strikes.ts ================================================ import type { NextApiRequest, NextApiResponse } from 'next' import { getAddressStrikeCount } from 'src/server/firebase-admin' const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { if (req.method !== 'GET') { return res.status(400).json({ message: 'Only GET is allowed' }) } const { address } = req.query try { const count = await getAddressStrikeCount(address as string) return res.status(200).json({ count }) } catch (error) { console.error(error) return res.status(500).json({ message: error }) } } export default handleRequest ================================================ FILE: packages/frontend/pages/api/tvl.ts ================================================ import axios from 'axios' import type { NextApiRequest, NextApiResponse } from 'next' const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { if (req.method === 'GET') { try { const response = await axios.get(`${process.env.DEFILLAMA_ENDPOINT}/tvl/opyn`) res.status(200).json(response.data) } catch (error) { res.status(500).json({ error: 'Error fetching data from Defillama' }) } } else { // Handle any other HTTP method res.setHeader('Allow', ['GET']) res.status(405).end(`Method ${req.method} Not Allowed`) } } export default handleRequest ================================================ FILE: packages/frontend/pages/api/twelvedata.ts ================================================ import axios from 'axios' import type { NextApiRequest, NextApiResponse } from 'next' const TWELVE_DATA_API = 'https://api.twelvedata.com' const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { const { path } = req.query const queryString = Object.keys(req.query).reduce((acc, key) => { if (key !== 'path') acc += `${key}=${req.query[key]}&` return acc }, '') if (!path) { res.status(400).json({ status: 'error', message: 'Path parameter is missing' }) return } const jsonResponse = await axios.get( `${TWELVE_DATA_API}/${path}?${queryString}&apikey=${process.env.NEXT_PUBLIC_TWELVEDATA_APIKEY}`, ) res.status(200).json(jsonResponse.data) } export default handleRequest ================================================ FILE: packages/frontend/pages/api/updateBlockedAddress.ts ================================================ import type { NextApiRequest, NextApiResponse } from 'next' import { updateBlockedAddress } from 'src/server/firebase-admin' const handleRequest = async (req: NextApiRequest, res: NextApiResponse) => { if (req.method !== 'POST') return res.status(400).json({ message: 'Only post is allowed' }) const { address } = req.body try { const visitCount = await updateBlockedAddress(address) return res.status(200).json({ message: 'success', visitCount }) } catch (error) { console.error(error) return res.status(500).json({ message: error }) } } export default handleRequest ================================================ FILE: packages/frontend/pages/blocked.tsx ================================================ import React from 'react' import { Box, Typography } from '@material-ui/core' import { createStyles, makeStyles } from '@material-ui/core/styles' const useStyles = makeStyles((theme) => createStyles({ container: { width: '50%', justifyContent: 'center', display: 'flex', alignItems: 'center', margin: 'auto', marginTop: theme.spacing(10), }, title: { marginTop: theme.spacing(10), }, }), ) const BlockedPage: React.FC = () => { const classes = useStyles() return ( Seems you are using a VPN service or accessing our website from a blocked country, which is a violation of our terms of service. Please disconnect from your VPN and refresh the page to continue using our service. ) } export default BlockedPage ================================================ FILE: packages/frontend/pages/index.tsx ================================================ import React, { useEffect } from 'react' import Hidden from '@material-ui/core/Hidden' import DesktopLandingPage from '@components/LandingPage/DesktopLandingPage' import MobileLandingPage from '@components/LandingPage/MobileLandingPage' import DefaultSiteSeo from '@components/DefaultSiteSeo/DefaultSiteSeo' import useAmplitude from '@hooks/useAmplitude' import { LANDING_EVENTS } from '@utils/amplitude' function LandingPage() { const { track } = useAmplitude() useEffect(() => { track(LANDING_EVENTS.LANDING_VISIT) }, [track]) return (
) } export default LandingPage ================================================ FILE: packages/frontend/pages/lp.tsx ================================================ import { createStyles, makeStyles } from '@material-ui/core' import { Box, Typography } from '@material-ui/core' import React, { useState } from 'react' import ObtainSqueeth from '@components/Lp/ObtainSqueeth' import SqueethInfo from '@components/Lp/SqueethInfo' import LPPosition from '@components/Lp/LPPosition' import LPBuyChart from '@components/Charts/LPBuyChart' import LPMintChart from '@components/Charts/LPMintChart' import Nav from '@components/Nav' import { LPProvider } from '@context/lp' import { SqueethTabNew, SqueethTabsNew } from '@components/Tabs' import { useETHPrice } from '@hooks/useETHPrice' import DefaultSiteSeo from '@components/DefaultSiteSeo/DefaultSiteSeo' const useStyles = makeStyles((theme) => createStyles({ container: { maxWidth: '1280px', width: '80%', display: 'flex', justifyContent: 'center', gridGap: '96px', flexWrap: 'wrap', padding: theme.spacing(6, 5), margin: '0 auto', [theme.breakpoints.down('lg')]: { maxWidth: 'none', width: '90%', }, [theme.breakpoints.down('md')]: { width: '100%', gridGap: '40px', }, [theme.breakpoints.down('sm')]: { padding: theme.spacing(3, 4), }, [theme.breakpoints.down('xs')]: { padding: theme.spacing(3, 3), }, }, leftColumn: { flex: 1, minWidth: '480px', [theme.breakpoints.down('xs')]: { minWidth: '320px', }, }, rightColumn: { flexBasis: '452px', [theme.breakpoints.down('xs')]: { flex: '1', }, }, title: { fontSize: '28px', fontWeight: 700, letterSpacing: '-0.02em', }, description: { fontSize: '18px', fontWeight: 400, color: theme.palette.grey[400], }, subtitle: { fontSize: '22px', fontWeight: 700, letterSpacing: '-0.01em', }, sectionTitle: { marginTop: theme.spacing(3), color: 'rgb(255, 255, 255)', fontWeight: 500, fontSize: '18px', letterSpacing: '-0.01em', }, details: { marginTop: theme.spacing(4), }, tradeSection: { position: 'sticky', top: '100px', border: '1px solid #242728', boxShadow: '0px 4px 40px rgba(0, 0, 0, 0.25)', borderRadius: theme.spacing(0.7), padding: '32px 24px', }, chartNav: { border: `1px solid ${theme.palette.primary.main}30`, }, content: { color: '#bdbdbd', marginTop: '4px', }, }), ) const LPInfo: React.FC<{ lpType: number }> = ({ lpType }) => { const classes = useStyles() const ethPrice = useETHPrice() if (lpType === 0) { return ( <> Buy squeeth and LP Earn a payoff similar to ETH1.5 Strategy Overview Buying and LPing gives you a leverage position with a payoff similar to ETH1.5. You give up some of your squeeth upside in exchange for trading fees. You are paying daily premiums for being long squeeth, but earning fees from LPing on Uniswap. Payoff This payoff diagram does not include premiums or trading fees and assumes implied volatility stays constant.{' '} Risks You are exposed to squeeth premiums, so if you hold the position for a long period of time without upward price movements in ETH, you can lose considerable funds to premium payments.
{' '} Squeeth smart contracts have been audited by Trail of Bits, Akira, and Sherlock. However, smart contracts are experimental technology and we encourage caution only risking funds you can afford to lose. ) } return ( <> Mint squeeth and LP Earn yield from trading fees while being long ETH Strategy Overview Minting and LPing is similar to a covered call. You start off with a position similar to 1x long ETH that gets less long ETH as the price moves up and longer ETH as the price moves down. Payoff This payoff diagram does not included premiums or trading fees and assumes implied volatility stays constant.{' '} Risks You enter this position neutral to squeeth exposure, but could end up long squeeth exposed to premiums or short squeeth depending on ETH price movements. If you fall below the minimum collateralization threshold (150%), you are at risk of liquidation.
Squeeth smart contracts have been audited by Trail of Bits, Akira, and Sherlock. However, smart contracts are experimental technology and we encourage caution only risking funds you can afford to lose. ) } export function LPCalculator() { const classes = useStyles() const [lpType, setLpType] = useState(0) return ( <>