Skip to content

Commit

Permalink
feat: gRPC method to calculate transaction fees
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Dec 27, 2024
1 parent 5abb19a commit ddecc2a
Show file tree
Hide file tree
Showing 14 changed files with 931 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Transaction, crypto } from 'bitcoinjs-lib';
import { OutputType, Scripts } from 'boltz-core';
import { randomBytes } from 'crypto';
import { ContractTransactionResponse, getAddress } from 'ethers';
import {
ContractTransactionResponse,
TransactionReceipt,
TransactionResponse,
getAddress,
} from 'ethers';
import { Transaction as LiquidTransaction, confidential } from 'liquidjs-lib';
import os from 'os';
import path from 'path';
Expand Down Expand Up @@ -551,7 +556,7 @@ export const calculateLiquidTransactionFee = (
* Calculates the transaction fee of an Ethereum contract interaction and rounds it to 10 ** -8 decimals
*/
export const calculateEthereumTransactionFee = (
transaction: ContractTransactionResponse,
transaction: TransactionResponse | ContractTransactionResponse,
): number => {
return Number(
(transaction.gasLimit! *
Expand All @@ -562,6 +567,12 @@ export const calculateEthereumTransactionFee = (
);
};

export const calculateEthereumTransactionFeeWithReceipt = (
receipt: TransactionReceipt,
): number => {
return Number((receipt.gasUsed * receipt.gasPrice) / etherDecimals);
};

/**
* Hashes an arbitrary UTF-8 string with SHA256
*
Expand Down
42 changes: 42 additions & 0 deletions lib/cli/commands/CalculateTransactionFee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Arguments } from 'yargs';
import { CalculateTransactionFeeRequest } from '../../proto/boltzrpc_pb';
import BuilderComponents, { ApiType, BuilderTypes } from '../BuilderComponents';
import { callback, loadBoltzClient } from '../Command';

export const command = 'txfee <symbol> <transactionId>';

export const describe = 'calculate the fee of a transaction';

export const builder = {
symbol: BuilderComponents.symbol,
transactionId: {
type: 'string',
describe: 'ID or hash of the transaction',
},
};

export const handler = async (
argv: Arguments<BuilderTypes<typeof builder> & ApiType>,
) => {
const request = new CalculateTransactionFeeRequest();
request.setSymbol(argv.symbol);
request.setTransactionId(argv.transactionId);

loadBoltzClient(argv).calculateTransactionFee(
request,
callback((res) => {
const data: Record<string, number> = {
absolute: res.getAbsolute(),
};

if (res.hasSatPerVbyte()) {
data.satPerVbyte = res.getSatPerVbyte();
}
if (res.hasGwei()) {
data.gwei = res.getGwei();
}

return data;
}),
);
};
1 change: 1 addition & 0 deletions lib/grpc/GrpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class GrpcServer {
getPendingSweeps: grpcService.getPendingSweeps,
getLabel: grpcService.getLabel,
setLogLevel: grpcService.setLogLevel,
calculateTransactionFee: grpcService.calculateTransactionFee,
});
}

Expand Down
24 changes: 24 additions & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,30 @@ class GrpcService {
});
};

public calculateTransactionFee: handleUnaryCall<
boltzrpc.CalculateTransactionFeeRequest,
boltzrpc.CalculateTransactionFeeResponse
> = async (call, callback) => {
await this.handleCallback(call, callback, async () => {
const { symbol, transactionId } = call.request.toObject();

const { absolute, satPerVbyte, gwei } =
await this.service.calculateTransactionFee(symbol, transactionId);

const res = new boltzrpc.CalculateTransactionFeeResponse();
res.setAbsolute(absolute);

if (satPerVbyte !== undefined) {
res.setSatPerVbyte(satPerVbyte);
}
if (gwei !== undefined) {
res.setGwei(gwei);
}

return res;
});
};

private handleCallback = async <R, T>(
call: R,
callback: (error: any, res: T | null) => void,
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.

33 changes: 33 additions & 0 deletions lib/proto/boltzrpc_grpc_pb.js

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

64 changes: 64 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 ddecc2a

Please sign in to comment.