Merge pull request #1294 from ethereum/fix/#1293

string with comma handled
pull/7/head
Aniket 5 years ago committed by GitHub
commit ba978eed28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 42
      remix-lib/src/execution/txFormat.js
  2. 38
      remix-lib/test/txFormat.js
  3. 0
      tasks.todo

@ -169,7 +169,7 @@ module.exports = {
* @param {Function} callbackDeployLibrary - callbackDeployLibrary
*/
buildData: function (contractName, contract, contracts, isConstructor, funAbi, params, callback, callbackStep, callbackDeployLibrary) {
var funArgs = ''
var funArgs = []
var data = ''
var dataHex = ''
@ -179,9 +179,9 @@ module.exports = {
data = Buffer.from(dataHex, 'hex')
} else {
try {
params = params.replace(/(^|,\s+|,)(\d+)(\s+,|,|$)/g, '$1"$2"$3') // replace non quoted number by quoted number
params = params.replace(/(^|,\s+|,)(0[xX][0-9a-fA-F]+)(\s+,|,|$)/g, '$1"$2"$3') // replace non quoted hex string by quoted hex string
funArgs = JSON.parse('[' + params + ']')
if (params.length > 0) {
funArgs = this.parseFunctionParams(params)
}
} catch (e) {
callback('Error encoding arguments: ' + e)
return
@ -384,6 +384,40 @@ module.exports = {
}
}
return {}
},
parseFunctionParams: function (params) {
let args = []
// Segregate params textbox string with respect to comma (,)
params = params.split(',')
for (let i = 0; i < params.length; i++) {
let param = params[i].trim()
// Check if param starts with " , it may be string, address etc.
if (param.charAt(0) === '"') {
// Check if param completes in one location by looking for end quote (case: address data type)
if (param.charAt(param.length - 1) === '"') {
args.push(param.slice(1, param.length - 1))
} else {
let lastIndex = false
let paramStr = param.slice(1, param.length)
// For a paramter got divided in multiple location(case: string data type containing comma(,))
for (let j = i + 1; !lastIndex; j++) {
// Check if end quote is reached
if (params[j].charAt(params[j].length - 1) === '"') {
paramStr += ',' + params[j].slice(0, params[j].length - 1)
i = j
args.push(paramStr)
lastIndex = true
} else {
paramStr += ',' + params[j]
}
}
}
} else {
args.push(param)
}
}
return args
}
}

@ -46,6 +46,35 @@ function testWithInput (st, params, expected) {
}, () => {}, () => {})
}
tape('ContractStringParameters - (TxFormat.buildData) - format string input parameters', function (t) {
var output = compiler.compile(compilerInput(stringContract))
output = JSON.parse(output)
console.log('-----------------------------', output)
var contract = output.contracts['test.sol']['stringContractTest']
context = { output, contract }
t.test('(TxFormat.buildData)', function (st) {
st.plan(3)
testWithStringInput(st, '"1,2,3,4qwerty,5", 0xf7a10e525d4b168f45f74db1b61f63d3e7619ea8, "1,a,5,34"', '0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000f7a10e525d4b168f45f74db1b61f63d3e7619ea800000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000f312c322c332c347177657274792c3500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008312c612c352c3334000000000000000000000000000000000000000000000000')
testWithStringInput(st, '"1,2,3,4qwerty,5", "0xf7a10e525d4b168f45f74db1b61f63d3e7619ea8", "1,a,5,34"', '0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000f7a10e525d4b168f45f74db1b61f63d3e7619ea800000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000f312c322c332c347177657274792c3500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008312c612c352c3334000000000000000000000000000000000000000000000000')
// string with space
testWithStringInput(st, '"1,2,3,,4qw erty,5", "0xf7a10e525d4b168f45f74db1b61f63d3e7619ea8", "abcdefghijkl"', '0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000f7a10e525d4b168f45f74db1b61f63d3e7619ea800000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000012312c322c332c2c3471772020657274792c350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c6162636465666768696a6b6c0000000000000000000000000000000000000000')
})
})
function testWithStringInput (st, params, expected) {
txFormat.buildData('stringContractTest', context.contract, context.output.contracts, true, context.contract.abi[0], params, (error, data) => {
if (error) { return st.fails(error) }
console.log(data)
if (!data.dataHex.endsWith(expected)) {
st.fail(`result of buildData ${data.dataHex} should end with ${expected} . `)
} else {
st.pass(`testWithStringInput. result of buildData ${data.dataHex} ends with correct data`)
}
}, () => {}, () => {})
}
/* tape *********************************************************** */
tape('ContractParameters - (TxFormat.buildData) - link Libraries', function (t) {
@ -236,6 +265,15 @@ var uintContract = `contract uintContractTest {
}
}`
var stringContract = `contract stringContractTest {
string _tp;
address _ap;
function test(string memory _t, address _a, string memory _i) public {
_tp = _t;
_ap = _a;
}
}`
var deploySimpleLib = `pragma solidity ^0.5.0;
library lib1 {

Loading…
Cancel
Save