From 84b63e20be8a764fd3992c4134284d4cc116a040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCndler?= Date: Tue, 14 Mar 2023 20:22:56 +0100 Subject: [PATCH] Restructure the examples section The reader now has some guidance through additional comments and a natural order between the examples --- eopsin/tests/test_misc.py | 42 ++++------- examples/__init__.py | 0 examples/a_introduction/__init__.py | 0 examples/a_introduction/a_hello_world.py | 22 ++++++ examples/a_introduction/b_mult_for.py | 35 ++++++++++ examples/a_introduction/c_mult_while.py | 24 +++++++ examples/a_introduction/d_fib_iter.py | 28 ++++++++ examples/a_introduction/e_fib_rec.py | 32 +++++++++ examples/a_introduction/f_branching.py | 28 ++++++++ examples/a_introduction/g_strict_typing.py | 27 +++++++ examples/b_datums/__init__.py | 0 examples/b_datums/a_complex_datum.py | 58 +++++++++++++++ examples/b_datums/b_extract_datum.py | 45 ++++++++++++ examples/b_datums/c_dict_datum.py | 27 +++++++ .../d_list_datum.py} | 5 ++ examples/broken.py | 21 ------ examples/c_advanced/__init__.py | 0 examples/c_advanced/a_list_comprehensions.py | 25 +++++++ .../b_datum_cast.py} | 0 .../{showcase.py => c_advanced/c_showcase.py} | 0 examples/complex_datum.py | 42 ----------- examples/dict_datum.py | 19 ----- examples/extract_datum.py | 70 ------------------- examples/fib_iter.py | 5 -- examples/fib_rec.py | 12 ---- examples/hello_world.py | 2 - examples/list_comprehensions.py | 11 --- examples/mult_for.py | 6 -- examples/mult_while.py | 7 -- examples/smart_contracts/__init__.py | 0 examples/sum.py | 2 - 31 files changed, 368 insertions(+), 227 deletions(-) create mode 100644 examples/__init__.py create mode 100644 examples/a_introduction/__init__.py create mode 100644 examples/a_introduction/a_hello_world.py create mode 100644 examples/a_introduction/b_mult_for.py create mode 100644 examples/a_introduction/c_mult_while.py create mode 100644 examples/a_introduction/d_fib_iter.py create mode 100644 examples/a_introduction/e_fib_rec.py create mode 100644 examples/a_introduction/f_branching.py create mode 100644 examples/a_introduction/g_strict_typing.py create mode 100644 examples/b_datums/__init__.py create mode 100644 examples/b_datums/a_complex_datum.py create mode 100644 examples/b_datums/b_extract_datum.py create mode 100644 examples/b_datums/c_dict_datum.py rename examples/{list_datum.py => b_datums/d_list_datum.py} (73%) delete mode 100644 examples/broken.py create mode 100644 examples/c_advanced/__init__.py create mode 100644 examples/c_advanced/a_list_comprehensions.py rename examples/{datum_cast.py => c_advanced/b_datum_cast.py} (100%) rename examples/{showcase.py => c_advanced/c_showcase.py} (100%) delete mode 100644 examples/complex_datum.py delete mode 100644 examples/dict_datum.py delete mode 100644 examples/extract_datum.py delete mode 100644 examples/fib_iter.py delete mode 100644 examples/fib_rec.py delete mode 100644 examples/hello_world.py delete mode 100644 examples/list_comprehensions.py delete mode 100644 examples/mult_for.py delete mode 100644 examples/mult_while.py create mode 100644 examples/smart_contracts/__init__.py delete mode 100644 examples/sum.py diff --git a/eopsin/tests/test_misc.py b/eopsin/tests/test_misc.py index dd6baeba..6674a0c9 100644 --- a/eopsin/tests/test_misc.py +++ b/eopsin/tests/test_misc.py @@ -58,7 +58,7 @@ def test_assert_sum_contract_fail(self): b=st.integers(min_value=0, max_value=10), ) def test_mult_for(self, a: int, b: int): - input_file = "examples/mult_for.py" + input_file = "examples/b_mult_for.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -76,7 +76,7 @@ def test_mult_for(self, a: int, b: int): b=st.integers(min_value=0, max_value=10), ) def test_mult_while(self, a: int, b: int): - input_file = "examples/mult_while.py" + input_file = "examples/c_mult_while.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -89,26 +89,8 @@ def test_mult_while(self, a: int, b: int): ret = uplc_eval(f) self.assertEqual(ret, uplc.PlutusInteger(a * b)) - @given( - a=st.integers(), - b=st.integers(), - ) - def test_sum(self, a: int, b: int): - input_file = "examples/sum.py" - with open(input_file) as fp: - source_code = fp.read() - ast = compiler.parse(source_code) - code = compiler.compile(ast) - code = code.compile() - f = code.term - # UPLC lambdas may only take one argument at a time, so we evaluate by repeatedly applying - for d in [uplc.PlutusInteger(a), uplc.PlutusInteger(b)]: - f = uplc.Apply(f, d) - ret = uplc_eval(f) - self.assertEqual(ret, uplc.PlutusInteger(a + b)) - def test_complex_datum_correct_vals(self): - input_file = "examples/complex_datum.py" + input_file = "examples/a_complex_datum.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -135,7 +117,7 @@ def test_complex_datum_correct_vals(self): ) def test_hello_world(self): - input_file = "examples/hello_world.py" + input_file = "examples/a_introduction/a_hello_world.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -148,7 +130,7 @@ def test_hello_world(self): ret = uplc_eval(f) def test_list_datum_correct_vals(self): - input_file = "examples/list_datum.py" + input_file = "examples/d_list_datum.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -165,7 +147,7 @@ def test_list_datum_correct_vals(self): ) def test_showcase(self): - input_file = "examples/showcase.py" + input_file = "examples/c_showcase.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -183,7 +165,7 @@ def test_showcase(self): @given(n=st.integers(min_value=0, max_value=5)) def test_fib_iter(self, n): - input_file = "examples/fib_iter.py" + input_file = "examples/a_introduction/d_fib_iter.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -201,7 +183,7 @@ def test_fib_iter(self, n): @given(n=st.integers(min_value=0, max_value=5)) def test_fib_rec(self, n): - input_file = "examples/fib_rec.py" + input_file = "examples/e_fib_rec.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -333,7 +315,7 @@ def a(x: int) -> int: self.assertEqual(uplc.PlutusInteger(100), ret) def test_datum_cast(self): - input_file = "examples/datum_cast.py" + input_file = "examples/b_datum_cast.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -416,7 +398,7 @@ def test_parameterized_compile(self): f = code.term def test_dict_datum(self): - input_file = "examples/dict_datum.py" + input_file = "examples/c_dict_datum.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -563,7 +545,7 @@ def validator(_: None) -> SomeOutputDatum: ) def test_list_comprehension_even(self): - input_file = "examples/list_comprehensions.py" + input_file = "examples/a_list_comprehensions.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) @@ -584,7 +566,7 @@ def test_list_comprehension_even(self): ) def test_list_comprehension_all(self): - input_file = "examples/list_comprehensions.py" + input_file = "examples/a_list_comprehensions.py" with open(input_file) as fp: source_code = fp.read() ast = compiler.parse(source_code) diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/a_introduction/__init__.py b/examples/a_introduction/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/a_introduction/a_hello_world.py b/examples/a_introduction/a_hello_world.py new file mode 100644 index 00000000..3ab8eb72 --- /dev/null +++ b/examples/a_introduction/a_hello_world.py @@ -0,0 +1,22 @@ +""" +A very basic eopsin program +it does nothing, takes only one parameter and returns nothing + +When executed, it will print "Hello World!" to the console! + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/a_hello_world.py '{"int": 0}' +> Starting execution +> ------------------ +> Hello world! +> ------------------ +> None +``` +""" + + +def validator(_: None) -> None: + # print a string into the debug console + print("Hello world!") diff --git a/examples/a_introduction/b_mult_for.py b/examples/a_introduction/b_mult_for.py new file mode 100644 index 00000000..326f09e8 --- /dev/null +++ b/examples/a_introduction/b_mult_for.py @@ -0,0 +1,35 @@ +""" +A re-implementation of a * b +This program will loop through all the numbers from 0 to b +and sum the values up + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/b_mult_for.py '{"int": 3}' '{"int": 4}' +> Starting execution +> Starting execution +> ------------------ +> 0 +> 1 +> 2 +> 3 +> ------------------ +> 12 +``` +""" + + +def validator(a: int, b: int) -> int: + # trivial implementation of c = a * b + + # We initialize the variable with a default + c = 0 + # Now loop through all numbers from 0 to b (range creates the list [0, 1, 2, ...]) + for i in range(b): + # for debugging purposes, you can print the number + # Note: we convert the `int` to a `str` by calling `str(...)` - print only prints strings! + print(str(i)) + # and we update the value of c in every iteration + c += a + return c diff --git a/examples/a_introduction/c_mult_while.py b/examples/a_introduction/c_mult_while.py new file mode 100644 index 00000000..e62b194e --- /dev/null +++ b/examples/a_introduction/c_mult_while.py @@ -0,0 +1,24 @@ +""" +A re-implementation of a * b, this time with a while loop. + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/c_mult_while.py '{"int": 3}' '{"int": 4}' +> Starting execution +> ------------------ +> ------------------ +> 12 +``` +""" + + +def validator(a: int, b: int) -> int: + # trivial implementation of c = a * b + c = 0 + # in a while-loop we check the condition and execute the inner body while it evaluates to True + while 0 < b: + # we update the values of c and b here + c += a + b -= 1 + return c diff --git a/examples/a_introduction/d_fib_iter.py b/examples/a_introduction/d_fib_iter.py new file mode 100644 index 00000000..21677ece --- /dev/null +++ b/examples/a_introduction/d_fib_iter.py @@ -0,0 +1,28 @@ +""" +A more complicated example of a program +This program will loop through all the numbers from 0 to n +and compute the fibonacci numbers on the way. + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/d_fib_iter.py '{"int": 3}' +> Starting execution +> ------------------ +> 0 +> 1 +> 2 +> ------------------ +> 2 +``` +""" + + +def validator(n: int) -> int: + a, b = 0, 1 + for i in range(n): + # due to strict typing, we need to first convert the integer to a string by calling str(..) + print(str(i)) + # this updates and re-assigns values in a tuple-like fashion + a, b = b, a + b + return a diff --git a/examples/a_introduction/e_fib_rec.py b/examples/a_introduction/e_fib_rec.py new file mode 100644 index 00000000..e32b81fa --- /dev/null +++ b/examples/a_introduction/e_fib_rec.py @@ -0,0 +1,32 @@ +""" +A more complicated example of a program +This program will loop through all the numbers from 0 to n +and compute the fibonacci numbers on the way. + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/d_fib_iter.py '{"int": 3}' +> Starting execution +> ------------------ +> 0 +> 1 +> 2 +> ------------------ +> 2 +``` +""" + + +def fib(n: int) -> int: + if n == 0: + res = 0 + elif n == 1: + res = 1 + else: + res = fib(n - 1) + fib(n - 2) + return res + + +def validator(n: int) -> int: + return fib(n) diff --git a/examples/a_introduction/f_branching.py b/examples/a_introduction/f_branching.py new file mode 100644 index 00000000..401c39f2 --- /dev/null +++ b/examples/a_introduction/f_branching.py @@ -0,0 +1,28 @@ +""" +We can branch the control flow using if/else +You can pass boolean values into the contract by passing integers with 0/1 + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/f_branching.py '{"int": 3}' '{"int": 0}' +> Starting execution +> ------------------ +> ------------------ +> 9 +(venv) $ eopsin eval examples/a_introduction/f_branching.py '{"int": 3}' '{"int": 1}' +> Starting execution +> ------------------ +> ------------------ +> 6 + +``` +""" + + +def validator(n: int, even: bool) -> int: + if even: + res = 2 * n + else: + res = 3 * n + return res diff --git a/examples/a_introduction/g_strict_typing.py b/examples/a_introduction/g_strict_typing.py new file mode 100644 index 00000000..d07d09d1 --- /dev/null +++ b/examples/a_introduction/g_strict_typing.py @@ -0,0 +1,27 @@ +""" +eopsin is strictly typed! Take care that your types match. The compiler will guide you there. + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/g_strict_typing.py '{"int": 3}' +Traceback (most recent call last): + File "/git/eopsin-lang/venv/bin/eopsin", line 33, in + sys.exit(load_entry_point('eopsin-lang', 'console_scripts', 'eopsin')()) + File "/git/eopsin-lang/eopsin/__main__.py", line 140, in main + raise SyntaxError( + File "examples/a_introduction/g_strict_typing.py", line 14 + if "s" == 4: + ^ +NotImplementedError: Comparison Eq for StringType and IntegerType is not implemented. This is likely intended because it would always evaluate to False. +Note that eopsin errors may be overly restrictive as they aim to prevent code with unintended consequences. + +``` +""" + + +def validator(value: int) -> bytes: + # eopsin is strictly typed! This comparison is invalid, strings ant integers can not be compared + if "s" == value: + print("test") + return b"" diff --git a/examples/b_datums/__init__.py b/examples/b_datums/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/b_datums/a_complex_datum.py b/examples/b_datums/a_complex_datum.py new file mode 100644 index 00000000..3f2cdb7c --- /dev/null +++ b/examples/b_datums/a_complex_datum.py @@ -0,0 +1,58 @@ +""" +You can define more complex datums using PyCardanos notation. +Every datum/object is a class that inherits from PlutusData and implements dataclass. +If you don't understand what this means, don't worry. Just stick to the way that datums are defined +in these examples. + +If something can be either object A or object B, you can create Union[A, B] as a new type. +You can distinguish between A and B using isinstance(x, A) and isinstance(x, B). +IMPORTANT: Make sure that all objects in a Union have different CONSTR_ID values. +The exact value does not matter too much actually, but they need to be different. + +Try it out! + +```bash +(venv) $ eopsin eval examples/b_datums/a_complex_datum.py '{ "constructor": 0, "fields": [ { "bytes": "0000000000000000000000000000000000" }, { "constructor": 1, "fields": [ { "int": 1000000 }, { "int": 20 } ] } ] }' +> Starting execution +> ------------------ +> ------------------ +> 20 +``` +""" +# We import the prelude now, it will bring all values for classes and types +from eopsin.prelude import * + + +@dataclass() +class Deposit(PlutusData): + CONSTR_ID = 0 + minimum_lp: int + + +@dataclass() +class Withdraw(PlutusData): + CONSTR_ID = 1 + minimum_coin_a: int + minimum_coin_b: int + + +OrderStep = Union[Deposit, Withdraw] + +# inspired by https://github.com/MuesliSwapTeam/muesliswap-cardano-pool-contracts/blob/main/dex/src/MuesliSwapPools/BatchOrder/Types.hs +@dataclass() +class BatchOrder(PlutusData): + owner: PubKeyHash + action: Union[Deposit, Withdraw] + + +# If some parameter might be ommited, just Union with Nothing and check for the instance at runtime! +def validator(datum: BatchOrder) -> int: + action = datum.action + if isinstance(action, Deposit): + res = action.minimum_lp + elif isinstance(action, Withdraw): + res = action.minimum_coin_b + # note we never initialized res. This means this will + # throw a NameError if the instances don't match - + # this is fine, it means that the contract was not invoked correctly! + return res diff --git a/examples/b_datums/b_extract_datum.py b/examples/b_datums/b_extract_datum.py new file mode 100644 index 00000000..b32105b0 --- /dev/null +++ b/examples/b_datums/b_extract_datum.py @@ -0,0 +1,45 @@ +# This is an example of how to determine the structure of the datum files to use with you eopsin custom datums. +# i.e. so that you can call the contract like on the previous example +# The JSON file is usually required by third party tools like the cardano-cli + +# Import the dataclass from the eopsin python file + +from examples.b_datums.a_complex_datum import BatchOrder, Withdraw, PubKeyHash + +# Create the datum structure in the correct order and print it to use in your transactions for locking unlocking in cardano-cli +datum = BatchOrder( + PubKeyHash(bytes.fromhex("0000000000000000000000000000000000")), + Withdraw( + 1000000, + 20, + ), +) + +# Export in JSON notation +print(datum.to_json(indent=2)) +""" +{ + "constructor": 0, + "fields": [ + { + "bytes": "0000000000000000000000000000000000" + }, + { + "constructor": 1, + "fields": [ + { + "int": 1000000 + }, + { + "int": 20 + } + ] + } + ] +} +""" +# Export as CBOR Hex +print(datum.to_cbor(encoding="hex")) +""" +d8799f510000000000000000000000000000000000d87a9f1a000f424014ffff +""" diff --git a/examples/b_datums/c_dict_datum.py b/examples/b_datums/c_dict_datum.py new file mode 100644 index 00000000..967083e5 --- /dev/null +++ b/examples/b_datums/c_dict_datum.py @@ -0,0 +1,27 @@ +""" +You can also define dictionaries (maps) as +attributes for datums + +""" +from eopsin.prelude import * + + +@dataclass() +class DictDatum(PlutusData): + pubkeyhash: bytes + + +@dataclass() +class DictDatum2(PlutusData): + dict_field: Dict[int, DictDatum] + + +def validator(dict_datum_2: DictDatum2) -> bool: + # field accesses + # we need to always define a default value! + a = dict_datum_2.dict_field.get(0, DictDatum(b"0")) + # accessing keys and values + # keys and values are lists and can be iterated and indexed + b = DictDatum(b"\x01") in DictDatum2.dict_field.values() + c = DictDatum2.dict_field.keys()[1] + return a and b and c diff --git a/examples/list_datum.py b/examples/b_datums/d_list_datum.py similarity index 73% rename from examples/list_datum.py rename to examples/b_datums/d_list_datum.py index 3f9debae..4cfb2148 100644 --- a/examples/list_datum.py +++ b/examples/b_datums/d_list_datum.py @@ -1,3 +1,8 @@ +""" +You can also define lists as +attributes for datums + +""" from eopsin.prelude import * diff --git a/examples/broken.py b/examples/broken.py deleted file mode 100644 index 2ddfcaf7..00000000 --- a/examples/broken.py +++ /dev/null @@ -1,21 +0,0 @@ -from eopsin.prelude import * - - -# inspired by https://github.com/MuesliSwapTeam/muesliswap-cardano-pool-contracts/blob/main/dex/src/MuesliSwapPools/BatchOrder/Types.hs -@dataclass() -class BatchOrder(PlutusData): - sender: Address - receiver: Address - receiver_datum_hash: Union[Nothing, SomeDatumHash] - batcher_fee: int - output_ada: int - pool_nft_tokenname: TokenName - script_version: bytes - - -def validator(d: Anything, r: Anything) -> bytes: - # this casts the input to BatchOrder - in the type system! In the contract this is a no-op - if "s" == 4: - e: BatchOrder = d - c = e.sender.payment_credential - return b"" diff --git a/examples/c_advanced/__init__.py b/examples/c_advanced/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/c_advanced/a_list_comprehensions.py b/examples/c_advanced/a_list_comprehensions.py new file mode 100644 index 00000000..9510066f --- /dev/null +++ b/examples/c_advanced/a_list_comprehensions.py @@ -0,0 +1,25 @@ +""" +We can branch the control flow using if/else +Also you can write more efficient loops with so-called list comprehensions + +Try it out! + +```bash +(venv) $ eopsin eval examples/a_introduction/a_list_comprehensions.py '{"int": 3}' '{"int": 0}' +> Starting execution +> ------------------ +> ------------------ +> [0, 1, 4] +``` +""" +from eopsin.prelude import * + + +def validator(n: int, even: bool) -> List[int]: + if even: + # generate even squares + res = [k * k for k in range(n) if k % 2 == 0] + else: + # generate all squares + res = [k * k for k in range(n)] + return res diff --git a/examples/datum_cast.py b/examples/c_advanced/b_datum_cast.py similarity index 100% rename from examples/datum_cast.py rename to examples/c_advanced/b_datum_cast.py diff --git a/examples/showcase.py b/examples/c_advanced/c_showcase.py similarity index 100% rename from examples/showcase.py rename to examples/c_advanced/c_showcase.py diff --git a/examples/complex_datum.py b/examples/complex_datum.py deleted file mode 100644 index 60d93f39..00000000 --- a/examples/complex_datum.py +++ /dev/null @@ -1,42 +0,0 @@ -from eopsin.prelude import * - - -@dataclass() -class Deposit(PlutusData): - CONSTR_ID = 0 - minimum_lp: int - - -@dataclass() -class Withdraw(PlutusData): - CONSTR_ID = 1 - minimum_coin_a: int - minimum_coin_b: int - - -OrderStep = Union[Deposit, Withdraw] - -# inspired by https://github.com/MuesliSwapTeam/muesliswap-cardano-pool-contracts/blob/main/dex/src/MuesliSwapPools/BatchOrder/Types.hs -@dataclass() -class BatchOrder(PlutusData): - sender: Address - receiver: Address - # If some property might be ommited, just Union with Nothing and check for the instance at runtime! - # Make sure the property is a PlutusData type, if not, wrap it - receiver_datum_hash: Union[Nothing, SomeDatumHash] - order_step: OrderStep - batcher_fee: int - output_ada: int - pool_nft_tokenname: TokenName - script_version: bytes - - -# If some parameter might be ommited, just Union with Nothing and check for the instance at runtime! -def validator(d: Union[Nothing, BatchOrder]) -> bytes: - if isinstance(d, BatchOrder): - c = d.sender.payment_credential - res = c.credential_hash - elif isinstance(d, Nothing): - res = b"" - # Throws a NameError if the instances don't match - this is fine, it means that the contract was not invoked correctly! - return res diff --git a/examples/dict_datum.py b/examples/dict_datum.py deleted file mode 100644 index a8e32c06..00000000 --- a/examples/dict_datum.py +++ /dev/null @@ -1,19 +0,0 @@ -from eopsin.prelude import * - - -@dataclass() -class D(PlutusData): - p: bytes - - -@dataclass() -class D2(PlutusData): - dict_field: Dict[D, int] - - -def validator(d: D2) -> bool: - return ( - D(b"\x01") in d.dict_field.keys() - and 2 in d.dict_field.values() - and not D(b"") in d.dict_field.keys() - ) diff --git a/examples/extract_datum.py b/examples/extract_datum.py deleted file mode 100644 index afa0c66c..00000000 --- a/examples/extract_datum.py +++ /dev/null @@ -1,70 +0,0 @@ -# This is an example of how to determine the structure of the datum files to use with you eopsin custom datums. -# The JSON file is usually required by third party tools like the cardano-cli - -# ExampleDatumStructure in Eopsin contract: -""" -@dataclass() -class Listing(PlutusData): - # Price of the listing in lovelace - price: int - # the owner of the listed object - vendor: Address - # whoever is allowed to withdraw the listing - owner: PubKeyHash -""" -# Import the dataclass from the eopsin python file - -from examples.smart_contracts.marketplace import ( - Listing, - Address, - PubKeyCredential, - NoStakingCredential, -) - -# Create the datum structure in the correct order and print it to use in your transactions for locking unlocking in cardano-cli -datum = Listing( - 5000000, # This price is in lovelace = 5 ADA - Address( - PubKeyCredential( - bytes.fromhex("f5b84180b5a3cca20052258fe73328c69ff8e0d41709505668101db7") - ), - NoStakingCredential(), - ), - bytes.fromhex("f5b84180b5a3cca20052258fe73328c69ff8e0d41709505668101db7"), -) - -# Export in JSON notation -print(datum.to_json(indent=2)) -""" -{ - "constructor": 0, - "fields": [ - { - "int": 5000000 - }, - { - "constructor": 0, - "fields": [ - { - "constructor": 0, - "fields": [ - { - "bytes": "f5b84180b5a3cca20052258fe73328c69ff8e0d41709505668101db7" - } - ] - }, - { - "constructor": 1, - "fields": [] - } - ] - }, - { - "bytes": "f5b84180b5a3cca20052258fe73328c69ff8e0d41709505668101db7" - } - ] -} -""" -# Export as CBOR Hex -print(datum.to_cbor(encoding="hex")) -"""d8799f1a004c4b40d8799fd8799f581cf5b84180b5a3cca20052258fe73328c69ff8e0d41709505668101db7ffd87a80ff581cf5b84180b5a3cca20052258fe73328c69ff8e0d41709505668101db7ff""" diff --git a/examples/fib_iter.py b/examples/fib_iter.py deleted file mode 100644 index c8d6a125..00000000 --- a/examples/fib_iter.py +++ /dev/null @@ -1,5 +0,0 @@ -def validator(n: int) -> int: - a, b = 0, 1 - for _ in range(n): - a, b = b, a + b - return a diff --git a/examples/fib_rec.py b/examples/fib_rec.py deleted file mode 100644 index e6b975b6..00000000 --- a/examples/fib_rec.py +++ /dev/null @@ -1,12 +0,0 @@ -def fib(n: int) -> int: - if n == 0: - res = 0 - elif n == 1: - res = 1 - else: - res = fib(n - 1) + fib(n - 2) - return res - - -def validator(n: int) -> int: - return fib(n) diff --git a/examples/hello_world.py b/examples/hello_world.py deleted file mode 100644 index d859895f..00000000 --- a/examples/hello_world.py +++ /dev/null @@ -1,2 +0,0 @@ -def validator(_: None) -> None: - print("Hello world!") diff --git a/examples/list_comprehensions.py b/examples/list_comprehensions.py deleted file mode 100644 index 14594c5a..00000000 --- a/examples/list_comprehensions.py +++ /dev/null @@ -1,11 +0,0 @@ -from eopsin.prelude import * - - -def validator(n: int, even: bool) -> List[int]: - if even: - # generate even squares - res = [k * k for k in range(n) if k % 2 == 0] - else: - # generate all squares - res = [k * k for k in range(n)] - return res diff --git a/examples/mult_for.py b/examples/mult_for.py deleted file mode 100644 index c9fa1b4b..00000000 --- a/examples/mult_for.py +++ /dev/null @@ -1,6 +0,0 @@ -def validator(a: int, b: int) -> int: - # trivial implementation of c = a * b - c = 0 - for k in range(b): - c += a - return c diff --git a/examples/mult_while.py b/examples/mult_while.py deleted file mode 100644 index a85033ae..00000000 --- a/examples/mult_while.py +++ /dev/null @@ -1,7 +0,0 @@ -def validator(a: int, b: int) -> int: - # trivial implementation of c = a * b - c = 0 - while 0 < b: - c += a - b -= 1 - return c diff --git a/examples/smart_contracts/__init__.py b/examples/smart_contracts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/examples/sum.py b/examples/sum.py deleted file mode 100644 index c78eb279..00000000 --- a/examples/sum.py +++ /dev/null @@ -1,2 +0,0 @@ -def validator(n: int, m: int) -> int: - return n + m