MCPcopy Create free account
hub / github.com/dharmaprotocol/dharmaOS / validateInputCombinations

Method validateInputCombinations

src/validator.js:911–989  ·  view source on GitHub ↗
(actions, actionScript, inputTokens)

Source from the content-addressed store, hash-verified

909 }
910
911 static validateInputCombinations(actions, actionScript, inputTokens) {
912 const { name } = actionScript;
913
914 const startingInputTokens = {...inputTokens};
915
916 // Determine each action that is a conditional
917 const conditionalActions = [];
918 for (let [i, action] of Object.entries(actions)) {
919 if (TYPE_CHECKERS.conditionalObject(action)) {
920 conditionalActions.push(i);
921 }
922 }
923
924 // Determine the total number of conditional (else or then) combinations
925 const conditionalCombinations = [...Array(2 ** conditionalActions.length).keys()]
926 .map(a => (
927 [...Array(conditionalActions.length).keys()]
928 .map(b => !((a >> b) & 1))
929 )).map(combination => combination
930 .map((condition, i) => ([conditionalActions[i], condition]))
931 .reduce(
932 (result, [key, value]) => Object.assign({}, result, {[key]: value}),
933 {}
934 )
935 );
936
937 // Ensure that each input token is only spent once for each combination
938 for (const conditionalCombination of conditionalCombinations) {
939 inputTokens = {...startingInputTokens};
940 for (let [i, action] of Object.entries(actions)) {
941 if (i in conditionalCombination) {
942 const splitCondition = action.if.condition.split(" ");
943 if (splitCondition.length !== 3) {
944 throw new Error(
945 `Action script "${name}" has a condition that does not contain exactly three arguments (for now only "<TOKEN_X> is <TOKEN_Y || ETHER>" and "<variable> is <true || false>" are supported)`
946 );
947 }
948
949 if (splitCondition[1] !== "is") {
950 throw new Error(
951 `Action script "${name}" has a condition that does not contain a direct comparison (for now only "<TOKEN_X> is <TOKEN_Y || ETHER>" and "<variable> is <true || false>" are supported)`
952 );
953 }
954
955 const conditionalVariable = splitCondition[0];
956 const comparisonVariable = splitCondition[2];
957 const isBooleanComparison = comparisonVariable === 'true' || comparisonVariable === 'false';
958
959 let replacement = false;
960 if (
961 conditionalCombination[i] &&
962 conditionalVariable in inputTokens &&
963 !isBooleanComparison
964 ) {
965 replacement = true;
966 const inputValuesToReplace = inputTokens[conditionalVariable];
967 delete inputTokens[conditionalVariable];
968 inputTokens[comparisonVariable] = inputValuesToReplace;

Callers 1

validateInputsMethod · 0.80

Calls 1

validateInputMethod · 0.80

Tested by

no test coverage detected