From cd52fed89a07485cf5bb37e8d28fa062842ed36f Mon Sep 17 00:00:00 2001 From: Foivos Date: Wed, 20 Nov 2024 14:49:51 +0200 Subject: [PATCH] feat(its)!: expose set_flow limit for the operator and add a corresponding event (#203) Co-authored-by: Milap Sheth --- .changeset/short-bananas-promise.md | 5 +++ move/its/sources/events.move | 15 +++++++ move/its/sources/its.move | 43 +++++++++++++++++++++ move/its/sources/types/coin_management.move | 24 ++++++------ move/its/sources/versioned/its_v0.move | 10 +++++ 5 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 .changeset/short-bananas-promise.md diff --git a/.changeset/short-bananas-promise.md b/.changeset/short-bananas-promise.md new file mode 100644 index 00000000..f5fdbffb --- /dev/null +++ b/.changeset/short-bananas-promise.md @@ -0,0 +1,5 @@ +--- +'@axelar-network/axelar-cgp-sui': patch +--- + +added access to set flow limit on its and a corresponding event diff --git a/move/its/sources/events.move b/move/its/sources/events.move index 8bbb7231..f0c54e7c 100644 --- a/move/its/sources/events.move +++ b/move/its/sources/events.move @@ -48,6 +48,11 @@ public struct UnregisteredCoinReceived has copy, drop { decimals: u8, } +public struct FlowLimitSet has copy, drop { + token_id: TokenId, + flow_limit: u64, +} + // ----------------- // Package Functions // ----------------- @@ -124,3 +129,13 @@ public(package) fun unregistered_coin_received( decimals, }); } + +public(package) fun flow_limit_set( + token_id: TokenId, + flow_limit: u64, +) { + event::emit(FlowLimitSet { + token_id, + flow_limit + }); +} diff --git a/move/its/sources/its.move b/move/its/sources/its.move index 5c15f18b..38fd761b 100644 --- a/move/its/sources/its.move +++ b/move/its/sources/its.move @@ -232,6 +232,21 @@ public fun burn_as_distributor( ); } +public fun set_flow_limit( + self: &mut ITS, + channel: &Channel, + token_id: TokenId, + limit: u64, +) { + let value = self.value_mut!(b"set_flow_limit"); + + value.set_flow_limit( + channel, + token_id, + limit, + ); +} + // --------------- // Owner Functions // --------------- @@ -308,6 +323,7 @@ fun version_control(): VersionControl { b"set_trusted_addresses", b"remove_trusted_addresses", b"register_transaction", + b"set_flow_limit", ].map!(|function_name| function_name.to_ascii_string()), ]) } @@ -830,6 +846,33 @@ fun test_set_trusted_address() { sui::test_utils::destroy(owner_cap); } +#[test] +fun test_set_flow_limit() { + let ctx = &mut tx_context::dummy(); + let mut its = create_for_testing(ctx); + let symbol = b"COIN"; + let decimals = 9; + let limit = 1234; + + let ( + treasury_cap, + coin_metadata, + ) = its::coin::create_treasury_and_metadata(symbol, decimals, ctx); + let coin_info = its::coin_info::from_metadata( + coin_metadata, + ); + let mut coin_management = its::coin_management::new_with_cap(treasury_cap); + + let channel = channel::new(ctx); + coin_management.add_operator(channel.to_address()); + + let token_id = register_coin(&mut its, coin_info, coin_management); + its.set_flow_limit(&channel, token_id, limit); + + sui::test_utils::destroy(its); + channel.destroy(); +} + #[test] fun test_init() { let mut ts = sui::test_scenario::begin(@0x0); diff --git a/move/its/sources/types/coin_management.move b/move/its/sources/types/coin_management.move index 54f7d0f6..5f6f94ad 100644 --- a/move/its/sources/types/coin_management.move +++ b/move/its/sources/types/coin_management.move @@ -73,19 +73,6 @@ public fun add_operator(self: &mut CoinManagement, operator: address) { self.operator.fill(operator); } -/// Adds a rate limit to the `CoinManagement`. -/// Note that this rate limit will be calculated for the remote decimals of the -/// token, not for the native decimals. -/// To be used by the designated operator of the contract. -public fun set_flow_limit( - self: &mut CoinManagement, - channel: &Channel, - flow_limit: u64, -) { - assert!(self.operator.contains(&channel.to_address()), ENotOperator); - self.flow_limit.set_flow_limit(flow_limit); -} - // === Protected Methods === /// Takes the given amount of Coins from user. Returns the amount that the ITS @@ -134,6 +121,17 @@ public(package) fun mint( public(package) fun burn(self: &mut CoinManagement, balance: Balance) { self.treasury_cap.borrow_mut().supply_mut().decrease_supply(balance); } + +/// Adds a rate limit to the `CoinManagement`. +public(package) fun set_flow_limit( + self: &mut CoinManagement, + channel: &Channel, + flow_limit: u64, +) { + assert!(self.operator.contains(&channel.to_address()), ENotOperator); + self.flow_limit.set_flow_limit(flow_limit); +} + // === Views === /// Checks if the given address is a `distributor`. diff --git a/move/its/sources/versioned/its_v0.move b/move/its/sources/versioned/its_v0.move index e516fd1d..91ebe9e4 100644 --- a/move/its/sources/versioned/its_v0.move +++ b/move/its/sources/versioned/its_v0.move @@ -482,6 +482,16 @@ public(package) fun burn_as_distributor( coin_management.burn(coin.into_balance()); } +public(package) fun set_flow_limit( + self: &mut ITS_v0, + channel: &Channel, + token_id: TokenId, + limit: u64, +) { + self.coin_management_mut(token_id).set_flow_limit(channel, limit); + events::flow_limit_set(token_id, limit); +} + // ----------------- // Private Functions // -----------------