-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* little improvements * add get_validators example with optional pagination * the example * fix example doc * update * python: add example * nodejs: add example * update year Co-authored-by: Thoralf-M <[email protected]> * rust: update example * python: update example * nodejs: update example * python: nit * docs * fix * single letter closures --------- Co-authored-by: Thoralf-M <[email protected]> Co-authored-by: Thibault Martinez <[email protected]>
- Loading branch information
1 parent
9e70832
commit 12e5ec8
Showing
7 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { Client, initLogger } from '@iota/sdk'; | ||
require('dotenv').config({ path: '.env' }); | ||
|
||
// Run with command: | ||
// yarn run-example ./client/get-validators.ts [PAGE_SIZE] [CURSOR] | ||
|
||
// This example returns the validators known by the node by querying the corresponding endpoint. | ||
// You can provide a custom PAGE_SIZE and additionally a CURSOR from a previous request. | ||
async function run() { | ||
initLogger(); | ||
for (const envVar of ['NODE_URL']) { | ||
if (!(envVar in process.env)) { | ||
throw new Error(`.env ${envVar} is undefined, see .env.example`); | ||
} | ||
} | ||
|
||
const client = await Client.create({ | ||
// Insert your node URL in the .env. | ||
nodes: [process.env.NODE_URL as string], | ||
}); | ||
|
||
let pageSize = 1; | ||
let cursor = ''; | ||
if (process.argv.length > 1) { | ||
pageSize = parseInt(process.argv[2]); | ||
if (process.argv.length > 2) { | ||
cursor = process.argv[3]; | ||
} | ||
} | ||
|
||
try { | ||
const validators = await client.getValidators(pageSize, cursor); | ||
console.log(validators); | ||
} catch (error) { | ||
console.error('Error: ', error); | ||
} | ||
} | ||
|
||
void run().then(() => process.exit()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import dataclasses | ||
import json | ||
import os | ||
import sys | ||
|
||
from dotenv import load_dotenv | ||
from iota_sdk import Client | ||
|
||
load_dotenv() | ||
|
||
node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') | ||
page_size = 1 | ||
cursor = "" | ||
|
||
if len(sys.argv) > 1: | ||
page_size = int(sys.argv[1]) | ||
if len(sys.argv) > 2: | ||
cursor = sys.argv[2] | ||
|
||
# Create a Client instance | ||
client = Client(nodes=[node_url]) | ||
|
||
validators = client.get_validators(page_size, cursor) | ||
print(f'{json.dumps(dataclasses.asdict(validators), indent=4)}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright 2024 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! This example returns the validators known by the node by querying the corresponding endpoint. | ||
//! You can provide a custom PAGE_SIZE and additionally a CURSOR from a previous request. | ||
//! | ||
//! Rename `.env.example` to `.env` first, then run the command: | ||
//! ```sh | ||
//! cargo run --release --all-features --example node_api_core_get_validators [PAGE_SIZE] [CURSOR] [NODE_URL] | ||
//! ``` | ||
|
||
use iota_sdk::client::{Client, ClientError}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), ClientError> { | ||
// If not provided we use the default node from the `.env` file. | ||
dotenvy::dotenv().ok(); | ||
|
||
let page_size = std::env::args().nth(1).map(|s| s.parse::<u32>().unwrap()); | ||
let cursor = std::env::args().nth(2); | ||
|
||
// Take the node URL from command line argument or use one from env as default. | ||
let node_url = std::env::args() | ||
.nth(3) | ||
.unwrap_or_else(|| std::env::var("NODE_URL").expect("NODE_URL not set")); | ||
|
||
// Create a node client. | ||
let client = Client::builder() | ||
.with_node(&node_url)? | ||
.with_ignore_node_health() | ||
.finish() | ||
.await?; | ||
|
||
// Get validators. | ||
let validators = client.get_validators(page_size, cursor).await?; | ||
|
||
println!("{validators:#?}"); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters