abi-util-lite
Version:
A light impletation to parse abi string array to abi json
101 lines • 4.04 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyState = exports.verifyIdentifier = exports.verifyType = exports.checkModifier = void 0;
var Logger_1 = require("./Logger");
var ModifiersBytes = { calldata: true, memory: true, storage: true };
var ModifiersNest = { calldata: true, memory: true };
function checkModifier(type, name) {
if (type === "bytes" || type === "string") {
if (ModifiersBytes[name]) {
return true;
}
}
else if (type === "address") {
if (name === "payable") {
return true;
}
}
else if (type.indexOf("[") >= 0 || type === "tuple") {
if (ModifiersNest[name]) {
return true;
}
}
if (ModifiersBytes[name] || name === "payable") {
Logger_1.logger.throwArgumentError("invalid modifier", "name", name);
}
return false;
}
exports.checkModifier = checkModifier;
function verifyType(type) {
// These need to be transformed to their full description
if (type.match(/^uint($|[^1-9])/)) {
type = "uint256" + type.substring(4);
}
else if (type.match(/^int($|[^1-9])/)) {
type = "int256" + type.substring(3);
}
// @TODO: more verification
return type;
}
exports.verifyType = verifyType;
// See: https://github.com/ethereum/solidity/blob/1f8f1a3db93a548d0555e3e14cfc55a10e25b60e/docs/grammar/SolidityLexer.g4#L234
var regexIdentifier = new RegExp("^[a-zA-Z$_][a-zA-Z0-9$_]*$");
function verifyIdentifier(value) {
if (!value || !value.match(regexIdentifier)) {
Logger_1.logger.throwArgumentError("invalid identifier \"" + value + "\"", "value", value);
}
return value;
}
exports.verifyIdentifier = verifyIdentifier;
function verifyState(value) {
var result = {
constant: false,
payable: true,
stateMutability: "payable"
};
if (value.stateMutability != null) {
result.stateMutability = value.stateMutability;
// Set (and check things are consistent) the constant property
result.constant = (result.stateMutability === "view" || result.stateMutability === "pure");
if (value.constant != null) {
if ((!!value.constant) !== result.constant) {
Logger_1.logger.throwArgumentError("cannot have constant function with mutability " + result.stateMutability, "value", value);
}
}
// Set (and check things are consistent) the payable property
result.payable = (result.stateMutability === "payable");
if (value.payable != null) {
if ((!!value.payable) !== result.payable) {
Logger_1.logger.throwArgumentError("cannot have payable function with mutability " + result.stateMutability, "value", value);
}
}
}
else if (value.payable != null) {
result.payable = !!value.payable;
// If payable we can assume non-constant; otherwise we can't assume
if (value.constant == null && !result.payable && value.type !== "constructor") {
Logger_1.logger.throwArgumentError("unable to determine stateMutability", "value", value);
}
result.constant = !!value.constant;
if (result.constant) {
result.stateMutability = "view";
}
else {
result.stateMutability = (result.payable ? "payable" : "nonpayable");
}
if (result.payable && result.constant) {
Logger_1.logger.throwArgumentError("cannot have constant payable function", "value", value);
}
}
else if (value.constant != null) {
result.constant = !!value.constant;
result.payable = !result.constant;
result.stateMutability = (result.constant ? "view" : "payable");
}
else if (value.type !== "constructor") {
Logger_1.logger.throwArgumentError("unable to determine stateMutability", "value", value);
}
return result;
}
exports.verifyState = verifyState;
//# sourceMappingURL=Checkers.js.map
;