From c1e503d6c1a9d1560256759eb6d9d2a90646d799 Mon Sep 17 00:00:00 2001 From: yann300 Date: Thu, 13 Apr 2023 09:46:50 +0200 Subject: [PATCH] allow scientific notation --- .../src/tests/transactionExecution.test.ts | 28 +++++++++++++++++++ libs/remix-lib/src/execution/txHelper.ts | 14 ++++++++++ package.json | 1 + yarn.lock | 5 ++++ 4 files changed, 48 insertions(+) diff --git a/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts b/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts index 543becddc2..137be15955 100644 --- a/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts +++ b/apps/remix-ide-e2e/src/tests/transactionExecution.test.ts @@ -134,6 +134,19 @@ module.exports = { .click('*[data-id="deployAndRunClearInstances"]') }, + 'Should use scientific notation as parameters #group2': function (browser: NightwatchBrowser) { + browser.testContracts('scientific_notation.sol', sources[8]['scientific_notation.sol'], ['test']) + .clickLaunchIcon('udapp') + .click('.udapp_contractActionsContainerSingle > button') + .clickInstance(0) + .clickFunction('inputValue1 - transact (not payable)', { types: 'uint256 _u, int256 _i', values: '"101e3", "-1.13e4"' }) + .waitForElementContainsText('*[data-id="terminalJournal"]', '101000', 60000) + .waitForElementContainsText('*[data-id="terminalJournal"]', '-11300', 60000) + .clickFunction('inputValue2 - transact (not payable)', { types: 'uint256 _u', values: '2.345e10' }) + .waitForElementContainsText('*[data-id="terminalJournal"]', '2340000000', 60000) + .click('*[data-id="deployAndRunClearInstances"]') + }, + 'Should Compile and Deploy a contract which define a custom error, the error should be logged in the terminal #group3': function (browser: NightwatchBrowser) { browser.testContracts('customError.sol', sources[4]['customError.sol'], ['C']) .clickLaunchIcon('udapp') @@ -476,5 +489,20 @@ contract C { } ` } + }, { + "scientific_notation.sol": { + content: ` + import "hardhat/console.sol"; + contract test { + function inputValue1 (uint _u, int _i) public { + console.log(_u); + console.logInt(_i); + } + function inputValue2 (uint _u) public { + console.log(_u); + } + } + ` + } } ] diff --git a/libs/remix-lib/src/execution/txHelper.ts b/libs/remix-lib/src/execution/txHelper.ts index f3c78930b1..95a27d784b 100644 --- a/libs/remix-lib/src/execution/txHelper.ts +++ b/libs/remix-lib/src/execution/txHelper.ts @@ -1,5 +1,6 @@ 'use strict' import { ethers } from 'ethers' +import fromExponential from 'from-exponential'; export function makeFullTypeDefinition (typeDef) { if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) { @@ -9,6 +10,10 @@ 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) { @@ -19,6 +24,15 @@ 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('') diff --git a/package.json b/package.json index 5fa05b185c..3039f466e5 100644 --- a/package.json +++ b/package.json @@ -162,6 +162,7 @@ "file-saver": "^2.0.5", "form-data": "^4.0.0", "formik": "^2.2.9", + "from-exponential": "1.1.1", "fs-extra": "^3.0.1", "html-react-parser": "^3.0.4", "http-server": "^14.1.1", diff --git a/yarn.lock b/yarn.lock index dfb48c036f..ba4025ff0f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13528,6 +13528,11 @@ fresh@0.5.2: resolved "https://registry.yarnpkg.com/fresh/-/fresh-0.5.2.tgz#3d8cadd90d976569fa835ab1f8e4b23a105605a7" integrity sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac= +from-exponential@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/from-exponential/-/from-exponential-1.1.1.tgz#41caff748d22e9c195713802cdac31acbe4b1b83" + integrity sha512-VBE7f5OVnYwdgB3LHa+Qo29h8qVpxhVO9Trlc+AWm+/XNAgks1tAwMFHb33mjeiof77GglsJzeYF7OqXrROP/A== + from2@^1.3.0: version "1.3.0" resolved "https://registry.yarnpkg.com/from2/-/from2-1.3.0.tgz#88413baaa5f9a597cfde9221d86986cd3c061dfd"