@morpho-labs/gnosis-tx-builder
Version:
Transform an array of transactions into a json for Gnosis Tx-Builder UX
85 lines (84 loc) • 3.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateContractMethod = exports.ChecksumParsingError = exports.TransactionParsingError = exports.ParsingError = exports.ErrorCode = void 0;
var ErrorCode;
(function (ErrorCode) {
ErrorCode["wrongFormat"] = "WRONG_FORMAT";
ErrorCode["wrongTxFormat"] = "WRONG_TRANSACTION_FORMAT";
ErrorCode["invalidChecksum"] = "INVALID_CHECKSUM";
})(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {}));
class ParsingError extends Error {
constructor(code) {
super(`Cannot parse transactions.\nError code: ${code}`);
this.code = code;
this.name = "TxBuilderParsingError";
}
get params() {
return { code: this.code };
}
}
exports.ParsingError = ParsingError;
class TransactionParsingError extends ParsingError {
constructor(index, parameter) {
super(ErrorCode.wrongTxFormat);
this.index = index;
this.parameter = parameter;
this.message = `Cannot parse transaction at index ${index}.`;
if (parameter)
this.message += `\nParameter: ${parameter}`;
}
get params() {
return { code: this.code, index: this.index, parameter: this.parameter };
}
}
exports.TransactionParsingError = TransactionParsingError;
class ChecksumParsingError extends ParsingError {
constructor(expected, computed) {
super(ErrorCode.invalidChecksum);
this.expected = expected;
this.computed = computed;
this.message = `Invalid checksum.\nExpected: ${expected}\nComputed: ${computed}`;
}
get params() {
return { code: this.code, expected: this.expected, computed: this.computed };
}
}
exports.ChecksumParsingError = ChecksumParsingError;
const validateContractMethod = (contractMethod, error) => {
if (contractMethod === undefined)
return contractMethod;
if (typeof contractMethod !== "object")
throw error;
const { inputs, name, payable } = contractMethod;
if (typeof payable !== "boolean")
throw error;
if (typeof name !== "string")
throw error;
if (!Array.isArray(inputs))
throw error;
if (!inputs.every((input) => validateContractInput(input, error)))
throw error;
return { inputs, name, payable };
};
exports.validateContractMethod = validateContractMethod;
const validateContractInput = (contractInput, error) => {
if (typeof contractInput !== "object")
throw error;
const { internalType, name, type, components } = contractInput;
if (typeof internalType !== "string")
throw error;
if (typeof name !== "string")
throw error;
if (typeof type !== "string")
throw error;
if (components === undefined)
return { internalType, name, type };
if (!Array.isArray(components))
throw error;
return {
internalType,
name,
type,
components: components.map((component) => validateContractInput(component, error)),
};
};