From 32b6b296fffc7084f7688a136c6a0eaf6e369012 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 21 May 2024 09:10:57 -0400 Subject: [PATCH 1/2] add gas conditional if simple transaction (#7043) * add gas conditional if simple transaction * update * format and add changelog * format * format * add test * fix test --- packages/web3-eth/CHANGELOG.md | 4 ++++ packages/web3-eth/src/utils/send_tx_helper.ts | 9 +++++++- .../web3-eth/test/unit/send_tx_helper.test.ts | 23 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth/CHANGELOG.md b/packages/web3-eth/CHANGELOG.md index 5fb80eef663..9d9b67b29ca 100644 --- a/packages/web3-eth/CHANGELOG.md +++ b/packages/web3-eth/CHANGELOG.md @@ -242,3 +242,7 @@ Documentation: ### Changed - Added parameter `customTransactionReceiptSchema` into methods `emitConfirmation`, `waitForTransactionReceipt`, `watchTransactionByPolling`, `watchTransactionBySubscription`, `watchTransactionForConfirmations` (#7000) + +### Fixed + +- Fixed issue with simple transactions, Within `checkRevertBeforeSending` if there is no data set in transaction, set gas to be `21000` (#7043) \ No newline at end of file diff --git a/packages/web3-eth/src/utils/send_tx_helper.ts b/packages/web3-eth/src/utils/send_tx_helper.ts index 282e778e519..0037e1051fa 100644 --- a/packages/web3-eth/src/utils/send_tx_helper.ts +++ b/packages/web3-eth/src/utils/send_tx_helper.ts @@ -122,7 +122,14 @@ export class SendTxHelper< public async checkRevertBeforeSending(tx: TransactionCall) { if (this.options.checkRevertBeforeSending !== false) { - const reason = await getRevertReason(this.web3Context, tx, this.options.contractAbi); + let formatTx = tx; + if (isNullish(tx.data) && isNullish(tx.input) && isNullish(tx.gas)) { // eth.call runs into error if data isnt filled and gas is not defined, its a simple transaction so we fill it with 21000 + formatTx = { + ...tx, + gas: 21000 + } + } + const reason = await getRevertReason(this.web3Context, formatTx, this.options.contractAbi); if (reason !== undefined) { throw await getTransactionError( this.web3Context, diff --git a/packages/web3-eth/test/unit/send_tx_helper.test.ts b/packages/web3-eth/test/unit/send_tx_helper.test.ts index 55a482395b8..6b40496f5a6 100644 --- a/packages/web3-eth/test/unit/send_tx_helper.test.ts +++ b/packages/web3-eth/test/unit/send_tx_helper.test.ts @@ -21,6 +21,7 @@ import { JsonRpcResponse, TransactionReceipt, Web3BaseWalletAccount, + TransactionCall } from 'web3-types'; import { Web3Context, Web3EventMap, Web3PromiEvent } from 'web3-core'; import { @@ -151,6 +152,28 @@ describe('sendTxHelper class', () => { expect(f).toHaveBeenCalledWith(error); promiEvent.off('error', f); }); + + it('add gas to simple transaction in checkRevertBeforeSending', async () => { + const _sendTxHelper = new SendTxHelper({ + web3Context, + promiEvent: promiEvent as PromiEvent, + options: { + checkRevertBeforeSending: true, + }, + returnFormat: DEFAULT_RETURN_FORMAT, + }); + + const tx = {from:"0x"} as TransactionCall + + await _sendTxHelper.checkRevertBeforeSending(tx); + + const expectedTx = { + ...tx, + gas: 21000, + }; + expect(utils.getRevertReason).toHaveBeenCalledWith(web3Context, expectedTx, undefined); + + }); it('emit handleError with handleRevert', async () => { const error = new ContractExecutionError({ code: 1, message: 'error' }); web3Context.handleRevert = true; From 553f270e49c35b68adec64454db8d32a03646de7 Mon Sep 17 00:00:00 2001 From: Adam Gordon <3723289+gordon-to@users.noreply.github.com> Date: Tue, 21 May 2024 22:25:09 +0200 Subject: [PATCH 2/2] fix(#7044): toWei trim strings with more than 20 decimal places (#7045) * fix(#7044): fix too many decimals passed to toWei * add test case * add tests and fix fromWei * add changelog and update tests * update test --------- Co-authored-by: Alex Luu --- packages/web3-utils/CHANGELOG.md | 3 +- packages/web3-utils/src/converters.ts | 41 ++++++++++--------- .../web3-utils/test/fixtures/converters.ts | 14 ++++++- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/packages/web3-utils/CHANGELOG.md b/packages/web3-utils/CHANGELOG.md index d0f162613d8..2310e93c49e 100644 --- a/packages/web3-utils/CHANGELOG.md +++ b/packages/web3-utils/CHANGELOG.md @@ -224,4 +224,5 @@ Documentation: ### Fixed -- `toWei` support numbers in scientific notation (#6908) \ No newline at end of file +- `toWei` support numbers in scientific notation (#6908) +- `toWei` and `fromWei` trims according to ether unit successfuly (#7044) \ No newline at end of file diff --git a/packages/web3-utils/src/converters.ts b/packages/web3-utils/src/converters.ts index 55143a4277c..3c5b09ed27f 100644 --- a/packages/web3-utils/src/converters.ts +++ b/packages/web3-utils/src/converters.ts @@ -532,8 +532,9 @@ export const fromWei = (number: Numbers, unit: EtherUnits): string => { if (fraction === '') { return integer; } + const updatedValue = `${integer}.${fraction}`; - return `${integer}.${fraction}`; + return updatedValue.slice(0, integer.length + numberOfZerosInDenomination + 1); }; /** @@ -559,50 +560,50 @@ export const toWei = (number: Numbers, unit: EtherUnits): string => { throw new InvalidUnitError(unit); } let parsedNumber = number; - if (typeof parsedNumber === 'number'){ - if (parsedNumber < 1e-15){ - console.warn(PrecisionLossWarning) + if (typeof parsedNumber === 'number') { + if (parsedNumber < 1e-15) { + console.warn(PrecisionLossWarning); } - if (parsedNumber > 1e+20) { - console.warn(PrecisionLossWarning) + if (parsedNumber > 1e20) { + console.warn(PrecisionLossWarning); - parsedNumber = BigInt(parsedNumber); + parsedNumber = BigInt(parsedNumber); } else { // in case there is a decimal point, we need to convert it to string - parsedNumber = parsedNumber.toLocaleString('fullwide', {useGrouping: false, maximumFractionDigits: 20}) + parsedNumber = parsedNumber.toLocaleString('fullwide', { + useGrouping: false, + maximumFractionDigits: 20, + }); } } - + // if value is decimal e.g. 24.56 extract `integer` and `fraction` part // to avoid `fraction` to be null use `concat` with empty string const [integer, fraction] = String( - typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) ? parsedNumber : toNumber(parsedNumber), + typeof parsedNumber === 'string' && !isHexStrict(parsedNumber) + ? parsedNumber + : toNumber(parsedNumber), ) .split('.') .concat(''); // join the value removing `.` from // 24.56 -> 2456 - + const value = BigInt(`${integer}${fraction}`); // multiply value with denomination // 2456 * 1000000 -> 2456000000 const updatedValue = value * denomination; - // count number of zeros in denomination - const numberOfZerosInDenomination = denomination.toString().length - 1; - - // check which either `fraction` or `denomination` have lower number of zeros - const decimals = Math.min(fraction.length, numberOfZerosInDenomination); - + // check if whole number was passed in + const decimals = fraction.length; if (decimals === 0) { return updatedValue.toString(); } - // Add zeros to make length equal to required decimal points - // If string is larger than decimal points required then remove last zeros - return updatedValue.toString().padStart(decimals, '0').slice(0, -decimals); + // trim the value to remove extra zeros + return updatedValue.toString().slice(0, -decimals); }; /** diff --git a/packages/web3-utils/test/fixtures/converters.ts b/packages/web3-utils/test/fixtures/converters.ts index 91f3a76f738..14dc271015e 100644 --- a/packages/web3-utils/test/fixtures/converters.ts +++ b/packages/web3-utils/test/fixtures/converters.ts @@ -292,12 +292,17 @@ const conversionBaseData: [[Numbers, EtherUnits], string][] = [ [['879123456788877661', 'tether'], '0.000000000000879123456788877661'], ]; -export const fromWeiValidData: [[Numbers, EtherUnits], string][] = [ +export const fromWeiValidData: [[Numbers, EtherUnits], Numbers][] = [ ...conversionBaseData, [['0xff', 'wei'], '255'], [[1e+22, 'ether'], '10000'], [[19999999999999991611392, 'ether'], '19999.999999999991611392'], [[1.9999999999999991611392e+22, 'ether'], '19999.999999999991611392'], + [['1000000', 'ether'], '0.000000000001'], + [['1123456789123456789', 'ether'], '1.123456789123456789'], + [['1123', 'kwei'], '1.123'], + [['1234100' ,'kwei'], '1234.1'], + [['3308685546611893', 'ether'], '0.003308685546611893'] ]; export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [ @@ -305,7 +310,12 @@ export const toWeiValidData: [[Numbers, EtherUnits], Numbers][] = [ [['255', 'wei'], '0xFF'], [['100000000000', 'ether'], 0.0000001], [['1000000000', 'ether'], 0.000000001], - [['1000000', 'ether'], 0.000000000001] + [['1000000', 'ether'], 0.000000000001], + [['1123456789123456789', 'ether'], '1.123456789123456789123'], + [['1123', 'kwei'], '1.12345'], + [['1234100' ,'kwei'], '1234.1'], + [['3308685546611893', 'ether'], '0.0033086855466118933'], + [['1123', 'kwei'], 1.12345], ];