use 0x1::option;
use 0x2::dynamic_field;
use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;
Resource Config
struct Config<WriteCap> has key
Fields
Struct Setting
struct Setting<Value: copy, drop, store> has store
Fields
Struct SettingData
struct SettingData<Value: copy, drop, store> has store
Fields
Constants
const EBCSSerializationFailure: u64 = 2;
const EAlreadySetForEpoch: u64 = 0;
const ENotSetForEpoch: u64 = 1;
Function new
public(friend) fun new<WriteCap>(_cap: &mut WriteCap, ctx: &mut tx_context::TxContext): config::Config<WriteCap>
Implementation
public(package) fun new<WriteCap>(_cap: &mut WriteCap, ctx: &mut TxContext): Config<WriteCap> {
Config<WriteCap> { id: object::new(ctx) }
}
Function create
public(friend) fun create<WriteCap>(cap: &mut WriteCap, ctx: &mut tx_context::TxContext)
Implementation
Function add_for_current_epoch
public(friend) fun add_for_current_epoch<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &mut config::Config<WriteCap>, _cap: &mut WriteCap, name: Name, value: Value, _ctx: &mut tx_context::TxContext): option::Option<Value>
Implementation
public(package) fun add_for_current_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: 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();
assert!(epoch > newer_value_epoch, EAlreadySetForEpoch);
sobj.data.fill(SettingData {
newer_value_epoch: epoch,
newer_value: value,
older_value_opt: option::some(newer_value),
});
older_value_opt
}
}
Function exists_with_type
public(friend) fun exists_with_type<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &config::Config<WriteCap>, name: Name): bool
Implementation
public(package) fun exists_with_type<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &Config<WriteCap>,
name: Name,
): bool {
field::exists_with_type<_, Setting<Value>>(&config.id, name)
}
Function exists_with_type_for_current_epoch
public(friend) fun exists_with_type_for_current_epoch<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &config::Config<WriteCap>, name: Name, ctx: &tx_context::TxContext): bool
Implementation
public(package) fun exists_with_type_for_current_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
}
}
Function borrow_for_current_epoch_mut
public(friend) fun borrow_for_current_epoch_mut<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &mut config::Config<WriteCap>, _cap: &mut WriteCap, name: Name, ctx: &mut tx_context::TxContext): &mut Value
Implementation
public(package) fun borrow_for_current_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);
&mut data.newer_value
}
Function borrow_most_recent
public(friend) fun borrow_most_recent<WriteCap, Name: copy, drop, store, Value: copy, drop, store>(config: &config::Config<WriteCap>, name: Name): &Value
Implementation
public(package) fun borrow_most_recent<
WriteCap,
Name: copy + drop + store,
Value: copy + drop + store,
>(
config: &Config<WriteCap>,
name: Name,
): &Value {
let sobj: &Setting<Value> = field::borrow(&config.id, name);
&sobj.data.borrow().newer_value
}
Function read_setting
public(friend) fun read_setting<Name: copy, drop, store, Value: copy, drop, store>(config: object::ID, name: Name, ctx: &tx_context::TxContext): option::Option<Value>
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): option::Option<Value>
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>;