Value from Vec inside structure is not equal expected one

Here is the contract that I am trying to run

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

but it fails with the next error:

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

18 Likes

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)

4 Likes

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

1 Like

What is the compilation error?

1 Like
 --> /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 | }
  |
____

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

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

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

Yes that is solution

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