Skip to content

Commit

Permalink
indexing updates to types and server library (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites authored Jan 15, 2024
1 parent cdd8948 commit ca5300d
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 212 deletions.
2 changes: 1 addition & 1 deletion source/pool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"devDependencies": {
"@airswap/constants": "4.1.8",
"@airswap/metadata": "4.1.15",
"@airswap/types": "4.1.3",
"@airswap/types": "4.1.4",
"@airswap/utils": "4.1.12",
"prompt-confirm": "^2.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion source/swap-erc20/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"devDependencies": {
"@airswap/constants": "4.1.8",
"@airswap/staking": "4.0.5",
"@airswap/types": "4.1.3",
"@airswap/types": "4.1.4",
"@airswap/utils": "4.1.12",
"prompt-confirm": "^2.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion source/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"devDependencies": {
"@airswap/constants": "4.1.8",
"@airswap/types": "4.1.3",
"@airswap/types": "4.1.4",
"@airswap/utils": "4.1.12",
"@nomicfoundation/hardhat-network-helpers": "^1.0.7"
},
Expand Down
2 changes: 1 addition & 1 deletion source/wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"devDependencies": {
"@airswap/constants": "4.1.8",
"@airswap/utils": "4.1.12",
"@airswap/types": "4.1.3",
"@airswap/types": "4.1.4",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"prompt-confirm": "^2.0.4"
},
Expand Down
2 changes: 1 addition & 1 deletion tools/libraries/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@airswap/staking": "4.0.5",
"@airswap/swap": "4.1.2",
"@airswap/swap-erc20": "4.1.6",
"@airswap/types": "4.1.3",
"@airswap/types": "4.1.4",
"@airswap/utils": "4.1.12",
"@airswap/wrapper": "4.1.5",
"browser-or-node": "^2.1.1",
Expand Down
122 changes: 24 additions & 98 deletions tools/libraries/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import {
OrderERC20,
Pricing,
ServerOptions,
OrderResponse,
SupportedProtocolInfo,
OrderFilter,
SortOrder,
SortField,
OrderResponse,
Indexes,
Direction,
} from '@airswap/types'
import { ChainIds, Protocols, protocolNames } from '@airswap/constants'

Expand All @@ -40,80 +40,8 @@ if (!isBrowser) {
})
}

const REQUEST_TIMEOUT = 4000

export function toSortOrder(key: string): SortOrder | undefined {
if (typeof key !== 'string') {
return undefined
}
if (key.toUpperCase() === SortOrder.ASC) {
return SortOrder.ASC
}
if (key.toUpperCase() === SortOrder.DESC) {
return SortOrder.DESC
}

return undefined
}

export function toSortField(key: string): SortField | undefined {
if (typeof key !== 'string') {
return undefined
}
key = key.toUpperCase()
if (Object.keys(SortField).includes(key)) {
return key as SortField
}

return undefined
}

export abstract class IndexedOrderError extends Error {
public code!: number
public constructor(message: string) {
super(message)
this.message = message
}
}
export class ErrorResponse {
public code: number
public message: string
public constructor(code: number, message: string) {
this.code = code
this.message = message
}
}
export class SuccessResponse {
public message: string
public constructor(message: string) {
this.message = message
}
}
export class JsonRpcResponse<Type> {
public id: string
public result:
| OrderResponse<Type>
| ErrorResponse
| SuccessResponse
| undefined
private jsonrpc = '2.0'

public constructor(
id: string,
result:
| OrderResponse<Type>
| IndexedOrderError
| SuccessResponse
| undefined
) {
this.id = id
if (result instanceof Error) {
this.result = new ErrorResponse(result.code, result.message)
} else {
this.result = result
}
}
}
const DEFAULT_LIMIT = 10
const DEFAULT_TIMEOUT = 4000

export interface ServerEvents {
'pricing-erc20': (pricing: Pricing[]) => void
Expand Down Expand Up @@ -331,12 +259,10 @@ export class Server extends TypedEmitter<ServerEvents> {
/**
* Protocols.IndexingERC20
*/
public async addOrderERC20(
fullOrder: FullOrderERC20
): Promise<SuccessResponse> {
public async addOrderERC20(fullOrder: FullOrderERC20): Promise<boolean> {
try {
return Promise.resolve(
(await this.httpCall('addOrderERC20', [fullOrder])) as SuccessResponse
(await this.httpCall('addOrderERC20', [fullOrder])) as boolean
)
} catch (err) {
return Promise.reject(err)
Expand All @@ -345,19 +271,19 @@ export class Server extends TypedEmitter<ServerEvents> {

public async getOrdersERC20(
orderFilter: OrderFilter,
offset: number,
limit: number,
sortField?: SortField,
sortOrder?: SortOrder
offset = 0,
limit = DEFAULT_LIMIT,
by = Indexes.EXPIRY,
direction = Direction.ASC
): Promise<OrderResponse<FullOrderERC20>> {
try {
return Promise.resolve(
(await this.httpCall('getOrdersERC20', [
{ ...this.toBigIntJson(orderFilter) },
offset,
limit,
sortField,
sortOrder,
by,
direction,
])) as OrderResponse<FullOrderERC20>
)
} catch (err) {
Expand All @@ -368,10 +294,10 @@ export class Server extends TypedEmitter<ServerEvents> {
/**
* Protocols.Indexing
*/
public async addOrder(order: FullOrder): Promise<SuccessResponse> {
public async addOrder(order: FullOrder): Promise<boolean> {
try {
return Promise.resolve(
(await this.httpCall('addOrder', [order])) as SuccessResponse
(await this.httpCall('addOrder', [order])) as boolean
)
} catch (err) {
return Promise.reject(err)
Expand All @@ -380,19 +306,19 @@ export class Server extends TypedEmitter<ServerEvents> {

public async getOrders(
orderFilter: OrderFilter,
offset: number,
limit: number,
sortField?: SortField,
sortOrder?: SortOrder
offset = 0,
limit = DEFAULT_LIMIT,
by = Indexes.NONCE,
direction = Direction.ASC
): Promise<OrderResponse<FullOrder>> {
try {
return Promise.resolve(
(await this.httpCall('getOrders', [
{ ...this.toBigIntJson(orderFilter) },
offset,
limit,
sortField,
sortOrder,
by,
direction,
])) as OrderResponse<FullOrder>
)
} catch (err) {
Expand All @@ -419,7 +345,7 @@ export class Server extends TypedEmitter<ServerEvents> {
}
}

private _init(initializeTimeout: number = REQUEST_TIMEOUT) {
private _init(initializeTimeout: number = DEFAULT_TIMEOUT) {
if (this.transportProtocol === 'http') {
return this._initHTTPClient(this.locator)
} else {
Expand All @@ -435,7 +361,7 @@ export class Server extends TypedEmitter<ServerEvents> {
hostname: parsedUrl.hostname,
path: parsedUrl.path,
port: parsedUrl.port,
timeout: REQUEST_TIMEOUT,
timeout: DEFAULT_TIMEOUT,
}

if (!clientOnly) {
Expand Down Expand Up @@ -483,7 +409,7 @@ export class Server extends TypedEmitter<ServerEvents> {
(resolve, reject) => {
this.webSocketClient = new JsonRpcWebsocket(
url.format(locator),
REQUEST_TIMEOUT,
DEFAULT_TIMEOUT,
(error: JsonRpcError) => {
if (!this.isInitialized) {
reject(error)
Expand Down
Loading

0 comments on commit ca5300d

Please sign in to comment.