I'm trying to ensure my contract is upgradable and I learn best by looking at existing examples. Does anyone have any good examples to look at?
Here are some links to previous stackoverflow Q&A's discussuing smart contract updates:
Tezos Stack Exchange is a question and answer site for users, experts, and developers of the Tezos blockchain project. It only takes a minute to sign up.
Sign up to join this communityI'm trying to ensure my contract is upgradable and I learn best by looking at existing examples. Does anyone have any good examples to look at?
Here are some links to previous stackoverflow Q&A's discussuing smart contract updates:
Stored lambdas approach:
It allows any packable value as a parameter and the method that computes the new value can be updated.
import smartpy as sp
class Upgradable(sp.Contract):
def __init__(self, **kargs):
self.init(**kargs)
@sp.entry_point
def calc(self, data):
self.data.value = self.data.logic(data)
@sp.entry_point
def updateLogic(self, logic):
self.data.logic = logic
@sp.add_test(name = "Upgradable")
def test():
scenario = sp.test_scenario()
scenario.h1("Upgradable")
def logic1(data):
t = sp.TRecord(x = sp.TNat, y = sp.TNat)
unpacked = sp.unpack(data, t).open_some(message = "Cannot UNPACK")
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 = "Cannot UNPACK")
sp.result(unpacked.x + unpacked.y + unpacked.z)
c1 = Upgradable(value = 0, logic = sp.build_lambda(logic1))
scenario += c1
# Use logic version 1
scenario += c1.calc(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(sp.pack(sp.record(x = 1, y = 2, z = 3)))
The proxy approach is also simple, it just points to the latest contract whenever a new one gets deployed.