Package: opshin
opshinYou are building what you want. Why not also buildhowyou want?This is an implementation of smart contracts for Cardano which are written in a strict subset of valid Python.
The general philosophy of this project is to write a compiler that
ensure the following:If the program compiles then:it is a valid Python programthe output running it with python is the same as running it on-chain.Why opshin?100% valid Python. Leverage the existing tool stack for Python, syntax highlighting, linting, debugging, unit-testing,property-based testing,verificationIntuitive. Just like Python.Flexible. Imperative, functional, the way you want it.Efficient & Secure. Static type inference ensures strict typing and optimized codeGetting StartedCheck out theOpShin Bookfor an introduction to this tool and details into writing smart contracts.
This document will just outline the basic usage of the tool.InstallationInstall Python 3.8, 3.9, 3.10 or 3.11. Then runpython3-mpipinstallopshinWriting a Smart ContractCheck out theOpShin Bookfor an introduction to this tool and details into writing smart contracts.CompilingWrite your program in python. You may start with the content ofexamples.
Arguments to scripts are passed in as Plutus Data objects in JSON notation.You can run any of the following commands# Evaluate script in Python - this can be used to make sure there are no obvious errorsopshinevalspendingexamples/smart_contracts/assert_sum.py"{\"int\": 4}""{\"int\": 38}"d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87980d87a80ffd8799fd87b80d87a80ffff80a1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820746957f0eb57f2b11119684e611a98f373afea93473fefbb7632d579af2f6259ffffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff# Compile script to 'uplc', the Cardano Smart Contract assemblyopshincompilespendingexamples/smart_contracts/assert_sum.pyFurthermore, you can add a shebang to the first line of the python file to indicate that it represents an opshin smart contract.
You can choose from the following options:a general shebang:#!opshin, which representsopshin eval anyor a more specific purpose:#!/usr/bin/env -S opshin eval mintingBy doing so, you can transform your python file to an executable:chmod +x your_file.pyand execute it with./your_file.py, which will runopshin eval any ./your_file.pyunder the hood.DeployingThe deploy process generates all artifacts required for usage with common libraries likepycardano,lucidand thecardano-cli.# Automatically generate all artifacts needed for using this contractopshinbuildspendingexamples/smart_contracts/assert_sum.pySee thetutorial bypycardanofor explanations how to build transactions withopshincontracts.API for Smart ContractsThe python interface offers a simple API to compile, load, apply parameters and evaluate smart contracts.fromopshin.builderimport*# Build a validator script from a python file that contains a validator functioncontract=build("path/to/contract.py")# You can apply parameters to the contract during compilationcontract=build("path/to/contract.py",arg1,arg2,arg3)# Store the compilation artifacts in a foldercontract.dump("path/to/store")# You can also load a compiled contract from a pathcontract=load("path/to/store")# And apply parameters after loading a contractcontract=contract.apply_parameters(arg1,arg2,arg3)# The artifacts contain the compiled script, the policy ID and the addresses and blueprintcontract_addr=contract.mainnet_addrcontract_blueprint=contract.blueprintDebugging artefactsFor debugging purposes, you can also run# Compile script to 'uplc', and evaluate the script in UPLC (for debugging purposes)opshineval_uplcspendingexamples/smart_contracts/assert_sum.py"{\"int\": 4}""{\"int\": 38}"d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87980d87a80ffd8799fd87b80d87a80ffff80a1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820746957f0eb57f2b11119684e611a98f373afea93473fefbb7632d579af2f6259ffffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff# Compile script to 'pluto', an intermediate language (for debugging purposes)opshincompile_plutospendingexamples/smart_contracts/assert_sum.pyContributingDeveloping and Technical DocumentationGenerally, all contributions on the code side are very welcome.
To get an overview over the architecture and idea behind OpShin, check outthe Technical Documentation.SponsoringYou can sponsor the development of opshin through GitHub orPatreonor just by sending ADA. Drop me a message on social media and let me know what it is for.PatreonSupport OpShin atPatreonto enjoy member benefits!GitHubSponsor the developers of this project through the button "Sponsor" next to themADADonation in ADA can be submitted to$opshinoraddr1qyz3vgd5xxevjy2rvqevz9n7n7dney8n6hqggp23479fm6vwpj9clsvsf85cd4xc59zjztr5zwpummwckmzr2myjwjns74lhmr.SupportersThe main sponsor of this project isInversion. Here is a word from them!At Inversion, we pride ourselves on our passion for life and our ability to create exceptional software solutions for our clients. Our team of experts, with over a century of cumulative experience, is dedicated to harnessing the power of the Cardano blockchain to bring innovative and scalable decentralized applications to life. We've successfully built applications for NFT management, staking and delegation, chain data monitoring, analytics, and web3 integrations, as well as countless non-blockchain systems. With a focus on security, transparency, and sustainability, our team is excited to contribute to the Cardano ecosystem, pushing the boundaries of decentralized technologies to improve lives worldwide. Trust Inversion to be your go-to partner for robust, effective, and forward-thinking solutions, whether blockchain based, traditional systems, or a mix of the two.They have recently started a podcast, called "Africa On Chain", which you can check out here:https://www.youtube.com/@africaonchain