let wallet, rpcUrl, connection;
async function envload() {
const envFilePath = ".env";
const defaultEnvContent = `# Please fill in the following environment variablesnRPC_URL=nPRIVATE_KEY=`;
try {
if (!fs.existsSync(envFilePath)) {
fs.writeFileSync(envFilePath, defaultEnvContent, "utf8");
console.log(chalk.green
("Everything is set!")
);
process.exit(0);
}
} catch (error) {
console.error(chalk.red
("Cannot create ENV file due to an error:",
error,
"Please try again.")
);
process.exit(1);
}
dotenv.config();
if (!process.env.PRIVATE_KEY || !process.env.RPC_URL) {
console.error(chalk.red
("Cannot find PRIVATE_KEY and RPC_URL variables. Are you sure you set them right?")
);
process.exit(1);
}
return [
new Wallet(
solanaWeb3.Keypair.fromSecretKey(
bs58.default.decode(process.env.PRIVATE_KEY)
)
),
process.env.RPC_URL,
];
}
async function initialize() { // rename func
await greeting();
[wallet, rpcUrl] = await envload();
connection = new Connection(rpcUrl, "confirmed", { // This can be used to interact to rpc
commitment: "confirmed",
confirmTransactionInitialTimeout: 30000,
});
console.log(wallet); // defined
}
console.log(wallet); // undefined
async function introduction() {
await initialize();
const envFilePath = ".env";
//
}
For some reason wallet outside the function is undefined even though it was declared in global scope and after assignment in function must be defined in the code like any other variable. Is the destucturing assignment making it impossible to do so?
It was impossible for me to use destructuring assignment without a function so everything I did was making it work inside a function and declaring all variables needed in a global scope(which gave me this error)