1

So, this is the piece of code in my smartpy test contracts. Both methods, calc_reward and updateLogic is working. This is in the test section of the contract.

enter image description here

So, basically, what I want to do is call the updateLogic method from taquito but how would I build lambda for it?

After much hassle, I was able to pack/unpack values similar to sp.pack in taquito but I'm again stuck in the updating part.

enter image description here

Is it not possible? Or, do I have a wrong understanding?

Here's the entire code of smart contract:

import smartpy as sp

class Testes(sp.Contract):
    def __init__(self, **kwargs):
        self.init(**kwargs)
        
    @sp.entry_point
    def calc_reward(self, data):
        self.data.value = self.data.logic(data)
    
    @sp.entry_point
    def updateLogic(self, logic):
        self.data.logic = logic
    
@sp.add_test("Testes contract test")
def test():
    scenario = sp.test_scenario()
    scenario.h1("Testing upgradability")
    
    def logic1(data):
        t = sp.TRecord(x=sp.TNat, y=sp.TNat)
        unpacked = sp.unpack(data, t).open_some(message="Can't unpack sed!!")
        
        sp.result(unpacked.x + unpacked.y)
    
    def logic2(data):
        t = sp.TRecord(x=sp.TNat, y=sp.TNat, z=sp.TNat)
        unpacked = sp.unpack(data, t).open_some(message="Can't unpack sed!!")
        
        sp.result(unpacked.x + unpacked.y)
    
    
    c1 = Testes(value=100, logic=sp.build_lambda(logic1))
    scenario+=c1
    
    # Using logic 1
    scenario += c1.calc_reward(sp.pack(sp.record(x=1, y=2)))
    
      # Update logic to version 2
    scenario += c1.updateLogic(sp.build_lambda(logic2))

    # Use logic version 2
    scenario += c1.calc_reward(sp.pack(sp.record(x = 1, y = 2, z = 3)))
    
    sp.add_compilation_target("testes", Testes(value=69, logic=sp.build_lambda(logic1)))
        
        

And here's my typescript file:

import * as dotenv from "dotenv";
import { InMemorySigner } from "@taquito/signer";
import { TezosToolkit } from "@taquito/taquito";
import { NETWORK } from "../config/config";
import {
  MichelsonData,
  MichelsonType,
  packDataBytes,
} from "@taquito/michel-codec";

dotenv.config();

const { ORIGINATOR_PRIVATE_KEY } = process.env;

const signer = await InMemorySigner.fromSecretKey(ORIGINATOR_PRIVATE_KEY!);
const Tezos = new TezosToolkit(NETWORK.GHOSTNET.url);
Tezos.setProvider({ signer: signer });

const updateContract = async () => {
    //idk what to do here
};

const updateValue = () => {
  const data: MichelsonData = {
    prim: "Pair",
    args: [{ int: "111" }, { int: "222" }],
  };

  const typ: MichelsonType = {
    prim: "pair",
    args: [{ prim: "int" }, { prim: "int" }],
  };

  const packed = packDataBytes(data, typ);
  console.log(packed.bytes);

  Tezos.contract
    .at("KT1AbLHMuiKaSufWo5MzU5pQGwx7BayQNLUf")
    .then((contract) => {
      return contract.methods.calc_reward(packed.bytes).send();
    })
    .then((op) => {
      console.log(`Waiting for ${op.hash} to be confirmed...`);
      return op.confirmation(1).then(() => op.hash);
    })
    .then((hash: any) => console.log("Done!"))
    .catch((error) => {
      console.log(error);
    });
};

Any help would be appreciated.

1 Answer 1

1

A lambda is just a piece of Michelson code, you will have to provide the Michelson code to Taquito before packing it and sending it as a parameter to the contract entrypoint. You can find an example in this draft for the upcoming multisig documentation in Taquito.

3
  • Is there a way to get michelson code from a python function? ``` 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 sed!!") sp.result(unpacked.x + unpacked.y) ans = sp.build_lambda(logic) ``` This doesn't work.
    – rpj
    Commented Nov 7, 2022 at 12:38
  • There might be a way to compile a SmartPy expression into Michelson (there is one in Ligo) but I don't know SmartPy so I can't answer that, sorry. You should try to ask on the SmartPy Telegram channel. Commented Nov 7, 2022 at 14:57
  • Okay, thanks @Claude Barde
    – rpj
    Commented Nov 7, 2022 at 15:46

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.