|
| 1 | +""" |
| 2 | +
|
| 3 | +Off-chain code of taker and giver in fortytwo. |
| 4 | +
|
| 5 | +""" |
| 6 | + |
| 7 | + |
| 8 | +import os |
| 9 | + |
| 10 | +import cbor2 |
| 11 | +from retry import retry |
| 12 | + |
| 13 | +from pycardano import * |
| 14 | + |
| 15 | +NETWORK = Network.TESTNET |
| 16 | + |
| 17 | + |
| 18 | +def get_env_val(key): |
| 19 | + val = os.environ.get(key) |
| 20 | + if not val: |
| 21 | + raise Exception(f"Environment variable {key} is not set!") |
| 22 | + return val |
| 23 | + |
| 24 | + |
| 25 | +payment_skey = PaymentSigningKey.load(get_env_val("PAYMENT_KEY_PATH")) |
| 26 | +payment_vkey = PaymentVerificationKey.from_signing_key(payment_skey) |
| 27 | + |
| 28 | +chain_context = BlockFrostChainContext( |
| 29 | + project_id=get_env_val("BLOCKFROST_ID"), network=NETWORK |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +@retry(delay=20) |
| 34 | +def wait_for_tx(tx_id): |
| 35 | + chain_context.api.transaction(tx_id) |
| 36 | + print(f"Transaction {tx_id} has been successfully included in the blockchain.") |
| 37 | + |
| 38 | + |
| 39 | +def submit_tx(tx): |
| 40 | + print("############### Transaction created ###############") |
| 41 | + print(tx) |
| 42 | + print(tx.to_cbor()) |
| 43 | + print("############### Submitting transaction ###############") |
| 44 | + chain_context.submit_tx(tx.to_cbor()) |
| 45 | + wait_for_tx(str(tx.id)) |
| 46 | + |
| 47 | + |
| 48 | +def create_collateral(target_address, skey): |
| 49 | + collateral_builder = TransactionBuilder(chain_context) |
| 50 | + |
| 51 | + collateral_builder.add_input_address(target_address) |
| 52 | + collateral_builder.add_output(TransactionOutput(target_address, 5000000)) |
| 53 | + |
| 54 | + submit_tx(collateral_builder.build_and_sign([skey], target_address)) |
| 55 | + |
| 56 | + |
| 57 | +# ----------- Giver sends 10 ADA to a script address --------------- |
| 58 | +with open("fortytwo.plutus", "r") as f: |
| 59 | + script_hex = f.read() |
| 60 | + forty_two_script = cbor2.loads(bytes.fromhex(script_hex)) |
| 61 | + |
| 62 | +script_hash = plutus_script_hash(forty_two_script) |
| 63 | + |
| 64 | +script_address = Address(script_hash, network=NETWORK) |
| 65 | + |
| 66 | +giver_address = Address(payment_vkey.hash(), network=NETWORK) |
| 67 | + |
| 68 | +builder = TransactionBuilder(chain_context) |
| 69 | +builder.add_input_address(giver_address) |
| 70 | +datum = PlutusData() # A Unit type "()" in Haskell |
| 71 | +builder.add_output( |
| 72 | + TransactionOutput(script_address, 10000000, datum_hash=datum_hash(datum)) |
| 73 | +) |
| 74 | + |
| 75 | +signed_tx = builder.build_and_sign([payment_skey], giver_address) |
| 76 | + |
| 77 | +submit_tx(signed_tx) |
| 78 | + |
| 79 | +# ----------- Taker takes 10 ADA from the script address --------------- |
| 80 | + |
| 81 | +# taker_address could be any address. In this example, we will use the same address as giver. |
| 82 | +taker_address = giver_address |
| 83 | + |
| 84 | +# Notice that transaction builder will automatically estimate execution units (num steps & memory) for a redeemer if |
| 85 | +# no execution units are provided in the constructor of Redeemer. |
| 86 | +# Put integer 42 (the secret that unlocks the fund) in the redeemer. |
| 87 | +redeemer = Redeemer(RedeemerTag.SPEND, 42) |
| 88 | + |
| 89 | +utxo_to_spend = chain_context.utxos(str(script_address))[0] |
| 90 | + |
| 91 | +builder = TransactionBuilder(chain_context) |
| 92 | + |
| 93 | +builder.add_script_input(utxo_to_spend, forty_two_script, datum, redeemer) |
| 94 | + |
| 95 | +# Send 5 ADA to taker address. The remaining ADA (~4.7) will be sent as change. |
| 96 | +take_output = TransactionOutput(taker_address, 5000000) |
| 97 | +builder.add_output(take_output) |
| 98 | + |
| 99 | +non_nft_utxo = None |
| 100 | +for utxo in chain_context.utxos(str(taker_address)): |
| 101 | + if isinstance(utxo.output.amount, int): |
| 102 | + non_nft_utxo = utxo |
| 103 | + break |
| 104 | + |
| 105 | +if non_nft_utxo is None: |
| 106 | + create_collateral(taker_address, payment_skey) |
| 107 | + |
| 108 | +builder.collaterals.append(non_nft_utxo) |
| 109 | + |
| 110 | +signed_tx = builder.build_and_sign([payment_skey], taker_address) |
| 111 | + |
| 112 | +submit_tx(signed_tx) |
0 commit comments