Skip to content

Commit b5e28ae

Browse files
committed
refactor(interchain-token-service): use contractstorage
1 parent a1ac57a commit b5e28ae

File tree

6 files changed

+118
-141
lines changed

6 files changed

+118
-141
lines changed

contracts/stellar-interchain-token-service/src/contract.rs

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use stellar_axelar_gateway::executable::AxelarExecutableInterface;
99
use stellar_axelar_gateway::AxelarGatewayMessagingClient;
1010
use stellar_axelar_std::address::AddressExt;
1111
use stellar_axelar_std::events::Event;
12-
use stellar_axelar_std::ttl::{extend_instance_ttl, extend_persistent_ttl};
12+
use stellar_axelar_std::ttl::extend_instance_ttl;
1313
use stellar_axelar_std::types::Token;
1414
use stellar_axelar_std::{
1515
ensure, interfaces, when_not_paused, Operatable, Ownable, Pausable, Upgradable,
@@ -23,7 +23,7 @@ use crate::event::{
2323
};
2424
use crate::flow_limit::FlowDirection;
2525
use crate::interface::InterchainTokenServiceInterface;
26-
use crate::storage_types::{DataKey, TokenIdConfigValue};
26+
use crate::storage::{self, TokenIdConfigValue};
2727
use crate::token_metadata::TokenMetadataExt;
2828
use crate::types::{
2929
DeployInterchainToken, HubMessage, InterchainTransfer, Message, TokenManagerType,
@@ -53,94 +53,59 @@ impl InterchainTokenService {
5353
) {
5454
interfaces::set_owner(&env, &owner);
5555
interfaces::set_operator(&env, &operator);
56-
env.storage().instance().set(&DataKey::Gateway, &gateway);
57-
env.storage()
58-
.instance()
59-
.set(&DataKey::GasService, &gas_service);
60-
env.storage()
61-
.instance()
62-
.set(&DataKey::ItsHubAddress, &its_hub_address);
63-
env.storage()
64-
.instance()
65-
.set(&DataKey::ChainName, &chain_name);
66-
env.storage()
67-
.instance()
68-
.set(&DataKey::NativeTokenAddress, &native_token_address);
69-
env.storage().instance().set(
70-
&DataKey::InterchainTokenWasmHash,
71-
&interchain_token_wasm_hash,
72-
);
73-
env.storage()
74-
.instance()
75-
.set(&DataKey::TokenManagerWasmHash, &token_manager_wasm_hash);
56+
storage::set_gateway(&env, &gateway);
57+
storage::set_gas_service(&env, &gas_service);
58+
storage::set_its_hub_address(&env, &its_hub_address);
59+
storage::set_chain_name(&env, &chain_name);
60+
storage::set_native_token_address(&env, &native_token_address);
61+
storage::set_interchain_token_wasm_hash(&env, &interchain_token_wasm_hash);
62+
storage::set_token_manager_wasm_hash(&env, &token_manager_wasm_hash);
7663
}
7764
}
7865

7966
#[contractimpl]
8067
impl InterchainTokenServiceInterface for InterchainTokenService {
8168
fn gas_service(env: &Env) -> Address {
82-
env.storage()
83-
.instance()
84-
.get(&DataKey::GasService)
85-
.expect("gas service not found")
69+
storage::gas_service(env)
8670
}
8771

8872
fn chain_name(env: &Env) -> String {
89-
env.storage()
90-
.instance()
91-
.get(&DataKey::ChainName)
92-
.expect("chain name not found")
73+
storage::chain_name(env)
9374
}
9475

9576
fn its_hub_chain_name(env: &Env) -> String {
9677
String::from_str(env, ITS_HUB_CHAIN_NAME)
9778
}
9879

9980
fn its_hub_address(env: &Env) -> String {
100-
env.storage()
101-
.instance()
102-
.get(&DataKey::ItsHubAddress)
103-
.expect("its hub address not found")
81+
storage::its_hub_address(env)
10482
}
10583

10684
fn native_token_address(env: &Env) -> Address {
107-
env.storage()
108-
.instance()
109-
.get(&DataKey::NativeTokenAddress)
110-
.expect("native token address not found")
85+
storage::native_token_address(env)
11186
}
11287

11388
fn interchain_token_wasm_hash(env: &Env) -> BytesN<32> {
114-
env.storage()
115-
.instance()
116-
.get(&DataKey::InterchainTokenWasmHash)
117-
.expect("interchain token wasm hash not found")
89+
storage::interchain_token_wasm_hash(env)
11890
}
11991

12092
fn token_manager_wasm_hash(env: &Env) -> BytesN<32> {
121-
env.storage()
122-
.instance()
123-
.get(&DataKey::TokenManagerWasmHash)
124-
.expect("token manager wasm hash not found")
93+
storage::token_manager_wasm_hash(env)
12594
}
12695

12796
fn is_trusted_chain(env: &Env, chain: String) -> bool {
128-
env.storage()
129-
.persistent()
130-
.has(&DataKey::TrustedChain(chain))
97+
storage::is_trusted_chain(env, chain)
13198
}
13299

133100
fn set_trusted_chain(env: &Env, chain: String) -> Result<(), ContractError> {
134101
Self::owner(env).require_auth();
135102

136-
let key = DataKey::TrustedChain(chain.clone());
137-
138103
ensure!(
139-
!env.storage().persistent().has(&key),
104+
!storage::is_trusted_chain(env, chain.clone()),
140105
ContractError::TrustedChainAlreadySet
141106
);
142107

143-
env.storage().persistent().set(&key, &());
108+
storage::set_trusted_chain_status(env, chain.clone());
144109

145110
TrustedChainSetEvent { chain }.emit(env);
146111

@@ -150,14 +115,12 @@ impl InterchainTokenServiceInterface for InterchainTokenService {
150115
fn remove_trusted_chain(env: &Env, chain: String) -> Result<(), ContractError> {
151116
Self::owner(env).require_auth();
152117

153-
let key = DataKey::TrustedChain(chain.clone());
154-
155118
ensure!(
156-
env.storage().persistent().has(&key),
119+
storage::is_trusted_chain(env, chain.clone()),
157120
ContractError::TrustedChainNotSet
158121
);
159122

160-
env.storage().persistent().remove(&key);
123+
storage::remove_trusted_chain_status(env, chain.clone());
161124

162125
TrustedChainRemovedEvent { chain }.emit(env);
163126

@@ -361,10 +324,7 @@ impl AxelarExecutableInterface for InterchainTokenService {
361324
type Error = ContractError;
362325

363326
fn gateway(env: &Env) -> Address {
364-
env.storage()
365-
.instance()
366-
.get(&DataKey::Gateway)
367-
.expect("gateway not found")
327+
storage::gateway(env)
368328
}
369329

370330
fn execute(
@@ -395,7 +355,6 @@ impl InterchainTokenService {
395355
Self::is_trusted_chain(env, destination_chain.clone()),
396356
ContractError::UntrustedChain
397357
);
398-
extend_persistent_ttl(env, &DataKey::TrustedChain(destination_chain.clone()));
399358

400359
let gateway = AxelarGatewayMessagingClient::new(env, &Self::gateway(env));
401360
let gas_service = AxelarGasServiceClient::new(env, &Self::gas_service(env));
@@ -449,7 +408,6 @@ impl InterchainTokenService {
449408
Message::DeployInterchainToken(message) => Self::execute_deploy_message(env, message),
450409
}?;
451410

452-
extend_persistent_ttl(env, &DataKey::TrustedChain(source_chain));
453411
extend_instance_ttl(env);
454412

455413
Ok(())
@@ -479,18 +437,15 @@ impl InterchainTokenService {
479437
return Err(ContractError::InvalidMessageType);
480438
};
481439
ensure!(
482-
Self::is_trusted_chain(env, original_source_chain.clone()),
440+
storage::is_trusted_chain(env, original_source_chain.clone()),
483441
ContractError::UntrustedChain
484442
);
485-
extend_persistent_ttl(env, &DataKey::TrustedChain(original_source_chain.clone()));
486443

487444
Ok((original_source_chain, message))
488445
}
489446

490447
fn set_token_id_config(env: &Env, token_id: BytesN<32>, token_data: TokenIdConfigValue) {
491-
env.storage()
492-
.persistent()
493-
.set(&DataKey::TokenIdConfig(token_id), &token_data);
448+
storage::set_token_id_config(env, token_id, &token_data);
494449
}
495450

496451
/// Retrieves the configuration value for the specified token ID.
@@ -507,10 +462,7 @@ impl InterchainTokenService {
507462
env: &Env,
508463
token_id: BytesN<32>,
509464
) -> Result<TokenIdConfigValue, ContractError> {
510-
env.storage()
511-
.persistent()
512-
.get::<_, TokenIdConfigValue>(&DataKey::TokenIdConfig(token_id))
513-
.ok_or(ContractError::InvalidTokenId)
465+
storage::try_token_id_config(env, token_id).ok_or(ContractError::InvalidTokenId)
514466
}
515467

516468
/// Retrieves the configuration value for the specified token ID and extends its TTL.
@@ -528,7 +480,7 @@ impl InterchainTokenService {
528480
token_id: BytesN<32>,
529481
) -> Result<TokenIdConfigValue, ContractError> {
530482
let config = Self::token_id_config(env, token_id.clone())?;
531-
extend_persistent_ttl(env, &DataKey::TokenIdConfig(token_id));
483+
532484
Ok(config)
533485
}
534486

@@ -764,9 +716,7 @@ impl InterchainTokenService {
764716

765717
fn ensure_token_not_registered(env: &Env, token_id: BytesN<32>) -> Result<(), ContractError> {
766718
ensure!(
767-
!env.storage()
768-
.persistent()
769-
.has(&DataKey::TokenIdConfig(token_id)),
719+
storage::try_token_id_config(env, token_id).is_none(),
770720
ContractError::TokenAlreadyRegistered
771721
);
772722

contracts/stellar-interchain-token-service/src/flow_limit.rs

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use soroban_sdk::{BytesN, Env};
22
use stellar_axelar_std::ensure;
33
use stellar_axelar_std::events::Event;
4-
use stellar_axelar_std::ttl::extend_persistent_ttl;
54

65
use crate::error::ContractError;
76
use crate::event::FlowLimitSetEvent;
8-
use crate::storage_types::{DataKey, FlowKey};
7+
use crate::storage::{self, FlowKey};
98

109
const EPOCH_TIME: u64 = 6 * 60 * 60; // 6 hours in seconds = 21600
1110

@@ -37,12 +36,10 @@ impl FlowDirection {
3736
epoch: current_epoch(env),
3837
};
3938

40-
let key = match self {
41-
Self::In => DataKey::FlowIn(flow_key),
42-
Self::Out => DataKey::FlowOut(flow_key),
39+
match self {
40+
Self::In => storage::set_flow_in(env, flow_key, &new_flow),
41+
Self::Out => storage::set_flow_out(env, flow_key, &new_flow),
4342
};
44-
45-
env.storage().temporary().set(&key, &new_flow);
4643
}
4744

4845
/// Adds flow amount in the specified direction (in/out) for a token.
@@ -79,8 +76,6 @@ impl FlowDirection {
7976

8077
self.update_flow(env, token_id.clone(), new_flow);
8178

82-
extend_persistent_ttl(env, &DataKey::FlowLimit(token_id));
83-
8479
Ok(())
8580
}
8681
}
@@ -90,9 +85,7 @@ fn current_epoch(env: &Env) -> u64 {
9085
}
9186

9287
pub fn flow_limit(env: &Env, token_id: BytesN<32>) -> Option<i128> {
93-
env.storage()
94-
.persistent()
95-
.get(&DataKey::FlowLimit(token_id))
88+
storage::try_flow_limit(env, token_id)
9689
}
9790

9891
pub fn set_flow_limit(
@@ -103,13 +96,9 @@ pub fn set_flow_limit(
10396
if let Some(flow_limit) = flow_limit {
10497
ensure!(flow_limit >= 0, ContractError::InvalidFlowLimit);
10598

106-
env.storage()
107-
.persistent()
108-
.set(&DataKey::FlowLimit(token_id.clone()), &flow_limit);
99+
storage::set_flow_limit(env, token_id.clone(), &flow_limit);
109100
} else {
110-
env.storage()
111-
.persistent()
112-
.remove(&DataKey::FlowLimit(token_id.clone()));
101+
storage::remove_flow_limit(env, token_id.clone());
113102
}
114103

115104
FlowLimitSetEvent {
@@ -122,21 +111,23 @@ pub fn set_flow_limit(
122111
}
123112

124113
pub fn flow_out_amount(env: &Env, token_id: BytesN<32>) -> i128 {
125-
env.storage()
126-
.temporary()
127-
.get(&DataKey::FlowOut(FlowKey {
114+
storage::try_flow_out(
115+
env,
116+
FlowKey {
128117
token_id,
129118
epoch: current_epoch(env),
130-
}))
131-
.unwrap_or(0)
119+
},
120+
)
121+
.unwrap_or(0)
132122
}
133123

134124
pub fn flow_in_amount(env: &Env, token_id: BytesN<32>) -> i128 {
135-
env.storage()
136-
.temporary()
137-
.get(&DataKey::FlowIn(FlowKey {
125+
storage::try_flow_in(
126+
env,
127+
FlowKey {
138128
token_id,
139129
epoch: current_epoch(env),
140-
}))
141-
.unwrap_or(0)
130+
},
131+
)
132+
.unwrap_or(0)
142133
}

contracts/stellar-interchain-token-service/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cfg_if::cfg_if! {
2121
mod abi;
2222
mod deployer;
2323
pub mod event;
24-
mod storage_types;
24+
mod storage;
2525
mod token_id;
2626
mod token_manager;
2727
mod token_metadata;

0 commit comments

Comments
 (0)