-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-wallets.js
More file actions
53 lines (47 loc) · 2.29 KB
/
Copy pathmigrate-wallets.js
File metadata and controls
53 lines (47 loc) · 2.29 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
const pool = require('./db');
const { initiateDeveloperControlledWalletsClient } = require('@circle-fin/developer-controlled-wallets');
const { createDeveloperWallet } = require('./services/circleService');
require('dotenv').config();
const sdk = initiateDeveloperControlledWalletsClient({
apiKey: process.env.CIRCLE_API_KEY,
entitySecret: process.env.ENTITY_SECRET,
});
async function migrateWallets() {
console.log('🔄 Starting wallet migration to ARC-TESTNET...');
try {
const result = await pool.query('SELECT id, username, wallet_id FROM users');
const users = result.rows;
for (const user of users) {
if (!user.wallet_id) {
console.log(`User ${user.username} has no wallet. Creating ARC-TESTNET wallet...`);
await createDeveloperWallet(user.id, user.username);
continue;
}
try {
const walletRes = await sdk.getWallet({ id: user.wallet_id });
const blockchain = walletRes.data?.wallet?.blockchain;
if (blockchain !== 'ARC-TESTNET') {
console.log(`User ${user.username} has wallet on ${blockchain}. Migrating to ARC-TESTNET...`);
// createDeveloperWallet automatically links the new wallet to the user in the DB
await createDeveloperWallet(user.id, user.username);
console.log(`✅ Migrated user ${user.username}`);
} else {
console.log(`User ${user.username} already has ARC-TESTNET wallet.`);
}
} catch (err) {
console.error(`Error checking wallet for ${user.username}: ${err.response?.data?.message || err.message}`);
// If the wallet is not found or error occurs, try generating a new one
if (err.response?.status === 404 || err.message.includes('not found')) {
console.log(`Wallet not found for ${user.username}, generating new ARC-TESTNET wallet...`);
await createDeveloperWallet(user.id, user.username);
}
}
}
console.log('🎉 Migration complete!');
} catch (e) {
console.error('Migration failed:', e);
} finally {
pool.end();
}
}
migrateWallets();