Skip to content

Commit

Permalink
fix: edits to writing data lesson (#2)
Browse files Browse the repository at this point in the history
* consistent formatting

I dunno if you want to include the ? in the `data?` key/field, but it's 
easier on my eye to have the three consistently formatted - mostly just 
cuz they are written about out of order.  Also - prolly my 
misunderstanding, are the three keys in the export type 
TransationInstructionCtorFields object supposed to be separated by semi 
colons or commas?

* miissing period

I suggest also replacing "Then,..." with "Following that,..."

* add pingProgram() to main()

* maybe it matters, but my markdown preview ignores the trailing >

* consistent spacing

* awk

* missing ![bang](pow)
  • Loading branch information
mixelpixel authored Apr 26, 2022
1 parent 02aae6f commit 0c61a5f
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions content/intro-to-writing-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
toPubkey: recipient,
lamports: LAMPORTS_PER_SOL * amount
})

transaction.add(sendSolInstruction)

const signature = sendAndConfirmTransaction(
connection,
transaction,
Expand Down Expand Up @@ -142,11 +142,11 @@ Per the definition above, the the object passed to the `TransactionInstruction`
- the public key for the program being called
- an optional `Buffer` containing data to pass to the program.

Well be ignoring the data field for now and will revisit it in a future lesson.
Well be ignoring the `data` field for now and will revisit it in a future lesson.

The `programId` field is fairly self explanatory: its the public key associated with the program. Youll need to know this in advance of calling the program in the same way that youd need to know the public key of someone to whom you want to send SOL.

The `keys` array requires a bit more explanation. Each object in this array represents an account that will be read from or written to during a transaction's execution. This means you need to know the behavior of the program you are calling and ensure that you provide all of the necessary accounts in the array.
The `keys` array requires a bit more explanation. Each object in this array represents an account that will be read from or written to during a transaction's execution. This means you need to know the behavior of the program you are calling and ensure that you provide all of the necessary accounts in the array.

Each object in the `keys` array must include the following:
- `pubkey` - the public key of the account
Expand Down Expand Up @@ -292,9 +292,9 @@ main().then(() => {
})
```

Most of this code is just boilerplate to run the file properly. The lines inside of the `main()` function generate a new keypair and log the secret key to the console.
Most of this code is just boilerplate to run the file properly. The lines inside of the `main()` function generate a new keypair and log the secret key to the console.

Run `npm start` after saving this file and you should see an array of numbers printed to the console. This array represents the secret key for your new keypair. **Do not** use this keypair for Mainnet operations. **Only use this keypair for testing.**
Run `npm start` after saving this file and you should see an array of numbers printed to the console. This array represents the secret key for your new keypair. **Do not** use this keypair for Mainnet operations. **Only use this keypair for testing.**

Copy the secret key array from the console log and paste it into the `.env` file as an environment variable called, `PRIVATE_KEY`. This way we can reuse this keypair in future development instead of generating a new keypair every time we run something. It should look something like this but with different numbers:

Expand Down Expand Up @@ -385,7 +385,7 @@ async function pingProgram(connection: web3.Connection, payer: web3.Keypair) {
}
```
Next, let’s add the instruction to the transaction we created at the start of the function Then, call upon `sendAndConfirmTransaction()` by passing in the connection, transaction, and payer. Finally, let’s log the result of that function call so we can look it up on the Solana Explorer.
Next, let’s add the instruction to the transaction we created at the start of the function. Then, call upon `sendAndConfirmTransaction()` by passing in the connection, transaction, and payer. Finally, let’s log the result of that function call so we can look it up on the Solana Explorer.
```tsx
async function pingProgram(connection: web3.Connection, payer: web3.Keypair) {
Expand Down Expand Up @@ -416,13 +416,21 @@ async function pingProgram(connection: web3.Connection, payer: web3.Keypair) {
console.log(sig)
}
```
Finally, let's invoke `pingProgram()` within `main()` using `connection` and `payer`:
```tsx
async function main() {
const payer = initializeKeypair()
const connection = new web3.Connection(web3.clusterApiUrl('devnet'))
await pingProgram(connection, payer)
}
```
### 5. Airdrop
Now run the code with `npm start` and see if it works. You may end up with the following error in the console:
> Transaction simulation failed: Attempt to debit an account but found no record of a prior credit.
>
If you get this error, it’s because your keypair is brand new and doesn’t have any SOL to cover the transaction fees. Let’s fix this by adding the following line in `main()` before the call to `pingProgram()`:
Expand All @@ -434,15 +442,15 @@ This will deposit 1 SOL into your account which you can use for testing. This wo
### 6. Check the Solana Explorer
Now run the code again. It may take a moment or two, but now the code should work and you should see a long string like the following printed to the console:
Now run the code again. It may take a moment or two, but now the code should work and you should see a long string printed to the console, like the following:
```
55S47uwMJprFMLhRSewkoUuzUs5V6BpNfRx21MpngRUQG3AswCzCSxvQmS3WEPWDJM7bhHm3bYBrqRshj672cUSG
```
Copy this confirmation signature. Open a browser and go to [https://explorer.solana.com/?cluster=devnet](https://explorer.solana.com/?cluster=devnet) (the query parameter at the end of the URL will ensure that you’ll explore transactions on Devnet instead of Mainnet). Paste the signature into the search bar at the top of Solana’s Devnet explorer and hit enter. You should see all the details about the transaction. If you scroll all the way to the bottom, then you will see `Program Logs`, which show how many times the program has been pinged including your ping.
[Screenshot of Solana Explorer with logs from calling the Ping program](../assets/solana-explorer-ping-result.png)
![Screenshot of Solana Explorer with logs from calling the Ping program](../assets/solana-explorer-ping-result.png)
If you want to make it easier to look at Solana Explorer for transactions in the future, simply change your `console.log` in `pingProgram()` to the following:
Expand All @@ -462,4 +470,4 @@ In the next few lessons you’ll learn how to
Go ahead and create a script from scratch that will allow you to transfer SOL from one account to another on Devnet. Be sure to print out the transaction signature so you can look at it on the Solana Explorer.
If you get stuck feel free to glance at the [solution code](https://github.com/Unboxed-Software/solana-send-sol-client).
If you get stuck feel free to glance at the [solution code](https://github.com/Unboxed-Software/solana-send-sol-client).

0 comments on commit 0c61a5f

Please sign in to comment.