You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+86-55Lines changed: 86 additions & 55 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,17 @@
1
-
# Solana helpers
1
+
# Solana web3.js v1 helpers
2
2
3
-
The `helpers` package contains Solana helper functions, for use in the browser and/or node.js, [made by the Solana Foundation Developer Ecosystem team](https://youtu.be/zvQIa68ObK8?t=319) and our friends at [Anza](https://anza.xyz), [Turbin3](https://turbin3.com/), [Unboxed Software](https://beunboxed.com/), and [StarAtlas](https://staratlas.com/).
3
+
The `@solana-developers/helpers` package contains Solana helper functions, for use in the browser
4
+
and/or node.js, made by the Solana Foundation's
5
+
[Developer Relations team](https://x.com/solana_devs) and contributions from
@@ -33,6 +44,7 @@ The `helpers` package contains Solana helper functions, for use in the browser a
33
44
[Get an airdrop if your balance is below some amount](#get-an-airdrop-if-your-balance-is-below-some-amount)
34
45
35
46
### Error Handling & Utilities:
47
+
36
48
[Resolve a custom error message](#resolve-a-custom-error-message)
37
49
38
50
[Get a Solana Explorer link for a transaction, address, or block](#get-a-solana-explorer-link-for-a-transaction-address-or-block)
@@ -53,9 +65,11 @@ npm i @solana-developers/helpers
53
65
54
66
## Contributing
55
67
56
-
PRs are very much welcome! Read the [CONTRIBUTING guidelines for the Solana course](https://github.com/Unboxed-Software/solana-course/blob/main/CONTRIBUTING.md#code) then send a PR!
68
+
PRs are very much welcome! Read the
69
+
[CONTRIBUTING guidelines for the Solana course](https://github.com/Unboxed-Software/solana-course/blob/main/CONTRIBUTING.md#code)
70
+
then send a PR!
57
71
58
-
## helpers for the browser and node.js
72
+
## Helpers for the browser and node.js
59
73
60
74
### Make multiple keypairs at once
61
75
@@ -65,17 +79,22 @@ Usage:
65
79
makeKeypairs(amount);
66
80
```
67
81
68
-
In some situations - like making tests for your onchain programs - you might need to make lots of keypairs at once. You can use `makeKeypairs()` combined with JS destructuring to quickly create multiple variables with distinct keypairs.
82
+
In some situations - like making tests for your onchain programs - you might need to make lots of
83
+
keypairs at once. You can use `makeKeypairs()` combined with JS destructuring to quickly create
84
+
multiple variables with distinct keypairs.
69
85
70
86
```typescript
71
87
const [sender, recipient] =makeKeypairs(2);
72
88
```
73
89
74
90
### Make a token mint with metadata
75
91
76
-
The `makeTokenMint` makes a new token mint. A token mint is effectively the factory that produces token of a particular type. So if you want to make a new token, this is the right function for you!
92
+
The `makeTokenMint` makes a new token mint. A token mint is effectively the factory that produces
93
+
token of a particular type. So if you want to make a new token, this is the right function for you!
77
94
78
-
Unlike older tools, the function uses Token Extensions Metadata and Metadata Pointer to put all metadata into the Mint Account, without needing an external Metadata account. If you don't know what that means, just know that things are simpler than they used to be!
95
+
Unlike older tools, the function uses Token Extensions Metadata and Metadata Pointer to put all
96
+
metadata into the Mint Account, without needing an external Metadata account. If you don't know what
97
+
that means, just know that things are simpler than they used to be!
79
98
80
99
Parameters
81
100
@@ -85,7 +104,8 @@ Parameters
85
104
-`symbol`: string, like a ticker symbol. Usually in all-caps.
86
105
-`decimals`: number, how many decimal places the new token will have.
87
106
-`uri`: string, URI to a JSON file containing at minimum a value for `image`.
88
-
-`additionalMetadata`: additional metadata as either `Record<string, string>` or `Array<[string, string]>`(optional).
107
+
-`additionalMetadata`: additional metadata as either `Record<string, string>` or
108
+
`Array<[string, string]>`(optional).
89
109
-`updateAuthority`: PublicKey (optional) - public key of the account that can update the token.
90
110
-`freezeAuthority`: PublicKey (optional) - public key of the freeze account, default to `null`
### Create users, mints and token accounts in a single step
104
124
105
-
Frequently, tests for onchain programs need to make not just users with SOL, but also token mints and give each user some balance of each token. To save this boilerplate, `createAccountsMintsAndTokenAccounts()` handles making user keypairs, giving them SOL, making mints, creating associated token accounts, and minting tokens directly to the associated token accounts.
125
+
Frequently, tests for onchain programs need to make not just users with SOL, but also token mints
126
+
and give each user some balance of each token. To save this boilerplate,
127
+
`createAccountsMintsAndTokenAccounts()` handles making user keypairs, giving them SOL, making mints,
128
+
creating associated token accounts, and minting tokens directly to the associated token accounts.
106
129
107
130
Eg, to make two new users, and two tokens:
108
131
@@ -133,7 +156,8 @@ The returned `usersMintsAndTokenAccounts` will be an object of the form:
133
156
}
134
157
```
135
158
136
-
tokenAccounts are indexed by the user, then the mint. Eg, the ATA of `user[0]` for `mint[0]` is `tokenAccounts[0][0]`.
159
+
tokenAccounts are indexed by the user, then the mint. Eg, the ATA of `user[0]` for `mint[0]` is
@@ -159,7 +184,8 @@ Allows you to turn this message into a more readable message from the custom pro
159
184
160
185
Just:
161
186
162
-
- Get the errors from the specific program's `error.rs` file - for example, there are [the errors for the Token Program](https://github.com/solana-labs/solana-program-library/blob/master/token/program/src/error.rs)
187
+
- Get the errors from the specific program's `error.rs` file - for example, there are
188
+
[the errors for the Token Program](https://github.com/solana-labs/solana-program-library/blob/master/token/program/src/error.rs)
Request and confirm an airdrop in one step. As soon as the `await` returns, the airdropped tokens will be ready to use, and the new balance of tokens will be returned. The `maximumBalance` is used to avoid errors caused by unnecessarily asking for SOL when there's already enough in the account, and makes `airdropIfRequired()` very handy in scripts that run repeatedly.
242
+
Request and confirm an airdrop in one step. As soon as the `await` returns, the airdropped tokens
243
+
will be ready to use, and the new balance of tokens will be returned. The `maximumBalance` is used
244
+
to avoid errors caused by unnecessarily asking for SOL when there's already enough in the account,
245
+
and makes `airdropIfRequired()` very handy in scripts that run repeatedly.
217
246
218
247
To ask for 0.5 SOL, if the balance is below 1 SOL, use:
Will return `"https://explorer.solana.com/address/dDCQNnDmNbFVi8cQhKAgXhyhXeJ625tvwsunRyRc7c8"`. The cluster name isn't included since mainnet-beta is the default.
272
+
Will return `"https://explorer.solana.com/address/dDCQNnDmNbFVi8cQhKAgXhyhXeJ625tvwsunRyRc7c8"`. The
273
+
cluster name isn't included since mainnet-beta is the default.
Then use `getSimulationComputeUnits` to get the number of compute units the instructions will use:
323
346
324
347
```typescript
325
-
const units =awaitgetSimulationComputeUnits(
326
-
connection,
327
-
[sendSol],
328
-
payer.publicKey,
329
-
);
348
+
const units =awaitgetSimulationComputeUnits(connection, [sendSol], payer.publicKey);
330
349
```
331
350
332
-
You can then use `ComputeBudgetProgram.setComputeUnitLimit({ units })` as the first instruction in your transaction. See [How to Request Optimal Compute Budget](https://solana.com/developers/guides/advanced/how-to-request-optimal-compute) for more information on compute units.
351
+
You can then use `ComputeBudgetProgram.setComputeUnitLimit({ units })` as the first instruction in
352
+
your transaction. See
353
+
[How to Request Optimal Compute Budget](https://solana.com/developers/guides/advanced/how-to-request-optimal-compute)
354
+
for more information on compute units.
333
355
334
356
### `addComputeInstructions`
335
357
@@ -358,7 +380,7 @@ This function:
358
380
3. Adds compute unit limit instruction with buffer
359
381
4. Returns the updated instructions array
360
382
361
-
## node.js specific helpers
383
+
## Node.js specific helpers
362
384
363
385
### Get a keypair from a keypair file
364
386
@@ -368,7 +390,8 @@ Usage:
368
390
getKeypairFromFile(filename);
369
391
```
370
392
371
-
Gets a keypair from a file - the format must be the same as [Solana CLI](https://docs.solana.com/wallet-guide/file-system-wallet) uses, ie, a JSON array of numbers:
393
+
Gets a keypair from a file - the format must be the same as
394
+
[Solana CLI](https://docs.anza.xyz/cli/wallets/file-system) uses, ie, a JSON array of numbers:
372
395
373
396
To load the default keypair `~/.config/solana/id.json`, just run:
374
397
@@ -396,7 +419,9 @@ Usage:
396
419
getKeypairFromEnvironment(environmentVariable);
397
420
```
398
421
399
-
Gets a keypair from a secret key stored in an environment variable. This is typically used to load secret keys from [env files](https://stackoverflow.com/questions/68267862/what-is-an-env-or-dotenv-file-exactly).
422
+
Gets a keypair from a secret key stored in an environment variable. This is typically used to load
By default, the keypair will be retrieved from the `.env` file. If a `.env` file does not exist, this function will create one with a new keypair under the optional `envVariableName`.
474
+
By default, the keypair will be retrieved from the `.env` file. If a `.env` file does not exist,
475
+
this function will create one with a new keypair under the optional `envVariableName`.
450
476
451
-
To load the keypair from the filesystem, pass in the `keypairPath`. When set, loading a keypair from the filesystem will take precedence over loading from the `.env` file.
477
+
To load the keypair from the filesystem, pass in the `keypairPath`. When set, loading a keypair from
478
+
the filesystem will take precedence over loading from the `.env` file.
452
479
453
-
If `airdropAmount` amount is set to something other than `null` or `0`, this function will then check the account's balance. If the balance is below the `minimumBalance`, it will airdrop the account `airdropAmount`.
480
+
If `airdropAmount` amount is set to something other than `null` or `0`, this function will then
481
+
check the account's balance. If the balance is below the `minimumBalance`, it will airdrop the
482
+
account `airdropAmount`.
454
483
455
484
To initialize a keypair from the `.env` file, and airdrop it 1 sol if it's beneath 0.5 sol:
We always save keys using the 'array of numbers' format, since most other Solana apps (like the CLI SDK and Rust tools) use the 'array of numbers' format.
532
+
We always save keys using the 'array of numbers' format, since most other Solana apps (like the CLI
533
+
SDK and Rust tools) use the 'array of numbers' format.
503
534
504
535
## Development
505
536
@@ -515,7 +546,8 @@ Then in a different tab, run:
515
546
npm run test
516
547
```
517
548
518
-
The tests use the [node native test runner](https://blog.logrocket.com/exploring-node-js-native-test-runner/).
549
+
The tests use the
550
+
[node native test runner](https://blog.logrocket.com/exploring-node-js-native-test-runner/).
519
551
520
552
If you'd like to run a single test, use:
521
553
@@ -569,8 +601,10 @@ The function will:
569
601
For RPC providers that support priority fees:
570
602
571
603
- Helius: minimum 10000 microLamports
572
-
- Triton: see their [priority fee API](https://docs.triton.one/chains/solana/improved-priority-fees-api)
573
-
- Quicknode: see their [priority fee estimation](https://www.quicknode.com/docs/solana/qn_estimatePriorityFees)
0 commit comments