Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/injective w batch update #302

Closed
26 changes: 26 additions & 0 deletions gateway/docs/swagger/clob-routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,32 @@ paths:
description: 'Bad request response.'
'404':
description: 'Not found response.'
/clob/batchOrders:
post:
tags:
- 'clob'
summary: 'Batch create or delete orders'
description: 'Create or delete orders in batch.'
operationId: 'batchOrders'
consumes:
- 'application/json'
produces:
- 'application/json'
parameters:
- in: 'body'
name: 'body'
description: 'Request body.'
required: true
schema:
$ref: '#/definitions/ClobBatchOrdersRequest'
responses:
'200':
description: 'Successful response.'
schema:
$ref: '#/definitions/ClobBatchOrdersResponse'
'400':
description: 'Bad request response.'

/clob/orders/open:
get:
tags:
Expand Down
66 changes: 66 additions & 0 deletions gateway/docs/swagger/definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,72 @@ definitions:
type: 'string'
example: '0x...'

ClobBatchOrdersRequest:
type: 'object'
required:
- 'chain'
- 'network'
- 'connector'
- 'address'
- 'createOrderParams'
- 'cancelOrderIds'
properties:
chain:
type: 'string'
example: 'injective'
network:
type: 'string'
example: 'mainnet'
connector:
type: 'string'
example: 'injective'
address:
type: 'string'
example: '0x...'
createOrderParams:
type: 'array'
items: 'object'
example: [{
market: 'AAVE-USDT',
price: '2',
amount: '0.10',
side: 'SELL',
orderType: 'LIMIT',
},
{
price: '3',
amount: '0.10',
side: 'SELL',
}]
cancelOrderParams:
type: 'array'
items: 'object'
example: [
{market: 'AAVE-USDT', orderId: '0x...'},
{market: 'AAVE-USDT', orderId: '0x...'},
]

ClobBatchOrdersResponse:
type: 'object'
required:
- 'network'
- 'timestamp'
- 'latency'
- 'txHash'
properties:
network:
type: 'string'
example: 'mainnet'
timestamp:
type: 'integer'
example: 1636368085740
latency:
type: 'number'
example: 0.5
txHash:
type: 'string'
example: '0x...'

TransferToSubAccountRequest:
type: 'object'
required:
Expand Down
6 changes: 6 additions & 0 deletions gateway/manual-tests/curl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ curl -s -X POST -k --key $GATEWAY_KEY --cert $GATEWAY_CERT -H "Content-Type: app
## delete orders
curl -s -X DELETE -k --key $GATEWAY_KEY --cert $GATEWAY_CERT -H "Content-Type: application/json" -d "$(envsubst < ./requests/injective_delete_order.json)" https://localhost:15888/clob/orders | jq

## post batch orders create
curl -s -X POST -k --key $GATEWAY_KEY --cert $GATEWAY_CERT -H "Content-Type: application/json" -d "$(envsubst < ./requests/injective_batch_create.json)" https://localhost:15888/clob/batchOrders | jq

## post batch orders delete
curl -s -X POST -k --key $GATEWAY_KEY --cert $GATEWAY_CERT -H "Content-Type: application/json" -d "$(envsubst < ./requests/injective_batch_delete.json)" https://localhost:15888/clob/batchOrders | jq


# Serum

Expand Down
19 changes: 19 additions & 0 deletions gateway/manual-tests/requests/injective_batch_create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"chain": "injective",
"network": "mainnet",
"connector": "injective",
"address": "$INJECTIVE_SUBACCOUNTID",
"createOrderParams": [{
"price": "2",
"amount": "0.10",
"side": "SELL",
"orderType": "LIMIT",
"market": "INJ-USDT"
}, {
"price": "3",
"amount": "0.10",
"side": "SELL",
"orderType": "LIMIT",
"market": "INJ-USDT"
}]
}
7 changes: 7 additions & 0 deletions gateway/manual-tests/requests/injective_batch_delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"chain": "injective",
"network": "mainnet",
"connector": "injective",
"address": "$INJECTIVE_SUBACCOUNTID",
"cancelOrderParams": [{"market": "INJ-USDT", "orderId": "0x17e1a82690a5916d5b338ce7b0fbf0ca70823da46a5d68ce7355b59a999d195f"}, {"market": "INJ-USDT", "orderId": "0x05f30f4ad9fe18ae55c7dcbf4ea302c69594d8ffa3904c696c840377f4ffb4dc"}]
}
2 changes: 2 additions & 0 deletions gateway/src/chains/injective/injective.message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ export class MsgBroadcasterLocal {
transaction.injectiveAddress,
currentNonce
);
} else {
throw new Error(txResponse.data.tx_response.raw_log);
}
} finally {
this._txQueue.shift();
Expand Down
26 changes: 26 additions & 0 deletions gateway/src/clob/clob.controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EstimateGasResponse } from '../amm/amm.requests';
import { NetworkSelectionRequest } from '../services/common-interfaces';
import { getChain, getConnector } from '../services/connection-manager';
import {
ClobBatchUpdateRequest,
ClobDeleteOrderRequest,
ClobDeleteOrderResponse,
ClobGetOrderRequest,
Expand Down Expand Up @@ -162,6 +163,31 @@ export async function deleteOrder(
};
}

/**
* POST /batchOrders
*
*
* @param request
*/
export async function batchOrders(
request: ClobBatchUpdateRequest
): Promise<ClobDeleteOrderResponse> {
const startTimestamp: number = Date.now();
await getChain(request.chain, request.network);
const connector: any = await getConnector(
request.chain,
request.network,
request.connector
);
const result = await connector.batchOrders(request);
return {
network: request.network,
timestamp: startTimestamp,
latency: latency(startTimestamp, Date.now()),
...result,
};
}

/**
* Estimate gas for a typical clob transaction.
*
Expand Down
25 changes: 21 additions & 4 deletions gateway/src/clob/clob.requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,29 @@ export interface ClobGetOrderResponse {
| [];
}

export interface ClobPostOrderRequest extends ClobOrderbookRequest {
address: string;
side: Side;
orderType: OrderType;
export interface CreateOrderParam {
price: string;
amount: string;
orderType: OrderType;
side: Side;
market: string;
}

export interface ClobPostOrderRequest
extends NetworkSelectionRequest,
CreateOrderParam {
address: string;
}

export interface ClobDeleteOrderRequestExtract {
market: string;
orderId: string;
}

export interface ClobBatchUpdateRequest extends NetworkSelectionRequest {
address: string;
createOrderParams?: CreateOrderParam[];
cancelOrderParams?: ClobDeleteOrderRequestExtract[];
}

export interface ClobPostOrderResponse {
Expand Down
16 changes: 16 additions & 0 deletions gateway/src/clob/clob.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
postOrder,
deleteOrder,
estimateGas,
batchOrders,
} from './clob.controllers';
import {
ClobBatchUpdateRequest,
ClobDeleteOrderRequest,
ClobDeleteOrderResponse,
ClobGetOrderRequest,
Expand All @@ -33,6 +35,7 @@ import {
validateMarketRequest,
validatePostOrderRequest,
validateOrderRequest,
validateBatchOrdersRequest,
} from './clob.validators';

export namespace CLOBRoutes {
Expand Down Expand Up @@ -126,6 +129,19 @@ export namespace CLOBRoutes {
)
);

router.post(
'/batchOrders',
asyncHandler(
async (
req: Request<{}, {}, ClobBatchUpdateRequest>,
res: Response<ClobPostOrderResponse | string, {}>
) => {
validateBatchOrdersRequest(req.body);
res.status(200).json(await batchOrders(req.body));
}
)
);

router.get(
'/estimateGas',
asyncHandler(
Expand Down
3 changes: 3 additions & 0 deletions gateway/src/clob/clob.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ export const validatePostOrderRequest: RequestValidator = mkRequestValidator(
export const validateOrderRequest: RequestValidator = mkRequestValidator(
NETWORL_VALIDATIONS.concat([validateOrderId, validateWallet])
);

export const validateBatchOrdersRequest: RequestValidator =
mkRequestValidator(NETWORL_VALIDATIONS);
Loading