├── .npmignore ├── .eslintignore ├── .gitignore ├── assets └── read_deploy_artifacts.png ├── .prettierrc ├── .editorconfig ├── src ├── type-extensions.ts ├── index.ts └── handler │ ├── index.ts │ └── artifacts.ts ├── tsconfig.json ├── package.json ├── .eslintrc.js ├── README.md └── LICENSE /.npmignore: -------------------------------------------------------------------------------- 1 | example 2 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | !/src 4 | !/types -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /dist/ 3 | /.idea/ 4 | /workFiles/ 5 | ./config.json 6 | 7 | pkg/locklift 8 | artifacts 9 | -------------------------------------------------------------------------------- /assets/read_deploy_artifacts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/venom-blockchain/locklift-deploy-artifacts/HEAD/assets/read_deploy_artifacts.png -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "trailingComma": "all", 3 | "singleQuote": false, 4 | "jsxBracketSameLine": false, 5 | "bracketSpacing": true, 6 | "printWidth": 120 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | # Unix-style newlines with a newline ending every file 7 | [*] 8 | charset = utf-8 9 | end_of_line = lf 10 | insert_final_newline = true 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /src/type-extensions.ts: -------------------------------------------------------------------------------- 1 | import { FactoryType } from "locklift/internal/factory"; 2 | import { DeploymentHandler } from "./handler"; 3 | 4 | export const PLUGIN_NAME = "deployArtifacts" as const; 5 | 6 | export type DeployArtifactsExtension = { 7 | [key in typeof PLUGIN_NAME]: DeploymentHandler; 8 | }; 9 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import "./type-extensions"; 2 | import { Locklift, LockliftConfig } from "locklift"; 3 | import { addPlugin } from "locklift/plugins"; 4 | import { PLUGIN_NAME } from "./type-extensions"; 5 | import { DeploymentHandler } from "./handler"; 6 | 7 | export * from "./type-extensions"; 8 | 9 | type LockliftConfigOptions = Locklift extends Locklift ? F : never; 10 | 11 | // add plugin flow 12 | addPlugin({ 13 | // plugin name 14 | pluginName: PLUGIN_NAME, 15 | //Initializer function that will be called by locklift 16 | initializer: async ({ 17 | locklift, 18 | config, 19 | network, 20 | }: { 21 | locklift: Locklift; 22 | config: LockliftConfig; 23 | network?: string; 24 | }) => { 25 | return new DeploymentHandler(locklift); 26 | }, 27 | }); 28 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2019" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */, 4 | "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */, 5 | "declaration": true, 6 | "strict": true /* Enable all strict type-checking options. */, 7 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, 8 | "outDir": "./dist", 9 | "skipLibCheck": true /* Skip type checking of declaration files. */, 10 | "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, 11 | "types": ["node"] 12 | }, 13 | "exclude": ["node_modules", "dist", "pkg", "example"] 14 | } 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "locklift-deploy-artifacts", 3 | "version": "1.1.1", 4 | "description": "Locklift plugin that enables you to store build artifacts across contract migration scripts.", 5 | "main": "dist/index.js", 6 | "homepage": "https://github.com/venom-blockchain/locklift-deploy-artifacts", 7 | "keywords": [ 8 | "locklift", 9 | "venom", 10 | "blockchain", 11 | "smart-contracts", 12 | "plugin" 13 | ], 14 | "scripts": { 15 | "build": "tsc --build .", 16 | "start": "ts-node --transpile-only src/index.ts" 17 | }, 18 | "author": "", 19 | "license": "Apache-2.0", 20 | "devDependencies": { 21 | "@types/fs-extra": "^11.0.1", 22 | "@types/node": "^18.15.0", 23 | "commander": "^10.0.0", 24 | "everscale-inpage-provider": "^0.3.61", 25 | "fs-extra": "^11.1.0", 26 | "locklift": "^2.5.7", 27 | "prettier": "^2.1.2", 28 | "ts-node-dev": "^2.0.0", 29 | "typescript": "^4.4.5" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parser: "@typescript-eslint/parser", 4 | plugins: ["import", "@typescript-eslint", "react", "react-hooks", "jest", "graphql"], 5 | extends: [ 6 | "eslint:recommended", 7 | "plugin:node/recommended", 8 | "plugin:@typescript-eslint/eslint-recommended", 9 | "plugin:@typescript-eslint/recommended", 10 | "plugin:jest/recommended", 11 | "plugin:react/recommended", 12 | ], 13 | rules: { 14 | quotes: ["error", "double"], 15 | "import/no-default-export": 2, 16 | "import/no-unresolved": 2, 17 | "import/named": 2, 18 | "no-unused-vars": 2, 19 | "react-hooks/rules-of-hooks": "error", 20 | "react-hooks/exhaustive-deps": "warn", 21 | "react/jsx-uses-react": "error", 22 | "react/jsx-uses-vars": "error", 23 | "react/prop-types": "off", 24 | "react/display-name": "off", 25 | "@typescript-eslint/no-explicit-any": "off", 26 | "@typescript-eslint/explicit-function-return-type": "off", 27 | "@typescript-eslint/ban-types": "off", 28 | "graphql/template-strings": [ 29 | "error", 30 | { 31 | env: "apollo", 32 | }, 33 | ], 34 | "node/no-missing-import": "off", 35 | "node/no-unsupported-features/es-syntax": "off", 36 | "node/no-unpublished-import": "off", 37 | "node/no-unsupported-features/node-builtins": "off", 38 | "node/no-restricted-import": [ 39 | "error", 40 | [ 41 | { 42 | name: "@brizy/**", 43 | message: "Do not use @brizy/ui components directly, make sure you import them from '~/components/brizyUi'.", 44 | }, 45 | ], 46 | ], 47 | }, 48 | settings: { 49 | react: { 50 | version: "detect", 51 | }, 52 | "import/parsers": { 53 | "@typescript-eslint/parser": [".ts", ".tsx"], 54 | }, 55 | "import/resolver": { 56 | typescript: { 57 | project: "./", 58 | }, 59 | }, 60 | }, 61 | parserOptions: { 62 | ecmaFeatures: { 63 | jsx: true, 64 | }, 65 | }, 66 | overrides: [ 67 | { 68 | files: "types/global.d.ts", 69 | rules: { 70 | "@typescript-eslint/interface-name-prefix": "off", 71 | }, 72 | }, 73 | ], 74 | }; 75 | -------------------------------------------------------------------------------- /src/handler/index.ts: -------------------------------------------------------------------------------- 1 | import { Address, Contract } from "everscale-inpage-provider"; 2 | import { Locklift } from "locklift"; 3 | import { DeployContractParams, FactoryType } from "locklift/internal/factory"; 4 | import { DeployTransaction, TransactionWithOutput } from "locklift/types"; 5 | 6 | import { Artifacts, ContractVersion } from "./artifacts"; 7 | 8 | const ARTIFACTS_HISTORY_FILE = "./artifacts/journal.json"; 9 | 10 | export class DeploymentHandler { 11 | private readonly artifacts: Artifacts; 12 | private readonly locklift: Locklift; 13 | 14 | constructor(locklift: Locklift) { 15 | this.artifacts = new Artifacts(ARTIFACTS_HISTORY_FILE); 16 | this.locklift = locklift; 17 | } 18 | 19 | getArtifacts(): IDeployArtifacts { 20 | return this.artifacts.getDataObj() as IDeployArtifacts; 21 | } 22 | 23 | getContract(ver: ContractVersion) { 24 | const contract = new this.locklift.provider.Contract(ver.abi as ABI, new Address(ver.address)); 25 | return contract; 26 | } 27 | 28 | async deployContract( 29 | alias: string, 30 | version: string, 31 | args: DeployContractParams, 32 | deployFn?: ( 33 | args: DeployContractParams, 34 | ) => Promise< 35 | { 36 | contract: Contract; 37 | } & { 38 | tx: TransactionWithOutput; 39 | } 40 | >, 41 | ): Promise<{ contract: Contract } & DeployTransaction> { 42 | const locklift = this.locklift; 43 | const { tvc, abi, codeHash, code } = locklift.factory.getContractArtifacts(args.contract); 44 | 45 | const network = locklift.context.network.name; 46 | const contractName = args.contract.toString(); 47 | 48 | const fn = deployFn || locklift.factory.deployContract; 49 | const { contract, tx } = await fn(args); 50 | 51 | this.artifacts.addContract(network, contractName, alias, version, { 52 | address: contract.address.toString(), 53 | codeHash: codeHash, 54 | initParams: args.initParams, 55 | constructorParams: args.constructorParams, 56 | publicKey: args.publicKey, 57 | abi: abi, 58 | tvc: tvc, 59 | code: code, 60 | }); 61 | 62 | return { contract, tx }; 63 | } 64 | 65 | reset() { 66 | this.artifacts.reset(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Logo 4 | 5 |

6 | 7 | # Deploy artifacts plugin 8 | 9 | [Locklift](https://github.com/broxus/locklift) plugin that enables you to store build artifacts across contract migration scripts. 10 | 11 | ## Installation 12 | 13 | 1. Install plugin. 14 | 15 | ```bash 16 | npm i --save-dev locklift-deploy-artifacts 17 | ``` 18 | 19 | 2. Initialize the plugin via the `locklift.config.ts` file. 20 | 21 | ```ts 22 | // locklift.config.ts 23 | // ... 24 | import { FactorySource } from "./build/factorySource"; 25 | import { DeployArtifactsExtension } from "locklift-deploy-artifacts"; 26 | import "locklift-deploy-artifacts"; 27 | 28 | declare module "locklift" { 29 | export interface Locklift extends DeployArtifactsExtension {} 30 | } 31 | // ... 32 | ``` 33 | 34 | ## Usage 35 | 36 | The deploy artifacts plugin provides methods to interact with the build artifacts. 37 | 38 | ### deployContract() 39 | 40 | This method deploys a contract and saves its artifacts to the `./artifacts` directory. 41 | 42 | It takes arguments: 43 | 44 | - `alias` - the alias of the contract 45 | - `version` - the version of the contract, use "latest" to increment version 46 | - `args` - the deployment parameters, the same as you use at `locklift.factory.deployContract()` 47 | - `deployFn` - the function to deploy contract, default value `locklift.factory.deployContract` 48 | 49 | ```ts 50 | const { contract, tx } = await locklift.deployArtifacts.deployContract(alias, version, args); 51 | ``` 52 | 53 | ### getArtifacts() 54 | 55 | This method returns an object with the current state of the build artifacts: 56 | 57 | ```ts 58 | // scripts/get_deploy_artifacts.ts 59 | import { IDeployArtifacts } from "../artifacts/artifacts"; 60 | const artifacts = locklift.deployArtifacts.getArtifacts(); 61 | console.log("SampleA contract address", artifacts.local.SampleA.v0.address) 62 | ``` 63 | 64 | ![read deploy artifacts](https://raw.githubusercontent.com/venom-blockchain/locklift-deploy-artifacts/main/assets/read_deploy_artifacts.png?token=GHSAT0AAAAAAB73MB3NU47KJSJHE3WWGGL4ZAPHIAQ) 65 | 66 | ### reset() 67 | 68 | This method deletes all artifacts and resets storage: 69 | 70 | ```ts 71 | locklift.deployArtifacts.reset(); 72 | ``` 73 | 74 | ### getContract() 75 | 76 | This method returns the instance of the deployed contract. 77 | 78 | ```ts 79 | // scripts/get_deployed_contract.ts 80 | import { IDeployArtifacts } from "../artifacts/artifacts"; 81 | 82 | async function main() { 83 | const artifacts = locklift.deployArtifacts.getArtifacts(); 84 | 85 | const sampleAv0 = locklift.deployArtifacts.getContract(artifacts.test.Sample.SampleA.v0); 86 | 87 | const details = await sampleAv0.methods.getDetails().call(); 88 | console.log("details", details); 89 | } 90 | main() 91 | .then(() => process.exit(0)) 92 | .catch(e => { 93 | console.log(e); 94 | process.exit(1); 95 | }); 96 | 97 | ``` 98 | 99 | ### deploy contract with [locklift-deploy-private](https://www.npmjs.com/package/locklift-private-deploy) plugin 100 | 101 | ```ts 102 | // locklift.config.ts 103 | import { LockliftConfig } from "locklift"; 104 | import { FactorySource } from "./build/factorySource"; 105 | 106 | import { DeployArtifactsExtension } from "locklift-deploy-artifacts"; 107 | import { LockliftConfigExtension, PrivateDeployerExtension } from "locklift-private-deploy"; 108 | import "locklift-deploy-artifacts"; 109 | import "locklift-private-deploy"; 110 | 111 | declare module "locklift" { 112 | export interface Locklift 113 | extends DeployArtifactsExtension, 114 | PrivateDeployerExtension {} 115 | export interface LockliftConfig extends LockliftConfigExtension {} 116 | } 117 | declare global { 118 | const locklift: import("locklift").Locklift; 119 | } 120 | 121 | const LOCAL_NETWORK_ENDPOINT = "http://localhost/graphql"; 122 | 123 | const VENOM_TESTNET_ENDPOINT = process.env.VENOM_TESTNET_ENDPOINT || "https://jrpc-testnet.venom.foundation/rpc"; 124 | const VENOM_TESTNET_TRACE_ENDPOINT = 125 | process.env.VENOM_TESTNET_TRACE_ENDPOINT || "https://gql-testnet.venom.foundation/graphql"; 126 | 127 | const config: LockliftConfig = { 128 | privateRPC: "https://private-rpc.com/rpc", 129 | // ... 130 | } 131 | ``` 132 | 133 | ```ts 134 | // scripts/deploy.ts 135 | async function main() { 136 | console.log("Starting Sample contract deployment..."); 137 | 138 | const signer = (await locklift.keystore.getSigner("0"))!; 139 | 140 | const { contract: sample } = await locklift.deployArtifacts.deployContract( 141 | "SampleA", 142 | "latest", 143 | { 144 | contract: "Sample", 145 | publicKey: signer.publicKey, 146 | initParams: { _nonce: locklift.utils.getRandomNonce() }, 147 | constructorParams: { _state: 0 }, 148 | value: locklift.utils.toNano(1), 149 | }, 150 | locklift.privateRPC.deployContract, // specify deploy function here 151 | ); 152 | 153 | console.log("sample contract address:", sample.address.toString()); 154 | } 155 | 156 | main() 157 | .then(() => process.exit(0)) 158 | .catch(e => { 159 | console.log(e); 160 | process.exit(1); 161 | }); 162 | ``` 163 | -------------------------------------------------------------------------------- /src/handler/artifacts.ts: -------------------------------------------------------------------------------- 1 | // import * as fs from "fs"; 2 | import * as p from "path"; 3 | import fs from "fs-extra"; 4 | 5 | const LATEST_VERSION = "latest"; 6 | const TYPE_DEFINITION_FILE = "artifacts.d.ts"; 7 | 8 | export type NetworkName = string; 9 | export type ContractName = string; 10 | export type AliasName = string; 11 | export type VersionName = string; 12 | 13 | export type Data = Map; 14 | export type Contracts = Map>>; 15 | 16 | export interface Version { 17 | address: string; 18 | codeHash: string; 19 | initParams: any; 20 | constructorParams: any; 21 | publicKey?: string; 22 | updatedAt?: number; 23 | abi: any; 24 | tvc: string; 25 | code: string; 26 | } 27 | 28 | export interface ContractVersion { 29 | address: string; 30 | codeHash: string; 31 | initParams: any; 32 | constructorParams: any; 33 | publicKey: string; 34 | updatedAt: number; 35 | abi: ABI; 36 | code: string; 37 | tvc: string; 38 | } 39 | 40 | export type JournalData = Map; 41 | export type JournalContracts = Map>>; 42 | 43 | export type JournalVersion = Omit; 44 | 45 | export class Artifacts { 46 | private data: Data; 47 | private dir: string; 48 | 49 | constructor(private readonly journalPath: string) { 50 | this.dir = p.dirname(journalPath); 51 | this.data = this.load(); 52 | } 53 | 54 | addContract( 55 | network: string, 56 | contractName: string, 57 | aliasName: string, 58 | versionName: string = LATEST_VERSION, 59 | version: Version, 60 | ) { 61 | let networkContracts = this.data.get(network); 62 | if (!networkContracts) { 63 | (networkContracts = new Map()), this.data.set(network, networkContracts); 64 | } 65 | 66 | let contract = networkContracts.get(contractName); 67 | if (!contract) { 68 | contract = new Map(); 69 | networkContracts.set(contractName, contract); 70 | } 71 | 72 | let alias = contract.get(aliasName); 73 | if (!alias) { 74 | alias = new Map(); 75 | contract.set(aliasName, alias); 76 | } 77 | 78 | version.updatedAt = version.updatedAt ? version.updatedAt : Date.now(); 79 | let ver = 80 | versionName == LATEST_VERSION ? this.getContractNextVersion(network, contractName, aliasName) : versionName; 81 | alias.set(ver, version); 82 | 83 | this.saveJournal(); 84 | this.saveContractArtifacts(network, contractName, aliasName, ver, { 85 | abi: version.abi, 86 | code: version.code, 87 | tvc: version.tvc, 88 | }); 89 | this.saveTypeDefinitions(); 90 | } 91 | getDataObj(): Data { 92 | return this.toObject(this.data); 93 | } 94 | 95 | reset() { 96 | deleteDirectory(this.dir); 97 | this.data = new Map(); 98 | this.saveJournal(); 99 | this.saveTypeDefinitions(); 100 | } 101 | 102 | // loadLogData - loads data from json file and fs and returns it as a map of networks 103 | private load(): Data { 104 | let data = new Map(); 105 | 106 | if (fs.existsSync(this.dir) && fs.existsSync(this.journalPath)) { 107 | const file = fs.readFileSync(this.journalPath, "utf8"); 108 | data = this.parseArtifactsData(file); 109 | } 110 | return data; 111 | } 112 | 113 | // saveLogFile - saves map of networks to json file 114 | private saveJournal() { 115 | fs.ensureDirSync(this.dir); 116 | const journal = this.toObject(this.dataToJournalView()); 117 | fs.writeFileSync(this.journalPath, JSON.stringify(journal, null, 2)); 118 | } 119 | 120 | private saveContractArtifacts( 121 | network: string, 122 | contractName: string, 123 | aliasName: string, 124 | versionName: string, 125 | params: { 126 | abi: any; 127 | code: string; 128 | tvc: string; 129 | }, 130 | ): void { 131 | const versionPath = p.join(this.dir, network, contractName, aliasName, versionName); 132 | 133 | if (!fs.existsSync(versionPath)) { 134 | fs.mkdirSync(versionPath, { recursive: true }); 135 | } 136 | 137 | fs.writeFileSync(p.join(versionPath, "tvc"), params.tvc); 138 | fs.writeFileSync(p.join(versionPath, "abi.json"), JSON.stringify(params.abi, null, 2)); 139 | fs.writeFileSync(p.join(versionPath, "code"), params.code); 140 | fs.writeFileSync(p.join(versionPath, "source.ts"), generateContractCode(JSON.stringify(params.abi), contractName)); 141 | } 142 | 143 | private saveTypeDefinitions() { 144 | const typeDefinitions = createTypeDefinitions("IDeployArtifacts", this.data); 145 | fs.writeFileSync(p.join(this.dir, TYPE_DEFINITION_FILE), typeDefinitions); 146 | } 147 | 148 | private dataToJournalView(): JournalData { 149 | const res = new Map(); 150 | for (const [networkName, networkContracts] of this.data.entries()) { 151 | const contracts = new Map>>(); 152 | for (const [contractName, contract] of networkContracts.entries()) { 153 | const aliases = new Map>(); 154 | for (const [aliasName, alias] of contract.entries()) { 155 | const versions = new Map(); 156 | for (const [versionName, version] of alias.entries()) { 157 | versions.set(versionName, toJournalVersion(version)); 158 | } 159 | aliases.set(aliasName, versions); 160 | } 161 | contracts.set(contractName, aliases); 162 | } 163 | res.set(networkName, contracts); 164 | } 165 | return res; 166 | } 167 | 168 | // parse - parses json string and returns map of networks 169 | private parseArtifactsData(jsonContent: string): Data { 170 | const parsed = new Map>>>(); 171 | if (!jsonContent) { 172 | return parsed; 173 | } 174 | 175 | const json = JSON.parse(jsonContent); 176 | 177 | Object.keys(json).forEach((networkName) => { 178 | const networkContracts = json[networkName]; 179 | if (!networkContracts) { 180 | return parsed; 181 | } 182 | const contracts = new Map>>(); 183 | 184 | Object.keys(networkContracts).forEach((contractName) => { 185 | const contract = networkContracts[contractName]; 186 | const aliases = new Map>(); 187 | Object.keys(contract).forEach((aliasName) => { 188 | const versions = new Map(); 189 | Object.keys(contract[aliasName]).forEach((versionName) => { 190 | const ver: Version = contract[aliasName][versionName]; 191 | 192 | const { abi, code, tvc } = this.getContractArtifacts(networkName, contractName, aliasName, versionName); 193 | const abiObj = JSON.parse(abi); 194 | const extended: Version = { ...ver, abi: abiObj, code, tvc }; 195 | 196 | versions.set(versionName, extended); 197 | }); 198 | aliases.set(aliasName, versions); 199 | }); 200 | contracts.set(contractName, aliases); 201 | }); 202 | 203 | parsed.set(networkName, contracts); 204 | }); 205 | return parsed; 206 | } 207 | 208 | // getContractArtifacts - returns contract artifacts from fs 209 | private getContractArtifacts( 210 | network: string, 211 | contractName: string, 212 | alias: string, 213 | versionName: string, 214 | ): { tvc: string; abi: string; code: string } { 215 | const versionPath = p.join(this.dir, network, contractName, alias, versionName); 216 | const tvc = fs.readFileSync(p.join(versionPath, "tvc"), "utf8"); 217 | const abi = fs.readFileSync(p.join(versionPath, "abi.json"), "utf8"); 218 | const code = fs.readFileSync(p.join(versionPath, "code"), "utf8"); 219 | return { tvc, abi, code }; 220 | } 221 | 222 | private getContractNextVersion(network: string, contractName: string, aliasName: string): string { 223 | let alias = this.data?.get(network)?.get(contractName)?.get(aliasName); 224 | return alias ? `v${alias.size}` : "v0"; 225 | } 226 | 227 | private toObject(map = new Map()): any { 228 | return Object.fromEntries( 229 | Array.from(map.entries(), ([k, v]) => (v instanceof Map ? [k, this.toObject(v)] : [k, v])), 230 | ); 231 | } 232 | } 233 | 234 | export function createTypeDefinitions(interfaceName: string, data: Data): string { 235 | let ii = ""; 236 | for (const [network, contracts] of data) { 237 | let indent = 2; 238 | ii += formatIndent(indent, `${network}: {`); 239 | 240 | indent += 2; 241 | for (const [contract, aliases] of contracts) { 242 | ii += formatIndent(indent, `${contract}: {`); 243 | indent += 2; 244 | for (const [alias, versions] of aliases) { 245 | ii += formatIndent(indent, `${alias}: {`); 246 | indent += 2; 247 | for (const [version, versionData] of versions) { 248 | ii += formatIndent(indent, `${version}: {`); 249 | const obj = typeLeafNode(versionData, network, contract, alias, version); 250 | ii += `\n${stringifyObj(obj, indent + 2)}${" ".repeat(indent)}};`; 251 | } 252 | indent -= 2; 253 | 254 | ii += formatIndent(indent, `};`); 255 | } 256 | indent -= 2; 257 | 258 | ii += formatIndent(indent, `};`); 259 | } 260 | indent -= 2; 261 | ii += formatIndent(indent, `};\n`); 262 | } 263 | return `\nexport interface ${interfaceName} {${ii}}\n`; 264 | } 265 | 266 | export function stringifyObj(obj: any, indent: number): string { 267 | let res = ""; 268 | 269 | for (const key in obj) { 270 | if (typeof obj[key] !== "object") { 271 | res += `${" ".repeat(indent)}${key}: ${obj[key]};\n`; 272 | } else { 273 | res += `${" ".repeat(indent)}${key}: {\n${stringifyObj(obj[key], indent + 2)}${" ".repeat(indent)}};\n`; 274 | } 275 | } 276 | return res; 277 | } 278 | 279 | function formatIndent(indention: number, mask: string): string { 280 | return `\n${" ".repeat(indention)}` + mask; 281 | } 282 | 283 | function typeLeafNode(obj: any, network: string, contract: string, alias: string, version: string): any { 284 | if (typeof obj !== "object") { 285 | return typeof obj; 286 | } 287 | const result: any = {}; 288 | 289 | for (const key in obj) { 290 | if (obj.hasOwnProperty(key)) { 291 | if (key === "abi") { 292 | result[key] = `import("./${network}/${contract}/${alias}/${version}/source").${contract}Abi`; 293 | continue; 294 | } 295 | result[key] = typeLeafNode(obj[key], network, contract, alias, version); 296 | } 297 | } 298 | return result; 299 | } 300 | 301 | function toJournalVersion(v: Version): JournalVersion { 302 | return { 303 | address: v.address, 304 | codeHash: v.codeHash, 305 | initParams: v.initParams, 306 | constructorParams: v.constructorParams, 307 | publicKey: v.publicKey, 308 | updatedAt: v.updatedAt, 309 | }; 310 | } 311 | 312 | function deleteDirectory(directoryPath: string) { 313 | if (fs.existsSync(directoryPath)) { 314 | fs.readdirSync(directoryPath).forEach((file: string) => { 315 | const filePath = p.join(directoryPath, file); 316 | if (fs.lstatSync(filePath).isDirectory()) { 317 | deleteDirectory(filePath); 318 | } else { 319 | fs.unlinkSync(filePath); 320 | } 321 | }); 322 | fs.rmdirSync(directoryPath); 323 | } 324 | } 325 | 326 | function generateContractCode(abiSource: string, contractName: string): string { 327 | const abiSourceName = contractName.slice(0, 1).toLowerCase() + contractName.slice(1) + "Abi"; 328 | 329 | let code = `const ${abiSourceName} = ${abiSource.replace(/\s/g, "")} as const`; 330 | code += `\nexport type ${contractName}Abi = typeof ${abiSourceName};\n`; 331 | 332 | return code; 333 | } 334 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------