navskit
Version:
Deploy TypeScript logic on Ethereum. Includes core library, CLI tools, and utilities.
148 lines (147 loc) • 5.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTypeName = getTypeName;
exports.extractParamTypesFromString = extractParamTypesFromString;
exports.extractParamNamesFromString = extractParamNamesFromString;
exports.getTypeMapping = getTypeMapping;
/**
* Helper function to extract a descriptive type name
*/
function getTypeName(type) {
if (type === String)
return 'string';
if (type === Number)
return 'number';
if (type === Boolean)
return 'boolean';
if (type === BigInt)
return 'bigint';
if (type === Object)
return 'object';
if (type === Array)
return 'array';
if (type === undefined)
return 'undefined';
if (type === null)
return 'null';
// Check for constructor name
if (type && type.name) {
return type.name.toLowerCase();
}
// Fallback
return typeof type === 'function' ? 'function' :
typeof type === 'object' ? 'object' :
'unknown';
}
/**
* Helper function to extract parameter types from a function string
* Fallback when reflection metadata is not available
*/
function extractParamTypesFromString(fn) {
// Convert function to string and parse out parameter names
const fnStr = fn.toString();
const paramNames = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).split(',').map(p => p.trim()).filter(p => p);
// For each parameter, try to infer type
return paramNames.map(param => {
// Clean the parameter name by removing default values and extracting just the name
const cleanParam = param.split('=')[0].split(':')[0].trim();
// Check for type annotations in the function string
// Handle various TypeScript type patterns including template literals
const typeMatch = fnStr.match(new RegExp(`${cleanParam}\\s*:\\s*([^,)=]+)`));
if (typeMatch && typeMatch[1]) {
const type = typeMatch[1].trim();
// Map specific TypeScript patterns to our type system
if (type.includes('0x${string}') || type.includes('`0x${string}`')) {
return 'address';
}
// Extract base type from complex patterns
const simpleTypeMatch = type.match(/^(\w+)/);
if (simpleTypeMatch) {
return simpleTypeMatch[1];
}
return type;
}
return 'unknown';
});
}
/**
* Helper function to extract parameter names from a function string
*/
function extractParamNamesFromString(fn) {
const fnStr = fn.toString();
const paramNames = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).split(',').map(p => p.trim()).filter(p => p);
return paramNames.map(param => {
// Clean the parameter name by removing type annotations and default values
return param.split(':')[0].split('=')[0].trim();
});
}
/**
* Maps a NAVS type to its ABI type and conversion functions
*/
function getTypeMapping(type) {
switch (type.toLowerCase()) {
case 'string':
return {
abiType: 'string',
encoder: (val) => val,
decoder: (val) => val
};
case 'number':
return {
abiType: 'uint256',
encoder: (val) => BigInt(val),
decoder: (val) => typeof val === 'bigint' ? Number(val) : val
};
case 'bigint':
return {
abiType: 'uint256',
encoder: (val) => BigInt(val),
decoder: (val) => val
};
case 'boolean':
return {
abiType: 'bool',
encoder: (val) => val,
decoder: (val) => val
};
case 'address':
return {
abiType: 'address',
encoder: (val) => val,
decoder: (val) => val
};
case 'array':
return {
abiType: 'string',
encoder: (val) => Array.isArray(val) ? JSON.stringify(val) : String(val),
decoder: (val) => {
try {
return JSON.parse(String(val));
}
catch (_e) {
return val;
}
}
};
case 'object':
return {
abiType: 'string',
encoder: (val) => typeof val === 'object' ? JSON.stringify(val) : String(val),
decoder: (val) => {
try {
return JSON.parse(String(val));
}
catch (_e) {
return val;
}
}
};
default:
// Default to string for unknown types
return {
abiType: 'string',
encoder: (val) => String(val),
decoder: (val) => val
};
}
}