Skip to content

Commit

Permalink
Merge pull request #829 from BoltzExchange/pending-evm-txs-grpc
Browse files Browse the repository at this point in the history
feat: gRPC method to get pending EVM transactions
  • Loading branch information
michael1011 authored Feb 28, 2025
2 parents 68b4fc8 + f89becc commit b36d12b
Show file tree
Hide file tree
Showing 24 changed files with 1,566 additions and 267 deletions.
3 changes: 3 additions & 0 deletions lib/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,6 @@ export const bigIntMax = (a: bigint, b: bigint) => (a > b ? a : b);

export const roundToDecimals = (value: number, decimals: number): number =>
Number(value.toFixed(decimals));

export const removeHexPrefix = (hex: string): string =>
hex.startsWith('0x') ? hex.substring(2) : hex;
27 changes: 27 additions & 0 deletions lib/cli/commands/GetPendingEvmTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Arguments } from 'yargs';
import { getHexString } from '../../Utils';
import { GetPendingEvmTransactionsRequest } from '../../proto/boltzrpc_pb';
import { ApiType, BuilderTypes } from '../BuilderComponents';
import { callback, loadBoltzClient } from '../Command';

export const command = 'pendingevmtransactions';

export const describe = 'get pending EVM transactions';

export const builder = {};

export const handler = (
argv: Arguments<BuilderTypes<typeof builder> & ApiType>,
) => {
const request = new GetPendingEvmTransactionsRequest();
loadBoltzClient(argv).getPendingEvmTransactions(
request,
callback((res) => {
return res.getTransactionsList().map((tx) => ({
...tx.toObject(),
hash: getHexString(Buffer.from(tx.getHash() as string, 'base64')),
hex: getHexString(Buffer.from(tx.getHex() as string, 'base64')),
}));
}),
);
};
4 changes: 2 additions & 2 deletions lib/db/models/PendingEthereumTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DataTypes, Model, Sequelize } from 'sequelize';
type PendingEthereumTransactionType = {
hash: string;
nonce: number;
etherAmount: bigint;
etherAmount: number;
hex: string;
};

Expand All @@ -13,7 +13,7 @@ class PendingEthereumTransaction
{
public hash!: string;
public nonce!: number;
public etherAmount!: bigint;
public etherAmount!: number;
public hex!: string;

public static load = (sequelize: Sequelize): void => {
Expand Down
1 change: 1 addition & 0 deletions lib/grpc/GrpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GrpcServer {
getLockedFunds: grpcService.getLockedFunds,
getPendingSweeps: grpcService.getPendingSweeps,
getLabel: grpcService.getLabel,
getPendingEvmTransactions: grpcService.getPendingEvmTransactions,
setLogLevel: grpcService.setLogLevel,
calculateTransactionFee: grpcService.calculateTransactionFee,
getReferrals: grpcService.getReferrals,
Expand Down
56 changes: 55 additions & 1 deletion lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ import { parseTransaction } from '../Core';
import { dumpHeap } from '../HeapDump';
import Logger, { LogLevel as BackendLevel } from '../Logger';
import { wait } from '../PromiseUtils';
import { getHexString, getUnixTime, stringify } from '../Utils';
import {
getHexBuffer,
getHexString,
getUnixTime,
removeHexPrefix,
stringify,
} from '../Utils';
import { CurrencyType, swapTypeToPrettyString } from '../consts/Enums';
import Referral, { ReferralConfig } from '../db/models/Referral';
import PendingEthereumTransactionRepository from '../db/repositories/PendingEthereumTransactionRepository';
import ReferralRepository from '../db/repositories/ReferralRepository';
import TransactionLabelRepository from '../db/repositories/TransactionLabelRepository';
import * as boltzrpc from '../proto/boltzrpc_pb';
import { LogLevel } from '../proto/boltzrpc_pb';
import Service from '../service/Service';
import Sidecar from '../sidecar/Sidecar';
import { Rsk } from '../wallet/ethereum/EvmNetworks';

class GrpcService {
constructor(
Expand Down Expand Up @@ -380,6 +388,52 @@ class GrpcService {
});
};

public getPendingEvmTransactions: handleUnaryCall<
boltzrpc.GetPendingEvmTransactionsRequest,
boltzrpc.GetPendingEvmTransactionsResponse
> = async (call, callback) => {
await this.handleCallback(call, callback, async () => {
const response = new boltzrpc.GetPendingEvmTransactionsResponse();
const txsGrpcList = response.getTransactionsList();

for (const tx of await PendingEthereumTransactionRepository.getTransactions()) {
const txGrpc =
new boltzrpc.GetPendingEvmTransactionsResponse.Transaction();

const symbol = Rsk.symbol;

txGrpc.setSymbol(symbol);
txGrpc.setHash(getHexBuffer(removeHexPrefix(tx.hash)));
txGrpc.setHex(getHexBuffer(removeHexPrefix(tx.hex)));
txGrpc.setNonce(tx.nonce);
txGrpc.setAmountSent(tx.etherAmount.toString());

{
const label = await TransactionLabelRepository.getLabel(tx.hash);
if (label !== null) {
txGrpc.setLabel(label.label);
}
}

{
const manager = this.service.walletManager.ethereumManagers.find(
(m) => m.hasSymbol(symbol),
);
if (manager !== undefined) {
const received = await manager.getClaimedAmount(tx.hex);
if (received !== undefined) {
txGrpc.setAmountReceived(received.toString());
}
}
}

txsGrpcList.push(txGrpc);
}

return response;
});
};

public setLogLevel: handleUnaryCall<
boltzrpc.SetLogLevelRequest,
boltzrpc.SetLogLevelResponse
Expand Down
17 changes: 17 additions & 0 deletions lib/proto/boltzrpc_grpc_pb.d.ts

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

35 changes: 34 additions & 1 deletion lib/proto/boltzrpc_grpc_pb.js

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

89 changes: 89 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.

Loading

0 comments on commit b36d12b

Please sign in to comment.