Skip to content

Configurable Constants

Sway introduces a powerful feature: configurable constants. When creating a contract, you can define constants, each assigned with a default value.

Before deploying the contract, you can then redefine the value for these constants, it can be all of them or as many as you need.

This feature provides flexibility for dynamic contract environments. It allows a high level of customization, leading to more efficient and adaptable smart contracts.

Defining Configurable Constants

Below is an example of a contract in which we declare four configurable constants:

rust
contract;

enum MyEnum {
    Checked: (),
    Pending: (),
}

struct MyStruct {
    x: u8,
    y: u8,
    state: MyEnum,
}

configurable {
    age: u8 = 25,
    tag: str[4] = __to_str_array("fuel"),
    grades: [u8; 4] = [3, 4, 3, 2],
    my_struct: MyStruct = MyStruct {
        x: 1,
        y: 2,
        state: MyEnum::Pending,
    },
}

abi EchoConfigurables {
    fn echo_configurables(updated_grades: bool) -> (u8, str[4], [u8; 4], MyStruct);
}

impl EchoConfigurables for Contract {
    fn echo_configurables(updated_grades: bool) -> (u8, str[4], [u8; 4], MyStruct) {
        if (updated_grades) {
            assert_eq(grades[0], 10);
            assert_eq(grades[1], 9);
            assert_eq(grades[2], 8);
            assert_eq(grades[3], 9);
        } else {
            assert_eq(grades[0], 3);
            assert_eq(grades[1], 4);
            assert_eq(grades[2], 3);
            assert_eq(grades[3], 2);
        }
        (age, tag, grades, my_struct)
    }
}
See code in context

In this contract, we have a function echo_configurables that returns the values of the configurable constants.

If each of these constants has new values that have been assigned to them, the function will return the updated values. Otherwise, the function will return the default values.

Setting New Values For Configurable Constants

During contract deployment, you can define new values for the configurable constants. This is achieved as follows:

ts
const configurableConstants: typeof defaultValues = {
  age: 30,
  tag: 'leuf',
  grades: [10, 9, 8, 9],
  my_struct: {
    x: 11,
    y: 22,
    state: 'Checked',
  },
};

const factory = new ContractFactory(bin, abi, wallet);

const { minGasPrice } = provider.getGasConfig();

const contract = await factory.deployContract({
  configurableConstants,
  gasPrice: minGasPrice,
});
See code in context

You can assign new values to any of these configurable constants.

If you wish to assign a new value to just one constant, you can do the following:

ts
const configurableConstants = {
  age: 10,
};

const factory = new ContractFactory(bin, abi, wallet);

const { minGasPrice } = provider.getGasConfig();

const contract = await factory.deployContract({
  configurableConstants,
  gasPrice: minGasPrice,
});
See code in context

Please note that when assigning new values for a Struct, all properties of the Struct must be defined. Failing to do so will result in an error:

ts
const configurableConstants = {
  my_struct: {
    x: 2,
  },
};

const factory = new ContractFactory(bin, abi, wallet);

const { minGasPrice } = provider.getGasConfig();

await expect(
  factory.deployContract({
    configurableConstants,
    gasPrice: minGasPrice,
  })
).rejects.toThrowError();
See code in context