From abd27d756572f5789868805b4cca2f03c4584774 Mon Sep 17 00:00:00 2001 From: yann300 Date: Wed, 19 Apr 2023 14:45:11 +0200 Subject: [PATCH] scientific notation with arrays --- .../src/tests/transactionExecution.test.ts | 7 ++++ libs/remix-lib/src/execution/txFormat.ts | 40 +++++++++++++++---- libs/remix-lib/src/execution/txHelper.ts | 14 ------- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts b/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts index 137be15955..0dd61da685 100644 --- a/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts +++ b/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts @@ -144,6 +144,9 @@ module.exports = { .waitForElementContainsText('*[data-id="terminalJournal"]', '-11300', 60000) .clickFunction('inputValue2 - transact (not payable)', { types: 'uint256 _u', values: '2.345e10' }) .waitForElementContainsText('*[data-id="terminalJournal"]', '2340000000', 60000) + .clickFunction('inputValue3 - transact (not payable)', { types: 'uint256[] _u', values: '["2.445e10", "13e1"]' }) + .waitForElementContainsText('*[data-id="terminalJournal"]', '24450000000', 60000) + .waitForElementContainsText('*[data-id="terminalJournal"]', '130', 60000) .click('*[data-id="deployAndRunClearInstances"]') }, @@ -501,6 +504,10 @@ contract C { function inputValue2 (uint _u) public { console.log(_u); } + function inputValue3 (uint[] memory _u) public { + console.log(_u[0]); + console.log(_u[1]); + } } ` } diff --git a/libs/remix-lib/src/execution/txFormat.ts b/libs/remix-lib/src/execution/txFormat.ts index e23f8de552..44492a2649 100644 --- a/libs/remix-lib/src/execution/txFormat.ts +++ b/libs/remix-lib/src/execution/txFormat.ts @@ -4,6 +4,7 @@ import { encodeParams as encodeParamsHelper, encodeFunctionId, makeFullTypeDefin import { eachOfSeries } from 'async' import { linkBytecode as linkBytecodeSolc } from 'solc/linker' import { isValidAddress, addHexPrefix } from '@ethereumjs/util' +import fromExponential from 'from-exponential'; /** * build the transaction data @@ -429,7 +430,7 @@ export function parseFunctionParams (params) { // look for closing quote. On success, push the complete string in arguments list for (let j = i + 1; !endQuoteIndex; j++) { if (params.charAt(j) === '"') { - args.push(params.substring(i + 1, j)) + args.push(normalizeParam(params.substring(i + 1, j))) endQuoteIndex = true i = j } @@ -461,13 +462,7 @@ export function parseFunctionParams (params) { // if startIndex >= 0, it means a parameter was being parsed, it can be first or other parameter if (startIndex >= 0) { let param = params.substring(startIndex, i === params.length - 1 ? undefined : i) - const trimmed = param.trim() - if (param.startsWith('0x')) param = `${param}` - if (/[0-9]/g.test(trimmed)) param = `${trimmed}` - if (typeof param === 'string') { - if (trimmed === 'true') param = true - if (trimmed === 'false') param = false - } + param = normalizeParam(param) args.push(param) } // Register start index of a parameter to parse @@ -477,6 +472,35 @@ export function parseFunctionParams (params) { return args } +export const normalizeParam = (param) => { + param = param.trim() + if (param.startsWith('0x')) param = `${param}` + if (/[0-9]/g.test(param)) param = `${param}` + + // fromExponential + const regSci = REGEX_SCIENTIFIC.exec(param) + const exponents = regSci ? regSci[2] : null + if (regSci && REGEX_DECIMAL.exec(exponents)) { + try { + let paramTrimmed = param.replace(/^'/g, '').replace(/'$/g, '') + paramTrimmed = paramTrimmed.replace(/^"/g, '').replace(/"$/g, '') + param = fromExponential(paramTrimmed) + } catch (e) { + console.log(e) + } + } + + if (typeof param === 'string') { + if (param === 'true') param = true + if (param === 'false') param = false + } + return param +} + +export const REGEX_SCIENTIFIC = /(\d+\.?\d*)e\d*(\d+)/; + +export const REGEX_DECIMAL = /^\d*/ + export function isArrayOrStringStart (str, index) { return str.charAt(index) === '"' || str.charAt(index) === '[' } diff --git a/libs/remix-lib/src/execution/txHelper.ts b/libs/remix-lib/src/execution/txHelper.ts index 95a27d784b..f3c78930b1 100644 --- a/libs/remix-lib/src/execution/txHelper.ts +++ b/libs/remix-lib/src/execution/txHelper.ts @@ -1,6 +1,5 @@ 'use strict' import { ethers } from 'ethers' -import fromExponential from 'from-exponential'; export function makeFullTypeDefinition (typeDef) { if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) { @@ -10,10 +9,6 @@ export function makeFullTypeDefinition (typeDef) { return typeDef.type } -export const REGEX_SCIENTIFIC = /(\d+\.?\d*)e\d*(\d+)/; - -export const REGEX_DECIMAL = /^\d*/ - export function encodeParams (funABI, args) { const types = [] if (funABI.inputs && funABI.inputs.length) { @@ -24,15 +19,6 @@ export function encodeParams (funABI, args) { if (type === 'bool' && args[i] === 'false') { args[i] = false } - const regSci = REGEX_SCIENTIFIC.exec(args[i]) - const exponents = regSci ? regSci[2] : null - if (regSci && REGEX_DECIMAL.exec(exponents) && (type.indexOf('uint') === 0 || type.indexOf('int') === 0)) { - try { - args[i] = fromExponential(args[i]) - } catch (e) { - console.log(e) - } - } types.push(type.indexOf('tuple') === 0 ? makeFullTypeDefinition(funABI.inputs[i]) : type) if (args.length < types.length) { args.push('')