0x0::registry
Registry holds all created pools.
REGISTRY
DeepbookAdminCap
Registry
RegistryInner
PoolKey
init
set_treasury_address
enable_version
disable_version
load_inner_mut
register_pool
unregister_pool
load_inner
get_pool_id
treasury_address
allowed_versions
use 0x0::constants;
use 0x1::type_name;
use 0x2::bag;
use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::vec_set;
use 0x2::versioned;
REGISTRY
struct REGISTRY has drop
dummy_field: bool
DeepbookAdminCap
DeepbookAdminCap is used to call admin functions.
struct DeepbookAdminCap has store, key
id: object::UID
Registry
struct Registry has key
id: object::UID
inner: versioned::Versioned
RegistryInner
struct RegistryInner has store
allowed_versions: vec_set::VecSet<u64>
pools: bag::Bag
treasury_address: address
PoolKey
struct PoolKey has copy, drop, store
base: type_name::TypeName
quote: type_name::TypeName
const ECannotDisableCurrentVersion: u64 = 6;
const EPackageVersionNotEnabled: u64 = 3;
const EPoolAlreadyExists: u64 = 1;
const EPoolDoesNotExist: u64 = 2;
const EVersionAlreadyEnabled: u64 = 5;
const EVersionNotEnabled: u64 = 4;
init
fun init(_: registry::REGISTRY, ctx: &mut tx_context::TxContext)
fun init(_: REGISTRY, ctx: &mut TxContext) {
let registry_inner = RegistryInner {
allowed_versions: vec_set::singleton(constants::current_version()),
pools: bag::new(ctx),
treasury_address: ctx.sender(),
};
let registry = Registry {
id: object::new(ctx),
inner: versioned::create(
constants::current_version(),
registry_inner,
ctx,
),
};
transfer::share_object(registry);
let admin = DeepbookAdminCap { id: object::new(ctx) };
transfer::public_transfer(admin, ctx.sender());
}
set_treasury_address
Sets the treasury address where the pool creation fees are sent By default, the treasury address is the publisher of the deepbook package
public fun set_treasury_address(self: &mut registry::Registry, treasury_address: address, _cap: ®istry::DeepbookAdminCap)
public fun set_treasury_address(
self: &mut Registry,
treasury_address: address,
_cap: &DeepbookAdminCap,
) {
let self = self.load_inner_mut();
self.treasury_address = treasury_address;
}
enable_version
Enables a package version Only Admin can enable a package version This function does not have version restrictions
public fun enable_version(self: &mut registry::Registry, version: u64, _cap: ®istry::DeepbookAdminCap)
public fun enable_version(
self: &mut Registry,
version: u64,
_cap: &DeepbookAdminCap,
) {
let self: &mut RegistryInner = self.inner.load_value_mut();
assert!(!self.allowed_versions.contains(&version), EVersionAlreadyEnabled);
self.allowed_versions.insert(version);
}
disable_version
Disables a package version Only Admin can disable a package version This function does not have version restrictions
public fun disable_version(self: &mut registry::Registry, version: u64, _cap: ®istry::DeepbookAdminCap)
public fun disable_version(
self: &mut Registry,
version: u64,
_cap: &DeepbookAdminCap,
) {
let self: &mut RegistryInner = self.inner.load_value_mut();
assert!(
version != constants::current_version(),
ECannotDisableCurrentVersion,
);
assert!(self.allowed_versions.contains(&version), EVersionNotEnabled);
self.allowed_versions.remove(&version);
}
load_inner_mut
public(friend) fun load_inner_mut(self: &mut registry::Registry): &mut registry::RegistryInner
public(package) fun load_inner_mut(self: &mut Registry): &mut RegistryInner {
let inner: &mut RegistryInner = self.inner.load_value_mut();
let package_version = constants::current_version();
assert!(
inner.allowed_versions.contains(&package_version),
EPackageVersionNotEnabled,
);
inner
}
register_pool
Register a new pool in the registry. Asserts if (Base, Quote) pool already exists or (Quote, Base) pool already exists.
public(friend) fun register_pool<BaseAsset, QuoteAsset>(self: &mut registry::Registry, pool_id: object::ID)
public(package) fun register_pool<BaseAsset, QuoteAsset>(
self: &mut Registry,
pool_id: ID,
) {
let self = self.load_inner_mut();
let key = PoolKey {
base: type_name::get<QuoteAsset>(),
quote: type_name::get<BaseAsset>(),
};
assert!(!self.pools.contains(key), EPoolAlreadyExists);
let key = PoolKey {
base: type_name::get<BaseAsset>(),
quote: type_name::get<QuoteAsset>(),
};
assert!(!self.pools.contains(key), EPoolAlreadyExists);
self.pools.add(key, pool_id);
}
unregister_pool
Only admin can call this function
public(friend) fun unregister_pool<BaseAsset, QuoteAsset>(self: &mut registry::Registry)
public(package) fun unregister_pool<BaseAsset, QuoteAsset>(
self: &mut Registry,
) {
let self = self.load_inner_mut();
let key = PoolKey {
base: type_name::get<BaseAsset>(),
quote: type_name::get<QuoteAsset>(),
};
assert!(self.pools.contains(key), EPoolDoesNotExist);
self.pools.remove<PoolKey, ID>(key);
}
load_inner
public(friend) fun load_inner(self: ®istry::Registry): ®istry::RegistryInner
public(package) fun load_inner(self: &Registry): &RegistryInner {
let inner: &RegistryInner = self.inner.load_value();
let package_version = constants::current_version();
assert!(
inner.allowed_versions.contains(&package_version),
EPackageVersionNotEnabled,
);
inner
}
get_pool_id
Get the pool id for the given base and quote assets.
public(friend) fun get_pool_id<BaseAsset, QuoteAsset>(self: ®istry::Registry): object::ID
public(package) fun get_pool_id<BaseAsset, QuoteAsset>(self: &Registry): ID {
let self = self.load_inner();
let key = PoolKey {
base: type_name::get<BaseAsset>(),
quote: type_name::get<QuoteAsset>(),
};
assert!(self.pools.contains(key), EPoolDoesNotExist);
*self.pools.borrow<PoolKey, ID>(key)
}
treasury_address
Get the treasury address
public(friend) fun treasury_address(self: ®istry::Registry): address
public(package) fun treasury_address(self: &Registry): address {
let self = self.load_inner();
self.treasury_address
}
allowed_versions
public(friend) fun allowed_versions(self: ®istry::Registry): vec_set::VecSet<u64>
public(package) fun allowed_versions(self: &Registry): VecSet<u64> {
let self = self.load_inner();
self.allowed_versions
}