From 84d7c0ae136354f920ffeab5d3b6f7adeacf7ac5 Mon Sep 17 00:00:00 2001 From: Andrew Skubarenko Date: Wed, 17 Aug 2022 12:23:56 +0300 Subject: [PATCH] Fix estimated fees in the legacy API --- package-lock.json | 4 +-- package.json | 2 +- src/legacy/config.ts | 8 ++++-- src/legacy/ethereum.ts | 39 +++++++++++--------------- tests/testHelpers/testLegacyHelpers.ts | 4 ++- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index 567f35b7..27b2ce79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "atomex-sdk", - "version": "0.5.5", + "version": "0.5.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "atomex-sdk", - "version": "0.5.5", + "version": "0.5.6", "license": "GPLv3", "dependencies": { "@taquito/beacon-wallet": "^13.0.0", diff --git a/package.json b/package.json index e60cf3b2..c710f204 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "atomex-sdk", - "version": "0.5.5", + "version": "0.5.6", "description": "Atomex SDK", "engines": { "node": ">=16.14.0", diff --git a/src/legacy/config.ts b/src/legacy/config.ts index 38725ed5..76531961 100644 --- a/src/legacy/config.ts +++ b/src/legacy/config.ts @@ -53,11 +53,15 @@ export default { contracts: { mainnet: { address: '0xe9c251cbb4881f9e056e40135e7d3ea9a7d037df', - gasLimit: 200000 + initiateGasLimitWithoutReward: 200000, + initiateGasLimitWithReward: 210000, + redeemGasLimit: 140000 }, testnet: { address: '0x512fe6B803bA327DCeFBF2Cec7De333f761B0f2b', - gasLimit: 170000 + initiateGasLimitWithoutReward: 200000, + initiateGasLimitWithReward: 200000, + redeemGasLimit: 140000 }, abi: [ { diff --git a/src/legacy/ethereum.ts b/src/legacy/ethereum.ts index b34f204c..7363a5b8 100644 --- a/src/legacy/ethereum.ts +++ b/src/legacy/ethereum.ts @@ -30,7 +30,9 @@ export class EthereumHelpers extends Helpers { private _contract: Contract; private _timeBetweenBlocks: number; private _functions: Map; - private _gasLimit: number; + private _initiateGasLimitWithoutReward: number; + private _initiateGasLimitWithReward: number; + private _redeemGasLimit: number; constructor( atomex: Atomex, @@ -38,13 +40,17 @@ export class EthereumHelpers extends Helpers { jsonInterface: AbiItem[], contractAddress: string, timeBetweenBlocks: number, - gasLimit: number, + initiateGasLimitWithoutReward: number, + initiateGasLimitWithReward: number, + redeemGasLimit: number, ) { super(atomex); this._web3 = web3; this._contract = this.createContract(jsonInterface, contractAddress); this._timeBetweenBlocks = timeBetweenBlocks; - this._gasLimit = gasLimit; + this._initiateGasLimitWithoutReward = initiateGasLimitWithoutReward; + this._initiateGasLimitWithReward = initiateGasLimitWithReward; + this._redeemGasLimit = redeemGasLimit; this._functions = new Map(); this.initializeFunctions(jsonInterface); } @@ -92,7 +98,9 @@ export class EthereumHelpers extends Helpers { config.currencies.ETH.contracts.abi as AbiItem[], config.currencies.ETH.contracts[network].address, networkSettings.blockTime, - config.currencies.ETH.contracts[network].gasLimit, + config.currencies.ETH.contracts[network].initiateGasLimitWithoutReward, + config.currencies.ETH.contracts[network].initiateGasLimitWithReward, + config.currencies.ETH.contracts[network].redeemGasLimit, ); } @@ -314,30 +322,17 @@ export class EthereumHelpers extends Helpers { return vrs.r.padStart(64, '0') + vrs.s.padStart(64, '0'); } - async estimateInitiateFees(source: string): Promise { - const dummyTx = { - receivingAddress: '0x0000000000000000000000000000000000000000', - secretHash: - '0000000000000000000000000000000000000000000000000000000000000000', - refundTimestamp: 2147483647, - rewardForRedeem: new BigNumber(0), - netAmount: new BigNumber(0), - }; - const txData = this.buildInitiateTransaction(dummyTx); + async estimateInitiateFees(_source: string): Promise { const gasPrice = await this._web3.eth.getGasPrice(); - const gasEstimate = await this._web3.eth.estimateGas({ - from: source, - to: txData.contractAddr, - data: txData.data, - value: txData.amount?.toString(10), - }); - const fee = parseInt(gasPrice) * gasEstimate; + const fee = parseInt(gasPrice) * this._initiateGasLimitWithReward * 1.2; + return fee; } async estimateRedeemFees(_recipient: string): Promise { const gasPrice = await this._web3.eth.getGasPrice(); - const fee = parseInt(gasPrice) * this._gasLimit; + const fee = parseInt(gasPrice) * this._redeemGasLimit; + return { totalCost: fee, rewardForRedeem: 2 * fee, diff --git a/tests/testHelpers/testLegacyHelpers.ts b/tests/testHelpers/testLegacyHelpers.ts index 4f754021..b17447ec 100644 --- a/tests/testHelpers/testLegacyHelpers.ts +++ b/tests/testHelpers/testLegacyHelpers.ts @@ -15,7 +15,9 @@ export const createEthereumHelpers = ( config.currencies.ETH.contracts.abi as AbiItem[], config.currencies.ETH.contracts[network].address, config.blockchains.ethereum.rpc[network].blockTime, - config.currencies.ETH.contracts[network].gasLimit + config.currencies.ETH.contracts[network].initiateGasLimitWithoutReward, + config.currencies.ETH.contracts[network].initiateGasLimitWithReward, + config.currencies.ETH.contracts[network].redeemGasLimit )); export const createTezoselpers = (