diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e8b388d33..930e257192 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - [#4579](https://github.com/blockscout/blockscout/pull/4579) - Write contract page: Resize inputs; Improve multiplier selector ### Fixes +- [#4711](https://github.com/blockscout/blockscout/pull/4711) - Add trimming to the contract functions inputs - [#4729](https://github.com/blockscout/blockscout/pull/4729) - Fix bugs with fees in cases of txs with `gas price = 0` - [#4725](https://github.com/blockscout/blockscout/pull/4725) - Fix hardcoded coin name on transaction's and block's page - [#4717](https://github.com/blockscout/blockscout/pull/4717) - Contract verification fix: check only success creation tx diff --git a/apps/block_scout_web/assets/__tests__/lib/smart_contract/common_helpers.js b/apps/block_scout_web/assets/__tests__/lib/smart_contract/common_helpers.js new file mode 100644 index 0000000000..73b51b1f46 --- /dev/null +++ b/apps/block_scout_web/assets/__tests__/lib/smart_contract/common_helpers.js @@ -0,0 +1,180 @@ +import { prepareMethodArgs } from '../../../js/lib/smart_contract/common_helpers' +import $ from 'jquery' + +const oneFieldHTML = + '
' + + ' ' + + ' ' + + '
' + + ' ' + + '
' + + ' ' + + '
' + +const twoFieldHTML = + '
' + + ' ' + + ' ' + + '
' + + ' ' + + '
' + + '
' + + ' ' + + '
' + + ' ' + + '
' + +test('prepare contract args | type: address[]*2', () => { + document.body.innerHTML = twoFieldHTML + + var inputs = [ + { + "type": "address[]", + "name": "arg1", + "internalType": "address[]" + }, + { + "type": "address[]", + "name": "arg2", + "internalType": "address[]" + } + ] + + document.getElementById('first').value = ' 0x0000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000001 ' + document.getElementById('second').value = ' 0x0000000000000000000000000000000000000002 , 0x0000000000000000000000000000000000000003 ' + const expectedValue = [ + [ + '0x0000000000000000000000000000000000000000', + '0x0000000000000000000000000000000000000001' + ], + [ + '0x0000000000000000000000000000000000000002', + '0x0000000000000000000000000000000000000003' + ] + ] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + +test('prepare contract args | type: address', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "address", + "name": "arg1", + "internalType": "address" + } + ] + + document.getElementById('first').value = ' 0x000000000000000000 0000000000000000000000 ' + const expectedValue = ['0x0000000000000000000000000000000000000000'] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + +test('prepare contract args | type: string', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "string", + "name": "arg1", + "internalType": "string" + } + ] + + document.getElementById('first').value = ' 0x0000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000001 ' + const expectedValue = ['0x0000000000000000000000000000000000000000 , 0x0000000000000000000000000000000000000001'] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + +test('prepare contract args | type: string[]', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "string[]", + "name": "arg1", + "internalType": "string[]" + } + ] + + document.getElementById('first').value = ' " 0x0000000000000000000000000000000000000000 " , " 0x0000000000000000000000000000000000000001 " ' + const expectedValue = [['0x0000000000000000000000000000000000000000', '0x0000000000000000000000000000000000000001']] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + +test('prepare contract args | type: bool[]', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "bool[]", + "name": "arg1", + "internalType": "bool[]" + } + ] + + document.getElementById('first').value = ' true , false ' + const expectedValue = [[true, false]] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + +test('prepare contract args | type: bool', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "bool", + "name": "arg1", + "internalType": "bool" + } + ] + + document.getElementById('first').value = ' fals e ' + const expectedValue = [false] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + + +test('prepare contract args | type: uint256', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "uint256", + "name": "arg1", + "internalType": "uint256" + } + ] + + document.getElementById('first').value = ' 9 876 543 210 ' + const expectedValue = ['9876543210'] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) + +test('prepare contract args | type: uint256[]', () => { + document.body.innerHTML = oneFieldHTML + + var inputs = [ + { + "type": "uint256[]", + "name": "arg1", + "internalType": "uint256[]" + } + ] + + document.getElementById('first').value = ' 156 000 , 10 690 000 , 59874 ' + const expectedValue = [['156000', '10690000', '59874']] + const $functionInputs = $('[data-function-form]').find('input[name=function_input]') + expect(prepareMethodArgs($functionInputs, inputs)).toEqual(expectedValue) +}) \ No newline at end of file diff --git a/apps/block_scout_web/assets/js/lib/smart_contract/common_helpers.js b/apps/block_scout_web/assets/js/lib/smart_contract/common_helpers.js index 58c7dd2db6..4ed34840be 100644 --- a/apps/block_scout_web/assets/js/lib/smart_contract/common_helpers.js +++ b/apps/block_scout_web/assets/js/lib/smart_contract/common_helpers.js @@ -23,8 +23,8 @@ export function prepareMethodArgs ($functionInputs, inputs) { const inputType = inputs[ind] && inputs[ind].type const inputComponents = inputs[ind] && inputs[ind].components let sanitizedInputValue - sanitizedInputValue = replaceSpaces(inputValue, inputType, inputComponents) - sanitizedInputValue = replaceDoubleQuotes(sanitizedInputValue, inputType, inputComponents) + sanitizedInputValue = replaceDoubleQuotes(inputValue, inputType, inputComponents) + sanitizedInputValue = replaceSpaces(sanitizedInputValue, inputType, inputComponents) if (isArrayInputType(inputType) || isTupleInputType(inputType)) { if (sanitizedInputValue === '' || sanitizedInputValue === '[]') { @@ -38,6 +38,7 @@ export function prepareMethodArgs ($functionInputs, inputs) { const elementInputType = inputType.split('[')[0] var sanitizedElementValue = replaceDoubleQuotes(elementValue, elementInputType) + sanitizedElementValue = replaceSpaces(sanitizedElementValue, elementInputType) if (isBoolInputType(elementInputType)) { sanitizedElementValue = convertToBool(elementValue) @@ -141,7 +142,7 @@ function replaceSpaces (value, type, components) { }) .join(',') } else { - return value + return value.trim() } }