Skip to content

Commit

Permalink
feat: add gas calculations to the response of the tx (#76)
Browse files Browse the repository at this point in the history
* fix:prettier

* fix:linter

* fix: Increate default surplus to the gas calculation

* fix: change return to null and typenames

* fix: auto check if has a pending approvals

* feat: gas calculation

* fix: apply changes on gas calculations

* feat: change limit

* fix: remove package-lock
  • Loading branch information
damarnez authored Feb 16, 2021
1 parent 095a878 commit 3ee1934
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 8 deletions.
36 changes: 36 additions & 0 deletions src/tx-builder/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,46 @@ export const uniswapEthAmount = '0.1';
export const SURPLUS = '0.05';

export const gasLimitRecommendations: GasRecommendationType = {
[ProtocolAction.default]: {
limit: '210000',
recommended: '210000',
},
[ProtocolAction.deposit]: {
limit: '300000',
recommended: '300000',
},
[ProtocolAction.withdraw]: {
limit: '230000',
recommended: '300000',
},
[ProtocolAction.liquidationCall]: {
limit: '700000',
recommended: '700000',
},
[ProtocolAction.liquidationFlash]: {
limit: '995000',
recommended: '995000',
},
[ProtocolAction.repay]: {
limit: '300000',
recommended: '300000',
},
[ProtocolAction.borrowETH]: {
limit: '450000',
recommended: '450000',
},
[ProtocolAction.withdrawETH]: {
limit: '640000',
recommended: '640000',
},
[ProtocolAction.swapCollateral]: {
limit: '700000',
recommended: '700000',
},
[ProtocolAction.repayCollateral]: {
limit: '700000',
recommended: '700000',
},
};

export const distinctStakingAddressesBetweenTokens: StakingConfigType = {
Expand Down
1 change: 1 addition & 0 deletions src/tx-builder/services/BaseDebtToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default class BaseDebtToken
return {
tx: txCallback,
txType: eEthereumTxType.ERC20_APPROVAL,
gas: this.generateTxPriceEstimation([], txCallback),
};
}

Expand Down
46 changes: 45 additions & 1 deletion src/tx-builder/services/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import {
tEthereumAddress,
TransactionGenerationMethod,
transactionType,
GasResponse,
ProtocolAction,
EthereumTransactionTypeExtended,
eEthereumTxType,
} from '../types';
import { ContractsFactory } from '../interfaces/ContractsFactory';
import { estimateGas } from '../utils/gasStation';
import { estimateGas, getGasPrice } from '../utils/gasStation';
import { DEFAULT_NULL_VALUE_ON_TX, gasLimitRecommendations } from '../config';

export default class BaseService<T extends Contract> {
Expand Down Expand Up @@ -61,4 +65,44 @@ export default class BaseService<T extends Contract> {

return tx;
};

readonly generateTxPriceEstimation = (
txs: EthereumTransactionTypeExtended[],
txCallback: () => Promise<transactionType>,
action: string = ProtocolAction.default
): GasResponse => async () => {
try {
const gasPrice = await getGasPrice(this.config);
const hasPendingApprovals = txs.find(
(tx) => tx.txType === eEthereumTxType.ERC20_APPROVAL
);
if (!hasPendingApprovals) {
const {
gasLimit,
gasPrice: gasPriceProv,
}: transactionType = await txCallback();
if (!gasLimit) {
// If we don't recieve the correct gas we throw a error
throw new Error('Transaction calculation error');
}

return {
gasLimit: gasLimit.toString(),
gasPrice: gasPriceProv
? gasPriceProv.toString()
: gasPrice.toString(),
};
}
return {
gasLimit: gasLimitRecommendations[action].recommended,
gasPrice: gasPrice.toString(),
};
} catch (error) {
console.error(
'Calculate error on calculate estimation gas price.',
error
);
return null;
}
};
}
1 change: 1 addition & 0 deletions src/tx-builder/services/ERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default class ERC20Service
return {
tx: txCallback,
txType: eEthereumTxType.ERC20_APPROVAL,
gas: this.generateTxPriceEstimation([], txCallback),
};
};

Expand Down
1 change: 1 addition & 0 deletions src/tx-builder/services/Faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class FaucetService
{
tx: txCallback,
txType: eEthereumTxType.FAUCET_MINT,
gas: this.generateTxPriceEstimation([], txCallback),
},
];
}
Expand Down
1 change: 1 addition & 0 deletions src/tx-builder/services/LTAMigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default class LTAMigratorService
txs.push({
txType: eEthereumTxType.MIGRATION_LEND_AAVE,
tx: txCallback,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down
1 change: 1 addition & 0 deletions src/tx-builder/services/LiquiditySwapAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default class LiquiditySwapAdapterService
return {
tx: txCallback,
txType: eEthereumTxType.DLP_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
};
}
}
6 changes: 5 additions & 1 deletion src/tx-builder/services/RepayWithCollateralAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export default class RepayWithCollateralAdapterService
from: user,
});

return { tx: txCallback, txType: eEthereumTxType.DLP_ACTION };
return {
tx: txCallback,
txType: eEthereumTxType.DLP_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
};
}
}
5 changes: 5 additions & 0 deletions src/tx-builder/services/Staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export default class StakingService
txs.push({
tx: txCallback,
txType: eEthereumTxType.STAKE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down Expand Up @@ -214,6 +215,7 @@ export default class StakingService
txs.push({
tx: txCallback,
txType: eEthereumTxType.STAKE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down Expand Up @@ -249,6 +251,7 @@ export default class StakingService
{
tx: txCallback,
txType: eEthereumTxType.STAKE_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
},
];
}
Expand All @@ -270,6 +273,7 @@ export default class StakingService
{
tx: txCallback,
txType: eEthereumTxType.STAKE_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
},
];
}
Expand Down Expand Up @@ -303,6 +307,7 @@ export default class StakingService
{
tx: txCallback,
txType: eEthereumTxType.STAKE_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
},
];
}
Expand Down
13 changes: 13 additions & 0 deletions src/tx-builder/services/WETHGateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
eEthereumTxType,
EthereumTransactionTypeExtended,
InterestRate,
ProtocolAction,
transactionType,
tStringDecimalUnits,
} from '../types';
Expand Down Expand Up @@ -81,6 +82,7 @@ export default class WETHGatewayService
{
tx: txCallback,
txType: eEthereumTxType.DLP_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
},
];
}
Expand Down Expand Up @@ -136,6 +138,11 @@ export default class WETHGatewayService
txs.push({
tx: txCallback,
txType: eEthereumTxType.DLP_ACTION,
gas: this.generateTxPriceEstimation(
txs,
txCallback,
ProtocolAction.borrowETH
),
});

return txs;
Expand Down Expand Up @@ -188,6 +195,11 @@ export default class WETHGatewayService
txs.push({
tx: txCallback,
txType: eEthereumTxType.DLP_ACTION,
gas: this.generateTxPriceEstimation(
txs,
txCallback,
ProtocolAction.withdrawETH
),
});

return txs;
Expand Down Expand Up @@ -221,6 +233,7 @@ export default class WETHGatewayService
{
tx: txCallback,
txType: eEthereumTxType.DLP_ACTION,
gas: this.generateTxPriceEstimation([], txCallback),
},
];
}
Expand Down
6 changes: 6 additions & 0 deletions src/tx-builder/services/v2/AaveGovernanceV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export default class AaveGovernanceV2Service
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOVERNANCE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});
return txs;
}
Expand All @@ -194,6 +195,7 @@ export default class AaveGovernanceV2Service
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOVERNANCE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});
return txs;
}
Expand All @@ -217,6 +219,7 @@ export default class AaveGovernanceV2Service
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOVERNANCE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});
return txs;
}
Expand All @@ -240,6 +243,7 @@ export default class AaveGovernanceV2Service
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOVERNANCE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});
return txs;
}
Expand All @@ -264,6 +268,7 @@ export default class AaveGovernanceV2Service
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOVERNANCE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});
return txs;
}
Expand Down Expand Up @@ -328,6 +333,7 @@ export default class AaveGovernanceV2Service
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOVERNANCE_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});
return txs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class GovernanceDelegationTokenService
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOV_DELEGATION_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down Expand Up @@ -98,6 +99,7 @@ export default class GovernanceDelegationTokenService
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOV_DELEGATION_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down Expand Up @@ -135,6 +137,7 @@ export default class GovernanceDelegationTokenService
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOV_DELEGATION_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down Expand Up @@ -180,6 +183,7 @@ export default class GovernanceDelegationTokenService
txs.push({
tx: txCallback,
txType: eEthereumTxType.GOV_DELEGATION_ACTION,
gas: this.generateTxPriceEstimation(txs, txCallback),
});

return txs;
Expand Down
Loading

0 comments on commit 3ee1934

Please sign in to comment.