Skip to content

Commit

Permalink
Fix estimated fees in the legacy API
Browse files Browse the repository at this point in the history
  • Loading branch information
skubarenko committed Aug 17, 2022
1 parent ae8fad7 commit 84d7c0a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "atomex-sdk",
"version": "0.5.5",
"version": "0.5.6",
"description": "Atomex SDK",
"engines": {
"node": ">=16.14.0",
Expand Down
8 changes: 6 additions & 2 deletions src/legacy/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
39 changes: 17 additions & 22 deletions src/legacy/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ export class EthereumHelpers extends Helpers {
private _contract: Contract;
private _timeBetweenBlocks: number;
private _functions: Map<string, Function>;
private _gasLimit: number;
private _initiateGasLimitWithoutReward: number;
private _initiateGasLimitWithReward: number;
private _redeemGasLimit: number;

constructor(
atomex: Atomex,
web3: Web3,
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<string, Function>();
this.initializeFunctions(jsonInterface);
}
Expand Down Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -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<number> {
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<number> {
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<RedeemFees> {
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,
Expand Down
4 changes: 3 additions & 1 deletion tests/testHelpers/testLegacyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

0 comments on commit 84d7c0a

Please sign in to comment.