0xaa::discovery
This module implements a discovery mechanic for the Relayer to be able to call some (!) transactions automatically.
Warning: this solution does allow for any transaction to be executed and should be treated as a reference and a temporary solution until there’s a proper discovery / execution mechanism in place.
RelayerDiscovery
init
allow_function
disallow_function
register_transaction
remove_transaction
get_transaction
version_control
use 0x1::ascii;
use 0x1::vector;
use 0x2::object;
use 0x2::transfer;
use 0x2::tx_context;
use 0x2::versioned;
use 0xa1::channel;
use 0xaa::owner_cap;
use 0xaa::relayer_discovery_v0;
use 0xaa::transaction;
use 0xb0::version_control;
RelayerDiscovery
Structs ——-
struct RelayerDiscovery has key
id: object::UID
inner: versioned::Versioned
Version ——- This is the version of the package that should change every package upgrade.
const VERSION: u64 = 0;
This is the version of the data that should change when we need to migrate
Versioned
type (e.g. from RelayerDiscovery_v0
to RelayerDiscovery_v1
)
const DATA_VERSION: u64 = 0;
init
fun init(ctx: &mut tx_context::TxContext)
fun init(ctx: &mut TxContext) {
let inner = versioned::create(
DATA_VERSION,
relayer_discovery_v0::new(
version_control(),
ctx,
),
ctx,
);
transfer::share_object(RelayerDiscovery {
id: object::new(ctx),
inner,
});
transfer::public_transfer(owner_cap::create(ctx), ctx.sender());
}
allow_function
entry fun allow_function(self: &mut discovery::RelayerDiscovery, _: &owner_cap::OwnerCap, version: u64, function_name: ascii::String)
entry fun allow_function(
self: &mut RelayerDiscovery,
_: &OwnerCap,
version: u64,
function_name: String,
) {
self.value_mut!(b"allow_function").allow_function(version, function_name);
}
disallow_function
entry fun disallow_function(self: &mut discovery::RelayerDiscovery, _: &owner_cap::OwnerCap, version: u64, function_name: ascii::String)
entry fun disallow_function(
self: &mut RelayerDiscovery,
_: &OwnerCap,
version: u64,
function_name: String,
) {
self
.value_mut!(b"disallow_function")
.disallow_function(version, function_name);
}
register_transaction
During the creation of the object, the UID should be passed here to receive the Channel and emit an event which will be handled by the Relayer.
Example:
let id = object::new(ctx);
let channel = discovery::create_configuration(
relayer_discovery, &id, contents, ctx
);
let wrapper = ExampleWrapper { id, channel };
transfer::share_object(wrapper);
Note: Wrapper must be a shared object so that Relayer can access it.
public fun register_transaction(self: &mut discovery::RelayerDiscovery, channel: &channel::Channel, tx: transaction::Transaction)
public fun register_transaction(
self: &mut RelayerDiscovery,
channel: &Channel,
tx: Transaction,
) {
let value = self.value_mut!(b"register_transaction");
let channel_id = channel.id();
value.set_transaction(channel_id, tx);
}
remove_transaction
public fun remove_transaction(self: &mut discovery::RelayerDiscovery, channel: &channel::Channel)
public fun remove_transaction(self: &mut RelayerDiscovery, channel: &Channel) {
let value = self.value_mut!(b"remove_transaction");
let channel_id = channel.id();
value.remove_transaction(channel_id);
}
get_transaction
Get a transaction for a specific channel by the channel ID
.
public fun get_transaction(self: &discovery::RelayerDiscovery, channel_id: object::ID): transaction::Transaction
public fun get_transaction(
self: &RelayerDiscovery,
channel_id: ID,
): Transaction {
let value = self.value!(b"register_transaction");
value.get_transaction(channel_id)
}
version_control
Private Functions —————–
fun version_control(): version_control::VersionControl
fun version_control(): VersionControl {
version_control::new(vector[
// version 0
vector[
b"register_transaction",
b"remove_transaction",
b"get_transaction",
b"allow_function",
b"disallow_function",
].map!(|function_name| function_name.to_ascii_string()),
])
}