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

Method validateInput

src/validator.js:855–909  ·  view source on GitHub ↗
(action, actionScript, inputTokens)

Source from the content-addressed store, hash-verified

853 }
854
855 static validateInput(action, actionScript, inputTokens) {
856 const { name } = actionScript;
857
858 const {
859 inputFunctions,
860 payableFunctions
861 } = Validator.getInputEntities(actionScript);
862
863 const splitAction = action.split(" ");
864 let actionContract = splitAction[0];
865 let actionFunction = splitAction[1];
866 let amountArgument;
867 if (actionContract === "ETHER") {
868 if (!actionFunction.includes(":")) {
869 throw new Error(
870 `Action script "${name}" does not specify both a recipient and an amount as part of an Ether transfer action (i.e. "ETHER recipient:amount")`
871 );
872 }
873 amountArgument = actionFunction.split(":")[1];
874 } else if (actionFunction.includes(":")) {
875 [actionFunction, amountArgument] = actionFunction.split(":");
876 actionContract = "ETHER";
877 if (!payableFunctions.has(actionFunction)) {
878 throw new Error(
879 `Action script "${name}" does not properly define "${actionFunction}" as a payable function`
880 );
881 }
882 } else if (inputFunctions.has(actionFunction)) {
883 amountArgument = splitAction[3];
884 } else {
885 return inputTokens; // Not an action requiring an input token declaration
886 }
887
888 if (!(actionContract in inputTokens)) {
889 console.log(inputTokens);
890 throw new Error(
891 `Action script "${name}" does not properly declare "${actionContract}" as an input token`
892 );
893 }
894
895 const candidates = inputTokens[actionContract];
896 if (!candidates.includes(amountArgument)) {
897 // TODO: handle <= raw amounts
898 throw new Error(
899 `Action script "${name}" action "${actionContract} ${actionFunction}" contains a token transfer or approval input argument "${amountArgument}" that does not map back to a declared input`
900 );
901 }
902
903 const indexToPop = candidates.indexOf(amountArgument);
904 inputTokens[actionContract] = candidates
905 .slice(0, indexToPop)
906 .concat(candidates.slice(indexToPop + 1));
907
908 return inputTokens;
909 }
910
911 static validateInputCombinations(actions, actionScript, inputTokens) {
912 const { name } = actionScript;

Callers 1

Calls 1

getInputEntitiesMethod · 0.80

Tested by

no test coverage detected