Skip to content

Commit

Permalink
feat: addContractDeployment methode added to config builder
Browse files Browse the repository at this point in the history
  • Loading branch information
ipapandinas committed Feb 5, 2024
1 parent 78e0208 commit 01b7cf6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
21 changes: 10 additions & 11 deletions src/commands/contract/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import chalk from "chalk";
import { Contract } from "../../lib/contract.js";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError, ConfigError, FileError } from "../../lib/errors.js";
import { ConfigBuilder } from "../../lib/config-builder.js";

export class DeployContract extends SwankyCommand<typeof DeployContract> {
static description = "Deploy contract to a running node";
Expand Down Expand Up @@ -145,17 +146,15 @@ export class DeployContract extends SwankyCommand<typeof DeployContract> {
}, "Deploying")) as string;

await this.spinner.runCommand(async () => {
contractRecord.deployments = [
...contractRecord.deployments,
{
timestamp: Date.now(),
address: contractAddress,
networkUrl,
deployerAlias: accountAlias!,
},
];

await this.storeConfig(localConfig, 'local');
const deploymentData = {
timestamp: Date.now(),
address: contractAddress,
networkUrl,
deployerAlias: accountAlias!,
};
const configBuilder = new ConfigBuilder(localConfig);
configBuilder.addContractDeployment(args.contractName, deploymentData);
await this.storeConfig(configBuilder.build(), 'local');
}, "Writing config");

this.log(`Contract deployed!`);
Expand Down
21 changes: 14 additions & 7 deletions src/lib/config-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountData, SwankyConfig, SwankySystemConfig } from "../index.js";
import { AccountData, DeploymentData, SwankyConfig, SwankySystemConfig } from "../index.js";
import { snakeCase } from "change-case";

export class ConfigBuilder<T extends SwankySystemConfig | SwankyConfig> {
Expand All @@ -25,31 +25,38 @@ export class ConfigBuilder<T extends SwankySystemConfig | SwankyConfig> {
return this;
}

updateNodeSettings(nodeSettings: Partial<SwankyConfig['node']>): ConfigBuilder<T> {
if ('node' in this.config) {
updateNodeSettings(nodeSettings: Partial<SwankyConfig["node"]>): ConfigBuilder<T> {
if ("node" in this.config) {
this.config.node = { ...this.config.node, ...nodeSettings };
}
return this;
}

updateContracts(contracts: SwankyConfig['contracts']): ConfigBuilder<T> {
if ('contracts' in this.config) {
updateContracts(contracts: SwankyConfig["contracts"]): ConfigBuilder<T> {
if ("contracts" in this.config) {
this.config.contracts = { ...contracts };
}
return this;
}

addContract(name: string, moduleName?: string): ConfigBuilder<T> {
if ('contracts' in this.config) {
if ("contracts" in this.config) {
this.config.contracts[name] = {
name: name,
moduleName: moduleName ?? snakeCase(name),
deployments: []
deployments: [],
};
}
return this;
}

addContractDeployment(name: string, data: DeploymentData): ConfigBuilder<T> {
if ("contracts" in this.config) {
this.config.contracts[name].deployments.push(data);
}
return this;
}

build(): T {
return this.config;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ContractData {
name: string;
moduleName: string;
build?: BuildData;
deployments: DeploymentData[] | [];
deployments: DeploymentData[];
}

export interface BuildData {
Expand Down

0 comments on commit 01b7cf6

Please sign in to comment.