scientific notation with arrays

pull/3637/head
yann300 2 years ago committed by Aniket
parent b9c78bce9c
commit abd27d7565
  1. 7
      apps/remix-ide-e2e/src/tests/transactionExecution.test.ts
  2. 40
      libs/remix-lib/src/execution/txFormat.ts
  3. 14
      libs/remix-lib/src/execution/txHelper.ts

@ -144,6 +144,9 @@ module.exports = {
.waitForElementContainsText('*[data-id="terminalJournal"]', '-11300', 60000) .waitForElementContainsText('*[data-id="terminalJournal"]', '-11300', 60000)
.clickFunction('inputValue2 - transact (not payable)', { types: 'uint256 _u', values: '2.345e10' }) .clickFunction('inputValue2 - transact (not payable)', { types: 'uint256 _u', values: '2.345e10' })
.waitForElementContainsText('*[data-id="terminalJournal"]', '2340000000', 60000) .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"]') .click('*[data-id="deployAndRunClearInstances"]')
}, },
@ -501,6 +504,10 @@ contract C {
function inputValue2 (uint _u) public { function inputValue2 (uint _u) public {
console.log(_u); console.log(_u);
} }
function inputValue3 (uint[] memory _u) public {
console.log(_u[0]);
console.log(_u[1]);
}
} }
` `
} }

@ -4,6 +4,7 @@ import { encodeParams as encodeParamsHelper, encodeFunctionId, makeFullTypeDefin
import { eachOfSeries } from 'async' import { eachOfSeries } from 'async'
import { linkBytecode as linkBytecodeSolc } from 'solc/linker' import { linkBytecode as linkBytecodeSolc } from 'solc/linker'
import { isValidAddress, addHexPrefix } from '@ethereumjs/util' import { isValidAddress, addHexPrefix } from '@ethereumjs/util'
import fromExponential from 'from-exponential';
/** /**
* build the transaction data * 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 // look for closing quote. On success, push the complete string in arguments list
for (let j = i + 1; !endQuoteIndex; j++) { for (let j = i + 1; !endQuoteIndex; j++) {
if (params.charAt(j) === '"') { if (params.charAt(j) === '"') {
args.push(params.substring(i + 1, j)) args.push(normalizeParam(params.substring(i + 1, j)))
endQuoteIndex = true endQuoteIndex = true
i = j 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, it means a parameter was being parsed, it can be first or other parameter
if (startIndex >= 0) { if (startIndex >= 0) {
let param = params.substring(startIndex, i === params.length - 1 ? undefined : i) let param = params.substring(startIndex, i === params.length - 1 ? undefined : i)
const trimmed = param.trim() param = normalizeParam(param)
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
}
args.push(param) args.push(param)
} }
// Register start index of a parameter to parse // Register start index of a parameter to parse
@ -477,6 +472,35 @@ export function parseFunctionParams (params) {
return args 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) { export function isArrayOrStringStart (str, index) {
return str.charAt(index) === '"' || str.charAt(index) === '[' return str.charAt(index) === '"' || str.charAt(index) === '['
} }

@ -1,6 +1,5 @@
'use strict' 'use strict'
import { ethers } from 'ethers' import { ethers } from 'ethers'
import fromExponential from 'from-exponential';
export function makeFullTypeDefinition (typeDef) { export function makeFullTypeDefinition (typeDef) {
if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) { if (typeDef && typeDef.type.indexOf('tuple') === 0 && typeDef.components) {
@ -10,10 +9,6 @@ export function makeFullTypeDefinition (typeDef) {
return typeDef.type return typeDef.type
} }
export const REGEX_SCIENTIFIC = /(\d+\.?\d*)e\d*(\d+)/;
export const REGEX_DECIMAL = /^\d*/
export function encodeParams (funABI, args) { export function encodeParams (funABI, args) {
const types = [] const types = []
if (funABI.inputs && funABI.inputs.length) { if (funABI.inputs && funABI.inputs.length) {
@ -24,15 +19,6 @@ export function encodeParams (funABI, args) {
if (type === 'bool' && args[i] === 'false') { if (type === 'bool' && args[i] === 'false') {
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) types.push(type.indexOf('tuple') === 0 ? makeFullTypeDefinition(funABI.inputs[i]) : type)
if (args.length < types.length) { if (args.length < types.length) {
args.push('') args.push('')

Loading…
Cancel
Save