-
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.
* Add Python exchange examples, fix listen_wallet * snake_case in comment, move example description to the top * Exit after receiving an event * Fix comment and remove empty newline
- Loading branch information
Showing
15 changed files
with
236 additions
and
31 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2023 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This example generates a new random mnemonic. | ||
|
||
from iota_sdk import Utils | ||
|
||
# Set the generated mnemonic as env variable MNEMONIC so it can be used in the next examples. | ||
mnemonic = Utils.generate_mnemonic() | ||
print(f'Mnemonic: {mnemonic}') |
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,47 @@ | ||
# Copyright 2023 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This example creates a new database and account. | ||
|
||
from iota_sdk import Wallet, StrongholdSecretManager, SyncOptions, CoinType | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
# This example uses secrets in environment variables for simplicity which should not be done in production. | ||
load_dotenv() | ||
|
||
if 'WALLET_DB_PATH' not in os.environ: | ||
raise Exception(".env WALLET_DB_PATH is undefined, see .env.example") | ||
if 'NODE_URL' not in os.environ: | ||
raise Exception(".env NODE_URL is undefined, see .env.example") | ||
if 'STRONGHOLD_SNAPSHOT_PATH' not in os.environ: | ||
raise Exception( | ||
".env STRONGHOLD_SNAPSHOT_PATH is undefined, see .env.example") | ||
if 'STRONGHOLD_PASSWORD' not in os.environ: | ||
raise Exception(".env STRONGHOLD_PASSWORD is undefined, see .env.example") | ||
if 'MNEMONIC' not in os.environ: | ||
raise Exception(".env MNEMONIC is undefined, see .env.example") | ||
|
||
client_options = { | ||
'nodes': [os.environ.get('NODE_URL')], | ||
} | ||
|
||
secret_manager = StrongholdSecretManager( | ||
os.environ.get('STRONGHOLD_SNAPSHOT_PATH'), os.environ['STRONGHOLD_PASSWORD']) | ||
|
||
wallet = Wallet(os.environ.get('WALLET_DB_PATH'), | ||
client_options, CoinType.IOTA, secret_manager) | ||
|
||
# Store the mnemonic in the Stronghold snapshot, this only needs to be done once | ||
wallet.store_mnemonic( | ||
os.environ['MNEMONIC']) | ||
|
||
created_account = wallet.create_account('Alice') | ||
account = wallet.get_account('Alice') | ||
|
||
# Set sync_only_most_basic_outputs to True if not interested in outputs that are timelocked, | ||
# have a storage deposit return, expiration or are nft/alias/foundry outputs. | ||
account.set_default_sync_options( | ||
SyncOptions(sync_only_most_basic_outputs=True)) | ||
|
||
print(created_account) |
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,25 @@ | ||
# Copyright 2023 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This example generates an address for an account. | ||
|
||
from iota_sdk import Wallet | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
# This example uses secrets in environment variables for simplicity which should not be done in production. | ||
load_dotenv() | ||
|
||
if 'WALLET_DB_PATH' not in os.environ: | ||
raise Exception(".env WALLET_DB_PATH is undefined, see .env.example") | ||
if 'STRONGHOLD_PASSWORD' not in os.environ: | ||
raise Exception(".env STRONGHOLD_PASSWORD is undefined, see .env.example") | ||
|
||
wallet = Wallet(os.environ.get('WALLET_DB_PATH')) | ||
|
||
wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
address = account.generate_ed25519_addresses(1)[0] | ||
print(f'Address:', address['address']) |
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,29 @@ | ||
# Copyright 2023 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This example gets the balance of an account. | ||
|
||
from iota_sdk import Wallet, SyncOptions | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
# This example uses secrets in environment variables for simplicity which should not be done in production. | ||
load_dotenv() | ||
|
||
if 'WALLET_DB_PATH' not in os.environ: | ||
raise Exception(".env WALLET_DB_PATH is undefined, see .env.example") | ||
|
||
wallet = Wallet(os.environ.get('WALLET_DB_PATH')) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
addresses = account.addresses() | ||
print(f'Addresses:', addresses) | ||
|
||
# Set sync_only_most_basic_outputs to True if not interested in outputs that are timelocked, | ||
# have a storage deposit return, expiration or are nft/alias/foundry outputs. | ||
balance = account.sync(SyncOptions(sync_only_most_basic_outputs=True)) | ||
print('Balance', balance) | ||
|
||
# Use the faucet to send tokens to your address. | ||
print('Fill your address with the Faucet: https://faucet.testnet.shimmer.network/') |
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,55 @@ | ||
# Copyright 2023 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This example listens to the NewOutput event. | ||
|
||
from iota_sdk import Wallet, SyncOptions | ||
from dotenv import load_dotenv | ||
import json | ||
import os | ||
import time | ||
|
||
# This example uses secrets in environment variables for simplicity which should not be done in production. | ||
load_dotenv() | ||
|
||
if 'WALLET_DB_PATH' not in os.environ: | ||
raise Exception(".env WALLET_DB_PATH is undefined, see .env.example") | ||
|
||
wallet = Wallet(os.environ.get('WALLET_DB_PATH')) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
received_event = False | ||
|
||
|
||
def callback(event): | ||
event_dict = json.loads(event) | ||
print('AccountIndex:', event_dict["accountIndex"]) | ||
print('Event:', event_dict["event"]) | ||
|
||
# Exit after receiving an event. | ||
global received_event | ||
received_event = True | ||
|
||
|
||
# Only interested in new outputs here. | ||
wallet.listen(callback, [2]) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
# Use the faucet to send testnet tokens to your address. | ||
print('Fill your address with the faucet: https://faucet.testnet.shimmer.network/') | ||
|
||
addresses = account.addresses() | ||
print('Send funds to:', addresses[0]["address"]) | ||
|
||
# Sync every 5 seconds until the faucet transaction gets confirmed. | ||
for _ in range(100): | ||
if received_event: | ||
exit() | ||
time.sleep(5) | ||
|
||
# Sync to detect new outputs | ||
# Set sync_only_most_basic_outputs to True if not interested in outputs that are timelocked, | ||
# have a storage deposit return , expiration or are nft/alias/foundry outputs. | ||
account.sync(SyncOptions(sync_only_most_basic_outputs=True)) |
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,37 @@ | ||
# Copyright 2023 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
# This example sends tokens to an address. | ||
|
||
from iota_sdk import Wallet, SyncOptions | ||
from dotenv import load_dotenv | ||
import os | ||
|
||
# This example uses secrets in environment variables for simplicity which should not be done in production. | ||
load_dotenv() | ||
|
||
if 'WALLET_DB_PATH' not in os.environ: | ||
raise Exception(".env WALLET_DB_PATH is undefined, see .env.example") | ||
if 'STRONGHOLD_PASSWORD' not in os.environ: | ||
raise Exception(".env STRONGHOLD_PASSWORD is undefined, see .env.example") | ||
if 'EXPLORER_URL' not in os.environ: | ||
raise Exception(".env EXPLORER_URL is undefined, see .env.example") | ||
|
||
wallet = Wallet(os.environ.get('WALLET_DB_PATH')) | ||
|
||
account = wallet.get_account('Alice') | ||
|
||
wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) | ||
|
||
# Set sync_only_most_basic_outputs to True if not interested in outputs that are timelocked, | ||
# have a storage deposit return, expiration or are nft/alias/foundry outputs. | ||
balance = account.sync(SyncOptions(sync_only_most_basic_outputs=True)) | ||
print('Balance', balance) | ||
|
||
transaction = account.send([{ | ||
"address": "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu", | ||
"amount": "1000000", | ||
}]) | ||
print(transaction) | ||
print( | ||
f'Check your block on: {os.environ["EXPLORER_URL"]}/block/{transaction.blockId}') |
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