1

I'd like to share a question that were being worked on Smartpy telegram, and also a few thoughts along the way (it's a long way -- random observation).

Some people were talking about local variables and in the meanwhile I was working in a smart contract in which I'd like to insert a list within a map -- also retrieve it, modify it, etc.

The only way I reached it was by declaring a list in the storage of the smart contract. Part of this question concerns some typos.

Previous solution:

...

    self.init(
            map_example = sp.big_map(tkey=sp.TInt, tvalue=sp.TList(t=sp.TInt)),
            aux_list = sp.list(t=sp.TInt)
    )


    @sp.entry_point
    def initialize(self, params):
        sp.for i in sp.range(1, 3):
            self.data.aux_list = []
            sp.for k in sp.range(0, 8):
                self.data.aux_list.push(k)
            self.data.map_example[i] = self.data.aux_list

...

With this typo it was possible to execute the procedure in mind. But how to insert a list within the map with a local list?

I tryied:

        sp.for i in sp.range(1, 3):
            aux_list = []
            sp.for k in sp.range(0, 8):
                aux_list.append(k)  #  .push(k)
            self.data.map_example[i] = aux_list

With the help of @FFF ? (on telegram), we got to this example, which is interesting to observe the typo so you can handle local variables (notice de value? method/attribute).

    sp.for i in sp.range(1, 3):
        aux_list = sp.local('aux_list', [])
        sp.for k in sp.range(0, 8):
            aux_list.value.push(k)
        self.data.map_example[i] = aux_list.value

Finally, a cleaner way to write it:

sp.for i in sp.range(1, 3):
    self.data.map_example[i] = sp.range(0, 8)

If someone has something to add, be my guest.

1

Maybe a precision: sp.range is computed on chain while simply range is computed at compile time.

For a small range, it's probably much cheaper to use range (cheaper in terms of gas + storage).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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