Module sui::config
- Struct
Config
- Struct
Setting
- Struct
SettingData
- Constants
- Function
new
- Function
share
- Function
transfer
- Function
add_for_next_epoch
- Function
remove_for_next_epoch
- Function
exists_with_type
- Function
exists_with_type_for_next_epoch
- Function
borrow_for_next_epoch_mut
- Function
read_setting_for_next_epoch
- Macro function
entry
- Macro function
update
- Function
read_setting
- Function
read_setting_impl
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::dynamic_field;
use sui::hex;
use sui::object;
use sui::transfer;
use sui::tx_context;
Struct Config
public struct Config<phantom WriteCap> has key
Click to open
Fields
- id: sui::object::UID
Struct Setting
public struct Setting<Value: copy, drop, store> has drop, store
Click to open
Fields
- data: std::option::Option<sui::config::SettingData<Value>>
Struct SettingData
public struct SettingData<Value: copy, drop, store> has drop, store
Click to open
Fields
- newer_value_epoch: u64
- newer_value: std::option::Option<Value>
- older_value_opt: std::option::Option<Value>
Constants
const EAlreadySetForEpoch: u64 = 0;
const EBCSSerializationFailure: u64 = 2;
const ENotSetForEpoch: u64 = 1;
Function new
public(package) fun new<WriteCap>(_cap: &mut WriteCap, ctx: &mut sui::tx_context::TxContext): sui::config::Config<WriteCap>
Click to open
Implementation
public(package) fun new<WriteCap>(_cap: &mut WriteCap, ctx: &mut TxContext): Config<WriteCap> {
Config<WriteCap> { id: object::new(ctx) }
}
Function share
public(package) fun share<WriteCap>(config: sui::config::Config<WriteCap>)
Click to open
Function transfer
public(package) fun transfer<WriteCap>(config: sui::config::Config<WriteCap>, owner: address)
Click to open
Function add_for_next_epoch
public(package) fun add_for_next_epoch<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &mut sui::config::Config<WriteCap>, _cap: &mut WriteCap, name: Name, value: Value, ctx: &mut sui::tx_context::TxContext): std::option::Option<Value>
Click to open
Implementation
public(package) fun add_for_next_epoch<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &mut Config<WriteCap>,
_cap: &mut WriteCap,
name: Name,
value: Value,
ctx: &mut TxContext,
): Option<Value> {
let epoch = ctx.epoch();
if (!field::exists_(&config.id, name)) {
let sobj = Setting {
data: option::some(SettingData {
newer_value_epoch: epoch,
newer_value: option::some(value),
older_value_opt: option::none(),
}),
};
field::add(&mut config.id, name, sobj);
option::none()
} else {
let sobj: &mut Setting<Value> = field::borrow_mut(&mut config.id, name);
let SettingData {
newer_value_epoch,
newer_value,
older_value_opt,
} = sobj.data.extract();
let (older_value_opt, removed_value) =
if (epoch > newer_value_epoch) {
// if the `newer_value` is for a previous epoch, move it to `older_value_opt`
(move newer_value, move older_value_opt)
} else {
// the current epoch cannot be less than the `newer_value_epoch`
assert!(epoch == newer_value_epoch);
// if the `newer_value` is for the current epoch, then the option must be `none`
assert!(newer_value.is_none(), EAlreadySetForEpoch);
(move older_value_opt, option::none())
};
sobj.data.fill(SettingData {
newer_value_epoch: epoch,
newer_value: option::some(value),
older_value_opt,
});
removed_value
}
}
Function remove_for_next_epoch
public(package) fun remove_for_next_epoch<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &mut sui::config::Config<WriteCap>, _cap: &mut WriteCap, name: Name, ctx: &mut sui::tx_context::TxContext): std::option::Option<Value>
Click to open
Implementation
public(package) fun remove_for_next_epoch<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &mut Config<WriteCap>,
_cap: &mut WriteCap,
name: Name,
ctx: &mut TxContext,
): Option<Value> {
let epoch = ctx.epoch();
if (!field::exists_(&config.id, name)) return option::none();
let sobj: &mut Setting<Value> = field::borrow_mut(&mut config.id, name);
let SettingData {
newer_value_epoch,
newer_value,
older_value_opt,
} = sobj.data.extract();
let (older_value_opt, removed_value) =
if (epoch > newer_value_epoch) {
// if the `newer_value` is for a previous epoch, move it to `older_value_opt`
(move newer_value, option::none())
} else {
// the current epoch cannot be less than the `newer_value_epoch`
assert!(epoch == newer_value_epoch);
(move older_value_opt, move newer_value)
};
let older_value_opt_is_none = older_value_opt.is_none();
sobj.data.fill(SettingData {
newer_value_epoch: epoch,
newer_value: option::none(),
older_value_opt,
});
if (older_value_opt_is_none) {
field::remove<_, Setting<Value>>(&mut config.id, name);
};
removed_value
}
Function exists_with_type
public(package) fun exists_with_type<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &sui::config::Config<WriteCap>, name: Name): bool
Click to open
Function exists_with_type_for_next_epoch
public(package) fun exists_with_type_for_next_epoch<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &sui::config::Config<WriteCap>, name: Name, ctx: &sui::tx_context::TxContext): bool
Click to open
Implementation
public(package) fun exists_with_type_for_next_epoch<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &Config<WriteCap>,
name: Name,
ctx: &TxContext,
): bool {
field::exists_with_type<_, Setting<Value>>(&config.id, name) && {
let epoch = ctx.epoch();
let sobj: &Setting<Value> = field::borrow(&config.id, name);
epoch == sobj.data.borrow().newer_value_epoch &&
sobj.data.borrow().newer_value.is_some()
}
}
Function borrow_for_next_epoch_mut
public(package) fun borrow_for_next_epoch_mut<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &mut sui::config::Config<WriteCap>, _cap: &mut WriteCap, name: Name, ctx: &mut sui::tx_context::TxContext): &mut Value
Click to open
Implementation
public(package) fun borrow_for_next_epoch_mut<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &mut Config<WriteCap>,
_cap: &mut WriteCap,
name: Name,
ctx: &mut TxContext,
): &mut Value {
let epoch = ctx.epoch();
let sobj: &mut Setting<Value> = field::borrow_mut(&mut config.id, name);
let data = sobj.data.borrow_mut();
assert!(data.newer_value_epoch == epoch, ENotSetForEpoch);
assert!(data.newer_value.is_some(), ENotSetForEpoch);
data.newer_value.borrow_mut()
}
Function read_setting_for_next_epoch
public(package) fun read_setting_for_next_epoch<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &sui::config::Config<WriteCap>, name: Name): std::option::Option<Value>
Click to open
Implementation
public(package) fun read_setting_for_next_epoch<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &Config<WriteCap>,
name: Name,
): Option<Value> {
if (!field::exists_with_type<_, Setting<Value>>(&config.id, name)) return option::none();
let sobj: &Setting<Value> = field::borrow(&config.id, name);
let data = sobj.data.borrow();
data.newer_value
}
Macro function entry
public(package) macro fun entry<$WriteCap, $Name: copy, drop, store, $Value: copy, drop, store>($config: &mut sui::config::Config<$WriteCap>, $cap: &mut $WriteCap, $name: $Name, $initial_for_next_epoch: |&mut sui::config::Config<$WriteCap>, &mut $WriteCap, &mut sui::tx_context::TxContext| -> $Value, $ctx: &mut sui::tx_context::TxContext): &mut $Value
Click to open
Implementation
public(package) macro fun entry<
$WriteCap,
$Name: copy + drop + store,
$Value: copy + drop + store,
>(
$config: &mut Config<$WriteCap>,
$cap: &mut $WriteCap,
$name: $Name,
$initial_for_next_epoch: |&mut Config<$WriteCap>, &mut $WriteCap, &mut TxContext| -> $Value,
$ctx: &mut TxContext,
): &mut $Value {
let config = $config;
let cap = $cap;
let name = $name;
let ctx = $ctx;
if (!config.exists_with_type_for_next_epoch<_, _, $Value>(name, ctx)) {
let initial = $initial_for_next_epoch(config, cap, ctx);
config.add_for_next_epoch(cap, name, initial, ctx);
};
config.borrow_for_next_epoch_mut(cap, name, ctx)
}
Macro function update
public(package) macro fun update<$WriteCap, $Name: copy, drop, store, $Value: copy, drop, store>($config: &mut sui::config::Config<$WriteCap>, $cap: &mut $WriteCap, $name: $Name, $initial_for_next_epoch: |&mut sui::config::Config<$WriteCap>, &mut $WriteCap, &mut sui::tx_context::TxContext| -> $Value, $update_for_next_epoch: |std::option::Option<$Value>, &mut $Value| -> (), $ctx: &mut sui::tx_context::TxContext)
Click to open
Implementation
public(package) macro fun update<
$WriteCap,
$Name: copy + drop + store,
$Value: copy + drop + store,
>(
$config: &mut Config<$WriteCap>,
$cap: &mut $WriteCap,
$name: $Name,
$initial_for_next_epoch: |&mut Config<$WriteCap>, &mut $WriteCap, &mut TxContext| -> $Value,
$update_for_next_epoch: |Option<$Value>, &mut $Value|,
$ctx: &mut TxContext,
) {
let config = $config;
let cap = $cap;
let name = $name;
let ctx = $ctx;
let old_value_opt =
if (!config.exists_with_type_for_next_epoch<_, _, $Value>(name, ctx)) {
let initial = $initial_for_next_epoch(config, cap, ctx);
config.add_for_next_epoch(cap, name, initial, ctx)
} else {
option::none()
};
$update_for_next_epoch(old_value_opt, config.borrow_for_next_epoch_mut(cap, name, ctx));
}
Function read_setting
public(package) fun read_setting<Name: copy, drop, store, Value: copy, drop, store>(config: sui::object::ID, name: Name, ctx: &sui::tx_context::TxContext): std::option::Option<Value>
Click to open
Implementation
public(package) fun read_setting<Name: copy + drop + store, Value: copy + drop + store>(
config: ID,
name: Name,
ctx: &TxContext,
): Option<Value> {
use sui::dynamic_field::Field;
let config_id = config.to_address();
let setting_df = field::hash_type_and_key(config_id, name);
read_setting_impl<Field<Name, Setting<Value>>, Setting<Value>, SettingData<Value>, Value>(
config_id,
setting_df,
ctx.epoch(),
)
}
Function read_setting_impl
fun read_setting_impl<FieldSettingValue: key, SettingValue: store, SettingDataValue: store, Value: copy, drop, store>(config: address, name: address, current_epoch: u64): std::option::Option<Value>
Click to open
Implementation
native fun read_setting_impl<
FieldSettingValue: key,
SettingValue: store,
SettingDataValue: store,
Value: copy + drop + store,
>(
config: address,
name: address,
current_epoch: u64,
): Option<Value>;