Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/features/customContract/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Constants
import { DATA_FEATURE, DATA_PROPERTY, DATA_MODIFIER } from '../../lib/constants';
import { DATA_FEATURE, DATA_PROPERTY, DATA_MODIFIER, ETHER_UNITS } from '../../lib/constants';

export const customContract = {
id: 'customContract',
Expand Down Expand Up @@ -85,13 +85,13 @@ export const customContract = {
id: 'displayUnits',
defaultValue: 'ether',
attribute: `${DATA_MODIFIER}-display-units`,
validator: (value) => ['wei', 'ether'].includes(value),
validator: (value) => ETHER_UNITS.includes(value),
},
{
id: 'contractUnits',
defaultValue: 'wei',
attribute: `${DATA_MODIFIER}-contract-units`,
validator: (value) => ['wei', 'ether'].includes(value),
validator: (value) => ETHER_UNITS.includes(value),
},
{
id: 'displayDecimals',
Expand Down
31 changes: 28 additions & 3 deletions src/features/customContract/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,51 @@ export const customContractParser = (
const parsedInputs = inputs
.map((input) => {
const value = input.getAttribute(property.attribute);

// we need to check if the start of value is [ and the end is ] and the middle item is a number.
const getIsArray = (value) => {
if (value.charAt(0) === '[' && value.charAt(value.length - 1) === ']' && !isNaN(value.substring(1, value.length - 1))) {
return parseInt(value.substring(1, value.length - 1), 10)
} else {
return false
}
}

const inputArrayValue = getIsArray(value)

const shouldAutoClear = input.getAttribute(`${DATA_PROPERTY}-auto-clear`) === 'true'

// Check each input name in ABI equals to the value defined in the DOM
const isInputFound = contractMethod.inputs.some((input) => input.name === value);
// check if the actual array value is less than or equal to the input array length
/ note that a zero position value would evaluate falsy, hence we deep equal to false.

const isInputArrayValueValid = inputArrayValue !== false && inputArrayValue <= contractMethod.inputs.length-1

const isInputFound = contractMethod.inputs.some(({ name }) => name === value || isInputArrayValueValid);

// Now that we know it's an array, check if the array value is valid.
// Note we will have to also check if ALL the array inputs are found.
// Note you could also mix&match the array idnex with a named input (Or too complicated?)

const emptyString = '$true';
const isEmptyString = value === emptyString;

if (!['EthValue', emptyString].includes(value) && !isInputFound) {
return console.error(
`Input name "${value}" for method ${methodName} does not exists on the contract ABI`,
`(DH-DOM) Input name "${value}" for method ${methodName} does not exists on the contract ABI`,
);
}

// TODO: [DEV-312] This is part of the error for anonymous inputs
if (isEmptyString && contractMethod.inputs.length > 1) {
return console.error(
`Input with empty string (anonymous input) cannot be set since exists more than one input on the contract ABI`,
`(DH-DOM) Input with empty string (anonymous input)
cannot be set since exists more than one input on the contract ABI,
use Array input array Index. (See documentation: docs.dapphero.io) `,
);
}


return {
element: input,
id: property.id,
Expand Down
20 changes: 20 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ export const ELEMENT_TYPES = {
video: 'video',
};

export const ETHER_UNITS = ['wei', 'ether',
'1',
'10',
'100',
'1000',
'10000',
'100000',
'1000000',
'10000000',
'100000000',
'1000000000',
'10000000000',
'100000000000',
'1000000000000',
'10000000000000',
'100000000000000',
'1000000000000000',
'100000000000000000',
'1000000000000000000']

export const TAG_TYPES = {
H1: ELEMENT_TYPES.text,
H2: ELEMENT_TYPES.text,
Expand Down