-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
executable file
·110 lines (98 loc) · 4.47 KB
/
index.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
import { MangoUtils } from "./utils/mango_utils";
import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
import { sleep, Cluster } from "@blockworks-foundation/mango-client";
import { getProgramMap } from "./utils/config"
import * as fs from 'fs';
export async function main() {
// cluster should be in 'devnet' | 'mainnet' | 'localnet' | 'testnet'
const cluster = (process.env.CLUSTER || 'localnet') as Cluster;
const programNameToId = getProgramMap(cluster);
const endpoint = process.env.ENDPOINT_URL || 'http://0.0.0.0:8899';
const connection = new Connection(endpoint, 'confirmed');
const nbUsers = Number(process.env.NB_USERS || "1");
console.log('Connecting to cluster ' + endpoint)
if (!fs.existsSync('authority.json')) {
//create an authority.json
const new_authority = Keypair.generate();
fs.writeFileSync('authority.json', '[' + new_authority.secretKey.toString() + ']');
}
const authority = Keypair.fromSecretKey(
Uint8Array.from(
JSON.parse(
process.env.KEYPAIR ||
fs.readFileSync('authority.json', 'utf-8'),
),
),
);
const do_log_str = process.env.LOG || "false";
const do_log = do_log_str === "true";
console.log('Configuring authority')
const balance = await connection.getBalance(authority.publicKey)
console.log('Authority balance is : ' + balance + ' lamports');
if (balance < (nbUsers + 100) * LAMPORTS_PER_SOL) {
console.log('Balance too low airdropping ' + (nbUsers + 100) + ' SOLs')
const signature = await connection.requestAirdrop(authority.publicKey, (nbUsers + 100) * LAMPORTS_PER_SOL);
await connection.confirmTransaction(signature, 'confirmed');
}
const beginSlot = await connection.getSlot();
console.log('Creating Mango Cookie')
const mangoProgramId = new PublicKey(programNameToId['mango'])
const dexProgramId = new PublicKey(programNameToId['serum_dex']);
const pythProgramId = new PublicKey(programNameToId['pyth_mock']);
let logId = 0
if (do_log) {
logId = connection.onLogs(mangoProgramId, (log, ctx) => {
if (log.err != null) {
console.log("mango error : ", log.err)
}
else {
for (const l of log.logs) {
console.log("mango log : " + l)
}
}
});
}
try {
const mangoUtils = new MangoUtils(connection, authority, mangoProgramId, dexProgramId, pythProgramId);
const cookie = await mangoUtils.createMangoCookie(['MNGO', 'SOL', 'BTC', 'ETH', 'AVAX', 'SRM', 'FTT', 'RAY', 'MNGO', 'BNB', 'GMT', 'ADA'])
console.log('Creating ids.json');
const json = mangoUtils.convertCookie2Json(cookie, cluster)
fs.writeFileSync('ids.json', JSON.stringify(json, null, 2));
fs.writeFileSync('authority.json', '[' + authority.secretKey.toString() + ']');
console.log('Mango cookie created successfully')
console.log('Creating ' + nbUsers + ' Users');
const users = (await mangoUtils.createAndMintUsers(cookie, nbUsers, authority)).map(x => {
const info = {};
info['publicKey'] = x.kp.publicKey.toBase58();
info['secretKey'] = Array.from(x.kp.secretKey);
info['mangoAccountPks'] = [x.mangoAddress.toBase58()];
return info;
})
console.log('created ' + nbUsers + ' Users');
fs.writeFileSync('accounts.json', JSON.stringify(users));
} finally {
if (logId) {
// to log mango logs
await sleep(5000)
const endSlot = await connection.getSlot();
const blockSlots = await connection.getBlocks(beginSlot, endSlot);
console.log("\n\n===============================================")
for (let blockSlot of blockSlots) {
const block = await connection.getBlock(blockSlot);
for (let i = 0; i < block.transactions.length; ++i) {
if (block.transactions[i].meta.logMessages) {
for (const msg of block.transactions[i].meta.logMessages) {
console.log('solana_message : ' + msg);
}
}
}
}
connection.removeOnLogsListener(logId);
}
}
}
main().then(x => {
console.log('finished sucessfully')
}).catch(e => {
console.log('caught an error : ' + e)
})