Skip to content

Commit

Permalink
Merge branch 'gasBufferLogging' of github.com:leolambo/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Sep 9, 2024
2 parents 241a89d + f19095e commit 642c812
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
9 changes: 7 additions & 2 deletions packages/bitcore-wallet-service/src/lib/chain/eth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ export class EthChain implements IChain {
let outputAddresses = []; // Parameter for MuliSend contract
let outputAmounts = []; // Parameter for MuliSend contract
let totalValue = toBN(0); // Parameter for MuliSend contract

logger.info(`getFee for address ${from} on network ${network} and chain ${chain}`);
logger.info('getFee.opts: %o', { from, txType, priorityFeePercentile, gasLimitBuffer });
for (let output of opts.outputs) {
if (opts.multiSendContractAddress) {
outputAddresses.push(output.toAddress);
Expand Down Expand Up @@ -227,7 +228,8 @@ export class EthChain implements IChain {
gasLimit = inGasLimit || defaultGasLimit;
fee += feePerKb * gasLimit;
}

logger.info(`[${from}] Add gas limit buffer?: ${!!gasLimitBuffer}`);
logger.info(`[${from}] Current gas limit: ${gasLimit}`);
if (opts.multiSendContractAddress) {
try {
const data = this.encodeContractParameters(
Expand All @@ -246,15 +248,18 @@ export class EthChain implements IChain {
data,
gasPrice
});
logger.info(`[${from}] Estimated gas limit: ${gasLimit}`);
} catch (error) {
logger.error('Error estimating gas for MultiSend contract: %o', error);
}
const buffer = gasLimitBuffer ? gasLimitBuffer / 100 : Defaults.MS_GAS_LIMIT_BUFFER_PERCENT;
gasLimit = gasLimit ? gasLimit : inGasLimit;
gasLimit += Math.ceil(gasLimit * buffer); // add gas limit buffer
fee += feePerKb * gasLimit;
logger.info(`[${from}] Gas limit with buffer: ${gasLimit}`);
} else if (gasLimitBuffer) {
gasLimit += Math.ceil(gasLimit * (gasLimitBuffer / 100));
logger.info(`[${from}] Gas limit with buffer: ${gasLimit}`);
}
if (Number(txType) === 2) {
maxGasFee = await server.estimateFee({ network, chain: wallet.chain || coin, txType: 2 });
Expand Down
50 changes: 28 additions & 22 deletions packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2242,15 +2242,15 @@ export class WalletService implements IWalletService {
} catch (addrErr) {
return addrErr;
}

if (!checkRequired(output, ['toAddress', 'amount'])) {
return new ClientError('Argument missing in output #' + (i + 1) + '.');
}

if (!ChainService.checkValidTxAmount(wallet.chain, output)) {
return new ClientError('Invalid amount');
}

const error = ChainService.checkDust(wallet.chain, output, opts);
if (error) return error;
output.valid = true;
Expand Down Expand Up @@ -2580,9 +2580,15 @@ export class WalletService implements IWalletService {
return next();
},
async next => {
logger.info('Calculating fee for new tx: %o', {
from: opts.from, fee: opts.fee, input: opts.inputs?.length, gasLimit: opts.gasLimit, gasLimitBuffer: opts.gasLimitBuffer
});
if (!isNaN(opts.fee) && (opts.inputs || []).length > 0) return next();
try {
({ feePerKb, gasPrice, maxGasFee, priorityGasFee, gasLimit, fee } = await ChainService.getFee(this, wallet, opts));
logger.info('ChainService.getFee return value %o', {
from: opts.from, feePerKb, gasPrice, maxGasFee, priorityGasFee, gasLimit, fee
});
} catch (error) {
return next(error);
}
Expand Down Expand Up @@ -2702,7 +2708,7 @@ export class WalletService implements IWalletService {
},
next => {
if (!txp.multiSendContractAddress || !txp.tokenAddress) {
return next();
return next();
}
// Check that the multisend contract is approved in the token contract for the total amount
const bc = this._getBlockchainExplorer(wallet.chain, wallet.network);
Expand Down Expand Up @@ -6167,24 +6173,24 @@ export class WalletService implements IWalletService {
'x-api-key': API_KEY
};

let qs = [];
if (!checkRequired(req.body, ['sellAsset', 'buyAsset', 'sellAmount'])) {
return reject(new ClientError("Thorswap's request missing arguments"));
}
qs.push('sellAsset=' + req.body.sellAsset);
qs.push('buyAsset=' + req.body.buyAsset);
qs.push('sellAmount=' + req.body.sellAmount);
if (req.body.senderAddress) qs.push('senderAddress=' + req.body.senderAddress);
if (req.body.recipientAddress) qs.push('recipientAddress=' + req.body.recipientAddress);
if (req.body.slippage) qs.push('slippage=' + req.body.slippage);
if (req.body.limit) qs.push('limit=' + req.body.limit);
if (req.body.providers) qs.push('providers=' + req.body.providers);
if (req.body.subProviders) qs.push('subProviders=' + req.body.subProviders);
if (req.body.preferredProvider) qs.push('preferredProvider=' + req.body.preferredProvider);
if (req.body.affiliateAddress) qs.push('affiliateAddress=' + req.body.affiliateAddress);
if (req.body.affiliateBasisPoints) qs.push('affiliateBasisPoints=' + req.body.affiliateBasisPoints);
if (req.body.isAffiliateFeeFlat) qs.push('isAffiliateFeeFlat=' + req.body.isAffiliateFeeFlat);
if (req.body.allowSmartContractRecipient) qs.push('allowSmartContractRecipient=' + req.body.allowSmartContractRecipient);
let qs = [];
if (!checkRequired(req.body, ['sellAsset', 'buyAsset', 'sellAmount'])) {
return reject(new ClientError("Thorswap's request missing arguments"));
}
qs.push('sellAsset=' + req.body.sellAsset);
qs.push('buyAsset=' + req.body.buyAsset);
qs.push('sellAmount=' + req.body.sellAmount);
if (req.body.senderAddress) qs.push('senderAddress=' + req.body.senderAddress);
if (req.body.recipientAddress) qs.push('recipientAddress=' + req.body.recipientAddress);
if (req.body.slippage) qs.push('slippage=' + req.body.slippage);
if (req.body.limit) qs.push('limit=' + req.body.limit);
if (req.body.providers) qs.push('providers=' + req.body.providers);
if (req.body.subProviders) qs.push('subProviders=' + req.body.subProviders);
if (req.body.preferredProvider) qs.push('preferredProvider=' + req.body.preferredProvider);
if (req.body.affiliateAddress) qs.push('affiliateAddress=' + req.body.affiliateAddress);
if (req.body.affiliateBasisPoints) qs.push('affiliateBasisPoints=' + req.body.affiliateBasisPoints);
if (req.body.isAffiliateFeeFlat) qs.push('isAffiliateFeeFlat=' + req.body.isAffiliateFeeFlat);
if (req.body.allowSmartContractRecipient) qs.push('allowSmartContractRecipient=' + req.body.allowSmartContractRecipient);

const URL: string = API + `/aggregator/tokens/quote?${qs.join('&')}`;

Expand Down

0 comments on commit 642c812

Please sign in to comment.