Mismatched types - StorageKey<u64>

Alright, so here is the Rust SDK fix, it’s a bit tricky.

First of all, what caused the revert(0)?
The increment() method first reads the current storage value, then increases it by 1, then writes it to storage with write(). Despite initializing the storage variable with 0, it seems that it is not actually initialized when we call read() on it since the read() internally just unwraps the value (in our case probably a None) and throws the revert(0).

How to fix that?
I might post the reproducer on the Discord because it seems the error shouldn’t happen in the first place (since we initialized count with 0 in storage decl). However, we can mitigate this problem by slightly changing our increment function to:

#[storage(read, write)]
fn increment() {
    storage.counter.write(storage.counter.try_read().unwrap_or(0) + 1)
}

As you can see, we use try_read() instead of read() which gives us back an Option. We then unwrap with unwrap_or() so that we can define the behavior in case of an error and set it to 0.

I hope that explanation helps. Let me know in case it doesn’t work on your end :slight_smile:

1 Like