Unboxed-Software / solana-course

A complete course for learning Solana, including creating and transferring tokens, making NFTs, on-chain app development, and more.
https://solana.com/developers
Mozilla Public License 2.0
395 stars 134 forks source link

Course Chapter 1, Page 3 has unclear and not correct code example #44 #270

Closed Noxdor closed 10 months ago

Noxdor commented 10 months ago

I am talking about this page: https://www.soldev.app/course/intro-to-writing-data

In the Lab section, you can find the following code example which I annotated with comments where I think the block could be improved:

import * as web3 from "@solana/web3.js";
import * as dotenv from "dotenv";
import base58 from "bs58"; // not actually used here (neither in the next block), can be removed
import { getKeypairFromEnvironment } from "@solana-developers/node-helpers"
const toPubkey = new PublicKey(suppliedToPubkey); // suppliedToPubkey is nowhere defined. Maybe show an example with a generated pub key here as in the first tutorial

dotenv.config(); // with es module syntax, it's actually better to just import dotenv/config

const senderKeypair = getKeypairFromEnvironment("SECRET_KEY");

const connection = new Connection("https://api.devnet.solana.com", "confirmed");

console.log(`✅ Loaded our own keypair, the destination public key, and connected to Solana`);

This is what I would suggest the code to update to:

import "dotenv/config"
import { Connection, clusterApiUrl, Keypair } from "@solana/web3.js"
import { getKeypairFromEnvironment } from "@solana-developers/node-helpers"

const { publicKey: toPubkey } = new Keypair()
const senderKeypair = getKeypairFromEnvironment("SECRET_KEY")

const connection = new Connection(clusterApiUrl("devnet"))

console.log(
  `✅ Loaded our own keypair, the destination public key, and connected to Solana`,
)

The original code block on the website is actually not working at all. You import * as web3 from "@solana/web3.js" and then use the PublicKey constructor directly, which is not possible. Instead you would have to call the constructor like this: new web3.PublicKey(suppliedToPubkey).

mikemaccana commented 10 months ago

Thanks @Noxdor! I actually didn't know about import "dotenv/config"; that's cool, and I've tested it here and it works. This is all in the PR above.

Noxdor commented 10 months ago

@mikemaccana I am happy to help and excited, my suggestions will make it to the website :slightly_smiling_face: The dotenv/config import was added, when ES modules were gaining traction and people started switching to them. Have a great week!