@@ -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+
2831const CLOCK_ADDRESS : address = @0x6 ;
2932
3033const EInvalidArguments : u64 = 1 ;
3134const 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 ;
3538const 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
446453public 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
463471public 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
494493public 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
613615public 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-
642617public 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);
0 commit comments