# Value from Vec inside structure is not equal expected one

**URL:** <https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306>\
**Category:** Sway\
**Created:** [January 6, 2023, 5:40pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306 "2023-01-06T17:40:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 6, 2023, 5:40pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/1 "2023-01-06T17:40:57Z")

</div>

Here is the contract that I am trying to run

```auto
contract;

pub struct MarketConfiguration {
    foo: u64,
    bar: u64,
    asset_configs: Vec<AssetConfig>,
}

pub struct AssetConfig {
    asset: ContractId,
    decimals: u8,
    blablabla: u64,
}

abi MyContract {
    #[storage(write)]
    fn initialize(config: MarketConfiguration);

    #[storage(read)]
    fn get_asset_config_by_asset_id(asset: ContractId) -> AssetConfig;
}

storage {
    config: Option<MarketConfiguration> = Option::None,
}

#[storage(read)]
fn get_config() -> MarketConfiguration {
    match storage.config {
        Option::Some(c) => c,
        _ => revert(0),
    }
}

#[storage(read)]
fn get_asset_config_by_asset_id_internal(asset: ContractId) -> AssetConfig {
    let mut out: Option<AssetConfig> = Option::None;
    let config = get_config();
    let mut i = 0;
    while i < config.asset_configs.len() {
        let asset_config = config.asset_configs.get(i).unwrap();
        if asset_config.asset == asset {
            out = Option::Some(asset_config);
            break;
        }
        i += 1;
    }
    match out {
        Option::Some(v) => v,
        Option::None(_) => revert(0),
    }
}

impl MyContract for Contract {
    #[storage(write)]
    fn initialize(config: MarketConfiguration) {
        storage.config = Option::Some(config);
    }

    #[storage(read)]
    fn get_asset_config_by_asset_id(asset: ContractId) -> AssetConfig {
        get_asset_config_by_asset_id_internal(asset)
    }
}

```

And this is method that should help me to get value out of assets\_config

> <https://github.com/chlenc/storage_vec_example/blob/6723abc8db5b456b4b74f726ec762eb349b9c948/tests/harness.rs#L90>

but it fails with the next error:

```auto
test can_get_contract_id ... FAILED

failures:

---- can_get_contract_id stdout ----
thread 'can_get_contract_id' panicked at 'called `Result::unwrap()` on an `Err` value: RevertTransactionError("Revert(0)", [Call { id: 0000000000000000000000000000000000000000000000000000000000000000, to: 24a5c44cb5bf5deed72e16117d6c0b1b9a78a15dd16f06a1faff48dcb252fb1f, amount: 0, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, gas: 1000000, param1: 4111974, param2: 10472, pc: 11664, is: 11664 }, Revert { id: 24a5c44cb5bf5deed72e16117d6c0b1b9a78a15dd16f06a1faff48dcb252fb1f, ra: 0, pc: 12368, is: 11664 }, ScriptResult { result: Revert, gas_used: 27264 }])', tests/harness.rs:94:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    can_get_contract_id

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.80s

```

The repo

> **[GitHub - chlenc/storage\_vec\_example](https://github.com/chlenc/storage_vec_example)**
>
> Contribute to chlenc/storage\_vec\_example development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/swaystar123/32/510_2.png) [@SwayStar123](https://forum.fuel.network/u/SwayStar123)\
**Post date:** [January 6, 2023, 6:33pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/2 "2023-01-06T18:33:35Z")

</div>

as Vec is a heap type, it cannot be used for persistant storage. Use the StorageVec type from `std::storage::StorageVec` for persistant vecs. Although this type also has its limitations as only one StorageVec/StorageMap can be used per storage variable (so no nested vecs or multiple vecs in a struct)

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 7, 2023, 11:28am UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/3 "2023-01-07T11:28:34Z")

</div>

If you do that, it gives a compilation error.

I just put the vector in the storage and put it next to the config, that’s the solution

> <https://github.com/chlenc/storage_vec_example/blob/master/src/main.sw>

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/swaystar123/32/510_2.png) [@SwayStar123](https://forum.fuel.network/u/SwayStar123)\
**Post date:** [January 7, 2023, 1:12pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/4 "2023-01-07T13:12:33Z")

</div>

What is the compilation error?

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 8, 2023, 9:42pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/5 "2023-01-08T21:42:40Z")

</div>

```auto
 --> /Users/alexey/projects/fuel/fuel-project/storage_vec_example/src/main.sw:7:5
  |
5 | 
6 | bar: u64,
7 | asset_configs: StorageVec<AssetConfig>,
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Type StorageVec<AssetConfig> can only be declared directly as a storage field
8 | }
  |
____

```

---

<div class="post-metadata">

**Author:** ![SwayStar123](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/swaystar123/32/510_2.png) [@SwayStar123](https://forum.fuel.network/u/SwayStar123)\
**Post date:** [January 9, 2023, 12:33pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/6 "2023-01-09T12:33:55Z")

</div>

That does not look like the same code as in the file linked

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 10, 2023, 5:42pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/7 "2023-01-10T17:42:27Z")

</div>

Sorry, I’m decided to move development on  
You should checkout to commit 6723abc8db5b456b4b74f726ec762eb349b9c948

---

<div class="post-metadata">

**Author:** ![Simon](https://avatars.discourse-cdn.com/v4/letter/s/f07891/32.png) [@Simon](https://forum.fuel.network/u/Simon)\
**Post date:** [January 16, 2023, 4:32pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/8 "2023-01-16T16:32:42Z")

</div>

Checking back here - from the latest on your repo it looks like you resolved this?

---

<div class="post-metadata">

**Author:** ![fuel](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.fuel.network/fuel/32/1677_2.png) [@fuel](https://forum.fuel.network/u/fuel)\
**Post date:** [January 16, 2023, 5:26pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/9 "2023-01-16T17:26:47Z")

</div>

Yes that is solution

 ![image](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/1X/b7686dea7c3da35a4ab19fe42000afb28a3013a8.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex022/uploads/fuel/original/2X/5/57d5a345cc15a64b636e0d56e042857f8a0e80b1.png) [@system](https://forum.fuel.network/u/system)\
**Post date:** [February 6, 2023, 1:27pm UTC](https://forum.fuel.network/t/value-from-vec-inside-structure-is-not-equal-expected-one/1306/10 "2023-02-06T13:27:37Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
