Skip to content

Commit

Permalink
feat: add json schemas for trading entities
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Nov 19, 2024
1 parent e99875c commit 18ad494
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
]
},

"ignorePatterns": ["dist", "node_modules", "src/subgraph/graphql.ts", "examples"]
"ignorePatterns": ["dist", "node_modules", "src/subgraph/graphql.ts", "examples", "schemas"]
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"prebuild": "rm -rf dist && yarn run codegen",
"build": "microbundle -f modern,esm,cjs",
"start": "microbundle -f modern,esm,cjs watch",
"postbuild": "cp package.json dist && cp README.md dist",
"postbuild": "cp package.json dist && cp README.md dist && npm run trading:generateSchemas",
"lint": "eslint src",
"format": "prettier --write \"src/**/*.+(ts|json)\"",
"test": "jest",
Expand Down
30 changes: 30 additions & 0 deletions scripts/generateTradingSchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Config, createGenerator } from 'ts-json-schema-generator'
import { writeFileSync, mkdirSync } from 'fs'

const config: Config = {
path: 'src/trading/types.ts',
expose: 'none',
topRef: false,
}

const types = [
'QuoterParameters',
'TradeParameters',
'LimitTradeParameters',
'SwapAdvancedSettings',
'LimitOrderAdvancedSettings',
'QuoteResultsSerialized',
]

const outputPath = 'dist/schemas/trading/'

const generator = createGenerator(config)

mkdirSync(outputPath, { recursive: true })

types.forEach((type) => {
const schema = generator.createSchema(type)
const schemaString = JSON.stringify(schema, null, 2)

writeFileSync(outputPath + `${type}.ts`, `export default ${schemaString} as const`)
})
4 changes: 2 additions & 2 deletions src/trading/getQuote.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
AccountAddress,
QuoteResults,
QuoterParameters,
SwapAdvancedSettings,
SwapParameters,
TradeParameters,
TraderParameters,
} from './types'
import { DEFAULT_QUOTE_VALIDITY, log } from './consts'

Expand All @@ -27,7 +27,7 @@ export type QuoteResultsWithSigner = { result: QuoteResults & { signer: Signer }

export async function getQuote(
tradeParameters: TradeParameters,
trader: Omit<TraderParameters, 'signer'> & { account: AccountAddress },
trader: QuoterParameters,
advancedSettings?: SwapAdvancedSettings,
_orderBookApi?: OrderBookApi
): Promise<{ result: QuoteResults; orderBookApi: OrderBookApi }> {
Expand Down
10 changes: 10 additions & 0 deletions src/trading/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface TraderParameters {
signer: Signer | ExternalProvider | PrivateKey
}

export type QuoterParameters = Omit<TraderParameters, 'signer'> & { account: AccountAddress }

export interface TradeParameters extends TradeBaseParameters, TradeOptionalParameters {}

export interface SwapParameters extends TradeParameters, TraderParameters {}
Expand Down Expand Up @@ -62,6 +64,14 @@ export interface QuoteResults {
appDataInfo: AppDataInfo
}

export interface QuoteResultsSerialized {
tradeParameters: TradeParameters
amountsAndCosts: QuoteAmountsAndCosts<string>
orderToSign: UnsignedOrder
quoteResponse: OrderQuoteResponse
appDataInfo: AppDataInfo
}

export interface QuoteAndPost {
quoteResults: QuoteResults
postSwapOrderFromQuote(): Promise<string>
Expand Down
Loading

0 comments on commit 18ad494

Please sign in to comment.