Skip to content

Commit

Permalink
Allow double quotes for (u)int arrays inputs during contract interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaranov committed Mar 19, 2021
1 parent 900c36f commit 0ad731c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Current

### Features
- [#3722](https://github.com/poanetwork/blockscout/pull/3722) - Allow double quotes for (u)int arrays inputs during contract interaction
- [#3694](https://github.com/poanetwork/blockscout/pull/3694) - LP tokens total liquidity
- [#3676](https://github.com/poanetwork/blockscout/pull/3676) - Bridged tokens TLV in USD
- [#3674](https://github.com/poanetwork/blockscout/pull/3674) - Display Sushiswap pools data
Expand Down
57 changes: 42 additions & 15 deletions apps/block_scout_web/assets/js/lib/smart_contract/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,27 +197,26 @@ function getMethodInputs (contractAbi, functionName) {

function prepareMethodArgs ($functionInputs, inputs) {
return $.map($functionInputs, (element, ind) => {
const val = $(element).val()
const inputValue = $(element).val()
const inputType = inputs[ind] && inputs[ind].type
let preparedVal
if (isNonSpaceInputType(inputType)) { preparedVal = val.replace(/\s/g, '') } else { preparedVal = val }
if (isAddressInputType(inputType)) {
if (typeof preparedVal.replaceAll === 'function') {
preparedVal = preparedVal.replaceAll('"', '')
} else {
preparedVal = preparedVal.replace(/"/g, '')
}
}
let sanitizedInputValue
sanitizedInputValue = replaceSpaces(inputValue, inputType)
sanitizedInputValue = replaceDoubleQuotes(sanitizedInputValue, inputType)
if (isArrayInputType(inputType)) {
if (preparedVal === '') {
if (sanitizedInputValue === '') {
return [[]]
} else {
if (preparedVal.startsWith('[') && preparedVal.endsWith(']')) {
preparedVal = preparedVal.substring(1, preparedVal.length - 1)
if (sanitizedInputValue.startsWith('[') && sanitizedInputValue.endsWith(']')) {
sanitizedInputValue = sanitizedInputValue.substring(1, sanitizedInputValue.length - 1)
}
return [preparedVal.split(',')]
const inputValueElements = sanitizedInputValue.split(',')
const sanitizedInputValueElements = inputValueElements.map(elementValue => {
const elementInputType = inputType.split('[')[0]
return replaceDoubleQuotes(elementValue, elementInputType)
})
return [sanitizedInputValueElements]
}
} else { return preparedVal }
} else { return sanitizedInputValue }
})
}

Expand All @@ -229,6 +228,14 @@ function isAddressInputType (inputType) {
return inputType.includes('address')
}

function isUintInputType (inputType) {
return inputType.includes('int') && !inputType.includes('[')
}

function isStringInputType (inputType) {
return inputType.includes('string') && !inputType.includes('[')
}

function isNonSpaceInputType (inputType) {
return isAddressInputType(inputType) || inputType.includes('int') || inputType.includes('bool')
}
Expand Down Expand Up @@ -279,6 +286,26 @@ function onTransactionHash (txHash, $element, functionName) {
const txReceiptPollingIntervalId = setInterval(() => { getTxReceipt(txHash) }, 5 * 1000)
}

function replaceSpaces (value, type) {
if (isNonSpaceInputType(type)) {
return value.replace(/\s/g, '')
} else {
return value
}
}

function replaceDoubleQuotes (value, type) {
if (isAddressInputType(type) || isUintInputType(type) || isStringInputType(type)) {
if (typeof value.replaceAll === 'function') {
return value.replaceAll('"', '')
} else {
return value.replace(/"/g, '')
}
} else {
return value
}
}

const formatError = (error) => {
let { message } = error
message = message && message.split('Error: ').length > 1 ? message.split('Error: ')[1] : message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ defmodule BlockScoutWeb.SmartContractView do
end

def values_with_type(value, type, _components) when type in [:address, "address", "address payable"] do
{:ok, address} = Address.cast(value)
render_type_value("address", to_string(address))
case Address.cast(value) do
{:ok, address} ->
render_type_value("address", to_string(address))

_ ->
""
end
end

def values_with_type(value, "string", _components), do: render_type_value("string", value)
Expand Down

0 comments on commit 0ad731c

Please sign in to comment.