Skip to content
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

Merged
merged 21 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
42cc331
tutorial done
anistark Aug 4, 2023
b34bbe5
tutorial done
anistark Aug 4, 2023
5dafbf1
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 17, 2023
1ad2daf
Merge branch 'main' of github.com:iotaledger/iota-wiki into devx/issu…
anistark Aug 19, 2023
9fa9d51
Merge branch 'devx/issue-175' of github.com:iotaledger/iota-wiki into…
anistark Aug 19, 2023
e0bd358
resolving comments
anistark Aug 19, 2023
a91f40e
updated iota-sdk to IOTA SDK
anistark Aug 29, 2023
6128709
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
768b93d
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
75f52a3
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
d506970
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
8df9cf2
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
40416b1
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
78de517
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
cfdddfa
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
204f4e4
Update tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
anistark Aug 29, 2023
b944f10
added shortcut links
anistark Aug 30, 2023
6615902
added shortcut links
anistark Aug 30, 2023
5946876
Fix broken links
Dr-Electron Aug 30, 2023
582868b
Merge branch 'main' into devx/issue-175
Dr-Electron Aug 30, 2023
e1ef5af
Format
Dr-Electron Aug 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tutorials/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ module.exports = {
],
},
],
[
'@iota-wiki/plugin-tutorial',
{
title: 'Send Shimmer Tokens with IOTA SDK in NodeJs',
description:
'In this tutorial you will learn how to send Shimmer Tokens with IOTA SDK in NodeJs.',
preview: '/send-shimmer-tokens-with-iota-sdk.png',
route: 'tutorials/send-shimmer-tokens-with-iota-sdk',
tags: ['text', 'iota-sdk', 'sdk', 'getting-started', 'rust', 'shimmer'],
},
],
],
staticDirectories: [path.resolve(__dirname, 'static')],
};
148 changes: 148 additions & 0 deletions tutorials/pages/send-shimmer-tokens-with-iota-sdk.md
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Create an account
## 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
After including the needed dependencies, we 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.


```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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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.


## Generate an address
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Generate an address
## Generate An Address


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`.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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`.
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`.

Shouldn't it be .js or .ts?

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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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 created in the previous step.


```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).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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).
All Testnet addresses begin with `rms`, the reverse of what real Shimmer addresses start with. This is how you can tell Testnet and real addresses apart. You can get Testnet tokens from the [faucet](https://faucet.testnet.shimmer.network).


## Check the balance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Check the balance
## Check the Balance


Now you should have some tokens. To validate that, we can use the library to inspect our account.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Now, you should have some tokens. To validate that, we can use the library to inspect our account.


```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!
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.