Skip to content

Commit

Permalink
feat: query chain swap transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Mar 23, 2024
1 parent 9dbcbf6 commit c1f72d3
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 197 deletions.
4 changes: 2 additions & 2 deletions cliff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ body = """
| filter(attribute="scope")
| sort(attribute="scope") %}
- **({{commit.scope}})**{% if commit.breaking %} [**breaking**]{% endif %} \
{{ commit.message }} - ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }})) - {{ commit.author.name }}
{{ commit.message }} - ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }}))
{%- endfor -%}
{% raw %}\n{% endraw %}\
{%- for commit in commits %}
{%- if commit.scope -%}
{% else -%}
- {% if commit.breaking %} [**breaking**]{% endif %}\
{{ commit.message }} - ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }})) - {{ commit.author.name }}
{{ commit.message }} - ([{{ commit.id | truncate(length=7, end="") }}]($REPO/commit/{{ commit.id }}))
{% endif -%}
{% endfor -%}
{% endfor %}\n
Expand Down
3 changes: 2 additions & 1 deletion lib/api/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ class Controller {
{ name: 'id', type: 'string' },
]);

const response = await this.service.getSwapTransaction(id);
const response =
await this.service.transactionFetcher.getSubmarineTransaction(id);
successResponse(res, response);
} catch (error) {
errorResponse(this.logger, req, res, error);
Expand Down
41 changes: 26 additions & 15 deletions lib/api/v2/routers/SwapRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,10 @@ class SwapRouter extends RouterBase {

router.get('/chain', this.handleError(this.getChain));
router.post('/chain', this.handleError(this.createChain));
router.get(
'/chain/:id/transactions',
this.handleError(this.getChainSwapTransactions),
);
router.get(
'/chain/:id/claim',
this.handleError(this.getChainSwapClaimDetails),
Expand All @@ -975,7 +979,7 @@ class SwapRouter extends RouterBase {

router.get(
'/chain/:id/refund',
// We can use the exact same handler sas for Submarine swaps
// We can use the exact same handler as for Submarine Swaps
this.handleError(this.refundEvm),
);
router.post(
Expand Down Expand Up @@ -1140,7 +1144,7 @@ class SwapRouter extends RouterBase {
]);

const { transactionHex, transactionId, timeoutBlockHeight, timeoutEta } =
await this.service.getSwapTransaction(id);
await this.service.transactionFetcher.getSubmarineTransaction(id);
successResponse(res, {
id: transactionId,
hex: transactionHex,
Expand Down Expand Up @@ -1290,7 +1294,7 @@ class SwapRouter extends RouterBase {
]);

const { transactionHex, transactionId, timeoutBlockHeight } =
await this.service.getReverseSwapTransaction(id);
await this.service.transactionFetcher.getReverseSwapTransaction(id);
successResponse(res, {
id: transactionId,
hex: transactionHex,
Expand Down Expand Up @@ -1408,10 +1412,6 @@ class SwapRouter extends RouterBase {
pubNonce: getHexString(details.pubNonce),
publicKey: getHexString(details.publicKey),
transactionHash: getHexString(details.transactionHash),
lockupTransaction: {
id: details.lockupTransaction.getId(),
hex: details.lockupTransaction.toHex(),
},
});
};

Expand Down Expand Up @@ -1458,6 +1458,23 @@ class SwapRouter extends RouterBase {
});
};

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

const chainSwap = await ChainSwapRepository.getChainSwap({ id });
if (chainSwap === null) {
errorResponse(this.logger, req, res, Errors.SWAP_NOT_FOUND(id), 404);
return;
}

successResponse(
res,
await this.service.transactionFetcher.getChainSwapTransactions(chainSwap),
);
};

private signUtxoRefund =
(signer: MusigSigner | ChainSwapSigner) =>
async (req: Request, res: Response) => {
Expand All @@ -1468,10 +1485,10 @@ class SwapRouter extends RouterBase {
: {};

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

const sig = await signer.signRefund(
Expand Down Expand Up @@ -1507,13 +1524,7 @@ class SwapRouter extends RouterBase {
if (response) {
successResponse(res, response);
} else {
errorResponse(
this.logger,
req,
res,
`could not find swap with id: ${id}`,
404,
);
errorResponse(this.logger, req, res, Errors.SWAP_NOT_FOUND(id), 404);
}
};
}
Expand Down
11 changes: 7 additions & 4 deletions lib/cli/BoltzApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios from 'axios';
import { SwapUpdateEvent } from '../consts/Enums';
import { ChainSwapTransactions } from '../service/TransactionFetcher';

type PartialSignature = {
pubNonce: string;
Expand Down Expand Up @@ -68,16 +69,18 @@ class BoltzApiClient {
})
).data;

public getChainTransactions = async (
swapId: string,
): Promise<ChainSwapTransactions> =>
(await axios.get(`${this.endpoint}/v2/swap/chain/${swapId}/transactions`))
.data;

public getChainSwapClaimDetails = async (
swapId: string,
): Promise<{
pubNonce: string;
publicKey: string;
transactionHash: string;
lockupTransaction: {
id: string;
hex: string;
};
}> =>
(await axios.get(`${this.endpoint}/v2/swap/chain/${swapId}/claim`)).data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ export const handler = async (
argv.blindingKey,
);

const apiClaimDetails = await boltzClient.getChainSwapClaimDetails(
argv.swapId,
);
const [apiClaimDetails, swapTransactions] = await Promise.all([
boltzClient.getChainSwapClaimDetails(argv.swapId),
boltzClient.getChainTransactions(argv.swapId),
]);

const theirClaimType = treeIsBitcoin(refundTree)
? CurrencyType.BitcoinLike
: CurrencyType.Liquid;
Expand All @@ -117,7 +119,10 @@ export const handler = async (
refundTree,
ECPair.fromPrivateKey(getHexBuffer(argv.refundKeys)),
extractClaimPublicKeyFromReverseSwapTree,
parseTransaction(theirClaimType, apiClaimDetails.lockupTransaction.hex),
parseTransaction(
theirClaimType,
swapTransactions.userLock!.transaction.hex!,
),
);
theirClaimDetails.musig.aggregateNonces([
[theirClaimDetails.theirPublicKey, getHexBuffer(apiClaimDetails.pubNonce)],
Expand Down
2 changes: 1 addition & 1 deletion lib/notifications/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class CommandHandler {

if (commandInfo) {
this.logger.debug(
`Executing Discord command: ${command} ${args.join(', ')}`,
`Executing ${this.notificationClient.serviceName} command: ${command} ${args.join(', ')}`,
);
await commandInfo.executor(args);
}
Expand Down
Loading

0 comments on commit c1f72d3

Please sign in to comment.