Skip to content

Commit 478f5ef

Browse files
authored
Add transaction builder with metadata example (#327)
* Add transaction builder example with metadata * Add transaction builder example with metadata * Add CIP-20 metadata example * Reformat file
1 parent bb41171 commit 478f5ef

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

examples/tx_builder_with_metadata.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from blockfrost import ApiUrls
2+
3+
from pycardano import (
4+
Address,
5+
AlonzoMetadata,
6+
AuxiliaryData,
7+
BlockFrostChainContext,
8+
Metadata,
9+
Network,
10+
PaymentSigningKey,
11+
PaymentVerificationKey,
12+
TransactionBuilder,
13+
TransactionOutput,
14+
)
15+
16+
# Use testnet
17+
network = Network.TESTNET
18+
19+
# Read keys to memory
20+
# Assume there is a payment.skey file sitting in current directory
21+
payment_signing_key = PaymentSigningKey.load("payment.skey")
22+
payment_verification_key = PaymentVerificationKey.from_signing_key(payment_signing_key)
23+
24+
from_address = Address(payment_verification_key.hash(), network=network)
25+
26+
# Create a BlockFrost chain context. In this example, we will use preprod network.
27+
context = BlockFrostChainContext(
28+
"your_blockfrost_project_id", base_url=ApiUrls.preprod.value
29+
)
30+
31+
# Metadata that follows the CIP-20 standard. More info here https://cips.cardano.org/cip/CIP-20/
32+
metadata = {
33+
674: {
34+
"msg": [
35+
"Invoice-No: 1234567890",
36+
"Customer-No: 555-1234",
37+
"P.S.: i will shop again at your store :-)",
38+
]
39+
}
40+
}
41+
42+
# Place metadata in AuxiliaryData, the format acceptable by a transaction.
43+
auxiliary_data = AuxiliaryData(AlonzoMetadata(metadata=Metadata(metadata)))
44+
# Set transaction metadata
45+
builder.auxiliary_data = auxiliary_data
46+
# Create a transaction builder
47+
builder = TransactionBuilder(context)
48+
# Set your address
49+
builder.add_input_address(from_address)
50+
# Recipient address
51+
to_address = "cardano_address_you_want_to_send_ada_to"
52+
# Add output and sign transaction
53+
builder.add_output(TransactionOutput.from_primitive([to_address, 100000000000]))
54+
signed_tx = builder.build_and_sign([payment_signing_key], change_address=from_address)
55+
# Send transaction
56+
context.submit_tx(signed_tx)

0 commit comments

Comments
 (0)