Skip to content

Commit a734b35

Browse files
Godspeed-execffls
andauthored
add more examples scripts (#399)
* add more examples scripts * exclude tempfiles * delete tempfiles * remove external file and generate local pdf * Fix format --------- Co-authored-by: Michiel <michiel.bellengmail.com> Co-authored-by: Jerry <[email protected]>
1 parent 1abada7 commit a734b35

16 files changed

+1417
-0
lines changed

examples/more_examples/.env-example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
network="testnet"
2+
wallet_mnemonic="select calm scorpion mask furnace nerve fade slam bid suggest avoid remove half depend turn little midnight fossil submit cart sick glance inner slide"
3+
blockfrost_api_key="preprod...."

examples/more_examples/.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.env
2+
/keys/*
3+
!keys/.gitkeep
4+
tempfile.pdf
5+
tempfile_copy.pdf

examples/more_examples/01_address.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import os
2+
3+
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS
4+
from dotenv import load_dotenv
5+
6+
from pycardano import *
7+
8+
load_dotenv()
9+
network = os.getenv("network")
10+
wallet_mnemonic = os.getenv("wallet_mnemonic")
11+
12+
13+
if network == "testnet":
14+
base_url = ApiUrls.preprod.value
15+
cardano_network = Network.TESTNET
16+
else:
17+
base_url = ApiUrls.mainnet.value
18+
cardano_network = Network.MAINNET
19+
20+
21+
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic)
22+
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0")
23+
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0")
24+
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key)
25+
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key)
26+
27+
28+
print("Enterprise address (only payment):")
29+
print("Payment Derivation path: m/1852'/1815'/0'/0/0")
30+
31+
enterprise_address = Address(
32+
payment_part=payment_skey.to_verification_key().hash(), network=cardano_network
33+
)
34+
print(enterprise_address)
35+
36+
print(" ")
37+
print("Staking enabled address:")
38+
print("Payment Derivation path: m/1852'/1815'/0'/0/0")
39+
print("Staking Derivation path: m/1852'/1815'/0'/2/0")
40+
41+
staking_enabled_address = Address(
42+
payment_part=payment_skey.to_verification_key().hash(),
43+
staking_part=staking_skey.to_verification_key().hash(),
44+
network=cardano_network,
45+
)
46+
print(staking_enabled_address)
47+
48+
print(" ")
49+
next_step = input("Press Enter to continue...")
50+
print(" ")
51+
52+
for i in range(5):
53+
derivation_path = f"m/1852'/1815'/0'/0/{i}"
54+
55+
payment_key = new_wallet.derive_from_path(derivation_path)
56+
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key)
57+
58+
enterprise_address = Address(
59+
payment_part=payment_skey.to_verification_key().hash(), network=cardano_network
60+
)
61+
print(f"Address {derivation_path}: {enterprise_address}")
62+
63+
print(" ")
64+
next_step = input("Press Enter to continue...")
65+
print(" ")
66+
67+
for i in range(5):
68+
derivation_path = f"m/1852'/1815'/0'/0/{i}"
69+
70+
payment_key = new_wallet.derive_from_path(derivation_path)
71+
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key)
72+
73+
staking_enabled_address = Address(
74+
payment_part=payment_skey.to_verification_key().hash(),
75+
staking_part=staking_skey.to_verification_key().hash(),
76+
network=cardano_network,
77+
)
78+
print(f"Address {derivation_path}: {staking_enabled_address}")
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import sys
3+
4+
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS
5+
from dotenv import load_dotenv
6+
7+
from pycardano import *
8+
9+
load_dotenv()
10+
network = os.getenv("network")
11+
wallet_mnemonic = os.getenv("wallet_mnemonic")
12+
blockfrost_api_key = os.getenv("blockfrost_api_key")
13+
14+
15+
if network == "testnet":
16+
base_url = ApiUrls.preprod.value
17+
cardano_network = Network.TESTNET
18+
else:
19+
base_url = ApiUrls.mainnet.value
20+
cardano_network = Network.MAINNET
21+
22+
23+
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic)
24+
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0")
25+
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0")
26+
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key)
27+
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key)
28+
29+
30+
main_address = Address(
31+
payment_part=payment_skey.to_verification_key().hash(),
32+
staking_part=staking_skey.to_verification_key().hash(),
33+
network=cardano_network,
34+
)
35+
36+
print(" ")
37+
print(f"Derived address: {main_address}")
38+
print(" ")
39+
40+
api = BlockFrostApi(project_id=blockfrost_api_key, base_url=base_url)
41+
42+
try:
43+
utxos = api.address_utxos(main_address)
44+
except Exception as e:
45+
if e.status_code == 404:
46+
print("Address does not have any UTXOs. ")
47+
if network == "testnet":
48+
print(
49+
"Request tADA from the faucet: https://docs.cardano.org/cardano-testnets/tools/faucet/"
50+
)
51+
else:
52+
print(e.message)
53+
sys.exit(1)
54+
55+
print(f"hash \t\t\t\t\t\t\t\t\t amount")
56+
print(
57+
"--------------------------------------------------------------------------------------"
58+
)
59+
60+
for utxo in utxos:
61+
tokens = ""
62+
for token in utxo.amount:
63+
if token.unit != "lovelace":
64+
tokens += f"{token.quantity} {token.unit} + "
65+
print(
66+
f"{utxo.tx_hash}#{utxo.tx_index} \t {int(utxo.amount[0].quantity)/1000000} ADA [{tokens}]"
67+
)
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import sys
3+
4+
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS
5+
from dotenv import load_dotenv
6+
7+
from pycardano import *
8+
9+
load_dotenv()
10+
network = os.getenv("network")
11+
wallet_mnemonic = os.getenv("wallet_mnemonic")
12+
blockfrost_api_key = os.getenv("blockfrost_api_key")
13+
14+
15+
if network == "testnet":
16+
base_url = ApiUrls.preprod.value
17+
cardano_network = Network.TESTNET
18+
else:
19+
base_url = ApiUrls.mainnet.value
20+
cardano_network = Network.MAINNET
21+
22+
23+
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic)
24+
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0")
25+
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0")
26+
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key)
27+
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key)
28+
29+
30+
main_address = Address(
31+
payment_part=payment_skey.to_verification_key().hash(),
32+
staking_part=staking_skey.to_verification_key().hash(),
33+
network=cardano_network,
34+
)
35+
36+
print(" ")
37+
print(f"Derived address: {main_address}")
38+
print(" ")
39+
40+
api = BlockFrostApi(project_id=blockfrost_api_key, base_url=base_url)
41+
42+
try:
43+
utxos = api.address_utxos(main_address)
44+
except Exception as e:
45+
if e.status_code == 404:
46+
print("Address does not have any UTXOs. ")
47+
if network == "testnet":
48+
print(
49+
"Request tADA from the faucet: https://docs.cardano.org/cardano-testnets/tools/faucet/"
50+
)
51+
else:
52+
print(e.message)
53+
sys.exit(1)
54+
55+
56+
cardano = BlockFrostChainContext(project_id=blockfrost_api_key, base_url=base_url)
57+
58+
builder = TransactionBuilder(cardano)
59+
60+
for i in range(20):
61+
output = TransactionOutput(main_address, Value(4000000))
62+
builder.add_output(output)
63+
64+
builder.add_input_address(main_address)
65+
signed_tx = builder.build_and_sign([payment_skey], change_address=main_address)
66+
result = cardano.submit_tx(signed_tx.to_cbor())
67+
print(f"Number of inputs: \t {len(signed_tx.transaction_body.inputs)}")
68+
print(f"Number of outputs: \t {len(signed_tx.transaction_body.outputs)}")
69+
print(f"Fee: \t\t\t {signed_tx.transaction_body.fee/1000000} ADA")
70+
print(f"Transaction submitted! ID: {result}")
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import os
2+
import sys
3+
4+
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS
5+
from dotenv import load_dotenv
6+
7+
from pycardano import *
8+
9+
load_dotenv()
10+
network = os.getenv("network")
11+
wallet_mnemonic = os.getenv("wallet_mnemonic")
12+
blockfrost_api_key = os.getenv("blockfrost_api_key")
13+
14+
15+
if network == "testnet":
16+
base_url = ApiUrls.preprod.value
17+
cardano_network = Network.TESTNET
18+
else:
19+
base_url = ApiUrls.mainnet.value
20+
cardano_network = Network.MAINNET
21+
22+
23+
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic)
24+
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0")
25+
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0")
26+
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key)
27+
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key)
28+
29+
30+
main_address = Address(
31+
payment_part=payment_skey.to_verification_key().hash(),
32+
staking_part=staking_skey.to_verification_key().hash(),
33+
network=cardano_network,
34+
)
35+
36+
print(" ")
37+
print(f"Derived address: {main_address}")
38+
print(" ")
39+
40+
api = BlockFrostApi(project_id=blockfrost_api_key, base_url=base_url)
41+
42+
try:
43+
utxos = api.address_utxos(main_address)
44+
except Exception as e:
45+
if e.status_code == 404:
46+
print("Address does not have any UTXOs. ")
47+
if network == "testnet":
48+
print(
49+
"Request tADA from the faucet: https://docs.cardano.org/cardano-testnets/tools/faucet/"
50+
)
51+
else:
52+
print(e.message)
53+
sys.exit(1)
54+
55+
56+
cardano = BlockFrostChainContext(project_id=blockfrost_api_key, base_url=base_url)
57+
58+
builder = TransactionBuilder(cardano)
59+
60+
inputs = []
61+
62+
total_ada_used = 0
63+
64+
for utxo in utxos:
65+
input = TransactionInput.from_primitive([utxo.tx_hash, utxo.tx_index])
66+
inputs.append(input)
67+
builder.add_input(input)
68+
total_ada_used += int(utxo.amount[0].quantity)
69+
70+
output = TransactionOutput(main_address, Value(total_ada_used))
71+
72+
tx_body = TransactionBody(inputs=inputs, outputs=[output], fee=100000)
73+
74+
signature = payment_skey.sign(tx_body.hash())
75+
vk = PaymentVerificationKey.from_signing_key(payment_skey)
76+
vk_witnesses = [VerificationKeyWitness(vk, signature)]
77+
signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses))
78+
79+
calculated_fee = fee(cardano, len(signed_tx.to_cbor()))
80+
81+
82+
total_ada_available = total_ada_used - calculated_fee
83+
output = TransactionOutput(main_address, Value(total_ada_available))
84+
85+
tx_body = TransactionBody(inputs=inputs, outputs=[output], fee=calculated_fee)
86+
87+
signature = payment_skey.sign(tx_body.hash())
88+
vk = PaymentVerificationKey.from_signing_key(payment_skey)
89+
vk_witnesses = [VerificationKeyWitness(vk, signature)]
90+
signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses))
91+
92+
try:
93+
result = cardano.submit_tx(signed_tx.to_cbor())
94+
print(f"Number of inputs: \t {len(signed_tx.transaction_body.inputs)}")
95+
print(f"Number of outputs: \t {len(signed_tx.transaction_body.outputs)}")
96+
print(f"Fee: \t\t\t {signed_tx.transaction_body.fee/1000000} ADA")
97+
print(f"Transaction submitted! ID: {result}")
98+
except Exception as e:
99+
if "BadInputsUTxO" in str(e):
100+
print("Trying to spend an input that doesn't exist (or no longer exist).")
101+
elif "ValueNotConservedUTxO" in str(e):
102+
print(
103+
"Transaction not correctly balanced. Inputs and outputs (+fee) don't match."
104+
)
105+
else:
106+
print(e)

0 commit comments

Comments
 (0)