Skip to content

Commit

Permalink
feat: batch claim Chain Swaps (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 authored Dec 12, 2024
1 parent 51e03ca commit 8978a59
Show file tree
Hide file tree
Showing 17 changed files with 924 additions and 587 deletions.
6 changes: 4 additions & 2 deletions lib/cli/ethereum/commands/Refund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export const handler = async (argv: Arguments<any>): Promise<void> => {
argv.queryStartHeightDelta,
),
);
transaction = await erc20Swap.refund(
transaction = await erc20Swap[
'refund(bytes32,uint256,address,address,uint256)'
](
preimageHash,
erc20SwapValues.amount,
erc20SwapValues.tokenAddress,
Expand All @@ -58,7 +60,7 @@ export const handler = async (argv: Arguments<any>): Promise<void> => {
argv.queryStartHeightDelta,
),
);
transaction = await etherSwap.refund(
transaction = await etherSwap['refund(bytes32,uint256,address,uint256)'](
preimageHash,
etherSwapValues.amount,
etherSwapValues.claimAddress,
Expand Down
16 changes: 16 additions & 0 deletions lib/db/repositories/ChainSwapRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ class ChainSwapRepository {
return swaps.filter((s) => symbols.includes(s.sendingData.symbol));
};

public static getChainSwapsClaimable = () =>
this.getChainSwaps({
status: SwapUpdateEvent.TransactionClaimPending,
});

public static addChainSwap = (args: {
chainSwap: ChainSwapType;
sendingData: ChainSwapDataType;
Expand Down Expand Up @@ -318,6 +323,17 @@ class ChainSwapRepository {
return swap;
});

public static setTransactionClaimPending = async (
swap: ChainSwapInfo,
preimage: Buffer,
) => {
swap.chainSwap = await swap.chainSwap.update({
preimage: getHexString(preimage),
status: SwapUpdateEvent.TransactionClaimPending,
});
return swap;
};

public static setClaimMinerFee = async (
swap: ChainSwapInfo,
preimage: Buffer,
Expand Down
7 changes: 4 additions & 3 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dumpHeap } from '../HeapDump';
import Logger, { LogLevel as BackendLevel } from '../Logger';
import { wait } from '../PromiseUtils';
import { getHexString, getUnixTime } from '../Utils';
import { CurrencyType } from '../consts/Enums';
import { CurrencyType, swapTypeToPrettyString } from '../consts/Enums';
import TransactionLabelRepository from '../db/repositories/TransactionLabelRepository';
import * as boltzrpc from '../proto/boltzrpc_pb';
import { LogLevel } from '../proto/boltzrpc_pb';
Expand Down Expand Up @@ -271,8 +271,9 @@ class GrpcService {
swapToClaim
.map((toClaim) => {
const pendingSweep = new boltzrpc.PendingSweep();
pendingSweep.setSwapId(toClaim.swap.id);
pendingSweep.setOnchainAmount(toClaim.swap.onchainAmount || 0);
pendingSweep.setSwapId(toClaim.id);
pendingSweep.setOnchainAmount(toClaim.onchainAmount || 0);
pendingSweep.setType(swapTypeToPrettyString(toClaim.type));
return pendingSweep;
})
.forEach((pendingSweep) =>
Expand Down
3 changes: 3 additions & 0 deletions lib/proto/boltzrpc_pb.d.ts

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

32 changes: 31 additions & 1 deletion lib/proto/boltzrpc_pb.js

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

3 changes: 1 addition & 2 deletions lib/service/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ import TimeoutDeltaProvider, {
} from './TimeoutDeltaProvider';
import TransactionFetcher from './TransactionFetcher';
import { calculateTimeoutDate, getCurrency } from './Utils';
import { SwapToClaimPreimage } from './cooperative/DeferredClaimer';
import MusigSigner from './cooperative/MusigSigner';

type NetworkContracts = {
Expand Down Expand Up @@ -999,7 +998,7 @@ class Service {
return res;
};

public getPendingSweeps = (): Map<string, SwapToClaimPreimage[]> => {
public getPendingSweeps = () => {
return this.swapManager.deferredClaimer.pendingSweepsValues();
};

Expand Down
14 changes: 7 additions & 7 deletions lib/service/cooperative/CoopSignerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getHexBuffer } from '../../Utils';
import { IChainClient } from '../../chain/ChainClient';
import { SwapType, swapTypeToPrettyString } from '../../consts/Enums';
import TypedEventEmitter from '../../consts/TypedEventEmitter';
import { AnySwap } from '../../consts/Types';
import ChainSwapData from '../../db/models/ChainSwapData';
import Swap from '../../db/models/Swap';
import { ChainSwapInfo } from '../../db/repositories/ChainSwapRepository';
Expand Down Expand Up @@ -82,7 +83,7 @@ abstract class CoopSignerBase<
await this.constructClaimDetails(
chainCurrency.chainClient!,
wallet,
toClaim,
toClaim.swap,
),
] as ClaimDetails[] | LiquidClaimDetails[],
address,
Expand All @@ -104,17 +105,16 @@ abstract class CoopSignerBase<
/**
* Cooperative when the preimage is undefined
*/
protected constructClaimDetails = async (
protected constructClaimDetails = async <T extends AnySwap>(
chainClient: IChainClient,
wallet: Wallet,
toClaim: SwapToClaim<T>,
toClaim: T,
preimage?: Buffer,
): Promise<ClaimDetails | LiquidClaimDetails> => {
const { swap } = toClaim;
const details =
swap.type === SwapType.Submarine
? (swap as Swap)
: (swap as ChainSwapInfo).receivingData;
toClaim.type === SwapType.Submarine
? (toClaim as Swap)
: (toClaim as ChainSwapInfo).receivingData;

const tx = parseTransaction(
wallet.type,
Expand Down
Loading

0 comments on commit 8978a59

Please sign in to comment.