Skip to content

Commit a6cce8f

Browse files
authored
add a rebalancer in LR token pool (#151)
* add a rebalancer in LR token pool * update ops * fix * fix build * fix test * update * update
1 parent 1ae798e commit a6cce8f

File tree

9 files changed

+222
-380
lines changed

9 files changed

+222
-380
lines changed

bindings/generated/ccip/ccip_token_pools/lock_release_token_pool/lock_release_token_pool.go

Lines changed: 87 additions & 191 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/ccip/ccip_token_pools/lock_release_token_pool/sources/lock_release_token_pool.move

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ public struct LockReleaseTokenPoolState<phantom T> has key {
2020
id: UID,
2121
token_pool_state: TokenPoolState,
2222
reserve: Coin<T>,
23-
/// the rebalancer is the address that can manage liquidity of the token pool
24-
rebalancer: address,
23+
rebalancer_cap_id: ID,
2524
ownable_state: OwnableState,
2625
}
2726

27+
public struct RebalancerCap has key, store {
28+
id: UID,
29+
}
30+
2831
const CLOCK_ADDRESS: address = @0x6;
2932

3033
const EInvalidArguments: u64 = 1;
3134
const ETokenPoolBalanceTooLow: u64 = 2;
32-
const EUnauthorized: u64 = 3;
33-
const EInvalidOwnerCap: u64 = 4;
34-
const EInvalidFunction: u64 = 5;
35+
const EInvalidOwnerCap: u64 = 3;
36+
const EInvalidFunction: u64 = 4;
37+
const EInvalidRebalancerCap: u64 = 5;
3538
const EPoolStillRegistered: u64 = 6;
3639

3740
// ================================================================
@@ -76,7 +79,11 @@ fun initialize_internal<T>(
7679
let coin_metadata_address: address = object::id_to_address(&object::id(coin_metadata));
7780
let (ownable_state, owner_cap) = ownable::new(ctx);
7881

79-
let mut lock_release_token_pool = LockReleaseTokenPoolState<T> {
82+
let rebalancer_cap = RebalancerCap {
83+
id: object::new(ctx),
84+
};
85+
86+
let lock_release_token_pool = LockReleaseTokenPoolState<T> {
8087
id: object::new(ctx),
8188
token_pool_state: token_pool::initialize(
8289
coin_metadata_address,
@@ -85,14 +92,14 @@ fun initialize_internal<T>(
8592
ctx,
8693
),
8794
reserve: coin::zero<T>(ctx),
88-
rebalancer: @0x0,
95+
rebalancer_cap_id: object::id(&rebalancer_cap),
8996
ownable_state,
9097
};
91-
set_rebalancer_internal(&mut lock_release_token_pool, rebalancer);
9298
let token_pool_state_address = object::uid_to_address(&lock_release_token_pool.id);
9399

94100
transfer::share_object(lock_release_token_pool);
95101
transfer::public_transfer(owner_cap, ctx.sender());
102+
transfer::public_transfer(rebalancer_cap, rebalancer);
96103

97104
token_pool_state_address
98105
}
@@ -445,58 +452,53 @@ public fun set_chain_rate_limiter_config<T>(
445452

446453
public fun provide_liquidity<T>(
447454
state: &mut LockReleaseTokenPoolState<T>,
455+
rebalancer_cap: &RebalancerCap,
448456
c: Coin<T>,
449-
ctx: &mut TxContext,
457+
_: &mut TxContext,
450458
) {
451-
assert!(ctx.sender() == state.rebalancer, EUnauthorized);
459+
assert!(object::id(rebalancer_cap) == state.rebalancer_cap_id, EInvalidRebalancerCap);
452460
let amount = c.value();
453461

454462
coin::join(&mut state.reserve, c);
455463

456464
token_pool::emit_liquidity_added(
457465
&state.token_pool_state,
458-
state.rebalancer,
466+
object::id_to_address(&state.rebalancer_cap_id),
459467
amount,
460468
);
461469
}
462470

463471
public fun withdraw_liquidity<T>(
464472
state: &mut LockReleaseTokenPoolState<T>,
473+
rebalancer_cap: &RebalancerCap,
465474
amount: u64,
466475
ctx: &mut TxContext,
467476
): Coin<T> {
468-
assert!(ctx.sender() == state.rebalancer, EUnauthorized);
477+
assert!(object::id(rebalancer_cap) == state.rebalancer_cap_id, EInvalidRebalancerCap);
469478

470479
assert!(state.reserve.value() >= amount, ETokenPoolBalanceTooLow);
471480

472-
token_pool::emit_liquidity_removed(&state.token_pool_state, state.rebalancer, amount);
481+
token_pool::emit_liquidity_removed(
482+
&state.token_pool_state,
483+
object::id_to_address(&state.rebalancer_cap_id),
484+
amount,
485+
);
473486
coin::split(&mut state.reserve, amount, ctx)
474487
}
475488

476-
public fun set_rebalancer<T>(
477-
owner_cap: &OwnerCap,
478-
state: &mut LockReleaseTokenPoolState<T>,
479-
rebalancer: address,
480-
) {
481-
assert!(object::id(owner_cap) == ownable::owner_cap_id(&state.ownable_state), EInvalidOwnerCap);
482-
set_rebalancer_internal(state, rebalancer);
483-
}
484-
485-
fun set_rebalancer_internal<T>(state: &mut LockReleaseTokenPoolState<T>, rebalancer: address) {
486-
token_pool::emit_rebalancer_set(
487-
&state.token_pool_state,
488-
state.rebalancer,
489-
rebalancer,
490-
);
491-
state.rebalancer = rebalancer;
489+
public fun get_balance<T>(state: &LockReleaseTokenPoolState<T>): u64 {
490+
state.reserve.value()
492491
}
493492

494493
public fun get_rebalancer<T>(state: &LockReleaseTokenPoolState<T>): address {
495-
state.rebalancer
494+
object::id_to_address(&state.rebalancer_cap_id)
496495
}
497496

498-
public fun get_balance<T>(state: &LockReleaseTokenPoolState<T>): u64 {
499-
state.reserve.value()
497+
#[test_only]
498+
public fun create_fake_rebalancer_cap(ctx: &mut TxContext): RebalancerCap {
499+
RebalancerCap {
500+
id: object::new(ctx),
501+
}
500502
}
501503

502504
// ================================================================
@@ -612,33 +614,6 @@ public fun mcms_register_upgrade_cap(
612614

613615
public struct McmsCallback<phantom T> has drop {}
614616

615-
public fun mcms_set_rebalancer<T>(
616-
state: &mut LockReleaseTokenPoolState<T>,
617-
registry: &mut Registry,
618-
params: ExecutingCallbackParams,
619-
) {
620-
let (owner_cap, function, data) = mcms_registry::get_callback_params_with_caps<
621-
McmsCallback<T>,
622-
OwnerCap,
623-
>(
624-
registry,
625-
McmsCallback<T> {},
626-
params,
627-
);
628-
assert!(function == string::utf8(b"set_rebalancer"), EInvalidFunction);
629-
630-
let mut stream = bcs_stream::new(data);
631-
bcs_stream::validate_obj_addrs(
632-
vector[object::id_address(owner_cap), object::id_address(state)],
633-
&mut stream,
634-
);
635-
636-
let rebalancer = bcs_stream::deserialize_address(&mut stream);
637-
bcs_stream::assert_is_consumed(&stream);
638-
639-
set_rebalancer(owner_cap, state, rebalancer);
640-
}
641-
642617
public fun mcms_set_allowlist_enabled<T>(
643618
state: &mut LockReleaseTokenPoolState<T>,
644619
registry: &mut Registry,
@@ -1082,7 +1057,7 @@ public fun destroy_token_pool<T>(
10821057
id: state_id,
10831058
token_pool_state,
10841059
reserve,
1085-
rebalancer: _,
1060+
rebalancer_cap_id: _,
10861061
ownable_state,
10871062
} = state;
10881063
token_pool::destroy_token_pool(token_pool_state);

contracts/ccip/ccip_token_pools/lock_release_token_pool/tests/lock_release_token_pool_ownable_test.move

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,7 @@ public fun test_accept_ownership_already_accepted() {
256256
public fun test_ownable_functions_with_owner_cap_validation() {
257257
let (mut env, owner_cap) = setup();
258258

259-
// Test that owner cap validation works in other functions
260-
// These should succeed because we have the correct owner cap
261-
lock_release_token_pool::set_rebalancer(&owner_cap, &mut env.state, @0x999);
262-
assert!(lock_release_token_pool::get_rebalancer(&env.state) == @0x999);
259+
assert!(lock_release_token_pool::get_rebalancer(&env.state) != @0x0);
263260

264261
lock_release_token_pool::set_allowlist_enabled(&mut env.state, &owner_cap, true);
265262
assert!(lock_release_token_pool::get_allowlist_enabled(&env.state) == true);

0 commit comments

Comments
 (0)