Skip to content

Commit

Permalink
Fix some examples (#787)
Browse files Browse the repository at this point in the history
* Fix some examples

* Update more examples

* Remove options
  • Loading branch information
Thoralf-M authored Jul 14, 2023
1 parent baa6c81 commit a296987
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ require('dotenv').config({ path: '.env' });
// This example syncs the account and prints the balance
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ require('dotenv').config({ path: '.env' });
// This example creates an address
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
try {
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}

const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

import { Wallet, initLogger } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/accounts_and_addresses/list-accounts.ts

// This example lists all accounts in the wallet
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

import { Wallet, initLogger } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/accounts_and_addresses/list-addresses.ts

// This example lists all addresses in the account
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

import { Wallet, initLogger } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/accounts_and_addresses/list-outputs.ts

// This example lists all outputs in the account
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@

import { Wallet, initLogger } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/accounts_and_addresses/list-transactions.ts

// This example lists all transactions in the account
async function run() {
initLogger();
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
try {
const wallet = new Wallet({
storagePath: process.env.WALLET_DB_PATH,
Expand Down
15 changes: 11 additions & 4 deletions bindings/nodejs/examples/how_tos/alias/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ async function run() {
if (!process.env.FAUCET_URL) {
throw new Error('.env FAUCET_URL is undefined, see .env.example');
}
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
try {
// Create the wallet
const wallet = new Wallet({
Expand All @@ -27,12 +35,11 @@ async function run() {
// May want to ensure the account is synced before sending a transaction.
let balance = await account.sync();

if (balance.aliases.length == 0) {
throw new Error(`No Alias available in account 'Alice'`);
}

console.log(`Aliases BEFORE:\n`, balance.aliases);

// To sign a transaction we need to unlock stronghold.
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);

console.log('Sending the create-alias transaction...');

// Create an alias
Expand Down
12 changes: 12 additions & 0 deletions bindings/nodejs/examples/how_tos/alias/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ async function run() {
if (!process.env.FAUCET_URL) {
throw new Error('.env FAUCET_URL is undefined, see .env.example');
}
if (!process.env.WALLET_DB_PATH) {
throw new Error('.env WALLET_DB_PATH is undefined, see .env.example');
}
if (!process.env.STRONGHOLD_PASSWORD) {
throw new Error(
'.env STRONGHOLD_PASSWORD is undefined, see .env.example',
);
}
try {
// Create the wallet
const wallet = new Wallet({
Expand All @@ -35,6 +43,10 @@ async function run() {
const aliasId = balance.aliases[0];

console.log(`Aliases BEFORE destroying:\n`, balance.aliases);

// To sign a transaction we need to unlock stronghold.
await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD);

console.log('Sending the destroy-alias transaction...');

// Destroy an alias
Expand Down

0 comments on commit a296987

Please sign in to comment.