-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialize.js
117 lines (92 loc) · 3.25 KB
/
initialize.js
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
import 'dotenv/config';
import { mkdirSync, writeFileSync, rmSync, readFileSync } from 'fs';
import { execSync } from 'child_process';
import path from 'path';
import { fileURLToPath } from 'url';
import { sync as glob } from 'glob';
// Load environment variables starting with PUBLIC_ into the environment,
// so we don't need to specify duplicate variables in .env
for (const key in process.env) {
if (key.startsWith('PUBLIC_')) {
process.env[key.substring(7)] = process.env[key];
}
}
console.log('###################### Initializing ########################');
// Get dirname (equivalent to the Bash version)
const __filename = fileURLToPath(import.meta.url);
const dirname = path.dirname(__filename);
// variable for later setting pinned version of soroban in "$(dirname/target/bin/soroban)"
const cli = 'stellar';
// Function to execute and log shell commands
function exe(command) {
console.log(command);
execSync(command, { stdio: 'inherit' });
}
function fundAll() {
exe(`${cli} keys generate ${process.env.SOROBAN_ACCOUNT}`);
}
function removeFiles(pattern) {
console.log(`remove ${pattern}`);
glob(pattern).forEach((entry) => rmSync(entry));
}
function buildAll() {
removeFiles(`${dirname}/target/wasm32-unknown-unknown/release/*.wasm`);
removeFiles(`${dirname}/target/wasm32-unknown-unknown/release/*.d`);
exe(`${cli} contract build`);
}
function filenameNoExtension(filename) {
return path.basename(filename, path.extname(filename));
}
function deploy(wasm) {
exe(`${cli} contract deploy --wasm ${wasm} --ignore-checks --alias ${filenameNoExtension(wasm)}`);
}
function deployAll() {
const contractsDir = `${dirname}/.soroban/contract-ids`;
mkdirSync(contractsDir, { recursive: true });
const wasmFiles = glob(`${dirname}/target/wasm32-unknown-unknown/release/*.wasm`);
wasmFiles.forEach(deploy);
}
function contracts() {
const contractFiles = glob(`${dirname}/.soroban/contract-ids/*.json`);
return contractFiles
.map(path => ({
alias: filenameNoExtension(path),
...JSON.parse(readFileSync(path))
}))
.filter(data => data.ids[process.env.SOROBAN_NETWORK_PASSPHRASE])
.map(data => ({alias: data.alias, id: data.ids[process.env.SOROBAN_NETWORK_PASSPHRASE]}));
}
function bind({alias, id}) {
exe(`${cli} contract bindings typescript --contract-id ${id} --output-dir ${dirname}/packages/${alias} --overwrite`);
}
function bindAll() {
contracts().forEach(bind);
}
function importContract({id, alias}) {
const outputDir = `${dirname}/src/contracts/`;
mkdirSync(outputDir, { recursive: true });
const importContent =
`import * as Client from '${alias}';\n` +
`import { rpcUrl } from './util';\n\n` +
`export default new Client.Client({\n` +
` ...Client.networks.${process.env.SOROBAN_NETWORK},\n` +
` rpcUrl,\n` +
`${
process.env.SOROBAN_NETWORK === "local" || "standalone"
? ` allowHttp: true,\n`
: null
}` +
`});\n`;
const outputPath = `${outputDir}/${alias}.ts`;
writeFileSync(outputPath, importContent);
console.log(`Created import for ${alias}`);
}
function importAll() {
contracts().forEach(importContract);
}
// Calling the functions (equivalent to the last part of your bash script)
fundAll();
buildAll();
deployAll();
bindAll();
importAll();