works fine, but my IDE using Sway VS Code plugin is giving me this error message:
Mismatched types.
expected: u64
found: StorageKey<u64>.
help: Implicit return must match up with block's type.
Mismatched types.
expected: u64
found: StorageKey<u64>.
help: Function body's return type does not match up with its return type annotation.
I have a couple of questions:
How to make this error message go away? or maybe the sway VS Code plugin is out of date?
If I’m creating a video tutorial, will the syntax change in the next few days or weeks? If so, should I wait until the new updates are made so the tutorial is not out of date?
Please run fuelup toolchain install latest in case you haven’t in a longer while.
You can also always run fuelup check to see if there are updates available :).
Hey @dabit3 - I messaged you privately but posting here too. Given that Sway is so new and new features are constantly being released, I wouldn’t encourage you to wait. There will always be something “new” around the corner that breaks code.
I would focus your video less on specific syntax, and focus more on high level things about the language, like how storage works, compile time safety, exhaustive pattern matching, generics, etc.
Its unfortunate but its just the reality of the state of the tech- it’s evolving extremely quickly and its far from being at a point where you can expect stability over a long period of time.
imo the main challenge here is that these updates seem to be breaking many of the existing guides and codebases including the quickstart and making it confusing if someone is using them, so that if someone wants to try it out right even following along in the docs, they will be unable to
I guess the main callout (which I’m sure your team is already aware of) is to consider prioritizing rolling out these new updates across the entire toolchain including the VS Code plugin
thanks for your help, I think I have enough to work with now
#[tokio::test]
async fn can_get_contract_id() {
let (instance, _id) = get_contract_instance().await;
// Increment the counter
instance.methods().increment().call().await;
// // Get the current value of the counter
let result = instance.methods().count().call().await.unwrap();
// // Check that the current value of the counter is 1.
// // Recall that the initial value of the counter was 0.
assert_eq!(result.value, 1);
}
Here is the error I’m getting:
failures:
---- can_get_contract_id stdout ----
thread 'can_get_contract_id' panicked at 'called `Result::unwrap()` on an `Err` value: RevertTransactionError { reason: "Revert(0)", revert_id: 0, receipts: [Call { id: 0000000000000000000000000000000000000000000000000000000000000000, to: 1b16bef0e1a5e4c32309a84e1a98fdbfd13a1ae7269e4e9d2b1d4f3b380f96be, amount: 0, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, gas: 1000000, param1: 1012642802, param2: 1, pc: 11624, is: 11624 }, Revert { id: 1b16bef0e1a5e4c32309a84e1a98fdbfd13a1ae7269e4e9d2b1d4f3b380f96be, ra: 0, pc: 11900, is: 11624 }, ScriptResult { result: Revert, gas_used: 343 }] }', tests/harness.rs:44:58
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
failures:
can_get_contract_i
Hi @dabit3, before trying to reproduce your Rust SDK test, let me just put my quick-and-dirty in-lang test here. Currently, I always code a quick in-lang (if there are no substantial storage initializations / TxParams / CallParams needed) before going over to the Rust SDK.
That should at least give you a working test for now:
contract;
////
// All the other contract code
////
#[test]
fn test_count() {
let caller = abi(Counter, CONTRACT_ID);
let res = caller.count();
assert(res == 0);
}
#[test]
fn test_increment() {
let caller = abi(Counter, CONTRACT_ID);
let res = caller.count();
assert(res == 0);
caller.increment();
let res = caller.count();
assert(res == 1);
}
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:
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