Skip to content

Instantly share code, notes, and snippets.

@mattdesl
Created December 18, 2021 17:12
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattdesl/1ed4308d8c83d272d815071428ed98ce to your computer and use it in GitHub Desktop.
Save mattdesl/1ed4308d8c83d272d815071428ed98ce to your computer and use it in GitHub Desktop.
import smartpy as sp
class SpectrumColors(sp.Contract):
def __init__(self):
self.init(
# fixed dimensions of the board
columns = 16,
rows = 16,
# limit to the total number of colors possible
max_colors = 12,
# the map<nat,nat> for the board display
display = sp.map(tkey = sp.TNat, tvalue = sp.TNat),
)
@sp.entry_point
def update(self, params):
# update a cell by index with a new color
sp.verify(sp.amount == sp.tez(0), "the operation does not need tez transfers")
color = params.color
idx = params.index
sp.verify((idx >= 0) & (idx < self.data.rows * self.data.columns), "index out of range")
sp.verify((color >= 0) & (color < self.data.max_colors), "color index out of range")
self.data.display[idx] = color
@sp.offchain_view(pure = True)
def get_color(self, idx):
# a view that gets us the color at the specified index
sp.set_type(idx, sp.TNat)
sp.verify((idx >= 0) & (idx < self.data.rows * self.data.columns), "index out of range")
sp.result(self.data.display.get(idx, 0))
if "templates" not in __name__:
@sp.add_test(name = "SpectrumColors")
def test():
c1 = SpectrumColors()
scenario = sp.test_scenario()
scenario.h1("SpectrumColors")
scenario += c1
x = 0
y = 2
rows = 3
columns = 4
index = (x + y * columns)
c1.update(color = 3, index = index)
c1.update(color = 2, index = index)
c1.update(color = 6, index = 1)
scenario.p("Epoch").show(c1.data.display)
scenario.p("Epoch").show(c1.get_color(index))
sp.add_compilation_target("storeValue", SpectrumColors())
@postspectacular
Copy link

@mattdesl noice! Is there an easy way for you to dump out the compiled Michelson source too? Would very much like to compare...

@mattdesl
Copy link
Author

Thanks! It’s deployed on test net so Michelson is visible there:

https://better-call.dev/hangzhou2net/KT1MeQytVramwMXJRyksTemX2B7qF4MJoRSG/code

If you have a testnet funds + dev wallet you can also interact with the contract through the block explorer.

@postspectacular
Copy link

Awesome, thanks! Good to see these side by side! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment