Skip to content

Commit

Permalink
refactor: consistent endpoint in API V2
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Feb 12, 2024
1 parent 12bc09d commit 0308974
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 134 deletions.
56 changes: 35 additions & 21 deletions lib/api/v2/WebSocketHandler.ts
@@ -1,6 +1,7 @@
import http from 'http';
import ws from 'ws';
import { formatError } from '../../Utils';
import Errors from '../../service/Errors';
import Service from '../../service/Service';
import Controller from '../Controller';

Expand Down Expand Up @@ -118,15 +119,19 @@ class WebSocketHandler {
const subscribeData = data as WsSubscribeRequest;
switch (subscribeData.channel) {
case SubscriptionChannel.SwapUpdate: {
const idsWithSwaps = subscribeData.args.filter((id) =>
this.controller.pendingSwapInfos.has(id),
);

const existingIds = this.socketToSwaps.get(socket) || [];
this.socketToSwaps.set(
socket,
existingIds.concat(
subscribeData.args.filter((id) => !existingIds.includes(id)),
idsWithSwaps.filter((id) => !existingIds.includes(id)),
),
);

for (const id of subscribeData.args) {
for (const id of idsWithSwaps) {
const existingSockets = this.swapToSockets.get(id) || [];
if (existingSockets.includes(socket)) {
continue;
Expand All @@ -135,31 +140,40 @@ class WebSocketHandler {
this.swapToSockets.set(id, existingSockets.concat(socket));
}

this.sendToSocket(socket, {
event: Operation.Subscribe,
channel: subscribeData.channel,
args: subscribeData.args,
});

const args = subscribeData.args.map((id) => {
const status = this.controller.pendingSwapInfos.get(id);
if (status === undefined) {
return {
id,
error: Errors.SWAP_NOT_FOUND(id).message,
};
}

return {
id,
...status,
};
});

this.sendToSocket(socket, {
event: Operation.Update,
channel: SubscriptionChannel.SwapUpdate,
args: args,
});

break;
}

default:
this.sendToSocket(socket, { error: 'unknown channel' });
return;
}

this.sendToSocket(socket, {
event: Operation.Subscribe,
channel: subscribeData.channel,
args: subscribeData.args,
});

if (subscribeData.channel === SubscriptionChannel.SwapUpdate) {
const args = subscribeData.args
.map((id) => [id, this.controller.pendingSwapInfos.get(id)])
.filter(([, data]) => data !== undefined);

this.sendToSocket(socket, {
event: Operation.Update,
channel: SubscriptionChannel.SwapUpdate,
args: args,
});
}
};

private listenSwapUpdates = () => {
Expand All @@ -173,7 +187,7 @@ class WebSocketHandler {
this.sendToSocket(socket, {
event: Operation.Update,
channel: SubscriptionChannel.SwapUpdate,
args: [[id, status]],
args: [{ id, ...status }],
});
}
});
Expand Down
123 changes: 75 additions & 48 deletions lib/api/v2/routers/SwapRouter.ts
Expand Up @@ -359,17 +359,48 @@ class SwapRouter extends RouterBase {
this.handleError(this.getSubmarineTransaction),
);

/**
* @openapi
* /swap/submarine/{id}/refund:
* get:
* tags: [Submarine]
* description: Get an EIP-712 signature for a cooperative EVM refund
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: ID of the Swap
* responses:
* '200':
* description: EIP-712 signature
* content:
* application/json:
* schema:
* properties:
* signature:
* type: string
* description: EIP-712 signature with which a cooperative refund can be executed onchain
* '400':
* description: Error that caused signature request to fail
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
*/
router.get(
'/submarine/:id/refund',
this.handleError(this.refundSubmarineEvm),
);

/**
* @openapi
* components:
* schemas:
* SubmarineRefundRequest:
* type: object
* properties:
* id:
* type: string
* required: true
* description: ID of the Submarine Swap that should be refunded
* pubNonce:
* type: string
* required: true
Expand Down Expand Up @@ -401,10 +432,17 @@ class SwapRouter extends RouterBase {

/**
* @openapi
* /swap/submarine/refund:
* /swap/submarine/{id}/refund:
* post:
* description: Requests a partial signature for a cooperative Submarine Swap refund transaction
* tags: [Submarine]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: ID of the Swap
* requestBody:
* required: true
* content:
Expand All @@ -425,43 +463,14 @@ class SwapRouter extends RouterBase {
* schema:
* $ref: '#/components/schemas/ErrorResponse'
*/
router.post('/submarine/refund', this.handleError(this.refundSubmarine));

/**
* @openapi
* /swap/submarine/{id}/refund:
* get:
* tags: [Submarine]
* description: Get an EIP-712 signature for a cooperative EVM refund
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: ID of the Swap
* responses:
* '200':
* description: EIP-712 signature
* content:
* application/json:
* schema:
* properties:
* signature:
* type: string
* description: EIP-712 signature with which a cooperative refund can be executed onchain
* '400':
* description: Error that caused signature request to fail
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/ErrorResponse'
*/
router.get(
router.post(
'/submarine/:id/refund',
this.handleError(this.refundSubmarineEvm),
this.handleError(this.refundSubmarine),
);

// Deprecated endpoint from first Taproot deployment
router.post('/submarine/refund', this.handleError(this.refundSubmarine));

/**
* @openapi
* components:
Expand Down Expand Up @@ -788,10 +797,6 @@ class SwapRouter extends RouterBase {
* ReverseClaimRequest:
* type: object
* properties:
* id:
* type: string
* required: true
* description: ID of the Reverse Swap that should be refunded
* preimage:
* type: string
* required: true
Expand All @@ -812,10 +817,17 @@ class SwapRouter extends RouterBase {

/**
* @openapi
* /swap/reverse/claim:
* /swap/reverse/{id}/claim:
* post:
* description: Requests a partial signature for a cooperative Reverse Swap claim transaction
* tags: [Reverse]
* parameters:
* - in: path
* name: id
* required: true
* schema:
* type: string
* description: ID of the Swap
* requestBody:
* required: true
* content:
Expand All @@ -836,6 +848,9 @@ class SwapRouter extends RouterBase {
* schema:
* $ref: '#/components/schemas/ErrorResponse'
*/
router.post('/reverse/:id/claim', this.handleError(this.claimReverse));

// Deprecated endpoint from first Taproot deployment
router.post('/reverse/claim', this.handleError(this.claimReverse));

/**
Expand Down Expand Up @@ -1055,15 +1070,21 @@ class SwapRouter extends RouterBase {
};

private refundSubmarine = async (req: Request, res: Response) => {
const params = req.params
? validateRequest(req.params, [
{ name: 'id', type: 'string', optional: true },
])
: {};

const { id, pubNonce, index, transaction } = validateRequest(req.body, [
{ name: 'id', type: 'string' },
{ name: 'id', type: 'string', optional: params.id !== undefined },
{ name: 'index', type: 'number' },
{ name: 'pubNonce', type: 'string', hex: true },
{ name: 'transaction', type: 'string', hex: true },
]);

const sig = await this.service.musigSigner.signSwapRefund(
id,
params.id || id,
pubNonce,
transaction,
index,
Expand Down Expand Up @@ -1232,10 +1253,16 @@ class SwapRouter extends RouterBase {
};

private claimReverse = async (req: Request, res: Response) => {
const params = req.params
? validateRequest(req.params, [
{ name: 'id', type: 'string', optional: true },
])
: {};

const { id, preimage, pubNonce, index, transaction } = validateRequest(
req.body,
[
{ name: 'id', type: 'string' },
{ name: 'id', type: 'string', optional: params.id !== undefined },
{ name: 'index', type: 'number' },
{ name: 'preimage', type: 'string', hex: true },
{ name: 'pubNonce', type: 'string', hex: true },
Expand All @@ -1244,7 +1271,7 @@ class SwapRouter extends RouterBase {
);

const sig = await this.service.musigSigner.signReverseSwapClaim(
id,
params.id || id,
preimage,
pubNonce,
transaction,
Expand Down
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -88,7 +88,7 @@
"csv-parse": "^5.5.3",
"discord.js": "^14.14.1",
"ecpair": "^2.1.0",
"ethers": "^6.10.0",
"ethers": "^6.11.0",
"express": "^4.18.2",
"google-protobuf": "^3.21.2",
"ip-address": "^9.0.5",
Expand Down

0 comments on commit 0308974

Please sign in to comment.