0
import smartpy as sp

def logic(data):
    t = sp.TRecord(x=sp.TNat, y=sp.TNat, z=sp.TNat)
    unpacked = sp.unpack(data, t).open_some(message="Can't unpack!!")
    
    sp.result(unpacked.x + unpacked.y)
    
i_need_this = sp.build_lambda(logic)

How will I be able to convert SmartPy functions into Michelson code?

Context: I need this lambda in michelson so that I am able to pass it as an argument to an entrypoint in typescript (with taquito).

1 Answer 1

0

I re-paste the answer given on Telegram.

Using the CLI:

scenario.show(lambda x:x+1, compile=True)

This creates a file when called from the CLI with the compilation target (named something like step_000_expression.tz).

Using the web browser:

import smartpy as sp

class MyContract(sp.Contract):
    def __init__(self):
        self.init()

    @sp.entry_point
    def f(self, p):
        sp.compute(p)

@sp.add_test(name = "Test")
def test():
    scenario = sp.test_scenario()
    c1 = MyContract()
    scenario += c1
    c1.f(lambda x:x+1)

(the sp.compute is almost irrelevant, it’s only there to avoid SmartPy complaining that p is unused)

Example here.

With a longer function you can do

def my_function(params):
    sp.set_type(params, sp.TRecord(a=sp.TNat, b=sp.TNat, c=sp.TNat))
    sp.result(params.a + params.b + params.c)

c1.f(my_function)

Example here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.