@clarigen/cli 0.3.6 → 1.0.0-next.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/index.d.ts +16 -0
- package/dist/commands/index.js +72 -63
- package/dist/commands/index.mjs +74 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +73 -18
- package/dist/index.mjs +74 -0
- package/package.json +15 -12
- package/src/clarinet-config.ts +13 -8
- package/src/commands/index.ts +2 -2
- package/src/config.ts +50 -32
- package/src/generate/declaration.ts +33 -57
- package/src/generate/files.ts +39 -36
- package/src/utils.ts +13 -14
- package/dist/clarinet-config.js +0 -71
- package/dist/commands/contract.js +0 -33
- package/dist/config.js +0 -31
- package/dist/generate/declaration.js +0 -157
- package/dist/generate/files.js +0 -141
- package/dist/start.js +0 -9
- package/dist/utils.js +0 -56
- package/src/commands/contract.ts +0 -35
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeTypes = exports.jsTypeFromAbiType = exports.cvFromType = void 0;
|
|
4
|
-
const transactions_1 = require("@stacks/transactions");
|
|
5
|
-
const core_1 = require("@clarigen/core");
|
|
6
|
-
const cvFromType = (val) => {
|
|
7
|
-
if (transactions_1.isClarityAbiPrimitive(val)) {
|
|
8
|
-
if (val === 'uint128') {
|
|
9
|
-
return 'ClarityTypes.UIntCV';
|
|
10
|
-
}
|
|
11
|
-
else if (val === 'int128') {
|
|
12
|
-
return 'ClarityTypes.IntCV';
|
|
13
|
-
}
|
|
14
|
-
else if (val === 'bool') {
|
|
15
|
-
return 'ClarityTypes.BooleanCV';
|
|
16
|
-
}
|
|
17
|
-
else if (val === 'principal') {
|
|
18
|
-
return 'ClarityTypes.PrincipalCV';
|
|
19
|
-
}
|
|
20
|
-
else if (val === 'none') {
|
|
21
|
-
return 'ClarityTypes.NoneCV';
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
throw new Error(`Unexpected Clarity ABI type primitive: ${JSON.stringify(val)}`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
else if (transactions_1.isClarityAbiBuffer(val)) {
|
|
28
|
-
return 'ClarityTypes.BufferCV';
|
|
29
|
-
}
|
|
30
|
-
else if (transactions_1.isClarityAbiResponse(val)) {
|
|
31
|
-
return 'ClarityTypes.ResponseCV';
|
|
32
|
-
}
|
|
33
|
-
else if (transactions_1.isClarityAbiOptional(val)) {
|
|
34
|
-
return 'ClarityTypes.OptionalCV';
|
|
35
|
-
}
|
|
36
|
-
else if (transactions_1.isClarityAbiTuple(val)) {
|
|
37
|
-
return 'ClarityTypes.TupleCV';
|
|
38
|
-
}
|
|
39
|
-
else if (transactions_1.isClarityAbiList(val)) {
|
|
40
|
-
return 'ClarityTypes.ListCV';
|
|
41
|
-
}
|
|
42
|
-
else if (transactions_1.isClarityAbiStringAscii(val)) {
|
|
43
|
-
return 'ClarityTypes.StringAsciiCV';
|
|
44
|
-
}
|
|
45
|
-
else if (transactions_1.isClarityAbiStringUtf8(val)) {
|
|
46
|
-
return 'ClarityTypes.StringUtf8CV';
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
throw new Error(`Unexpected Clarity ABI type: ${JSON.stringify(val)}`);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
exports.cvFromType = cvFromType;
|
|
53
|
-
const jsTypeFromAbiType = (val, isArgument = false) => {
|
|
54
|
-
if (transactions_1.isClarityAbiPrimitive(val)) {
|
|
55
|
-
if (val === 'uint128') {
|
|
56
|
-
if (isArgument)
|
|
57
|
-
return 'number | bigint';
|
|
58
|
-
return 'bigint';
|
|
59
|
-
}
|
|
60
|
-
else if (val === 'int128') {
|
|
61
|
-
if (isArgument)
|
|
62
|
-
return 'number | bigint';
|
|
63
|
-
return 'bigint';
|
|
64
|
-
}
|
|
65
|
-
else if (val === 'bool') {
|
|
66
|
-
return 'boolean';
|
|
67
|
-
}
|
|
68
|
-
else if (val === 'principal') {
|
|
69
|
-
return 'string';
|
|
70
|
-
}
|
|
71
|
-
else if (val === 'none') {
|
|
72
|
-
return 'null';
|
|
73
|
-
}
|
|
74
|
-
else if (val === 'trait_reference') {
|
|
75
|
-
return 'string';
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
throw new Error(`Unexpected Clarity ABI type primitive: ${JSON.stringify(val)}`);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
else if (transactions_1.isClarityAbiBuffer(val)) {
|
|
82
|
-
return 'Buffer';
|
|
83
|
-
}
|
|
84
|
-
else if (transactions_1.isClarityAbiResponse(val)) {
|
|
85
|
-
const ok = exports.jsTypeFromAbiType(val.response.ok);
|
|
86
|
-
const err = exports.jsTypeFromAbiType(val.response.error);
|
|
87
|
-
return `ClarityTypes.Response<${ok}, ${err}>`;
|
|
88
|
-
}
|
|
89
|
-
else if (transactions_1.isClarityAbiOptional(val)) {
|
|
90
|
-
const innerType = exports.jsTypeFromAbiType(val.optional);
|
|
91
|
-
return `${innerType} | null`;
|
|
92
|
-
}
|
|
93
|
-
else if (transactions_1.isClarityAbiTuple(val)) {
|
|
94
|
-
const tupleDefs = [];
|
|
95
|
-
val.tuple.forEach(({ name, type }) => {
|
|
96
|
-
const innerType = exports.jsTypeFromAbiType(type);
|
|
97
|
-
tupleDefs.push(`"${name}": ${innerType}`);
|
|
98
|
-
});
|
|
99
|
-
return `{
|
|
100
|
-
${tupleDefs.join(';\n ')}
|
|
101
|
-
}`;
|
|
102
|
-
}
|
|
103
|
-
else if (transactions_1.isClarityAbiList(val)) {
|
|
104
|
-
const innerType = exports.jsTypeFromAbiType(val.list.type);
|
|
105
|
-
return `${innerType}[]`;
|
|
106
|
-
}
|
|
107
|
-
else if (transactions_1.isClarityAbiStringAscii(val)) {
|
|
108
|
-
return 'string';
|
|
109
|
-
}
|
|
110
|
-
else if (transactions_1.isClarityAbiStringUtf8(val)) {
|
|
111
|
-
return 'string';
|
|
112
|
-
}
|
|
113
|
-
else if (val === 'trait_reference') {
|
|
114
|
-
return 'string';
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
throw new Error(`Unexpected Clarity ABI type: ${JSON.stringify(val)}`);
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
exports.jsTypeFromAbiType = jsTypeFromAbiType;
|
|
121
|
-
const makeTypes = (abi) => {
|
|
122
|
-
let typings = '';
|
|
123
|
-
abi.functions.forEach((func, index) => {
|
|
124
|
-
if (func.access === 'private')
|
|
125
|
-
return;
|
|
126
|
-
let functionLine = `${core_1.toCamelCase(func.name)}: `;
|
|
127
|
-
const args = func.args.map((arg) => {
|
|
128
|
-
return `${core_1.toCamelCase(arg.name)}: ${exports.jsTypeFromAbiType(arg.type, true)}`;
|
|
129
|
-
});
|
|
130
|
-
functionLine += `(${args.join(', ')}) => `;
|
|
131
|
-
if (func.access === 'public') {
|
|
132
|
-
const { type } = func.outputs;
|
|
133
|
-
if (!transactions_1.isClarityAbiResponse(type))
|
|
134
|
-
throw new Error('Expected response type for public function');
|
|
135
|
-
functionLine += 'Transaction';
|
|
136
|
-
const ok = exports.jsTypeFromAbiType(type.response.ok);
|
|
137
|
-
const err = exports.jsTypeFromAbiType(type.response.error);
|
|
138
|
-
functionLine += `<${ok}, ${err}>;`;
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
const jsType = exports.jsTypeFromAbiType(func.outputs.type);
|
|
142
|
-
functionLine += `Promise<${jsType}>;`;
|
|
143
|
-
// const { type } = func.outputs;
|
|
144
|
-
// if (isClarityAbiResponse(type)) {
|
|
145
|
-
// const ok = jsTypeFromAbiType(type.response.ok);
|
|
146
|
-
// const err = jsTypeFromAbiType(type.response.error);
|
|
147
|
-
// functionLine += `Promise<ClarityTypes.Response<${ok}, ${err}>>;`;
|
|
148
|
-
// } else {
|
|
149
|
-
// const jsType = jsTypeFromAbiType(func.outputs.type);
|
|
150
|
-
// functionLine += `Promise<${jsType}>;`;
|
|
151
|
-
// }
|
|
152
|
-
}
|
|
153
|
-
typings += `${index === 0 ? '' : '\n'} ${functionLine}`;
|
|
154
|
-
});
|
|
155
|
-
return typings;
|
|
156
|
-
};
|
|
157
|
-
exports.makeTypes = makeTypes;
|
package/dist/generate/files.js
DELETED
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateProjectIndexFile = exports.generateTypesFile = exports.generateIndexFile = exports.generateInterfaceFile = exports.generateInterface = void 0;
|
|
4
|
-
const native_bin_1 = require("@clarigen/native-bin");
|
|
5
|
-
const core_1 = require("@clarigen/core");
|
|
6
|
-
const declaration_1 = require("./declaration");
|
|
7
|
-
const path_1 = require("path");
|
|
8
|
-
const generateInterface = async ({ provider: _provider, contractFile, contractAddress = 'S1G2081040G2081040G2081040G208105NK8PE5', }) => {
|
|
9
|
-
const provider = _provider || (await native_bin_1.createClarityBin());
|
|
10
|
-
const contractName = core_1.getContractNameFromPath(contractFile);
|
|
11
|
-
const receipt = await provider.runCommand([
|
|
12
|
-
'launch',
|
|
13
|
-
`${contractAddress}.${contractName}`,
|
|
14
|
-
contractFile,
|
|
15
|
-
provider.dbFilePath,
|
|
16
|
-
'--output_analysis',
|
|
17
|
-
'--costs',
|
|
18
|
-
'--assets',
|
|
19
|
-
]);
|
|
20
|
-
if (native_bin_1.hasStdErr(receipt.stderr)) {
|
|
21
|
-
throw new Error(`Error on ${contractFile}:
|
|
22
|
-
${receipt.stderr}
|
|
23
|
-
`);
|
|
24
|
-
}
|
|
25
|
-
const output = JSON.parse(receipt.stdout);
|
|
26
|
-
if (output.error) {
|
|
27
|
-
const { initialization } = output.error;
|
|
28
|
-
if (initialization?.includes('\nNear:\n')) {
|
|
29
|
-
const [error, trace] = initialization.split('\nNear:\n');
|
|
30
|
-
let startLine = '';
|
|
31
|
-
const matcher = /start_line: (\d+),/;
|
|
32
|
-
const matches = matcher.exec(trace);
|
|
33
|
-
if (matches)
|
|
34
|
-
startLine = matches[1];
|
|
35
|
-
throw new Error(`Error on ${contractFile}:
|
|
36
|
-
${error}
|
|
37
|
-
${startLine ? `Near line ${startLine}` : ''}
|
|
38
|
-
Raw trace:
|
|
39
|
-
${trace}
|
|
40
|
-
`);
|
|
41
|
-
}
|
|
42
|
-
throw new Error(`Error on ${contractFile}:
|
|
43
|
-
${JSON.stringify(output.error, null, 2)}
|
|
44
|
-
`);
|
|
45
|
-
}
|
|
46
|
-
const abi = output.analysis;
|
|
47
|
-
return abi;
|
|
48
|
-
};
|
|
49
|
-
exports.generateInterface = generateInterface;
|
|
50
|
-
const generateInterfaceFile = ({ contractFile, abi, }) => {
|
|
51
|
-
const contractName = core_1.getContractNameFromPath(contractFile);
|
|
52
|
-
const variableName = core_1.toCamelCase(contractName, true);
|
|
53
|
-
const abiString = JSON.stringify(abi, null, 2);
|
|
54
|
-
const fileContents = `import { ClarityAbi } from '@clarigen/core';
|
|
55
|
-
|
|
56
|
-
// prettier-ignore
|
|
57
|
-
export const ${variableName}Interface: ClarityAbi = ${abiString};
|
|
58
|
-
`;
|
|
59
|
-
return fileContents;
|
|
60
|
-
};
|
|
61
|
-
exports.generateInterfaceFile = generateInterfaceFile;
|
|
62
|
-
const generateIndexFile = ({ contractFile, address, }) => {
|
|
63
|
-
const contractName = core_1.getContractNameFromPath(contractFile);
|
|
64
|
-
const contractTitle = core_1.toCamelCase(contractName, true);
|
|
65
|
-
const varName = core_1.toCamelCase(contractName);
|
|
66
|
-
const contractType = `${contractTitle}Contract`;
|
|
67
|
-
const fileContents = `import { proxy, BaseProvider, Contract } from '@clarigen/core';
|
|
68
|
-
import type { ${contractType} } from './types';
|
|
69
|
-
import { ${contractTitle}Interface } from './abi';
|
|
70
|
-
export type { ${contractType} } from './types';
|
|
71
|
-
|
|
72
|
-
export const ${varName}Contract = (provider: BaseProvider) => {
|
|
73
|
-
const contract = proxy<${contractType}>(${contractTitle}Interface, provider);
|
|
74
|
-
return contract;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export const ${varName}Info: Contract<${contractType}> = {
|
|
78
|
-
contract: ${varName}Contract,
|
|
79
|
-
address: '${address}',
|
|
80
|
-
contractFile: '${contractFile}',
|
|
81
|
-
};
|
|
82
|
-
`;
|
|
83
|
-
return fileContents;
|
|
84
|
-
};
|
|
85
|
-
exports.generateIndexFile = generateIndexFile;
|
|
86
|
-
const generateTypesFile = (abi, contractName) => {
|
|
87
|
-
const name = core_1.toCamelCase(contractName, true);
|
|
88
|
-
const typings = declaration_1.makeTypes(abi);
|
|
89
|
-
const fileContents = `import { ClarityTypes, Transaction } from '@clarigen/core';
|
|
90
|
-
|
|
91
|
-
// prettier-ignore
|
|
92
|
-
export interface ${name}Contract {
|
|
93
|
-
${typings}
|
|
94
|
-
}
|
|
95
|
-
`;
|
|
96
|
-
return fileContents;
|
|
97
|
-
};
|
|
98
|
-
exports.generateTypesFile = generateTypesFile;
|
|
99
|
-
const generateProjectIndexFile = (config) => {
|
|
100
|
-
const imports = [];
|
|
101
|
-
const exports = [];
|
|
102
|
-
const contractMap = [];
|
|
103
|
-
let accounts = '';
|
|
104
|
-
if ('accounts' in config) {
|
|
105
|
-
const accountLines = Object.keys(config.accounts).map((key) => {
|
|
106
|
-
const account = config.accounts[key];
|
|
107
|
-
return `"${key}": {
|
|
108
|
-
mnemonic: "${account.mnemonic}",
|
|
109
|
-
balance: ${account.balance.toString()}n,
|
|
110
|
-
address: "${account.address}",
|
|
111
|
-
},`;
|
|
112
|
-
});
|
|
113
|
-
accounts = `\n\n// prettier-ignore
|
|
114
|
-
export const accounts = {
|
|
115
|
-
${accountLines.join('\n ')}
|
|
116
|
-
};`;
|
|
117
|
-
}
|
|
118
|
-
config.contracts.forEach((contract) => {
|
|
119
|
-
const contractName = core_1.getContractNameFromPath(contract.file);
|
|
120
|
-
const contractVar = core_1.toCamelCase(contractName);
|
|
121
|
-
const contractInfo = `${contractVar}Info`;
|
|
122
|
-
const contractInterface = `${core_1.toCamelCase(contractName, true)}Contract`;
|
|
123
|
-
const dirName = path_1.dirname(contract.file);
|
|
124
|
-
const importPath = `'./${path_1.join(dirName || '.', contractName)}'`;
|
|
125
|
-
const _import = `import { ${contractInfo} } from ${importPath};`;
|
|
126
|
-
imports.push(_import);
|
|
127
|
-
const _export = `export type { ${contractInterface} } from ${importPath};`;
|
|
128
|
-
exports.push(_export);
|
|
129
|
-
const map = `${contractVar}: ${contractInfo},`;
|
|
130
|
-
contractMap.push(map);
|
|
131
|
-
});
|
|
132
|
-
const file = `${imports.join('\n')}
|
|
133
|
-
${exports.join('\n')}
|
|
134
|
-
|
|
135
|
-
export const contracts = {
|
|
136
|
-
${contractMap.join('\n ')}
|
|
137
|
-
};${accounts}
|
|
138
|
-
`;
|
|
139
|
-
return file;
|
|
140
|
-
};
|
|
141
|
-
exports.generateProjectIndexFile = generateProjectIndexFile;
|
package/dist/start.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const errors_1 = require("@oclif/errors");
|
|
7
|
-
const flush_1 = __importDefault(require("@oclif/command/flush"));
|
|
8
|
-
const index_1 = require("./index");
|
|
9
|
-
index_1.run().then(flush_1.default, errors_1.handle);
|
package/dist/utils.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateProject = exports.generateFilesForContract = void 0;
|
|
4
|
-
const files_1 = require("./generate/files");
|
|
5
|
-
const core_1 = require("@clarigen/core");
|
|
6
|
-
const native_bin_1 = require("@clarigen/native-bin");
|
|
7
|
-
const path_1 = require("path");
|
|
8
|
-
const promises_1 = require("fs/promises");
|
|
9
|
-
const config_1 = require("./config");
|
|
10
|
-
const generateFilesForContract = async ({ contractFile: _contractFile, outputFolder, provider, contractAddress, dirName, }) => {
|
|
11
|
-
const contractFile = path_1.resolve(process.cwd(), _contractFile);
|
|
12
|
-
const contractName = core_1.getContractNameFromPath(contractFile);
|
|
13
|
-
const abi = await files_1.generateInterface({
|
|
14
|
-
contractFile,
|
|
15
|
-
provider,
|
|
16
|
-
contractAddress,
|
|
17
|
-
});
|
|
18
|
-
const typesFile = files_1.generateTypesFile(abi, contractName);
|
|
19
|
-
if (!contractAddress && process.env.NODE_ENV !== 'test') {
|
|
20
|
-
console.warn('Please provide an address with every contract.');
|
|
21
|
-
}
|
|
22
|
-
const indexFile = files_1.generateIndexFile({
|
|
23
|
-
contractFile: path_1.relative(process.cwd(), contractFile),
|
|
24
|
-
address: contractAddress || '',
|
|
25
|
-
});
|
|
26
|
-
const abiFile = files_1.generateInterfaceFile({ contractFile, abi });
|
|
27
|
-
const baseDir = outputFolder || path_1.resolve(process.cwd(), `tmp/${contractName}`);
|
|
28
|
-
const outputPath = path_1.resolve(baseDir, dirName || '.', contractName);
|
|
29
|
-
await promises_1.mkdir(outputPath, { recursive: true });
|
|
30
|
-
await promises_1.writeFile(path_1.resolve(outputPath, 'abi.ts'), abiFile);
|
|
31
|
-
await promises_1.writeFile(path_1.resolve(outputPath, 'index.ts'), indexFile);
|
|
32
|
-
await promises_1.writeFile(path_1.resolve(outputPath, 'types.ts'), typesFile);
|
|
33
|
-
};
|
|
34
|
-
exports.generateFilesForContract = generateFilesForContract;
|
|
35
|
-
const generateProject = async (projectPath) => {
|
|
36
|
-
const configFile = await config_1.getConfigFile(projectPath);
|
|
37
|
-
const { contractsDir, outputDir, contracts } = configFile;
|
|
38
|
-
const outputFolder = path_1.resolve(projectPath, outputDir);
|
|
39
|
-
const provider = await native_bin_1.createClarityBin();
|
|
40
|
-
// this needs to be serial
|
|
41
|
-
for (const contract of contracts) {
|
|
42
|
-
const contractFile = path_1.resolve(projectPath, contractsDir, contract.file);
|
|
43
|
-
const dirName = path_1.dirname(contract.file);
|
|
44
|
-
await exports.generateFilesForContract({
|
|
45
|
-
contractFile,
|
|
46
|
-
outputFolder: outputFolder,
|
|
47
|
-
provider,
|
|
48
|
-
contractAddress: contract.address,
|
|
49
|
-
dirName,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
const indexFile = files_1.generateProjectIndexFile(configFile);
|
|
53
|
-
const indexPath = path_1.resolve(outputFolder, 'index.ts');
|
|
54
|
-
await promises_1.writeFile(indexPath, indexFile);
|
|
55
|
-
};
|
|
56
|
-
exports.generateProject = generateProject;
|
package/src/commands/contract.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Command, flags } from '@oclif/command';
|
|
2
|
-
import { generateFilesForContract } from '../utils';
|
|
3
|
-
|
|
4
|
-
export class Contract extends Command {
|
|
5
|
-
static description = `Generate files for a single contract`;
|
|
6
|
-
static strict = true;
|
|
7
|
-
static hidden = true;
|
|
8
|
-
|
|
9
|
-
static flags = {
|
|
10
|
-
help: flags.help({ char: 'h' }),
|
|
11
|
-
output: flags.string({
|
|
12
|
-
char: 'o',
|
|
13
|
-
description: 'Output destination folder',
|
|
14
|
-
default: 'clarion',
|
|
15
|
-
}),
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
static args = [
|
|
19
|
-
{
|
|
20
|
-
name: 'contract',
|
|
21
|
-
description: 'The file path for your contract',
|
|
22
|
-
},
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
async run() {
|
|
26
|
-
const { argv, flags } = this.parse(Contract);
|
|
27
|
-
|
|
28
|
-
const [contractFile] = argv;
|
|
29
|
-
|
|
30
|
-
await generateFilesForContract({
|
|
31
|
-
contractFile,
|
|
32
|
-
outputFolder: flags.output,
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|