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.
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.
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.