Skip to content

Commit 0b2d38a

Browse files
authored
refactor(its): add BytesExt trait (#108)
1 parent 6fa56a9 commit 0b2d38a

File tree

9 files changed

+46
-41
lines changed

9 files changed

+46
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ alloy-sol-types = { version = "0.8.14", default-features = false, features = [
2323
"std",
2424
] }
2525
goldie = "0.5.0"
26+
hex = { version = "0.4" }
2627

2728
[workspace.lints.clippy]
2829
nursery = { level = "warn", priority = -1 }

contracts/interchain-token-service/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ axelar-gas-service = { workspace = true, features = ["testutils"] }
2323
axelar-gateway = { workspace = true, features = ["testutils"] }
2424
axelar-soroban-std = { workspace = true, features = ["testutils"] }
2525
goldie = { workspace = true }
26-
hex = "0.4"
26+
hex = { workspace = true }
2727
soroban-sdk = { workspace = true, features = ["testutils", "alloc"] }
2828

2929
[lints]

contracts/interchain-token-service/src/abi.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,10 @@ impl From<MessageType> for U256 {
246246
mod tests {
247247
use super::*;
248248
use alloc::vec;
249-
use axelar_soroban_std::assert_ok;
249+
use axelar_soroban_std::{assert_ok, traits::BytesExt};
250250
use soroban_sdk::{Bytes, BytesN, Env, String};
251251
use std::vec::Vec;
252252

253-
pub fn bytes_from_hex(env: &Env, hex_string: &str) -> Bytes {
254-
let bytes_vec: Vec<u8> = hex::decode(hex_string).unwrap();
255-
Bytes::from_slice(env, &bytes_vec)
256-
}
257-
258253
#[test]
259254
fn soroban_str_to_std_string() {
260255
let env = Env::default();
@@ -361,8 +356,8 @@ mod tests {
361356
destination_chain: remote_chain.clone(),
362357
message: types::Message::InterchainTransfer(types::InterchainTransfer {
363358
token_id: BytesN::from_array(&env, &[0u8; 32]),
364-
source_address: bytes_from_hex(&env, "00"),
365-
destination_address: bytes_from_hex(&env, "00"),
359+
source_address: Bytes::from_hex(&env, "00"),
360+
destination_address: Bytes::from_hex(&env, "00"),
366361
amount: 1u64.into(),
367362
data: None,
368363
}),
@@ -371,24 +366,24 @@ mod tests {
371366
destination_chain: remote_chain.clone(),
372367
message: types::Message::InterchainTransfer(types::InterchainTransfer {
373368
token_id: BytesN::from_array(&env, &[255u8; 32]),
374-
source_address: bytes_from_hex(
369+
source_address: Bytes::from_hex(
375370
&env,
376371
"4F4495243837681061C4743b74B3eEdf548D56A5",
377372
),
378-
destination_address: bytes_from_hex(
373+
destination_address: Bytes::from_hex(
379374
&env,
380375
"4F4495243837681061C4743b74B3eEdf548D56A5",
381376
),
382377
amount: i128::MAX,
383-
data: Some(bytes_from_hex(&env, "abcd")),
378+
data: Some(Bytes::from_hex(&env, "abcd")),
384379
}),
385380
},
386381
types::HubMessage::ReceiveFromHub {
387382
source_chain: remote_chain.clone(),
388383
message: types::Message::InterchainTransfer(types::InterchainTransfer {
389384
token_id: BytesN::from_array(&env, &[0u8; 32]),
390-
source_address: bytes_from_hex(&env, "00"),
391-
destination_address: bytes_from_hex(&env, "00"),
385+
source_address: Bytes::from_hex(&env, "00"),
386+
destination_address: Bytes::from_hex(&env, "00"),
392387
amount: 1u64.into(),
393388
data: None,
394389
}),
@@ -397,16 +392,16 @@ mod tests {
397392
source_chain: remote_chain,
398393
message: types::Message::InterchainTransfer(types::InterchainTransfer {
399394
token_id: BytesN::from_array(&env, &[255u8; 32]),
400-
source_address: bytes_from_hex(
395+
source_address: Bytes::from_hex(
401396
&env,
402397
"4F4495243837681061C4743b74B3eEdf548D56A5",
403398
),
404-
destination_address: bytes_from_hex(
399+
destination_address: Bytes::from_hex(
405400
&env,
406401
"4F4495243837681061C4743b74B3eEdf548D56A5",
407402
),
408403
amount: i128::MAX,
409-
data: Some(bytes_from_hex(&env, "abcd")),
404+
data: Some(Bytes::from_hex(&env, "abcd")),
410405
}),
411406
},
412407
];
@@ -454,7 +449,7 @@ mod tests {
454449
name: String::from_str(&env, "Test Token"),
455450
symbol: String::from_str(&env, "TST"),
456451
decimals: 18,
457-
minter: Some(bytes_from_hex(&env, "1234")),
452+
minter: Some(Bytes::from_hex(&env, "1234")),
458453
}),
459454
},
460455
types::HubMessage::SendToHub {
@@ -464,7 +459,7 @@ mod tests {
464459
name: String::from_str(&env, "Unicode Token 🪙"),
465460
symbol: String::from_str(&env, "UNI🔣"),
466461
decimals: 255,
467-
minter: Some(bytes_from_hex(&env, "abcd")),
462+
minter: Some(Bytes::from_hex(&env, "abcd")),
468463
}),
469464
},
470465
types::HubMessage::ReceiveFromHub {
@@ -484,7 +479,7 @@ mod tests {
484479
name: String::from_str(&env, "Test Token"),
485480
symbol: String::from_str(&env, "TST"),
486481
decimals: 18,
487-
minter: Some(bytes_from_hex(&env, "1234")),
482+
minter: Some(Bytes::from_hex(&env, "1234")),
488483
}),
489484
},
490485
types::HubMessage::ReceiveFromHub {
@@ -494,7 +489,7 @@ mod tests {
494489
name: String::from_str(&env, "Unicode Token 🪙"),
495490
symbol: String::from_str(&env, "UNI🔣"),
496491
decimals: 255,
497-
minter: Some(bytes_from_hex(&env, "abcd")),
492+
minter: Some(Bytes::from_hex(&env, "abcd")),
498493
}),
499494
},
500495
];
@@ -531,8 +526,8 @@ mod tests {
531526
let invalid_hub_message_type = assert_ok!(types::Message::InterchainTransfer(
532527
types::InterchainTransfer {
533528
token_id: BytesN::from_array(&env, &[0u8; 32]),
534-
source_address: bytes_from_hex(&env, "00"),
535-
destination_address: bytes_from_hex(&env, "00"),
529+
source_address: Bytes::from_hex(&env, "00"),
530+
destination_address: Bytes::from_hex(&env, "00"),
536531
amount: 1u64.into(),
537532
data: None,
538533
}

contracts/interchain-token-service/tests/interchain_transfer.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
mod utils;
22
use axelar_gateway::testutils::{generate_proof, get_approve_hash};
33
use axelar_gateway::types::Message as GatewayMessage;
4+
use axelar_soroban_std::traits::BytesExt;
45
use axelar_soroban_std::{assert_emitted_event, assert_last_emitted_event, assert_ok};
56
use interchain_token_service::types::{HubMessage, InterchainTransfer, Message};
67
use soroban_sdk::xdr::ToXdr;
7-
use soroban_sdk::{testutils::Address as _, vec, Address, BytesN, String, Symbol};
8-
use utils::{bytes_from_hex, register_chains, setup_env, setup_gas_token, HUB_CHAIN};
8+
use soroban_sdk::{testutils::Address as _, vec, Address, Bytes, BytesN, String, Symbol};
9+
use utils::{register_chains, setup_env, setup_gas_token, HUB_CHAIN};
910

1011
#[test]
1112
fn interchain_transfer_send() {
@@ -16,9 +17,9 @@ fn interchain_transfer_send() {
1617

1718
let token_id = BytesN::from_array(&env, &[255u8; 32]);
1819
let destination_chain = String::from_str(&env, HUB_CHAIN);
19-
let destination_address = bytes_from_hex(&env, "4F4495243837681061C4743b74B3eEdf548D56A5");
20+
let destination_address = Bytes::from_hex(&env, "4F4495243837681061C4743b74B3eEdf548D56A5");
2021
let amount = i128::MAX;
21-
let data = Some(bytes_from_hex(&env, "abcd"));
22+
let data = Some(Bytes::from_hex(&env, "abcd"));
2223

2324
let msg = Message::InterchainTransfer(InterchainTransfer {
2425
token_id: token_id.clone(),
@@ -76,7 +77,7 @@ fn interchain_transfer_receive() {
7677

7778
let token_id = BytesN::from_array(&env, &[255u8; 32]);
7879
let amount = i128::MAX;
79-
let data = Some(bytes_from_hex(&env, "abcd"));
80+
let data = Some(Bytes::from_hex(&env, "abcd"));
8081

8182
let msg = HubMessage::ReceiveFromHub {
8283
source_chain: String::from_str(&env, HUB_CHAIN),

contracts/interchain-token-service/tests/message_routing.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
mod utils;
2-
use axelar_soroban_std::assert_contract_err;
2+
use axelar_soroban_std::{assert_contract_err, traits::BytesExt};
33
use interchain_token_service::error::ContractError;
4-
use soroban_sdk::{testutils::Address as _, Address, BytesN, String};
5-
use utils::{bytes_from_hex, register_chains, setup_env, setup_gas_token};
4+
use soroban_sdk::{testutils::Address as _, Address, Bytes, BytesN, String};
5+
use utils::{register_chains, setup_env, setup_gas_token};
66

77
#[test]
88
fn send_directly_to_hub_chain_fails() {
@@ -15,7 +15,7 @@ fn send_directly_to_hub_chain_fails() {
1515
&sender,
1616
&BytesN::from_array(&env, &[255u8; 32]),
1717
&client.its_hub_chain_name(),
18-
&bytes_from_hex(&env, "4F4495243837681061C4743b74B3eEdf548D56A5"),
18+
&Bytes::from_hex(&env, "4F4495243837681061C4743b74B3eEdf548D56A5"),
1919
&i128::MAX,
2020
&None,
2121
&gas_token,
@@ -34,7 +34,7 @@ fn send_to_untrusted_chain_fails() {
3434
&sender,
3535
&BytesN::from_array(&env, &[255u8; 32]),
3636
&String::from_str(&env, "untrusted_chain"),
37-
&bytes_from_hex(&env, "4F4495243837681061C4743b74B3eEdf548D56A5"),
37+
&Bytes::from_hex(&env, "4F4495243837681061C4743b74B3eEdf548D56A5"),
3838
&i128::MAX,
3939
&None,
4040
&gas_token,

contracts/interchain-token-service/tests/utils/mod.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use axelar_soroban_std::types::Token;
55
// #[cfg(any(test, feature = "testutils"))]
66
// use interchain_token_service::testutils::setup_its;
77
use interchain_token_service::{InterchainTokenService, InterchainTokenServiceClient};
8-
use soroban_sdk::Bytes;
98
use soroban_sdk::{testutils::Address as _, token::StellarAssetClient, Address, Env, String};
109

1110
pub const HUB_CHAIN: &str = "hub_chain";
@@ -75,8 +74,3 @@ pub fn register_chains(env: &Env, client: &InterchainTokenServiceClient) {
7574
let addr = String::from_str(env, HUB_ADDRESS);
7675
client.mock_all_auths().set_trusted_address(&chain, &addr);
7776
}
78-
79-
pub fn bytes_from_hex(env: &Env, hex_string: &str) -> Bytes {
80-
let bytes_vec: Vec<u8> = hex::decode(hex_string).unwrap();
81-
Bytes::from_slice(env, &bytes_vec)
82-
}

packages/axelar-soroban-std/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ publish = true
1010
crate-type = ["rlib"]
1111

1212
[dependencies]
13+
hex = { workspace = true, optional = true }
1314
soroban-sdk = { workspace = true }
1415

1516
[dev-dependencies]
1617
goldie = { workspace = true }
1718
soroban-sdk = { workspace = true, features = ["testutils"] }
1819

1920
[features]
20-
testutils = ["soroban-sdk/testutils"]
21+
testutils = ["soroban-sdk/testutils", "hex"]
2122

2223
[lints]
2324
workspace = true

packages/axelar-soroban-std/src/traits.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ impl<T, E> ThenOk<T, E> for bool {
1414
#[cfg(any(test, feature = "testutils"))]
1515
mod testutils {
1616
extern crate std;
17+
use std::vec::Vec as StdVec;
1718

18-
use soroban_sdk::{Env, IntoVal, TryFromVal, Val, Vec};
19+
use soroban_sdk::{Bytes, Env, IntoVal, TryFromVal, Val, Vec};
1920

2021
pub trait IntoVec<T> {
2122
fn into_vec(self, env: &Env) -> Vec<T>;
@@ -26,4 +27,15 @@ mod testutils {
2627
Vec::from_slice(env, self.as_slice())
2728
}
2829
}
30+
31+
pub trait BytesExt {
32+
fn from_hex(env: &Env, hex_string: &str) -> Bytes;
33+
}
34+
35+
impl BytesExt for Bytes {
36+
fn from_hex(env: &Env, hex_string: &str) -> Bytes {
37+
let bytes_vec: StdVec<u8> = hex::decode(hex_string).expect("hex decoding failed");
38+
Self::from_slice(env, &bytes_vec)
39+
}
40+
}
2941
}

0 commit comments

Comments
 (0)