diff --git a/move/interchain_token_service/sources/events.move b/move/interchain_token_service/sources/events.move index 7d2ce0d3..89569249 100644 --- a/move/interchain_token_service/sources/events.move +++ b/move/interchain_token_service/sources/events.move @@ -59,7 +59,7 @@ public struct TrustedAddressRemoved has copy, drop { public struct FlowLimitSet<phantom T> has copy, drop { token_id: TokenId, - flow_limit: u64, + flow_limit: Option<u64>, } // ----------------- @@ -159,7 +159,7 @@ public(package) fun trusted_address_removed(chain_name: String) { }); } -public(package) fun flow_limit_set<T>(token_id: TokenId, flow_limit: u64) { +public(package) fun flow_limit_set<T>(token_id: TokenId, flow_limit: Option<u64>) { event::emit(FlowLimitSet<T> { token_id, flow_limit, diff --git a/move/interchain_token_service/sources/interchain_token_service.move b/move/interchain_token_service/sources/interchain_token_service.move index a2b9b627..540d656b 100644 --- a/move/interchain_token_service/sources/interchain_token_service.move +++ b/move/interchain_token_service/sources/interchain_token_service.move @@ -278,7 +278,7 @@ public fun set_flow_limit_as_token_operator<T>( self: &mut InterchainTokenService, channel: &Channel, token_id: TokenId, - limit: u64, + limit: Option<u64>, ) { let value = self.value_mut!(b"set_flow_limit_as_token_operator"); @@ -295,13 +295,13 @@ public fun set_flow_limit<T>( self: &mut InterchainTokenService, _: &OperatorCap, token_ids: TokenId, - limits: u64, + limit: Option<u64>, ) { let value = self.value_mut!(b"set_flow_limit"); value.set_flow_limit<T>( token_ids, - limits, + limit, ); } @@ -931,7 +931,7 @@ fun test_set_flow_limit_as_token_operator() { let mut its = create_for_testing(ctx); let symbol = b"COIN"; let decimals = 9; - let limit = 1234; + let limit = option::some(1234); let (treasury_cap, coin_metadata) = interchain_token_service::coin::create_treasury_and_metadata( symbol, @@ -959,7 +959,7 @@ fun test_set_flow_limit() { let mut its = create_for_testing(ctx); let symbol = b"COIN"; let decimals = 9; - let limit = 1234; + let limit = option::some(1234); let (treasury_cap, coin_metadata) = interchain_token_service::coin::create_treasury_and_metadata( symbol, diff --git a/move/interchain_token_service/sources/types/coin_management.move b/move/interchain_token_service/sources/types/coin_management.move index 5e8234e1..aeaa7016 100644 --- a/move/interchain_token_service/sources/types/coin_management.move +++ b/move/interchain_token_service/sources/types/coin_management.move @@ -126,7 +126,7 @@ public(package) fun burn<T>(self: &mut CoinManagement<T>, balance: Balance<T>) { public(package) fun set_flow_limit<T>( self: &mut CoinManagement<T>, channel: &Channel, - flow_limit: u64, + flow_limit: Option<u64>, ) { assert!(self.operator.contains(&channel.to_address()), ENotOperator); self.set_flow_limit_internal(flow_limit); @@ -135,7 +135,7 @@ public(package) fun set_flow_limit<T>( /// Adds a rate limit to the `CoinManagement`. public(package) fun set_flow_limit_internal<T>( self: &mut CoinManagement<T>, - flow_limit: u64, + flow_limit: Option<u64>, ) { self.flow_limit.set_flow_limit(flow_limit); } @@ -257,7 +257,7 @@ fun test_set_flow_limit() { let channel = axelar_gateway::channel::new(ctx); management.add_operator(channel.to_address()); - management.set_flow_limit(&channel, 1); + management.set_flow_limit(&channel, option::some(1)); sui::test_utils::destroy(management); sui::test_utils::destroy(channel); @@ -273,7 +273,7 @@ fun test_set_flow_limit_not_operator() { let operator = @0x1; management.add_operator(operator); - management.set_flow_limit(&channel, 1); + management.set_flow_limit(&channel, option::some(1)); sui::test_utils::destroy(management); sui::test_utils::destroy(channel); diff --git a/move/interchain_token_service/sources/types/flow_limit.move b/move/interchain_token_service/sources/types/flow_limit.move index e8cffb1f..a610589d 100644 --- a/move/interchain_token_service/sources/types/flow_limit.move +++ b/move/interchain_token_service/sources/types/flow_limit.move @@ -8,7 +8,7 @@ const EPOCH_TIME: u64 = 6 * 60 * 60 * 1000; const EFlowLimitExceeded: vector<u8> = b"flow limit exceeded"; public struct FlowLimit has store, copy, drop { - flow_limit: u64, + flow_limit: Option<u64>, flow_in: u128, flow_out: u128, current_epoch: u64, @@ -16,7 +16,7 @@ public struct FlowLimit has store, copy, drop { public(package) fun new(): FlowLimit { FlowLimit { - flow_limit: 0, + flow_limit: option::none(), flow_in: 0, flow_out: 0, current_epoch: 0, @@ -37,11 +37,14 @@ public(package) fun add_flow_in( amount: u64, clock: &Clock, ) { - if (self.flow_limit == 0) return; + if (self.flow_limit.is_none()) { + return + }; + let flow_limit = *self.flow_limit.borrow() as u128; update_epoch(self, clock); assert!( - self.flow_in + (amount as u128) < (self.flow_limit as u128) + self.flow_out, + self.flow_in + (amount as u128) < flow_limit + self.flow_out, EFlowLimitExceeded, ); self.flow_in = self.flow_in + (amount as u128); @@ -51,18 +54,21 @@ public(package) fun add_flow_out( self: &mut FlowLimit, amount: u64, clock: &Clock, -) { - if (self.flow_limit == 0) return; +) { + if (self.flow_limit.is_none()) { + return + }; + let flow_limit = *self.flow_limit.borrow() as u128; update_epoch(self, clock); assert!( - self.flow_out + (amount as u128) < (self.flow_limit as u128) + self.flow_in, + self.flow_out + (amount as u128) < flow_limit + self.flow_in, EFlowLimitExceeded, ); self.flow_out = self.flow_out + (amount as u128); } -public(package) fun set_flow_limit(self: &mut FlowLimit, flow_limit: u64) { +public(package) fun set_flow_limit(self: &mut FlowLimit, flow_limit: Option<u64>) { self.flow_limit = flow_limit; } @@ -85,7 +91,7 @@ fun test_add_flow_in() { let ctx = &mut tx_context::dummy(); let mut flow_limit = new(); let clock = sui::clock::create_for_testing(ctx); - flow_limit.set_flow_limit(2); + flow_limit.set_flow_limit(option::some(2)); flow_limit.add_flow_in(1, &clock); clock.destroy_for_testing(); } @@ -95,7 +101,7 @@ fun test_add_flow_out() { let ctx = &mut tx_context::dummy(); let mut flow_limit = new(); let clock = sui::clock::create_for_testing(ctx); - flow_limit.set_flow_limit(2); + flow_limit.set_flow_limit(option::some(2)); flow_limit.add_flow_out(1, &clock); clock.destroy_for_testing(); } @@ -124,7 +130,7 @@ fun test_add_flow_in_limit_exceeded() { let ctx = &mut tx_context::dummy(); let mut flow_limit = new(); let clock = sui::clock::create_for_testing(ctx); - flow_limit.set_flow_limit(1); + flow_limit.set_flow_limit(option::some(1)); flow_limit.add_flow_in(1, &clock); clock.destroy_for_testing(); } @@ -135,7 +141,7 @@ fun test_add_flow_out_limit_exceeded() { let ctx = &mut tx_context::dummy(); let mut flow_limit = new(); let clock = sui::clock::create_for_testing(ctx); - flow_limit.set_flow_limit(1); + flow_limit.set_flow_limit(option::some(1)); flow_limit.add_flow_out(1, &clock); clock.destroy_for_testing(); } diff --git a/move/interchain_token_service/sources/versioned/interchain_token_service_v0.move b/move/interchain_token_service/sources/versioned/interchain_token_service_v0.move index 4240d61b..64a23349 100644 --- a/move/interchain_token_service/sources/versioned/interchain_token_service_v0.move +++ b/move/interchain_token_service/sources/versioned/interchain_token_service_v0.move @@ -464,7 +464,7 @@ public(package) fun set_flow_limit_as_token_operator<T>( self: &mut InterchainTokenService_v0, channel: &Channel, token_id: TokenId, - limit: u64, + limit: Option<u64>, ) { self.coin_management_mut<T>(token_id).set_flow_limit(channel, limit); events::flow_limit_set<T>(token_id, limit); @@ -473,7 +473,7 @@ public(package) fun set_flow_limit_as_token_operator<T>( public(package) fun set_flow_limit<T>( self: &mut InterchainTokenService_v0, token_id: TokenId, - limit: u64, + limit: Option<u64>, ) { self.coin_management_mut<T>(token_id).set_flow_limit_internal(limit); events::flow_limit_set<T>(token_id, limit); diff --git a/test/testdata/interface_interchain_token_service_flow_limit.json b/test/testdata/interface_interchain_token_service_flow_limit.json index 3a48725b..12aa28a0 100644 --- a/test/testdata/interface_interchain_token_service_flow_limit.json +++ b/test/testdata/interface_interchain_token_service_flow_limit.json @@ -10,7 +10,7 @@ "fields": [ { "name": "flow_limit", - "type": "u64" + "type": "Option<u64>" }, { "name": "flow_in",