From 94a48543660f72252c3dde5a06cc91552fc662d0 Mon Sep 17 00:00:00 2001 From: kraikov Date: Tue, 18 Aug 2020 18:12:03 +0300 Subject: [PATCH] Add transferFee estimation. --- packages/aeternity/src/fee.ts | 19 +++++++++++++++++++ packages/bitcoin/src/fee.ts | 16 ++++++++++++++++ packages/erc20/src/fee.ts | 23 +++++++++++++++++++++++ packages/ethereum/src/fee.ts | 16 ++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 packages/aeternity/src/fee.ts create mode 100644 packages/bitcoin/src/fee.ts create mode 100644 packages/erc20/src/fee.ts create mode 100644 packages/ethereum/src/fee.ts diff --git a/packages/aeternity/src/fee.ts b/packages/aeternity/src/fee.ts new file mode 100644 index 0000000..1d01645 --- /dev/null +++ b/packages/aeternity/src/fee.ts @@ -0,0 +1,19 @@ +import BigNumber from 'bignumber.js'; +import { TxBuilder } from '@aeternity/aepp-sdk/es'; + +const MAX_UINT = '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'; +const TO = 'ak_2cjPjJJfKSosxSs3HY9mU7CW5DfAC1ywxzzps8EzaR3rpUVeTH'; + +export const tranferFee = async (provider: any, balance: any) => { + const MAGNITUDE = 18; + + const MIN_FEE = TxBuilder.calculateMinFee('spendTx', { senderId: TO, amount: MAX_UINT }); + + const txFee = new BigNumber(MIN_FEE).shiftedBy(-MAGNITUDE).toString(); + + const amount = new BigNumber(balance).minus(txFee).toString(); + + const maxAmount = balance; + + return { txFee, amount, maxAmount, feeAsset: 'AE', gasPrice: 1000000000, gasLimit: 15000 }; +}; diff --git a/packages/bitcoin/src/fee.ts b/packages/bitcoin/src/fee.ts new file mode 100644 index 0000000..2cb7177 --- /dev/null +++ b/packages/bitcoin/src/fee.ts @@ -0,0 +1,16 @@ +import BigNumber from 'bignumber.js'; + +export const tranferFee = async (provider: any, balance: any) => { + const feePerByte = await provider.getFeePerByte(); + + const vBytes = 160; // approx + + const txFee = new BigNumber(feePerByte).multipliedBy(vBytes).div(100000000).toString(); + + const amount = balance; + + const maxAmount = new BigNumber(balance).minus(txFee).toString(); + + // to keep consistency use gasPrice and gasLimit as terms + return { txFee, amount, maxAmount, feeAsset: 'BTC', gasPrice: feePerByte, gasLimit: vBytes }; +}; diff --git a/packages/erc20/src/fee.ts b/packages/erc20/src/fee.ts new file mode 100644 index 0000000..ee70a93 --- /dev/null +++ b/packages/erc20/src/fee.ts @@ -0,0 +1,23 @@ +import { utils, Contract } from 'ethers'; +import BigNumber from 'bignumber.js'; + +import ERC20ABI from './config/abi/erc20'; + +const EXTRA_GAS = 20000; +const TO = '0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF'; + +export const tranferFee = async (provider: any, balance: any, tokenAddress: string) => { + const contract = new Contract(tokenAddress, ERC20ABI, provider); + + const gasPrice = await provider.getGasPrice(); + + const gasLimit = (await contract.estimate.transfer(TO, Number(balance))).add(EXTRA_GAS); + + const txFee = utils.formatEther(gasPrice.mul(gasLimit)); + + const amount = new BigNumber(balance).minus(txFee); + + const maxAmount = balance; + + return { txFee, amount, maxAmount, feeAsset: 'ETH', gasPrice, gasLimit }; +}; diff --git a/packages/ethereum/src/fee.ts b/packages/ethereum/src/fee.ts new file mode 100644 index 0000000..0d5fb8a --- /dev/null +++ b/packages/ethereum/src/fee.ts @@ -0,0 +1,16 @@ +import { utils } from 'ethers'; +import BigNumber from 'bignumber.js'; + +export const tranferFee = async (provider: any, balance: any) => { + const gasPrice = await provider.getGasPrice(); + + const gasLimit = 21000; + + const txFee = utils.formatEther(gasPrice.mul(gasLimit)); + + const amount = new BigNumber(balance).minus(txFee).toString(); + + const maxAmount = amount; + + return { txFee, amount, maxAmount, feeAsset: 'ETH', gasPrice, gasLimit }; +};