Skip to content

Commit

Permalink
feat: Optimism Newport trading
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelxuwu authored May 25, 2023
1 parent e66f5ca commit 3fb17cf
Show file tree
Hide file tree
Showing 20 changed files with 1,364 additions and 1,810 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,32 @@ const AdminMarketAdapterConfigurationParams = ({ market, isExpanded, onClickExpa
const val = params[typedKey] ?? ''
return (
<Input
mb={4}
key={key}
label={key}
value={val.toString()}
placeholder={value}
onChange={evt => setParams({ ...params, [key]: evt.target.value })}
/>
)
} else if (typeof value === 'number') {
const val = params[typedKey] ?? ''
return (
<Input
mb={4}
key={key}
label={key}
value={val.toString()}
placeholder={value.toString()}
onChange={evt => {
const toParams = { ...params, [key]: parseFloat(evt.target.value) }
if (evt.target.value === '') {
delete toParams[typedKey]
}
setParams(toParams)
}}
/>
)
}
const val = (params[typedKey] ?? ZERO_BN) as BigNumber
return (
Expand All @@ -82,18 +101,18 @@ const AdminMarketAdapterConfigurationParams = ({ market, isExpanded, onClickExpa
min={ZERO_BN}
value={val}
key={key}
placeholder={isZeroDecimals ? fromBigNumber(val, 0).toString() : val}
placeholder={isZeroDecimals ? fromBigNumber(value, 0).toString() : value}
onEmpty={() => {
const toParams = { ...params }
delete toParams[typedKey]
setParams(toParams)
}}
onChange={val => {
const newGreekCacheParams = {
const newParams = {
...params,
[key]: val,
}
setParams(newGreekCacheParams)
setParams(newParams)
}}
/>
</Flex>
Expand All @@ -116,7 +135,8 @@ const AdminMarketAdapterConfigurationParams = ({ market, isExpanded, onClickExpa
<Modal
isOpen={isConfirmOpen}
onClose={() => setIsConfirmOpen(false)}
title="Confirm New Adapter Market Pricing Params"
title="Confirm New Adapter Market Configuration Params"
desktopWidth={700}
>
<CardBody>
<Box>
Expand All @@ -131,7 +151,7 @@ const AdminMarketAdapterConfigurationParams = ({ market, isExpanded, onClickExpa
return (
<Flex my={2} key={key} justifyContent="space-between">
<Text color="secondaryText">{key}</Text>
{typeof val === 'string' ? (
{typeof val === 'string' || typeof val === 'number' ? (
<Text ml={1}>{val}</Text>
) : (
<Text ml={1}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import Modal from '@lyra/ui/components/Modal'
import Shimmer from '@lyra/ui/components/Shimmer'
import Text from '@lyra/ui/components/Text'
import formatNumber from '@lyra/ui/utils/formatNumber'
import { FuturesPoolHedgerParams, Market } from '@lyrafinance/lyra-js'
import {
FuturesPoolHedgerParams,
GMXFuturesPoolHedgerParams,
Market,
SNXFuturesPoolHedgerParams,
} from '@lyrafinance/lyra-js'
import React, { useState } from 'react'

import { ZERO_BN } from '@/app/constants/bn'
Expand All @@ -29,7 +34,7 @@ type Props = {
onClickExpand: () => void
}

const zeroDecimalKeys: (keyof FuturesPoolHedgerParams)[] = ['minCancelDelay']
const zeroDecimalKeys: (keyof GMXFuturesPoolHedgerParams | keyof SNXFuturesPoolHedgerParams)[] = ['minCancelDelay']

const AdminMarketFuturesPoolHedgerParams = withSuspense(
({ market, isExpanded, onClickExpand }: Props) => {
Expand Down
7 changes: 2 additions & 5 deletions app/src/hooks/market/useTradePageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import coerce from '@/app/utils/coerce'
import fetchMarkets from '@/app/utils/fetchMarkets'
import getLyraSDK from '@/app/utils/getLyraSDK'
import getPageHeartbeat from '@/app/utils/getPageHeartbeat'
import { lyraAvalon } from '@/app/utils/lyra'

import useWalletAccount from '../account/useWalletAccount'
import useFetch, { useMutate } from '../data/useFetch'
Expand All @@ -18,12 +17,10 @@ type TradeRoot = {
}

const fetchTradePageData = async (network: Network, owner: string | null): Promise<TradeRoot> => {
const lyra = network === Network.Optimism ? lyraAvalon : getLyraSDK(network)
const maybeFetchPositions = async (): Promise<Position[]> => (owner ? lyra.openPositions(owner) : [])
const maybeFetchPositions = async (): Promise<Position[]> => (owner ? getLyraSDK(network).openPositions(owner) : [])
const [markets, openPositions] = await Promise.all([fetchMarkets([network]), maybeFetchPositions()])
return {
// TODO @michaelxuwu update filtering when trading goes live
markets: markets.filter(m => m.lyra.network !== Network.Optimism || m.lyra.version === Version.Avalon),
markets: markets.filter(m => m.lyra.version !== Version.Avalon),
openPositions: openPositions.sort((a, b) => a.expiryTimestamp - b.expiryTimestamp),
}
}
Expand Down
3 changes: 1 addition & 2 deletions app/src/utils/getDefaultMarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Network } from '@lyrafinance/lyra-js'
export const getDefaultMarket = (network: Network) => {
switch (network) {
case Network.Arbitrum:
return 'eth-usdc'
case Network.Optimism:
return 'seth-susd'
return 'eth-usdc'
}
}
3 changes: 2 additions & 1 deletion app/src/utils/lyra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const arbitrumProvider = new CachedStaticJsonRpcProvider(
arbitrumNetworkConfig.chainId
)

const getLyraSubgraphURI = (chain: Chain, version: Version): string | undefined => {
const getLyraSubgraphURI = (chain: Chain, version: Version = Version.Newport): string | undefined => {
const SATSUMA_API_KEY = process.env.REACT_APP_SATSUMA_API_KEY
if (!SATSUMA_API_KEY) {
// Use SDK default
Expand All @@ -26,6 +26,7 @@ const getLyraSubgraphURI = (chain: Chain, version: Version): string | undefined
case Version.Avalon:
return `https://subgraph.satsuma-prod.com/${SATSUMA_API_KEY}/lyra/optimism-mainnet/api`
case Version.Newport:
default:
return `https://subgraph.satsuma-prod.com/${SATSUMA_API_KEY}/lyra/optimism-mainnet-newport/api`
}
case Chain.OptimismGoerli:
Expand Down
55 changes: 30 additions & 25 deletions sdk/src/admin/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { BigNumber } from '@ethersproject/bignumber'
import { PopulatedTransaction } from '@ethersproject/contracts'

import Lyra, { MarketContractAddresses, Version } from '..'
import Lyra, { MarketContractAddresses, Network, Version } from '..'
import { LyraContractId, LyraGlobalContractId, LyraMarketContractId } from '../constants/contracts'
import { LyraContractMap, LyraMarketContractMap } from '../constants/mappings'
import { GMXAdapter, NewportGMXAdapter } from '../contracts/newport/typechain/NewportGMXAdapter'
import {
GMXFuturesPoolHedger,
NewportGMXFuturesPoolHedger,
} from '../contracts/newport/typechain/NewportGMXFuturesPoolHedger'
import { NewportGMXFuturesPoolHedger } from '../contracts/newport/typechain/NewportGMXFuturesPoolHedger'
import { OptionGreekCache } from '../contracts/newport/typechain/NewportOptionGreekCache'
import { SNXPerpsV2PoolHedger } from '../contracts/newport/typechain/NewportSNXPerpsV2PoolHedger'
import { NewportSNXPerpsV2PoolHedger } from '../contracts/newport/typechain/NewportSNXPerpsV2PoolHedger'
import { NewportSNXPerpV2Adapter, SNXPerpV2Adapter } from '../contracts/newport/typechain/NewportSNXPerpV2Adapter'
import buildTx from '../utils/buildTx'
import fetchGlobalOwner from '../utils/fetchGlobalOwner'
Expand Down Expand Up @@ -211,7 +208,19 @@ export type PoolHedgerParams = {
hedgeCap: BigNumber
}

export type FuturesPoolHedgerParams = {
export type FuturesPoolHedgerParams = GMXFuturesPoolHedgerParams | SNXFuturesPoolHedgerParams

export type SNXFuturesPoolHedgerParams = {
targetLeverage: BigNumber
maximumFundingRate: BigNumber
deltaThreshold: BigNumber
marketDepthBuffer: BigNumber
priceDeltaBuffer: BigNumber
worstStableRate: BigNumber
maxOrderCap: BigNumber
}

export type GMXFuturesPoolHedgerParams = {
acceptableSpotSlippage: BigNumber
deltaThreshold: BigNumber
marketDepthBuffer: BigNumber
Expand Down Expand Up @@ -779,7 +788,7 @@ export class Admin {
) {
const market = await this.lyra.market(marketAddressOrName)
if (!(market.params.adapterView as SNXPerpV2Adapter.MarketAdapterStateStructOutput)) {
throw new Error('Adapter market pricing parameters not supported on this market')
throw new Error('Adapter market configuration parameters not supported on this market')
}
const marketAdapterConfig = (market.params.adapterView as SNXPerpV2Adapter.MarketAdapterStateStructOutput).config
const fromParamsFlat: AdminAdapterMarketConfigurationParams = {
Expand Down Expand Up @@ -837,6 +846,7 @@ export class Admin {
params: Partial<FuturesPoolHedgerParams>
): Promise<AdminSetMarketParamsReturn<FuturesPoolHedgerParams>> {
const market = await this.lyra.market(marketAddressOrName)

if (market.lyra.version !== Version.Newport || !market.params.hedgerView) {
throw new Error('Parameters not supported on version')
}
Expand All @@ -847,30 +857,25 @@ export class Admin {
market.lyra.version,
LyraMarketContractId.PoolHedger
)

const futurePoolHedgerParams:
| SNXPerpsV2PoolHedger.SNXPerpsV2PoolHedgerParametersStructOutput
| GMXFuturesPoolHedger.FuturesPoolHedgerParametersStructOutput =
(market.params.hedgerView as GMXFuturesPoolHedger.GMXFuturesPoolHedgerViewStructOutput)
?.futuresPoolHedgerParams ??
(market.params.hedgerView as SNXPerpsV2PoolHedger.HedgerStateStructOutput)?.futuresPoolHedgerParams

const toParams = {
...futurePoolHedgerParams,
...market.params.hedgerView.futuresPoolHedgerParams,
...params,
}

const owner = await market.owner()

const calldata = (futuresPoolHedger as NewportGMXFuturesPoolHedger).interface.encodeFunctionData(
'setFuturesPoolHedgerParams',
[toParams]
)
const calldata =
market.lyra.network === Network.Optimism
? (futuresPoolHedger as NewportSNXPerpsV2PoolHedger).interface.encodeFunctionData(
'setFuturesPoolHedgerParams',
[toParams as SNXFuturesPoolHedgerParams]
)
: (futuresPoolHedger as NewportGMXFuturesPoolHedger).interface.encodeFunctionData(
'setFuturesPoolHedgerParams',
[toParams as GMXFuturesPoolHedgerParams]
)
const tx = buildTx(
this.lyra.provider,
this.lyra.provider.network.chainId,
futuresPoolHedger.address,
owner,
market.params.owner,
calldata
)
tx.gasLimit = BigNumber.from(10_000_000)
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/constants/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
NewportOptionMarketViewer,
NewportOptionToken,
NewportShortCollateral,
NewportSNXPerpsV2PoolHedger,
NewportSNXPerpV2Adapter,
NewportTestFaucet,
} from '../contracts/newport/typechain'
Expand Down Expand Up @@ -71,7 +72,7 @@ export type LyraMarketNewportContractMap = {
[LyraMarketContractId.OptionGreekCache]: NewportOptionGreekCache
[LyraMarketContractId.LiquidityToken]: NewportLiquidityToken
[LyraMarketContractId.LiquidityPool]: NewportLiquidityPool
[LyraMarketContractId.PoolHedger]: NewportGMXFuturesPoolHedger
[LyraMarketContractId.PoolHedger]: NewportGMXFuturesPoolHedger | NewportSNXPerpsV2PoolHedger
}

export type LyraMarketContractMap<V extends Version, C extends LyraMarketContractId> = V extends Version.Avalon
Expand Down
Loading

0 comments on commit 3fb17cf

Please sign in to comment.