@aibtc/types
Version:
TypeScript types for AIBTC
150 lines (149 loc) • 4.55 kB
JavaScript
export class ContractBase {
name;
type;
subtype;
deploymentOrder;
templatePath;
clarityVersion;
_displayName;
_source;
_hash;
// Generate template path based on contract type
static generateTemplatePath(type, name) {
switch (type) {
case "BASE":
return `dao/${name}.clar`;
case "ACTIONS":
return `dao/actions/${name}.clar`;
case "EXTENSIONS":
return `dao/extensions/${name}.clar`;
case "PROPOSALS":
return `dao/proposals/${name}.clar`;
case "TOKEN":
return `dao/token/${name}.clar`;
case "AGENT":
return `agent/${name}.clar`;
default:
return `${name}.clar`;
}
}
// Dependencies
requiredAddresses = [];
requiredTraits = [];
requiredContractAddresses = [];
requiredRuntimeValues = [];
constructor(name, type, subtype, deploymentOrder, templatePath, clarityVersion) {
this.name = name;
this.type = type;
this.subtype = subtype;
this.deploymentOrder = deploymentOrder;
this.templatePath = templatePath;
this.clarityVersion = clarityVersion;
}
// Getters
get displayName() {
return this._displayName;
}
get source() {
return this._source;
}
get hash() {
return this._hash;
}
// Setters for generated content
setDisplayName(displayName) {
this._displayName = displayName;
return this;
}
setSource(source) {
this._source = source;
return this;
}
setHash(hash) {
this._hash = hash;
return this;
}
// Add dependencies
addAddressDependency(ref, key) {
this.requiredAddresses.push({ ref, key });
return this;
}
addTraitDependency(ref, key) {
this.requiredTraits.push({ ref, key });
return this;
}
addContractDependency(key, category, subcategory) {
this.requiredContractAddresses.push({ key, category, subcategory });
return this;
}
addRuntimeValue(key) {
this.requiredRuntimeValues.push({ key });
return this;
}
/**
* Scan the template content for /g/ variables and add them as dependencies
* @param templateContent The content of the template file
*/
scanTemplateVariables(templateContent) {
// Extract all variables from template
const variableRegex = /;;\s*\/g\/([^\/]+)\/([^\/\n]+)/g;
const matches = [...templateContent.matchAll(variableRegex)];
// Add each unique variable as a dependency
const uniqueVars = new Set();
for (const match of matches) {
const toReplace = match[1];
const keyName = match[2];
const key = `${toReplace}/${keyName}`;
if (!uniqueVars.has(key)) {
uniqueVars.add(key);
}
}
return this;
}
// Convert to registry entry format for backward compatibility
/**
* Get all dependencies for this contract
*/
getDependencies() {
return [
...this.requiredAddresses,
...this.requiredTraits,
...this.requiredContractAddresses,
...this.requiredRuntimeValues,
];
}
toRegistryEntry() {
const entry = {
name: this.name,
type: this.type,
subtype: this.subtype,
deploymentOrder: this.deploymentOrder,
templatePath: this.templatePath,
};
if (this.clarityVersion) {
entry.clarityVersion = this.clarityVersion;
}
if (this.requiredAddresses.length > 0) {
entry.requiredAddresses = [...this.requiredAddresses];
}
if (this.requiredTraits.length > 0) {
entry.requiredTraits = [...this.requiredTraits];
}
if (this.requiredContractAddresses.length > 0) {
entry.requiredContractAddresses = [...this.requiredContractAddresses];
}
if (this.requiredRuntimeValues.length > 0) {
entry.requiredRuntimeValues = [...this.requiredRuntimeValues];
}
if (this._displayName) {
entry.displayName = this._displayName;
}
if (this._source) {
entry.source = this._source;
}
if (this._hash) {
entry.hash = this._hash;
}
return entry;
}
}