-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tutorial to send Shimmer Tokens with iota-sdk in NodeJs #1084
Changes from all commits
42cc331
b34bbe5
5dafbf1
1ad2daf
9fa9d51
e0bd358
a91f40e
6128709
768b93d
75f52a3
d506970
8df9cf2
40416b1
78de517
cfdddfa
204f4e4
b944f10
6615902
5946876
582868b
e1ef5af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,148 @@ | ||||||
# Send Shimmer tokens with IOTA SDK in NodeJs | ||||||
|
||||||
## Introduction | ||||||
|
||||||
This tutorial shows you how to get Shimmer Testnet tokens and send them to another address using JavaScript/TypesSript. | ||||||
|
||||||
### What you will learn | ||||||
|
||||||
- [Create a Stronghold account](#create-an-account) | ||||||
- [Generate a Shimmer address](#generate-an-address) | ||||||
- [Get some Shimmer testnet tokens](https://faucet.testnet.shimmer.network/) | ||||||
- [Fetch your balance](#check-the-balance) | ||||||
- [Send tokens to another address](#send-tokens) | ||||||
|
||||||
## Set Up Your Environment | ||||||
|
||||||
Before you begin, please ensure you have [Installed NodeJs](https://nodejs.org/) and updated it to the latest stable version. | ||||||
|
||||||
Setup a `.env` file, and if you want, you can change the [BIP39 mnemonic](https://en.bitcoin.it/wiki/BIP_0039) and choose a different password, but be sure to set the node URLs to a Shimmer Testnet node (we use the nodes provided by IF in this tutorial). For example: | ||||||
|
||||||
```sh | ||||||
# Mnemonics (Don't ever use them to manage real funds!) | ||||||
MNEMONIC="endorse answer radar about source reunion marriage tag sausage weekend frost daring base attack because joke dream slender leisure group reason prepare broken river" | ||||||
MNEMONIC_2="width scatter jaguar sponsor erosion enable cave since ancient first garden royal luggage exchange ritual exotic play wall clinic ride autumn divert spin exchange" | ||||||
# The Wallet database folder used to store account data | ||||||
WALLET_DB_PATH="./example-walletdb" | ||||||
# The Stronghold snapshot file location used to store secrets | ||||||
STRONGHOLD_SNAPSHOT_PATH="./example.stronghold" | ||||||
# The password to unlock the Stronghold snapshot file (Don't use it to protect real secrets!) | ||||||
STRONGHOLD_PASSWORD="24?drowssap" | ||||||
# The node URL to issue transactions with | ||||||
NODE_URL="https://api.testnet.shimmer.network" | ||||||
# The faucet URL to request test coins from | ||||||
FAUCET_URL="https://faucet.testnet.shimmer.network/api/enqueue" | ||||||
# The explorer URL to look up transactions, blocks, addresses and more | ||||||
EXPLORER_URL="https://explorer.shimmer.network/testnet" | ||||||
``` | ||||||
|
||||||
## Create an account | ||||||
|
||||||
Now that we have all details, let's go through the example code. | ||||||
|
||||||
After including the needed dependencies, we have have the main function that loads the environment variables from the `.env` file we created earlier. It uses the information to set up [Stronghold](/stronghold.rs/welcome) to store our seed safely. | ||||||
anistark marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```js | ||||||
import { Wallet, CoinType, initLogger, WalletOptions } from '@iota/sdk'; | ||||||
|
||||||
// This example uses secrets in environment variables for simplicity which should not be done in production. | ||||||
require('dotenv').config({ path: '.env' }); | ||||||
|
||||||
const walletOptions: WalletOptions = { | ||||||
storagePath: process.env.WALLET_DB_PATH, | ||||||
clientOptions: { | ||||||
nodes: [process.env.NODE_URL], | ||||||
}, | ||||||
coinType: CoinType.Shimmer, | ||||||
secretManager: { | ||||||
stronghold: { | ||||||
snapshotPath: process.env.STRONGHOLD_SNAPSHOT_PATH, | ||||||
password: process.env.STRONGHOLD_PASSWORD, | ||||||
}, | ||||||
}, | ||||||
}; | ||||||
|
||||||
const wallet = new Wallet(walletOptions); | ||||||
|
||||||
// A mnemonic can be generated with `Utils.generateMnemonic()`. | ||||||
// Store the mnemonic in the Stronghold snapshot, this needs to be done only the first time. | ||||||
// The mnemonic can't be retrieved from the Stronghold file, so make a backup in a secure place! | ||||||
await wallet.storeMnemonic(process.env.MNEMONIC); | ||||||
|
||||||
// Create a new account | ||||||
const account = await wallet.createAccount({ | ||||||
alias: 'Alice', | ||||||
}); | ||||||
console.log('Generated new account:', account.getMetadata().alias); | ||||||
``` | ||||||
|
||||||
If everything worked correctly, you will see the message `Generated a new account` and you will find a Stronghold file and a database directory have been created to store the current state of your wallet. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Generate an address | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
In this step, we will generate a new address to receive some testnet tokens. For this we will use the second example called `02_generate_address.rs`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Shouldn't it be As well, if there examples are in a GH repo, could you import them as in the SDK docs? https://github.com/iotaledger/iota-wiki/blob/main/shimmer/docs/iota-sdk/docs/how-tos/exchange-guide.mdx#L267-L269 |
||||||
|
||||||
Here again we read the environment variables from the `.env` file and then we recreate the account manager which will use the Stronghold file and database that were created in the previous step. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```js | ||||||
// To create an address we need to unlock stronghold. | ||||||
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); | ||||||
|
||||||
const address = (await account.generateEd25519Addresses(1))[0]; | ||||||
|
||||||
console.log(`Generated address:`, address.address); | ||||||
``` | ||||||
|
||||||
You can see all testnet addresses begin with `rms`, which is the reverse of what real Shimmer addresses start with. This is how you can tell testnet and real addresses apart. You can get some testnet tokens from the [faucet](https://faucet.testnet.shimmer.network). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Check the balance | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Now you should have some tokens. To validate that, we can use the library to inspect our account. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```js | ||||||
// Sync new outputs from the node. | ||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||||||
const _syncBalance = await account.sync(); | ||||||
|
||||||
// After syncing the balance can also be computed with the local data. (This is optional.) | ||||||
const balance = await account.getBalance(); | ||||||
console.log('Balance', balance); | ||||||
``` | ||||||
|
||||||
This should show a positive balance. If no tokens appear, try to request tokens from the faucet again. If that still doesn't work, please come over to [our Discord](https://discord.iota.org/), and we'll sort it out. | ||||||
|
||||||
## Send Tokens | ||||||
|
||||||
Now that you have some tokens, you can send them to a valid address using the `Account.send` function. | ||||||
|
||||||
```js | ||||||
await account.sync(); | ||||||
|
||||||
// To sign a transaction we need to unlock stronghold. | ||||||
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); | ||||||
|
||||||
// Replace with the address of your choice! | ||||||
const address = | ||||||
'rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu'; | ||||||
const amount = BigInt(1); | ||||||
|
||||||
const transaction = await account.send(amount, address, { | ||||||
allowMicroAmount: true, | ||||||
}); | ||||||
|
||||||
console.log(`Transaction sent: ${transaction.transactionId}`); | ||||||
|
||||||
const blockId = await account.retryTransactionUntilIncluded( | ||||||
transaction.transactionId, | ||||||
); | ||||||
|
||||||
console.log(`Block sent: ${process.env.EXPLORER_URL}/block/${blockId}`); | ||||||
``` | ||||||
|
||||||
This could take some time. The manager will automatically review your addresses to find enough tokens to match the amount you want to send. Then, it will sign and send the resulting transaction to the node. It will warn you when you don't have enough balance, but otherwise, it will show you the transaction ID, which you can use to find your transaction in the [Testnet explorer](https://explorer.testnet.shimmer.network/testnet). | ||||||
|
||||||
Congratulations, you are now able to manage your tokens programmatically! | ||||||
|
||||||
## What's next? | ||||||
|
||||||
You can now create an account, generate addresses, and transfer tokens. Check out the [documentation](https://wiki.iota.org/shimmer/iota-sdk/welcome) to see what more you can do. [Create native tokens](https://wiki.iota.org/shimmer/iota-sdk/how-tos/native-tokens/create/), or [mint non-fungible tokens (NFTs)](https://wiki.iota.org/shimmer/iota-sdk/how-tos/nfts/mint-nft/) and develop your application! Have fun and good luck! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.