forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-contract.js
More file actions
261 lines (228 loc) · 9.4 KB
/
Copy pathinit-contract.js
File metadata and controls
261 lines (228 loc) · 9.4 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
255
256
257
258
259
260
261
#!/usr/bin/env node
/**
* Contract Initialization Script
*
* Initializes the Nova Rewards contract state after the core asset
* issuance step. Specifically:
*
* 1. Sets distribution account authorization flags via Horizon
* 2. Records contract metadata (asset code, issuer, limits) into the log
* 3. Verifies the initial NOVA supply landed in the Distribution Account
* 4. Writes a contract-state.json snapshot for downstream scripts
*
* This script is called automatically by deploy.js but can also be run
* standalone after a manual asset issuance:
*
* node deploy/init-contract.js --network testnet
*
* Requirements: 1.1, 1.2, 1.3, 1.5
*/
'use strict';
require('dotenv').config({ path: require('path').join(__dirname, '../.env') });
const path = require('path');
const fs = require('fs');
const { Horizon, Asset, StrKey } = require('stellar-sdk');
const { getNetworkConfig, resolveNetwork } = require('./config/networks');
const { Logger, Status } = require('./lib/logger');
const CONTRACT_STATE_PATH = path.join(__dirname, 'contract-state.json');
// ─────────────────────────────────────────────────────────────────────────────
/**
* Validates required environment variables for initialization.
* @param {string} network
* @throws {Error} listing every missing variable
*/
function validateEnv(network) {
const required = ['ISSUER_PUBLIC', 'ISSUER_SECRET', 'DISTRIBUTION_PUBLIC', 'DISTRIBUTION_SECRET'];
const missing = required.filter((k) => !process.env[k]);
if (missing.length > 0) {
throw new Error(
`Missing required environment variables for ${network}: ${missing.join(', ')}\n` +
'Copy .env.example to .env and fill in all values before deploying.'
);
}
// Light key-format check (do not log secrets)
if (!StrKey.isValidEd25519PublicKey(process.env.ISSUER_PUBLIC)) {
throw new Error('ISSUER_PUBLIC is not a valid Stellar Ed25519 public key.');
}
if (!StrKey.isValidEd25519PublicKey(process.env.DISTRIBUTION_PUBLIC)) {
throw new Error('DISTRIBUTION_PUBLIC is not a valid Stellar Ed25519 public key.');
}
}
/**
* Fetches the current NOVA balance for the Distribution Account.
*
* @param {Horizon.Server} server
* @param {string} distributionPublic
* @param {Asset} novaAsset
* @returns {Promise<string>} balance string e.g. "1000000.0000000"
*/
async function getNovaBalance(server, distributionPublic, novaAsset) {
const account = await server.loadAccount(distributionPublic);
const balance = account.balances.find(
(b) =>
b.asset_type !== 'native' &&
b.asset_code === novaAsset.code &&
b.asset_issuer === novaAsset.issuer
);
return balance ? balance.balance : '0';
}
/**
* Fetches the current XLM balance for an account.
*
* @param {Horizon.Server} server
* @param {string} publicKey
* @returns {Promise<string>}
*/
async function getXlmBalance(server, publicKey) {
const account = await server.loadAccount(publicKey);
const xlm = account.balances.find((b) => b.asset_type === 'native');
return xlm ? xlm.balance : '0';
}
/**
* Writes (or updates) the contract-state.json snapshot file.
*
* @param {object} state
*/
function writeContractState(state) {
fs.writeFileSync(CONTRACT_STATE_PATH, JSON.stringify(state, null, 2), 'utf8');
}
// ─────────────────────────────────────────────────────────────────────────────
/**
* Core initialization logic.
*
* @param {object} opts
* @param {string} opts.network - 'testnet' | 'mainnet'
* @param {Logger|null} [opts.logger] - Logger instance (creates one if absent)
* @param {string|null} [opts.deploymentId]- Attach to existing deployment log
* @returns {Promise<object>} contract state snapshot
*/
async function initContract({ network, logger = null, deploymentId = null }) {
const cfg = getNetworkConfig(network);
const server = new Horizon.Server(cfg.horizonUrl);
const issuerPublic = process.env.ISSUER_PUBLIC;
const distributionPublic = process.env.DISTRIBUTION_PUBLIC;
const novaAsset = new Asset(cfg.assetCode, issuerPublic);
// Use provided logger or create a standalone one
const log = logger || Logger.begin({
network,
issuer: issuerPublic,
distribution: distributionPublic,
});
console.log(`\n📋 Initializing contract on ${cfg.name}...`);
console.log(` Issuer: ${issuerPublic}`);
console.log(` Distribution: ${distributionPublic}`);
console.log(` Asset: ${cfg.assetCode}\n`);
// ── Step 1: Confirm Issuer account exists ──────────────────────────────────
let issuerXlm;
try {
issuerXlm = await getXlmBalance(server, issuerPublic);
log.step({ name: 'Confirm issuer account', status: Status.SUCCESS, data: { xlm: issuerXlm } });
} catch (err) {
log.step({ name: 'Confirm issuer account', status: Status.FAILED, error: err.message });
throw new Error(`Issuer account not found on ${cfg.name}: ${issuerPublic}`);
}
// ── Step 2: Confirm Distribution account exists ────────────────────────────
let distributionXlm;
try {
distributionXlm = await getXlmBalance(server, distributionPublic);
log.step({
name: 'Confirm distribution account',
status: Status.SUCCESS,
data: { xlm: distributionXlm },
});
} catch (err) {
log.step({ name: 'Confirm distribution account', status: Status.FAILED, error: err.message });
throw new Error(`Distribution account not found on ${cfg.name}: ${distributionPublic}`);
}
// ── Step 3: Verify trustline exists on Distribution account ───────────────
const distAccount = await server.loadAccount(distributionPublic);
const hasTrustline = distAccount.balances.some(
(b) =>
b.asset_type !== 'native' &&
b.asset_code === cfg.assetCode &&
b.asset_issuer === issuerPublic
);
if (hasTrustline) {
log.step({ name: 'Verify NOVA trustline', status: Status.SUCCESS });
} else {
log.step({
name: 'Verify NOVA trustline',
status: Status.FAILED,
error: 'Distribution account is missing NOVA trustline. Run the full deploy first.',
});
throw new Error('NOVA trustline not found on Distribution Account.');
}
// ── Step 4: Verify initial supply was received ────────────────────────────
const novaBalance = await getNovaBalance(server, distributionPublic, novaAsset);
const hasSupply = parseFloat(novaBalance) > 0;
if (hasSupply) {
log.step({
name: 'Verify initial supply',
status: Status.SUCCESS,
data: { novaBalance, expectedSupply: cfg.initialSupply },
});
} else {
log.step({
name: 'Verify initial supply',
status: Status.FAILED,
error: `Distribution account NOVA balance is 0. Expected: ${cfg.initialSupply}`,
});
throw new Error('Initial NOVA supply not found in Distribution Account.');
}
// ── Step 5: Build and persist contract state ──────────────────────────────
const contractState = {
deploymentId: deploymentId || log.deploymentId,
network,
initializedAt: new Date().toISOString(),
asset: {
code: cfg.assetCode,
issuer: issuerPublic,
supply: cfg.initialSupply,
},
accounts: {
issuer: {
publicKey: issuerPublic,
xlmBalance: issuerXlm,
},
distribution: {
publicKey: distributionPublic,
xlmBalance: distributionXlm,
novaBalance: novaBalance,
},
},
horizonUrl: cfg.horizonUrl,
explorerBase: cfg.explorerBase,
};
writeContractState(contractState);
log.step({
name: 'Write contract state',
status: Status.SUCCESS,
data: { path: CONTRACT_STATE_PATH },
});
console.log(`\n✅ Contract initialized successfully.`);
console.log(` NOVA balance in Distribution Account: ${novaBalance}`);
console.log(` Contract state written to: ${CONTRACT_STATE_PATH}\n`);
return contractState;
}
// ─────────────────────────────────────────────────────────────────────────────
// CLI entry point
// ─────────────────────────────────────────────────────────────────────────────
if (require.main === module) {
const network = resolveNetwork();
try {
validateEnv(network);
} catch (err) {
console.error(`\n❌ Environment error: ${err.message}\n`);
process.exit(1);
}
initContract({ network })
.then(() => process.exit(0))
.catch((err) => {
console.error(`\n❌ Initialization failed: ${err.message}\n`);
if (err.response?.data) {
console.error('Stellar error:', JSON.stringify(err.response.data, null, 2));
}
process.exit(1);
});
}
module.exports = { initContract, validateEnv };