Package: pycardano
PyCardanoPyCardano is a Cardano library written in Python. It allows users to create and sign transactions without
depending on third-party Cardano serialization tools, such ascardano-cliandcardano-serialization-lib, making it a lightweight library, which
is simple and fast to set up in all types of environments.Current goal of this project is to enable developers to write off-chain code and tests in pure Python for Plutus DApps.
Nevertheless, we see the potential in expanding this project to a full Cardano node client, which
could be beneficial for faster R&D iterations.FeaturesShelly addressTransaction builderTransaction signingMulti-assetChain backend integrationFee calculationUTxO selectionNative scriptNative tokenMetadataPlutus scriptStaking certificatesReward withdrawMnemonicHD WalletByron AddressPool certificateProtocol proposal updateInstallationInstall the library usingpip:pip install pycardanoDocumentationhttps://pycardano.readthedocs.io/en/latest/ExamplesFull stack DAppA full stack testnet DApp is hosted on replit:https://pycardano.cffls.repl.co/To learn more details, go to theDApp page.Transaction creation and signingExpand code"""Build a transaction using transaction builder"""fromblockfrostimportApiUrlsfrompycardanoimport*# Use testnetnetwork=Network.TESTNET# Read keys to memory# Assume there is a payment.skey file sitting in current directorypsk=PaymentSigningKey.load("payment.skey")# Assume there is a stake.skey file sitting in current directoryssk=StakeSigningKey.load("stake.skey")pvk=PaymentVerificationKey.from_signing_key(psk)svk=StakeVerificationKey.from_signing_key(ssk)# Derive an address from payment verification key and stake verification keyaddress=Address(pvk.hash(),svk.hash(),network)# Create a BlockFrost chain contextcontext=BlockFrostChainContext("your_blockfrost_project_id",base_url=ApiUrls.preprod.value)# Create a transaction builderbuilder=TransactionBuilder(context)# Tell the builder that transaction input will come from a specific address, assuming that there are some ADA and native# assets sitting at this address. "add_input_address" could be called multiple times with different address.builder.add_input_address(address)# Get all UTxOs currently sitting at this addressutxos=context.utxos(address)# We can also tell the builder to include a specific UTxO in the transaction.# Similarly, "add_input" could be called multiple times.builder.add_input(utxos[0])# Send 1.5 ADA and a native asset (CHOC) in quantity of 2000 to an address.builder.add_output(TransactionOutput(Address.from_primitive("addr_test1vrm9x2zsux7va6w892g38tvchnzahvcd9tykqf3ygnmwtaqyfg52x"),Value.from_primitive([1500000,{bytes.fromhex("57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522"# Policy ID):{b"CHOC":2000# Asset name and amount}},]),))# We can add multiple outputs, similar to what we can do with inputs.# Send 2 ADA and a native asset (CHOC) in quantity of 200 to ourselvesbuilder.add_output(TransactionOutput(address,Value.from_primitive([2000000,{bytes.fromhex("57fca08abbaddee36da742a839f7d83a7e1d2419f1507fcbf3916522"# Policy ID):{b"CHOC":200# Asset name and amount}},]),))# Create final signed transactionsigned_tx=builder.build_and_sign([psk],change_address=address)# Submit signed transaction to the networkcontext.submit_tx(signed_tx)See more usages underexamples.DevelopmentClick to expandWorkspace setupClone the repository:git clone https://github.com/Python-Cardano/pycardano.gitPyCardano usespoetryto manage its dependencies.
Install poetry for osx / linux / bashonwindows:curl -sSL https://install.python-poetry.org | python3 -Go topoetry installationfor more details.Change directory into the repo, install all dependencies using poetry, and you are all set!cd pycardano && poetry installWhen testing or running any program, it is recommended to enter
apoetry shellin which all python dependencies are automatically
configured:poetry shell.TestPyCardano usespytestfor unit testing.Run all tests:make testRun all tests in a specific test file:poetry run pytest test/pycardano/test_transaction.pyRun a specific test function:poetry run pytest -k "test_transaction_body"Run a specific test function in a test file:poetry run pytest test/pycardano/test_transaction.py -k "test_transaction_body"Test coverageWe useCoverageto calculate the test coverage.Test coverage could be generated by:make covA html report could be generated and opened in browser by:make cov-htmlStyle guidelinesThe package usesGoogle styledocstring.Code could be formatted with command:make formatThe code style could be checked byflake8:make qaDocs generationThe majority of package documentation is created by the docstrings in python files.
We usesphinxwithRead the Docs themeto generate the
html pages.Build docs and open the docs in browser:make docsDonation and SponsorIf you find this project helpful, please consider donate or sponsor us. Your donation and sponsor will allow us to
spend more time on improving PyCardano and adding more features in the future.You can support us by 1) sponsoring through Github, or 2) donating ADA to our ADA Handlepycardanoor to the address below:addr1vxa4qadv7hk2dd3jtz9rep7sp92lkgwzefwkmsy3qkglq5qzv8c0dSponsors :heart: