Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Added tuumservice
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Oct 30, 2023
1 parent e88fe30 commit d92eb41
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/snap/src/utils/tuumService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import BigNumber from 'bignumber.js';
import {
AccountBalance,
SimpleHederaClient,
SimpleTransfer,
} from 'src/services/hedera';
import { TUUMACCOUNTID } from '../types/constants';

export type QueryCost = {
serviceFee: number;
estimatedCost: number;
maxCost: number;
};

export const calculateHederaQueryFees = (queryCost: BigNumber): QueryCost => {
const serviceFee = queryCost.multipliedBy(0.01); // 1% service fee to Tuum Tech for the query; This can be changed in the future

const estimatedCost = queryCost.plus(serviceFee);

// add a 5% margin to allow for spot fluctuations
const maxCost = estimatedCost.multipliedBy(1.05);

const serviceFeeResult = Number(serviceFee.toFixed(8));
const estimatedCostResult = Number(estimatedCost.toFixed(8));
const maxCostResult = Number(maxCost.toFixed(8));

return {
serviceFee: serviceFeeResult,
estimatedCost: estimatedCostResult,
maxCost: maxCostResult,
} as QueryCost;
};

export const deductServiceFee = async (
currentBalance: AccountBalance,
serviceFee: number,
hederaClient: SimpleHederaClient,
) => {
try {
const transfers: SimpleTransfer[] = [
{
asset: 'HBAR',
to: TUUMACCOUNTID,
amount: serviceFee,
} as SimpleTransfer,
];
await hederaClient.transferCrypto({
currentBalance,
transfers,
memo: null,
maxFee: null,
serviceFees: null,
});
} catch (error: any) {
// eslint-disable-next-line no-empty
}
};

0 comments on commit d92eb41

Please sign in to comment.