|
| 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