-
Notifications
You must be signed in to change notification settings - Fork 30
/
deploy.ts
171 lines (150 loc) · 5.04 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Args, Flags } from "@oclif/core";
import { cryptoWaitReady } from "@polkadot/util-crypto/crypto";
import {
AbiType,
ChainAccount,
ChainApi,
decrypt,
resolveNetworkUrl,
ensureAccountIsSet,
getSwankyConfig,
findContractRecord,
} from "../../lib/index.js";
import { BuildMode, Encrypted, SwankyConfig } from "../../types/index.js";
import inquirer from "inquirer";
import chalk from "chalk";
import { SwankyCommand } from "../../lib/swankyCommand.js";
import { ApiError, ProcessError } from "../../lib/errors.js";
import { ConfigBuilder } from "../../lib/config-builder.js";
import {
contractFromRecord,
ensureArtifactsExist,
ensureDevAccountNotInProduction,
} from "../../lib/checks.js";
export class DeployContract extends SwankyCommand<typeof DeployContract> {
static description = "Deploy contract to a running node";
static flags = {
account: Flags.string({
description: "Account alias to deploy contract with",
}),
gas: Flags.integer({
char: "g",
}),
args: Flags.string({
char: "a",
multiple: true,
}),
constructorName: Flags.string({
default: "new",
char: "c",
description: "Constructor function name of a contract to deploy",
}),
network: Flags.string({
char: "n",
default: "local",
description: "Network name to connect to",
}),
};
static args = {
contractName: Args.string({
name: "contractName",
required: true,
description: "Name of the contract to deploy",
}),
};
async run(): Promise<void> {
const { args, flags } = await this.parse(DeployContract);
const localConfig = getSwankyConfig("local") as SwankyConfig;
const contractRecord = findContractRecord(localConfig, args.contractName);
const contract = await contractFromRecord(contractRecord);
await ensureArtifactsExist(contract);
if (contract.buildMode === undefined) {
throw new ProcessError(
`Build mode is undefined for contract ${args.contractName}. Please ensure the contract is correctly compiled.`
);
} else if (contract.buildMode !== BuildMode.Verifiable) {
await inquirer
.prompt([
{
type: "confirm",
message: `You are deploying a not verified contract in ${
contract.buildMode === BuildMode.Release ? "release" : "debug"
} mode. Are you sure you want to continue?`,
name: "confirm",
},
])
.then((answers) => {
if (!answers.confirm) {
this.log(
`${chalk.redBright("✖")} Aborted deployment of ${chalk.yellowBright(
args.contractName
)}`
);
process.exit(0);
}
});
}
ensureAccountIsSet(flags.account, this.swankyConfig);
const accountAlias = (flags.account ?? this.swankyConfig.defaultAccount)!;
const accountData = this.findAccountByAlias(accountAlias);
ensureDevAccountNotInProduction(accountData, flags.network);
const mnemonic = accountData.isDev
? (accountData.mnemonic as string)
: decrypt(
accountData.mnemonic as Encrypted,
(
await inquirer.prompt([
{
type: "password",
message: `Enter password for ${chalk.yellowBright(accountData.alias)}: `,
name: "password",
},
])
).password
);
const account = (await this.spinner.runCommand(async () => {
await cryptoWaitReady();
return new ChainAccount(mnemonic);
}, "Initialising")) as ChainAccount;
const { abi, wasm } = (await this.spinner.runCommand(async () => {
const abi = await contract.getABI();
const wasm = await contract.getWasm();
return { abi, wasm };
}, "Getting WASM")) as { abi: AbiType; wasm: Buffer };
const networkUrl = resolveNetworkUrl(this.swankyConfig, flags.network ?? "");
const api = (await this.spinner.runCommand(async () => {
const api = await ChainApi.create(networkUrl);
await api.start();
return api;
}, "Connecting to node")) as ChainApi;
const contractAddress = (await this.spinner.runCommand(async () => {
try {
const contractAddress = await api.deploy(
abi,
wasm,
flags.constructorName,
account.pair,
flags.args!,
flags.gas
);
return contractAddress;
} catch (cause) {
throw new ApiError("Error deploying", { cause });
}
}, "Deploying")) as string;
await this.spinner.runCommand(async () => {
const deploymentData = {
timestamp: Date.now(),
address: contractAddress,
networkUrl,
deployerAlias: accountAlias,
};
const newLocalConfig = new ConfigBuilder(localConfig)
.addContractDeployment(args.contractName, deploymentData)
.build();
await this.storeConfig(newLocalConfig, "local");
}, "Writing config");
this.log(`Contract deployed!`);
this.log(`Contract address: ${contractAddress}`);
}
}