Giter VIP home page Giter VIP logo

opshin / opshin-starter-kit Goto Github PK

View Code? Open in Web Editor NEW
15.0 15.0 8.0 86 KB

On-chain and off-chain code for a Python Smart Contract using opshin and pycardano

Home Page: https://demeter.run/code?repository=https://github.com/opshin/opshin-starter-kit.git&template=python&source=demeter&key=opshin-starter-kit

License: MIT License

Python 100.00%
cardano demeter-run off-chain on-chain plutus python smart-contracts

opshin-starter-kit's Introduction

opshin


Build Status PyPI version PyPI - Python Version PyPI - Status Coverage Status

You are building what you want. Why not also build how you 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:

  1. it is a valid Python program
  2. the 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, verification
  • Intuitive. Just like Python.
  • Flexible. Imperative, functional, the way you want it.
  • Efficient & Secure. Static type inference ensures strict typing and optimized code

Getting Started

Check out the OpShin Book for an introduction to this tool and details into writing smart contracts. This document will just outline the basic usage of the tool.

Installation

Install Python 3.8, 3.9, 3.10 or 3.11. Then run

python3 -m pip install opshin

Writing a Smart Contract

Check out the OpShin Book for an introduction to this tool and details into writing smart contracts.

Compiling

Write your program in python. You may start with the content of examples. 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 errors
opshin eval spending examples/smart_contracts/assert_sum.py "{\"int\": 4}" "{\"int\": 38}" d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87980d87a80ffd8799fd87b80d87a80ffff80a1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820746957f0eb57f2b11119684e611a98f373afea93473fefbb7632d579af2f6259ffffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff

# Compile script to 'uplc', the Cardano Smart Contract assembly
opshin compile spending examples/smart_contracts/assert_sum.py

Furthermore, 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 represents opshin eval any
  • or a more specific purpose: #!/usr/bin/env -S opshin eval minting

By doing so, you can transform your python file to an executable: chmod +x your_file.py and execute it with ./your_file.py, which will run opshin eval any ./your_file.py under the hood.

Deploying

The deploy process generates all artifacts required for usage with common libraries like pycardano, lucid and the cardano-cli.

# Automatically generate all artifacts needed for using this contract
opshin build spending examples/smart_contracts/assert_sum.py

See the tutorial by pycardano for explanations how to build transactions with opshin contracts.

API for Smart Contracts

The python interface offers a simple API to compile, load, apply parameters and evaluate smart contracts.

from opshin.builder import *

# Build a validator script from a python file that contains a validator function
contract = build("path/to/contract.py")

# You can apply parameters to the contract during compilation
contract = build("path/to/contract.py", arg1, arg2, arg3)

# Store the compilation artifacts in a folder
contract.dump("path/to/store")

# You can also load a compiled contract from a path
contract = load("path/to/store")

# And apply parameters after loading a contract
contract = contract.apply_parameters(arg1, arg2, arg3)

# The artifacts contain the compiled script, the policy ID and the addresses and blueprint
contract_addr = contract.mainnet_addr
contract_blueprint = contract.blueprint

Debugging artefacts

For debugging purposes, you can also run

# Compile script to 'uplc', and evaluate the script in UPLC (for debugging purposes)
opshin eval_uplc spending examples/smart_contracts/assert_sum.py "{\"int\": 4}" "{\"int\": 38}" d8799fd8799f9fd8799fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffd8799fd8799fd87a9f581cdbe769758f26efb21f008dc097bb194cffc622acc37fcefc5372eee3ffd87a80ffa140a1401a00989680d87a9f5820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dffd87a80ffffff809fd8799fd8799fd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd87a80ffa140a14000d87980d87a80ffffa140a14000a140a1400080a0d8799fd8799fd87980d87a80ffd8799fd87b80d87a80ffff80a1d87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffd87980a15820dfab81872ce2bbe6ee5af9bbfee4047f91c1f57db5e30da727d5fef1e7f02f4dd8799f581cdc315c289fee4484eda07038393f21dc4e572aff292d7926018725c2ffd8799f5820746957f0eb57f2b11119684e611a98f373afea93473fefbb7632d579af2f6259ffffd87a9fd8799fd8799f582055d353acacaab6460b37ed0f0e3a1a0aabf056df4a7fa1e265d21149ccacc527ff01ffffff

# Compile script to 'pluto', an intermediate language (for debugging purposes)
opshin compile_pluto spending examples/smart_contracts/assert_sum.py

Contributing

Developing and Technical Documentation

Generally, all contributions on the code side are very welcome. To get an overview over the architecture and idea behind OpShin, check out the Technical Documentation. A bug bounty has been set up and funded by Project Catalyst, which awards Github issue resolution wiht ADA rewards. This is a great opportunity to get involved and earn some ADA. Check out the detailed introduction to the bounty program for more information.

Sponsoring

You can sponsor the development of opshin through GitHub or Patreon or just by sending ADA. Drop me a message on social media and let me know what it is for.

  • Patreon Support OpShin at Patreon to enjoy member benefits!
  • GitHub Sponsor the developers of this project through the button "Sponsor" next to them
  • ADA Donation in ADA can be submitted to $opshin or addr1qyz3vgd5xxevjy2rvqevz9n7n7dney8n6hqggp23479fm6vwpj9clsvsf85cd4xc59zjztr5zwpummwckmzr2myjwjns74lhmr.

Supporters

The main sponsor of this project is Inversion. 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

opshin-starter-kit's People

Contributors

gabrielmarcos2001 avatar juliusfrost avatar nielstron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

opshin-starter-kit's Issues

Adding a CI (also for deploying the SC)

I currently see two options for this

  • we supply secret keys with access to preview Ogmios instances on demeter.run and funds on preview testnet, then run the script in the CI
    • pro: small effort
    • pro: actually reflects whats happening on preview testnet
    • con: nondeterministic, depends on a contract address loaded with funds, the network up and running
  • we launch a small node testnet, spin up ogmios etc
    • pro: deterministic and independent of whats happening on the preview testnet
    • con: large effort

Would help catch issues such as #1 quickly

Update opshin dependency

I followed the instructions to run on demeter and got this error:

Traceback (most recent call last):
  File "/config/workspace/repo/src/off_chain/collect_vest.py", line 98, in <module>
    main()
  File "/config/.cache/pypoetry/virtualenvs/opshin-starter-kit-d63vU2Dj-py3.10/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/config/.cache/pypoetry/virtualenvs/opshin-starter-kit-d63vU2Dj-py3.10/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/config/.cache/pypoetry/virtualenvs/opshin-starter-kit-d63vU2Dj-py3.10/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/config/.cache/pypoetry/virtualenvs/opshin-starter-kit-d63vU2Dj-py3.10/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/config/workspace/repo/src/off_chain/collect_vest.py", line 68, in main
    redeemer = Redeemer(0)
TypeError: Redeemer.__init__() missing 1 required positional argument: 'data'

It seems the new pycardano version caused this so we would need to set opshin = "^0.12.0" in

opshin = "0.10.0"
and do poetry update to update poetry.lock.

ModuleNotFoundError: No module named 'src'

Just going through the steps for the starket kit. Have got Poetry shell running, and ran Poetry install.

I ran python3 scripts/build.py from the main folder:
and got:

Traceback (most recent call last):
  File "D:\opshin-starter-kit\scripts\build.py", line 4, in <module>
    from src.on_chain import vesting
ModuleNotFoundError: No module named 'src'

This error is due to the /src folder not being in Python's path, which I believe can be rectified by adding (in my case):
sys.path.append(r"D:/opshin-starter-kit")

to the top of the build.py code.

But then this gave a new error:

from opshin.ledger.interval import *
ModuleNotFoundError: No module named 'opshin'

From reading around I believe the most pythonic thing to do would be to just have it all set up as proper pypy libraries that people can install using pip?

My sources:
https://wellsr.com/python/how-to-create-custom-modules-in-python/
https://stackoverflow.com/questions/68432247/absolute-relative-import-in-python-modulenotfounderror-and-more

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.