Skip to content

Commit

Permalink
cleanup listing presets/edit mango v4 tokens (solana-labs#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
abrzezinski94 authored Sep 19, 2023
1 parent f40b550 commit 9277f4b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 39 deletions.
36 changes: 21 additions & 15 deletions components/instructions/programs/mangoV4.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import { Connection, Keypair, PublicKey } from '@solana/web3.js'
import EmptyWallet, {
getSuggestedCoinTier,
compareObjectsAndGetDifferentKeys,
ListingArgs,
FlatListingArgs,
ListingArgsFormatted,
getOracle,
getBestMarket,
EditTokenArgsFormatted,
isPythOracle,
getFormattedListingPresets,
FlatEditArgs,
} from '@utils/Mango/listingTools'
import { secondsToHours } from 'date-fns'
import WarningFilledIcon from '@carbon/icons-react/lib/WarningFilled'
Expand Down Expand Up @@ -225,7 +226,7 @@ const instructions = () => ({
const [info, proposedOracle, args] = await Promise.all([
displayArgs(connection, data),
getOracle(connection, oracle),
getDataObjectFlattened<ListingArgs>(connection, data),
getDataObjectFlattened<FlatListingArgs>(connection, data),
])
const liqudityTier = await getSuggestedCoinTier(
proposedMint.toBase58(),
Expand All @@ -244,8 +245,9 @@ const instructions = () => ({
? getFormattedListingValues({
tokenIndex: args.tokenIndex,
name: args.name,
oracle: args.oracle,
...suggestedPreset,
} as ListingArgs)
})
: ({} as ListingArgsFormatted)

const invalidKeys: (keyof ListingArgsFormatted)[] = Object.keys(
Expand Down Expand Up @@ -692,7 +694,7 @@ const instructions = () => ({
const [mangoGroup, info, args] = await Promise.all([
client.getGroup(group),
displayArgs(connection, data),
getDataObjectFlattened<any>(connection, data),
getDataObjectFlattened<FlatEditArgs>(connection, data),
])
const mint = [...mangoGroup.mintInfosMapByMint.values()].find((x) =>
x.publicKey.equals(mintInfo)
Expand All @@ -708,7 +710,7 @@ const instructions = () => ({

const parsedArgs: Partial<EditTokenArgsFormatted> = {
tokenIndex: args.tokenIndex,
tokenName: args.name,
tokenName: args.nameOpt,
oracleConfidenceFilter:
args['oracleConfigOpt.confFilter'] !== undefined
? (args['oracleConfigOpt.confFilter'] * 100)?.toFixed(2)
Expand Down Expand Up @@ -785,7 +787,10 @@ const instructions = () => ({
tokenConditionalSwapTakerFeeRate:
args.tokenConditionalSwapTakerFeeRateOpt,
flashLoanDepositFeeRate: args.flashLoanDepositFeeRateOpt,
reduceOnly: args.reduceOnlyOpt,
reduceOnly:
args.reduceOnlyOpt !== undefined
? REDUCE_ONLY_OPTIONS[args.reduceOnlyOpt].name
: undefined,
}

if (mint) {
Expand All @@ -807,9 +812,10 @@ const instructions = () => ({
? {
...getFormattedListingValues({
tokenIndex: args.tokenIndex,
name: args.name,
name: args.nameOpt,
oracle: args.oracleOpt,
...suggestedPreset,
} as ListingArgs),
}),
groupInsuranceFund: suggestedPreset.insuranceFound,
}
: {}
Expand Down Expand Up @@ -1078,18 +1084,17 @@ const instructions = () => ({
value={parsedArgs.flashLoanDepositFeeRate}
suggestedVal={invalidFields.flashLoanDepositFeeRate}
/>
{typeof parsedArgs.reduceOnly === 'number' && (
<DisplayNullishProperty
label="Reduce only"
value={REDUCE_ONLY_OPTIONS[parsedArgs.reduceOnly].name}
/>
)}
<DisplayNullishProperty
label="Reduce only"
value={parsedArgs.reduceOnly}
/>
</div>
<h3>Raw values</h3>
<div>{info}</div>
</div>
)
} catch (e) {
console.log(e)
const info = await displayArgs(connection, data)

try {
Expand Down Expand Up @@ -1375,10 +1380,11 @@ const DisplayListingProperty = ({
</div>
)

const getFormattedListingValues = (args: ListingArgs) => {
const getFormattedListingValues = (args: FlatListingArgs) => {
const formattedArgs: ListingArgsFormatted = {
tokenIndex: args.tokenIndex,
tokenName: args.name,
oracle: args.oracle?.toBase58(),
oracleConfidenceFilter: (args['oracleConfig.confFilter'] * 100).toFixed(2),
oracleMaxStalenessSlots: args['oracleConfig.maxStalenessSlots'],
interestRateUtilizationPoint0: (
Expand Down
80 changes: 56 additions & 24 deletions utils/Mango/listingTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const MAINNET_PYTH_PROGRAM = new PublicKey(
'FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH'
)

export type ListingArgs = {
export type FlatListingArgs = {
name: string
tokenIndex: number
'oracleConfig.confFilter': number
Expand Down Expand Up @@ -56,6 +56,42 @@ export type ListingArgs = {
tokenConditionalSwapTakerFeeRate: number
flashLoanDepositFeeRate: number
reduceOnly: number
groupInsuranceFund: boolean
oracle: PublicKey
}

export type FlatEditArgs = {
nameOpt: string
tokenIndex: number
'oracleConfigOpt.confFilter': number
'oracleConfigOpt.maxStalenessSlots': number
'interestRateParamsOpt.util0': number
'interestRateParamsOpt.rate0': number
'interestRateParamsOpt.util1': number
'interestRateParamsOpt.rate1': number
'interestRateParamsOpt.maxRate': number
'interestRateParamsOpt.adjustmentFactor': number
loanFeeRateOpt: number
loanOriginationFeeRateOpt: number
maintAssetWeightOpt: number
initAssetWeightOpt: number
maintLiabWeightOpt: number
initLiabWeightOpt: number
liquidationFeeOpt: number
minVaultToDepositsRatioOpt: number
netBorrowLimitPerWindowQuoteOpt: number
netBorrowLimitWindowSizeTsOpt: number
borrowWeightScaleStartQuoteOpt: number
depositWeightScaleStartQuoteOpt: number
stablePriceDelayGrowthLimitOpt: number
stablePriceDelayIntervalSecondsOpt: number
stablePriceGrowthLimitOpt: number
tokenConditionalSwapMakerFeeRateOpt: number
tokenConditionalSwapTakerFeeRateOpt: number
flashLoanDepositFeeRateOpt: number
reduceOnlyOpt: number
groupInsuranceFundOpt: boolean
oracleOpt: PublicKey
}

export type ListingArgsFormatted = {
Expand Down Expand Up @@ -88,41 +124,37 @@ export type ListingArgsFormatted = {
tokenConditionalSwapTakerFeeRate: number
flashLoanDepositFeeRate: number
reduceOnly: string
oracle: string
}

export type EditTokenArgsFormatted = ListingArgsFormatted & {
groupInsuranceFund: boolean
}

const transformPresetToProposed = (
listingPreset: ListingPreset | Record<string, never>
) => {
const proposedPreset: PureListingArgsOrEmptyObj =
Object.keys(listingPreset).length !== 0
? {
...(listingPreset as ListingPreset),
'oracleConfig.maxStalenessSlots': listingPreset.maxStalenessSlots!,
'oracleConfig.confFilter': listingPreset.oracleConfFilter,
'interestRateParams.adjustmentFactor': listingPreset.adjustmentFactor,
'interestRateParams.util0': listingPreset.util0,
'interestRateParams.rate0': listingPreset.rate0,
'interestRateParams.util1': listingPreset.util1,
'interestRateParams.rate1': listingPreset.rate1,
'interestRateParams.maxRate': listingPreset.maxRate,
}
: {}
const transformPresetToProposed = (listingPreset: ListingPreset) => {
const proposedPreset: FormattedListingPreset = {
...listingPreset,
'oracleConfig.maxStalenessSlots': listingPreset.maxStalenessSlots!,
'oracleConfig.confFilter': listingPreset.oracleConfFilter,
'interestRateParams.adjustmentFactor': listingPreset.adjustmentFactor,
'interestRateParams.util0': listingPreset.util0,
'interestRateParams.rate0': listingPreset.rate0,
'interestRateParams.util1': listingPreset.util1,
'interestRateParams.rate1': listingPreset.rate1,
'interestRateParams.maxRate': listingPreset.maxRate,
groupInsuranceFund: listingPreset.insuranceFound,
}

return proposedPreset
}

type PureListingArgsOrEmptyObj =
| Record<string, never>
| (Omit<ListingArgs, 'name' | 'tokenIndex'> & {
preset_name: string
})
type FormattedListingPreset = Omit<
FlatListingArgs,
'name' | 'tokenIndex' | 'oracle'
>

type ProposedListingPresets = {
[key in LISTING_PRESETS_KEYS]: PureListingArgsOrEmptyObj
[key in LISTING_PRESETS_KEYS]: FormattedListingPreset
}

export const getFormattedListingPresets = (isPythOracle: boolean) => {
Expand Down

0 comments on commit 9277f4b

Please sign in to comment.