Skip to content

Commit

Permalink
feat: managed assets are divided into structs and objects to improve …
Browse files Browse the repository at this point in the history
…discoverability
  • Loading branch information
thounyy committed Nov 22, 2024
1 parent d0af6d2 commit fa7863a
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 358 deletions.
2 changes: 1 addition & 1 deletion packages/actions/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[move]
version = 3
manifest_digest = "09983F337E1A9CACD0ABE0FAE9A215C4EF33258AF9D6B2C3D2C5C461DB71CFD3"
manifest_digest = "CEACB32873B162152E20CB3F4CF5715A1753405B31E348BE3142DF816E25DA13"
deps_digest = "397E6A9F7A624706DBDFEE056CE88391A15876868FD18A88504DA74EB458D697"
dependencies = [
{ id = "AccountConfig", name = "AccountConfig" },
Expand Down
26 changes: 11 additions & 15 deletions packages/actions/sources/access_control.move
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ const EWrongAccount: vector<u8> = b"This Cap has not been borrowed from this acc

// === Structs ===

/// Dynamic Field key for the AccessLock
public struct AccessKey<phantom Cap> has copy, drop, store {}
/// Dynamic Field wrapper storing a cap
public struct AccessLock<Cap: store> has store {
cap: Cap,
}
/// Dynamic Object Field key for the Cap
public struct CapKey<phantom Cap> has copy, drop, store {}

/// [COMMAND] witness defining the lock cap command, and associated role
public struct LockCommand() has drop;
Expand All @@ -64,20 +60,20 @@ public struct Borrow<phantom Cap> {
// === [COMMAND] Public functions ===

/// Only a member can lock a Cap, the Cap must have at least store ability
public fun lock_cap<Config, Outcome, Cap: store>(
public fun lock_cap<Config, Outcome, Cap: key + store>(
auth: Auth,
account: &mut Account<Config, Outcome>,
cap: Cap,
) {
auth.verify_with_role<LockCommand>(account.addr(), b"".to_string());
assert!(!has_lock<Config, Outcome, Cap>(account), EAlreadyLocked);
account.add_managed_asset(AccessKey<Cap> {}, AccessLock { cap }, version::current());
account.add_managed_object(CapKey<Cap> {}, cap, version::current());
}

public fun has_lock<Config, Outcome, Cap>(
account: &Account<Config, Outcome>
): bool {
account.has_managed_asset(AccessKey<Cap> {})
account.has_managed_object(CapKey<Cap> {})
}

// === [PROPOSAL] Public functions ===
Expand Down Expand Up @@ -116,15 +112,15 @@ public fun propose_access<Config, Outcome, Cap>(
// step 3: execute the proposal and return the action (AccountConfig::module::execute_proposal)

// step 4: mint the coins and send them to the account
public fun execute_access<Config, Outcome, Cap: store>(
public fun execute_access<Config, Outcome, Cap: key + store>(
executable: &mut Executable,
account: &mut Account<Config, Outcome>,
): (Borrow<Cap>, Cap) {
do_access(executable, account, version::current(), AccessProposal())
}

// step 5: return the cap to destroy Borrow, the action and executable
public fun complete_access<Config, Outcome, Cap: store>(
public fun complete_access<Config, Outcome, Cap: key + store>(
executable: Executable,
account: &mut Account<Config, Outcome>,
borrow: Borrow<Cap>,
Expand All @@ -143,7 +139,7 @@ public fun new_access<Outcome, Cap, W: drop>(
proposal.add_action(AccessAction<Cap> {}, witness);
}

public fun do_access<Config, Outcome, Cap: store, W: copy + drop>(
public fun do_access<Config, Outcome, Cap: key + store, W: copy + drop>(
executable: &mut Executable,
account: &mut Account<Config, Outcome>,
version: TypeName,
Expand All @@ -152,12 +148,12 @@ public fun do_access<Config, Outcome, Cap: store, W: copy + drop>(
assert!(has_lock<Config, Outcome, Cap>(account), ENoLock);
// check to be sure this cap type has been approved
let AccessAction<Cap> {} = executable.action(account.addr(), version, witness);
let AccessLock<Cap> { cap } = account.remove_managed_asset(AccessKey<Cap> {}, version);
let cap = account.remove_managed_object(CapKey<Cap> {}, version);

(Borrow<Cap> { account_addr: account.addr() }, cap)
}

public fun return_cap<Config, Outcome, Cap: store>(
public fun return_cap<Config, Outcome, Cap: key + store>(
account: &mut Account<Config, Outcome>,
borrow: Borrow<Cap>,
cap: Cap,
Expand All @@ -166,7 +162,7 @@ public fun return_cap<Config, Outcome, Cap: store>(
let Borrow<Cap> { account_addr } = borrow;
assert!(account_addr == account.addr(), EWrongAccount);

account.add_managed_asset(AccessKey<Cap> {}, AccessLock { cap }, version);
account.add_managed_object(CapKey<Cap> {}, cap, version);
}

public fun delete_access_action<Outcome, Cap>(expired: &mut Expired<Outcome>) {
Expand Down
4 changes: 2 additions & 2 deletions packages/actions/sources/config.move
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ public fun new_config_deps<Config, Outcome, W: drop>(

if (extensions.is_extension(name, package, version)) {
deps.add(extensions, name, package, version);
} else if (upgrade_policies::has_lock(account, package)) {
let cap = upgrade_policies::borrow_lock(account, package).upgrade_cap();
} else if (upgrade_policies::has_cap(account, package)) {
let cap = upgrade_policies::borrow_cap(account, package);
deps.add_with_upgrade_cap(cap, name, package, version);
} else abort ENoExtensionOrUpgradeCap;
});
Expand Down
Loading

0 comments on commit fa7863a

Please sign in to comment.