Coder Social home page Coder Social logo

eopsin's Introduction

eopsin


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

This is an implementation of smart contracts for Cardano which are written in a very 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 eopsin?

  • 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

Installation

Install Python 3.8. Then run

python3.8 -m pip install eopsin-lang

Writing a Smart Contract

A simple contract called the "Gift Contract" verifies that only specific wallets can withdraw money. They are authenticated by a signature. See the tutorial by pycardano for explanations on what each of the parameters to the validator means and how to build transactions with the contract.

from eopsin.prelude import *

@dataclass()
class CancelDatum(PlutusData):
    pubkeyhash: bytes


def validator(datum: CancelDatum, redeemer: None, context: ScriptContext) -> None:
    sig_present = False
    for s in context.tx_info.signatories:
        if datum.pubkeyhash == s:
            sig_present = True
    assert sig_present, "Required signature missing"

All contracts written in eopsin are 100% valid python. See the examples directory for more.

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
eopsin eval examples/smart_contracts/assert_sum.py "{\"int\": 4}" "{\"int\": 38}" "{\"constructor\": 0, \"fields\": []}"

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

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
eopsin build examples/smart_contracts/assert_sum.py

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

Deploying manually

Deploying via aiken

Run the following to obtain a cardano-cli compatible version of your smart contract:

This requires you to install aiken

cat examples/smart_contracts/assert_sum.py | bash scripts/python_to_plutus_via_aiken.sh > assert_sum.plutus

You can generate a script address from this using the official cardano-cli

cardano-cli address build --payment-script-file assert_sum.plutus --mainnet

Now you can continue and send/spend ADA with this address following the official documentation!

Deploying via pluto

You may alternatively use pluto to compile the contracts. Beware that it might not be very fast.

cat examples/smart_contracts/assert_sum.py | bash scripts/python_to_plutus_via_pluto.sh > assert_sum.plutus

The small print

Not every valid python program is a valid smart contract. Not all language features of python will or can be supported. The reasons are mainly of practical nature (i.e. we can't infer types when functions like eval are allowed). Specifically, only a pure subset of python is allowed. Further, only immutable objects may be generated.

For your program to be accepted, make sure to only make use of language constructs supported by the compiler. You will be notified of which constructs are not supported when trying to compile.

Name

Eopsin (Korean: 업신; Hanja: 業神) is the goddess of the storage and wealth in Korean mythology and shamanism. [...] Eopsin was believed to be a pitch-black snake that had ears. [1]

Since this project tries to merge Python (a large serpent) and Pluto/Plutus (Greek wealth gods), the name appears fitting.

Contributing

Architecture

This program consists of a few independent components:

  1. An aggressive static type inferencer
  2. Rewriting tools to simplify complex python expressions
  3. A compiler from a subset of python into UPLC

Debugging artefacts

For debugging purposes, you can also run

# Compile script to 'uplc', and evaluate the script in UPLC (for debugging purposes)
python3 -m eopsin eval_uplc examples/smart_contracts/assert_sum.py "{\"int\": 4}" "{\"int\": 38}" "{\"constructor\": 0, \"fields\": []}"

# Compile script to 'pluto', an intermediate language (for debugging purposes)
python3 -m eopsin compile_pluto examples/smart_contracts/assert_sum.py

Sponsoring

You can sponsor the development of eopsin. Just drop me a message on social media and let me know what it is for.

Supporters

eopsin's People

Contributors

nielstron avatar

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.