forked from Invoice-Liquidity-Network/ILN-Smart-Contract
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ts
More file actions
254 lines (215 loc) · 7.92 KB
/
Copy pathdeploy.ts
File metadata and controls
254 lines (215 loc) · 7.92 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env node
import { execSync } from "child_process";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { resolve } from "path";
const WASM_SIZE_BUDGET = 100 * 1024;
const CONTRACTS: Record<string, string> = {
invoice_liquidity: "target/wasm32v1-none/release/invoice_liquidity.wasm",
iln_governance: "target/wasm32v1-none/release/iln_governance.wasm",
iln_distribution: "target/wasm32v1-none/release/iln_distribution.wasm",
reputation_bonus: "target/wasm32v1-none/release/reputation_bonus.wasm",
};
interface DeployResult {
contractName: string;
wasmHash: string;
contractId: string;
}
function parseArgs() {
const args = process.argv.slice(2);
let network: string | undefined;
let dryRun = false;
for (let i = 0; i < args.length; i++) {
if (args[i] === "--network" && i + 1 < args.length) {
network = args[++i];
} else if (args[i] === "--dry-run") {
dryRun = true;
}
}
return { network, dryRun };
}
function runCommand(cmd: string): string {
return execSync(cmd, { encoding: "utf-8", stdio: "pipe" }).trim();
}
function checkNetwork(network: string): void {
const networks = execSync("stellar network ls", { encoding: "utf-8" });
if (!networks.includes(network)) {
console.error(`Network '${network}' is not configured.`);
console.error(`Configure it with: stellar network add --global ${network} --rpc-url <url> --network-passphrase "<passphrase>"`);
process.exit(1);
}
console.log(`Network '${network}' is configured.`);
}
function checkDeployerBalance(network: string): void {
try {
const balance = execSync(
`stellar keys balance deployer --network ${network}`,
{ encoding: "utf-8" }
).trim();
const balanceNum = parseFloat(balance);
if (balanceNum < 5) {
console.error(`Deployer balance is ${balance} XLM — below the recommended minimum of 5 XLM.`);
process.exit(1);
}
console.log(`Deployer balance: ${balance} XLM (sufficient).`);
} catch {
console.error("Failed to check deployer balance. Ensure the 'deployer' key exists and is funded.");
process.exit(1);
}
}
function checkWasmFiles(): void {
let allExist = true;
for (const [name, path] of Object.entries(CONTRACTS)) {
const fullPath = resolve(path);
if (!existsSync(fullPath)) {
console.error(`WASM not found: ${path} (contract: ${name})`);
allExist = false;
continue;
}
const size = readFileSync(fullPath).length;
if (size > WASM_SIZE_BUDGET) {
console.error(`WASM ${path} is ${size} bytes — exceeds ${WASM_SIZE_BUDGET} byte budget.`);
allExist = false;
} else {
console.log(` ${name}: ${(size / 1024).toFixed(1)} KB (OK)`);
}
}
if (!allExist) {
console.error("WASM validation failed. Run 'cargo build --target wasm32v1-none --release' first.");
process.exit(1);
}
}
function computeExpectedContractIds(): Record<string, string> {
const ids: Record<string, string> = {};
for (const [name, path] of Object.entries(CONTRACTS)) {
const fullPath = resolve(path);
const hash = runCommand(
`sha256sum "${fullPath}" | cut -d' ' -f1`
);
ids[name] = hash.substring(0, 8);
}
return ids;
}
function deployContracts(network: string): DeployResult[] {
const results: DeployResult[] = [];
for (const [name, path] of Object.entries(CONTRACTS)) {
console.log(`\nDeploying ${name}...`);
const uploadOutput = runCommand(
`stellar contract upload --network ${network} --source deployer --wasm "${path}"`
);
const wasmHash = uploadOutput.match(/WASM hash: ([a-f0-9]+)/)?.[1];
if (!wasmHash) {
console.error(`Failed to extract WASM hash for ${name}`);
console.error(uploadOutput);
process.exit(1);
}
console.log(` WASM hash: ${wasmHash}`);
const deployOutput = runCommand(
`stellar contract deploy --network ${network} --source deployer --wasm-hash ${wasmHash}`
);
const contractId = deployOutput.match(/Contract ID: ([A-Z0-9]+)/)?.[1];
if (!contractId) {
console.error(`Failed to extract contract ID for ${name}`);
console.error(deployOutput);
process.exit(1);
}
console.log(` Contract ID: ${contractId}`);
results.push({ contractName: name, wasmHash, contractId });
}
return results;
}
function updateReadme(results: DeployResult[]): void {
const readmePath = resolve("README.md");
if (!existsSync(readmePath)) {
console.error("README.md not found — skipping update.");
return;
}
const content = readFileSync(readmePath, "utf-8");
const tableRows = results.map((r) => {
let notes: string;
switch (r.contractName) {
case "invoice_liquidity":
notes = "Primary integration contract; used in [SDK examples](docs/sdk-integration.md)";
break;
case "iln_governance":
notes = "Governance proposals and voting";
break;
case "iln_distribution":
notes = "Rewards distribution";
break;
case "reputation_bonus":
notes = "Reputation-based bonus rules";
break;
default:
notes = "";
}
return `| **\`${r.contractName}\`** | \`${r.contractId}\` | ${notes} |`;
});
const testnetUsdcSac = process.env.TESTNET_USDC_SAC || "CBIELTK6YBZJU5UP2WWQEUCYKLPU6AUNZ2BQ4WWFEIE3USCIHMXQDAMA";
const table = [
"| Resource | Contract ID | Notes |",
"|----------|-------------|-------|",
...tableRows,
`| **Testnet USDC (SAC)** | \`${testnetUsdcSac}\` | Referenced in SDK integration guide |`,
].join("\n");
const startMarker = "<!-- TESTNET_CONTRACT_IDS_START -->";
const endMarker = "<!-- TESTNET_CONTRACT_IDS_END -->";
if (!content.includes(startMarker) || !content.includes(endMarker)) {
console.error("Missing TESTNET_CONTRACT_IDS markers in README.md — skipping update.");
return;
}
const updated = content.replace(
new RegExp(`${startMarker}[\\s\\S]*?${endMarker}`, "m"),
`${startMarker}\n${table}\n${endMarker}`
);
writeFileSync(readmePath, updated, "utf-8");
console.log("README.md updated with new contract IDs.");
}
function promptNetworkConfirmation(network: string): void {
const netLabel = network === "mainnet" ? "MAINNET (PRODUCTION)" : "testnet";
console.log(`\n⚠️ You are about to deploy to: ${netLabel}`);
console.log("Pre-flight checks passed. Proceeding...\n");
}
async function main() {
const { network, dryRun } = parseArgs();
if (!network) {
console.error("Error: --network flag is required (testnet|mainnet).");
console.error("Usage: npx tsx scripts/deploy.ts --network testnet [--dry-run]");
process.exit(1);
}
if (network !== "testnet" && network !== "mainnet") {
console.error(`Error: network must be 'testnet' or 'mainnet', got '${network}'.`);
process.exit(1);
}
console.log(`=== ILN Deploy Script ===`);
console.log(`Network: ${network}`);
console.log(`Mode: ${dryRun ? "DRY-RUN" : "LIVE"}\n`);
console.log("[1/4] Checking network configuration...");
checkNetwork(network);
console.log("\n[2/4] Checking deployer balance...");
checkDeployerBalance(network);
console.log("\n[3/4] Validating WASM files...");
checkWasmFiles();
console.log("\n[4/4] Running deployment...");
promptNetworkConfirmation(network);
if (dryRun) {
console.log("\n[dry-run] Computing expected contract IDs...");
const expectedIds = computeExpectedContractIds();
for (const [name, id] of Object.entries(expectedIds)) {
console.log(` ${name}: ${id} (prefix derived from WASM hash)`);
}
console.log("\n[dry-run] All checks passed. No transactions submitted.");
console.log("Run without --dry-run to deploy.");
process.exit(0);
}
const results = deployContracts(network);
console.log("\n=== Deployment Summary ===");
for (const r of results) {
console.log(`${r.contractName}: ${r.contractId}`);
}
updateReadme(results);
console.log("\nDeployment complete.");
}
main().catch((err) => {
console.error("Deployment failed:", err.message);
process.exit(1);
});