UNPKG

@flipflop-sdk/node

Version:

FlipFlop Node.js SDK for programmatic token operations

127 lines • 5.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initializeSystemConfigAccount = void 0; const web3_js_1 = require("@solana/web3.js"); const utils_1 = require("./utils"); const constants_1 = require("./constants"); const config_1 = require("./config"); const anchor_1 = require("@coral-xyz/anchor"); // Init function const initializeSystemConfigAccount = async (options) => { if (!options.rpc) { return { success: false, message: "Missing rpc parameter", }; } if (!options.systemManager) { return { success: false, message: "Missing system-manager parameter", }; } const rpcUrl = options.rpc; const rpc = new web3_js_1.Connection(rpcUrl, "confirmed"); const systemManager = options.systemManager; const { program, provider, programId } = await (0, utils_1.initProvider)(rpc, systemManager); let lookupTableAddress; let createdNewLUT = false; try { lookupTableAddress = new web3_js_1.PublicKey(config_1.CONFIGS[(0, config_1.getNetworkType)(rpcUrl)].lookupTableAccount || ""); const accountInfo = await provider.connection.getParsedAccountInfo(lookupTableAddress); if (!accountInfo.value) { console.log("āš ļø LUT account does not exist, creating new LUT..."); const lut = await (0, utils_1.createLookupTable)(provider.connection, systemManager); lookupTableAddress = lut.key; console.log("\nšŸŽ‰ New LUT created successfully!"); console.log(`šŸ“‹ LUT Address: ${lookupTableAddress.toBase58()}`); console.log("\nšŸ“ Next Steps:"); console.log(" 1. Update LOOKUP_TABLE_ACCOUNT in config.ts with this address"); console.log(" 2. Run the init command again to complete system setup"); process.exit(0); } } catch (error) { console.log("āš ļø Invalid LUT address in config, creating new LUT..."); const lut = await (0, utils_1.createLookupTable)(provider.connection, systemManager); lookupTableAddress = lut.key; console.log("\nšŸŽ‰ New LUT created successfully!"); console.log(`šŸ“‹ LUT Address: ${lookupTableAddress.toBase58()}`); console.log("\nšŸ“ Next Steps:"); console.log(" 1. Update LOOKUP_TABLE_ACCOUNT in config.ts with this address"); console.log(" 2. Run the init command again to complete system setup"); process.exit(0); } const [systemConfigAccount] = web3_js_1.PublicKey.findProgramAddressSync([ Buffer.from(constants_1.SYSTEM_CONFIG_SEEDS), new web3_js_1.PublicKey(config_1.CONFIGS[(0, config_1.getNetworkType)(rpcUrl)].systemManagerAccount).toBuffer(), ], programId); // Check if system config exists let systemConfigExists = false; let existingConfig = null; if (await (0, utils_1.checkAccountExists)(rpc, systemConfigAccount)) { systemConfigExists = true; existingConfig = await program.account.systemConfigData.fetch(systemConfigAccount); } let initializationTx = null; if (!systemConfigExists) { const context = { admin: systemManager.publicKey, systemConfigAccount: systemConfigAccount, systemProgram: web3_js_1.SystemProgram.programId, }; initializationTx = await program.methods .initializeSystem() .accounts(context) .signers([systemManager]) .rpc(); await provider.connection.confirmTransaction(initializationTx, "confirmed"); } // Initialize Launch Rule Account const launchRuleAccount = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(constants_1.LAUNCH_RULE_SEEDS), systemManager.publicKey.toBuffer()], program.programId)[0]; console.log("Launch rule account: ", launchRuleAccount.toBase58()); const info = await provider.connection.getAccountInfo(launchRuleAccount); if (info) { console.log("Launch rule account was created, launch rule address: " + launchRuleAccount.toBase58()); const launchRuleData = await program.account.launchRuleData.fetch(launchRuleAccount); console.log("Launch rule data", Object.fromEntries(Object.entries(launchRuleData).map(([key, value]) => [ key, value.toString(), ]))); } const context2 = { admin: systemManager.publicKey, launchRuleData: launchRuleAccount, systemProgram: web3_js_1.SystemProgram.programId, }; const tx = await program.methods .initializeLaunchRule( // ###### Update the start_slot and slots_per_period for different network new anchor_1.BN("0"), // start_slot new anchor_1.BN("10"), // slots_per_period new anchor_1.BN("3"), // base_launch_limit new anchor_1.BN("4"), // increasement_launch_limit new anchor_1.BN("12") // max_period ) .accounts(context2) .signers([systemManager]) .rpc(); await provider.connection.confirmTransaction(tx, "confirmed"); console.log("Initialize LaunchRule tx: ", tx); // Return structured data return { success: true, data: { lookupTableAddress: lookupTableAddress, systemConfigAddress: systemConfigAccount, systemManager: systemManager.publicKey, createdNewLUT, // systemConfigExists, // initializationTx, // configuration: existingConfig }, }; }; exports.initializeSystemConfigAccount = initializeSystemConfigAccount; //# sourceMappingURL=init.js.map