Using vectors on predicate configurable

I currently have a predicate with some configurable ones, and one of them is an array of type [b256;10].

image

This is a problem if I want to have this array with non-zero lengths.

How could I use a vector in these configurables?

Remembering that configurables cannot receive values that have not yet been instantiated, such as (ref):

SIGNERS: Vec<b256> = Vec::new()

You can instantiate an empty vec with Sway by Example
SIGNERS_VEC: StorageVec<u64> = StorageVec {}

predicate;

use std::{storage::storage_vec::*};

configurable {
    REQUIRED_SIGNATURES: u64 = 0,
    SIGNERS: [Address; 3] = [
        Address::from(0x0000000000000000000000000000000000000000000000000000000000000000),
        Address::from(0x0000000000000000000000000000000000000000000000000000000000000000),
        Address::from(0x0000000000000000000000000000000000000000000000000000000000000000)
    ],
    SIGNERS_VEC: StorageVec<u64> = StorageVec {},
}

fn main() -> bool {
    false
} 
1 Like

Hmmm, StorageVec is meant to be used only in the storage. Having it as a configurable just puts an empty struct in the configurable and that struct cannot be changed any more.
In general, configurables must have a size known at the compile time. That’s why we cannot store dynamic structures there.
What is your exact use case? Do you know the upper bound of the signers? E.g. knowing there will be no more then 10. Then you can have a configurable tuple where the first element would be the actual count e.g. SIGNERS: (u8, [b256;10]) = [ all zeros ]. In the SDK you can then set the SIGNERS.0 to e.g. 3 and fill the first three elements of the array and left others to be zero.

Just heard back from the sway team and according to them, at the moment, we do not support “dynamic types” on configurable. It is something we are aiming for in the “near future”. Especially because it seems necessary in some cases. But using two configurable: (len, array) is the best workaround we have at the moment.

1 Like

Great suggestion, seems to work well for me
thanks :slight_smile:

1 Like

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