Skip to content

Commit 0a33b5a

Browse files
committed
refractor code
1 parent 7bdb069 commit 0a33b5a

File tree

4 files changed

+657
-1166
lines changed

4 files changed

+657
-1166
lines changed
Binary file not shown.

contracts/cw-ics20-latest/src/testing/ibc_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ fn test_follow_up_msgs() {
11691169
},
11701170
"foobar",
11711171
receiver,
1172-
&DestinationInfo::from_str(""),
1172+
&DestinationInfo::parse_str(""),
11731173
None,
11741174
)
11751175
.unwrap();
@@ -1204,7 +1204,7 @@ fn test_follow_up_msgs() {
12041204
},
12051205
"foobar",
12061206
"foobar",
1207-
&DestinationInfo::from_str(memo),
1207+
&DestinationInfo::parse_str(memo),
12081208
None,
12091209
)
12101210
.unwrap();
@@ -1241,7 +1241,7 @@ fn test_follow_up_msgs() {
12411241
},
12421242
"foobar",
12431243
"foobar",
1244-
&DestinationInfo::from_str(memo),
1244+
&DestinationInfo::parse_str(memo),
12451245
None,
12461246
)
12471247
.unwrap();

packages/cw20-ics20-msg/src/receiver.rs

+31-30
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Default for DestinationInfo {
2424

2525
impl DestinationInfo {
2626
// destination string format: <destination-channel>/<receiver>:<denom>
27-
pub fn from_str(value: &str) -> Self {
27+
pub fn parse_str(value: &str) -> Self {
2828
let (destination, denom) = value.split_once(':').unwrap_or((value, ""));
2929
let (channel, receiver) = destination.split_once('/').unwrap_or(("", destination));
3030

@@ -81,24 +81,23 @@ impl DestinationInfo {
8181
}
8282

8383
pub fn is_receiver_cosmos_based(&self) -> bool {
84-
get_prefix_decode_bech32(&self.receiver)
84+
!get_prefix_decode_bech32(&self.receiver)
8585
.unwrap_or_default() // empty string if error
86-
.len()
87-
> 0
86+
.is_empty()
8887
}
8988
}
9089

9190
#[test]
9291
fn test_is_evm_based() {
93-
let d1 = DestinationInfo::from_str("cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
92+
let d1 = DestinationInfo::parse_str("cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
9493
assert_eq!(false, d1.is_receiver_evm_based().0);
95-
let d1 = DestinationInfo::from_str("0x3C5C6b570C1DA469E8B24A2E8Ed33c278bDA3222");
94+
let d1 = DestinationInfo::parse_str("0x3C5C6b570C1DA469E8B24A2E8Ed33c278bDA3222");
9695
// false here because we need the evm-prefix as well!
9796
assert_eq!(false, d1.is_receiver_evm_based().0);
98-
let d1 = DestinationInfo::from_str("foobar0x3C5C6b570C1DA469E8B24A2E8Ed33c278b");
97+
let d1 = DestinationInfo::parse_str("foobar0x3C5C6b570C1DA469E8B24A2E8Ed33c278b");
9998
// false here because of the wrong eth address, not enough in length
10099
assert_eq!(false, d1.is_receiver_evm_based().0);
101-
let d1 = DestinationInfo::from_str(
100+
let d1 = DestinationInfo::parse_str(
102101
"channel-15/foobar0x3C5C6b570C1DA469E8B24A2E8Ed33c278bDA3222:usdt",
103102
);
104103
let (is_evm_based, prefix) = d1.is_receiver_evm_based();
@@ -112,55 +111,56 @@ fn test_is_evm_based() {
112111

113112
#[test]
114113
fn test_is_cosmos_based() {
115-
let d1 = DestinationInfo::from_str("foo");
114+
let d1 = DestinationInfo::parse_str("foo");
116115
assert_eq!(false, d1.is_receiver_cosmos_based());
117116

118-
let d1 = DestinationInfo::from_str("channel-15/foo:usdt");
117+
let d1 = DestinationInfo::parse_str("channel-15/foo:usdt");
119118
assert_eq!(false, d1.is_receiver_cosmos_based());
120119

121120
let d1 =
122-
DestinationInfo::from_str("channel-15/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:usdt");
121+
DestinationInfo::parse_str("channel-15/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:usdt");
123122
let result = d1.is_receiver_cosmos_based();
124123
assert_eq!(true, result);
125124

126125
let d1 =
127-
DestinationInfo::from_str("channel-15/akash1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejjpn5xp:usdt");
126+
DestinationInfo::parse_str("channel-15/akash1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejjpn5xp:usdt");
128127
let result = d1.is_receiver_cosmos_based();
129128
assert_eq!(true, result);
130129

131-
let d1 =
132-
DestinationInfo::from_str("channel-15/bostrom1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejuf2qpu:usdt");
130+
let d1 = DestinationInfo::parse_str(
131+
"channel-15/bostrom1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejuf2qpu:usdt",
132+
);
133133
let result = d1.is_receiver_cosmos_based();
134134
assert_eq!(true, result);
135135

136-
let d1 = DestinationInfo::from_str("channel-124/cosmos1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejl67nlm:orai17l2zk3arrx0a0fyuneyx8raln68622a2lrsz8ph75u7gw9tgz3esayqryf");
136+
let d1 = DestinationInfo::parse_str("channel-124/cosmos1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejl67nlm:orai17l2zk3arrx0a0fyuneyx8raln68622a2lrsz8ph75u7gw9tgz3esayqryf");
137137
let result = d1.is_receiver_cosmos_based();
138138
assert_eq!(true, result);
139139
}
140140

141141
#[test]
142-
fn test_destination_info_from_str() {
143-
let d1 = DestinationInfo::from_str("cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
142+
fn test_destination_info_parse_str() {
143+
let d1 = DestinationInfo::parse_str("cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
144144
assert_eq!(d1.destination_channel, "");
145145
assert_eq!(d1.receiver, "cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
146146
assert_eq!(d1.destination_denom, "");
147147

148-
let d1 = DestinationInfo::from_str("cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:foo");
148+
let d1 = DestinationInfo::parse_str("cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:foo");
149149
assert_eq!(d1.destination_channel, "");
150150
assert_eq!(d1.receiver, "cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
151151
assert_eq!(d1.destination_denom, "foo");
152152

153-
let d1 = DestinationInfo::from_str("foo/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
153+
let d1 = DestinationInfo::parse_str("foo/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
154154
assert_eq!(d1.destination_channel, "foo");
155155
assert_eq!(d1.receiver, "cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
156156
assert_eq!(d1.destination_denom, "");
157157

158-
let d1 = DestinationInfo::from_str("foo/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:bar");
158+
let d1 = DestinationInfo::parse_str("foo/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:bar");
159159
assert_eq!(d1.destination_channel, "foo");
160160
assert_eq!(d1.receiver, "cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
161161
assert_eq!(d1.destination_denom, "bar");
162162

163-
let d1 = DestinationInfo::from_str("");
163+
let d1 = DestinationInfo::parse_str("");
164164
assert_eq!(d1.destination_channel, "");
165165
assert_eq!(d1.receiver, "");
166166
assert_eq!(d1.destination_denom, "");
@@ -176,7 +176,7 @@ mod tests {
176176
#[test]
177177
fn test_parse_destination_info() {
178178
// swap to orai then orai to atom, then use swapped amount to transfer ibc to destination
179-
let d1 = DestinationInfo::from_str(
179+
let d1 = DestinationInfo::parse_str(
180180
"channel-15/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz:atom",
181181
);
182182
assert_eq!(
@@ -188,7 +188,7 @@ mod tests {
188188
}
189189
);
190190
// swap to orai then orai to usdt with 'to' as the receiver when swapping, then we're done
191-
let d2 = DestinationInfo::from_str("orai14n3tx8s5ftzhlxvq0w5962v60vd82h30rha573:usdt");
191+
let d2 = DestinationInfo::parse_str("orai14n3tx8s5ftzhlxvq0w5962v60vd82h30rha573:usdt");
192192
assert_eq!(
193193
d2,
194194
DestinationInfo {
@@ -199,7 +199,7 @@ mod tests {
199199
);
200200
// this case returns an error (because it has channel but no destination denom)
201201
let d3 =
202-
DestinationInfo::from_str("channel-15/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
202+
DestinationInfo::parse_str("channel-15/cosmos14n3tx8s5ftzhlxvq0w5962v60vd82h30sythlz");
203203
assert_eq!(
204204
d3,
205205
DestinationInfo {
@@ -208,8 +208,9 @@ mod tests {
208208
destination_denom: "".to_string()
209209
}
210210
);
211-
let d4 =
212-
DestinationInfo::from_str("trx-mainnet0x73Ddc880916021EFC4754Cb42B53db6EAB1f9D64:usdt");
211+
let d4 = DestinationInfo::parse_str(
212+
"trx-mainnet0x73Ddc880916021EFC4754Cb42B53db6EAB1f9D64:usdt",
213+
);
213214
assert_eq!(
214215
d4,
215216
DestinationInfo {
@@ -219,7 +220,7 @@ mod tests {
219220
}
220221
);
221222

222-
let d5 = DestinationInfo::from_str("orai14n3tx8s5ftzhlxvq0w5962v60vd82h30rha573");
223+
let d5 = DestinationInfo::parse_str("orai14n3tx8s5ftzhlxvq0w5962v60vd82h30rha573");
223224
assert_eq!(
224225
d5,
225226
DestinationInfo {
@@ -229,7 +230,7 @@ mod tests {
229230
}
230231
);
231232

232-
let d6 = DestinationInfo::from_str(
233+
let d6 = DestinationInfo::parse_str(
233234
"channel-5/trx-mainnet0x73Ddc880916021EFC4754Cb42B53db6EAB1f9D64:usdt",
234235
);
235236
assert_eq!(
@@ -241,7 +242,7 @@ mod tests {
241242
}
242243
);
243244
// ibc hash case
244-
let d7 = DestinationInfo::from_str("channel-5/trx-mainnet0x73Ddc880916021EFC4754Cb42B53db6EAB1f9D64:ibc/A2E2EEC9057A4A1C2C0A6A4C78B0239118DF5F278830F50B4A6BDD7A66506B78");
245+
let d7 = DestinationInfo::parse_str("channel-5/trx-mainnet0x73Ddc880916021EFC4754Cb42B53db6EAB1f9D64:ibc/A2E2EEC9057A4A1C2C0A6A4C78B0239118DF5F278830F50B4A6BDD7A66506B78");
245246
assert_eq!(
246247
d7,
247248
DestinationInfo {
@@ -252,7 +253,7 @@ mod tests {
252253
.to_string()
253254
}
254255
);
255-
let d8 = DestinationInfo::from_str("channel-124/cosmos1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejl67nlm:orai17l2zk3arrx0a0fyuneyx8raln68622a2lrsz8ph75u7gw9tgz3esayqryf");
256+
let d8 = DestinationInfo::parse_str("channel-124/cosmos1g4h64yjt0fvzv5v2j8tyfnpe5kmnetejl67nlm:orai17l2zk3arrx0a0fyuneyx8raln68622a2lrsz8ph75u7gw9tgz3esayqryf");
256257
assert_eq!(
257258
d8,
258259
DestinationInfo {

0 commit comments

Comments
 (0)