-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcontract.rs
797 lines (742 loc) · 31.9 KB
/
contract.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
use common::{
ibc::{core::ics04_channel::channel::State, Height},
rlp::{self},
};
use cosmwasm_std::{coins, BankMsg, IbcChannel};
use cw_common::raw_types::channel::RawPacket;
use cw_xcall_lib::network_address::NetId;
use cw_common::cw_println;
use super::*;
use crate::{
state::{
HOST_SEND_MESSAGE_REPLY_ID, HOST_WRITE_ACKNOWLEDGEMENT_REPLY_ID,
XCALL_HANDLE_ERROR_REPLY_ID, XCALL_HANDLE_MESSAGE_REPLY_ID,
},
types::{
channel_config::ChannelConfig, config::Config, config_response::to_config_response,
connection_config::ConnectionConfig, message::Message, LOG_PREFIX,
},
};
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw-xcall-ibc-connection";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
impl<'a> CwIbcConnection<'a> {
/// This function instantiates a contract and initializes it with the provided message and
/// information.
///
/// Arguments:
///
/// * `deps`: `deps` is a `DepsMut` object, which is short for "dependencies mutable". It is a struct
/// that provides access to the contract's dependencies, such as the storage, API, and querier. The
/// `DepsMut` object is passed as a parameter to most of the
/// * `_env`: The `_env` parameter in the `instantiate` function is of type `Env`, which represents
/// the environment in which the contract is being executed. It contains information such as the
/// current block height, the current time, and the address of the contract being executed. However,
/// in the given code snippet
/// * `info`: `info` is a struct that contains information about the message sender, such as their
/// address, the amount of tokens they sent with the message, and the maximum amount of gas they are
/// willing to pay for the transaction. This information can be used to determine whether the sender
/// is authorized to perform certain actions
/// * `msg`: The `msg` parameter in the `instantiate` function is of type `InstantiateMsg` and
/// contains the message sent by the user when instantiating the contract. It can contain any custom
/// data that the user wants to pass to the contract during instantiation. The `msg` parameter is
/// used by the
///
/// Returns:
///
/// The `instantiate` function returns a `Result<Response, ContractError>` where `Response` is a
/// struct representing the response to a message and `ContractError` is an enum representing the
/// possible errors that can occur during contract execution. The function returns the result of
/// calling the `init` function with the provided arguments.
pub fn instantiate(
&self,
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
CwIbcConnection::validate_address(deps.api, info.sender.as_str())?;
self.init(deps.storage, info, msg)
}
/// This function executes various messages based on their type and returns a response or an error.
///
/// Arguments:
///
/// * `deps`: `deps` is a `DepsMut` struct which provides access to the contract's dependencies such
/// as storage, API, and querier. It is mutable, meaning the contract can modify its dependencies.
/// * `env`: `env` is a struct that contains information about the current blockchain environment,
/// such as the block height, time, and chain ID. It is passed as a parameter to the `execute`
/// function in order to provide context for the execution of the contract.
/// * `info`: `info` is a struct that contains information about the message sender, including their
/// address, public key, and the amount of tokens they sent with the message. It is of type
/// `MessageInfo`.
/// * `msg`: The `msg` parameter is of type `ExecuteMsg` and represents the message that is being
/// executed by the contract. It is matched against different variants to determine the action to be
/// taken.
///
/// Returns:
///
/// a `Result<Response, ContractError>` where `Response` is a struct representing the response to a
/// message and `ContractError` is an enum representing an error that occurred during contract
/// execution.
pub fn execute(
&mut self,
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::SetAdmin { address } => {
self.ensure_admin(deps.as_ref().storage, info.sender)?;
let validated_address =
CwIbcConnection::validate_address(deps.api, address.as_str())?;
self.update_admin(deps.storage, validated_address)
}
ExecuteMsg::SendMessage { to, sn, msg } => {
println!("{LOG_PREFIX} Received Payload From XCall App");
// return Ok(Response::new());
self.send_message(deps, info, env, to, sn, msg)
}
ExecuteMsg::SetXCallHost { address } => {
self.ensure_admin(deps.as_ref().storage, info.sender)?;
let validated_address =
CwIbcConnection::validate_address(deps.api, address.as_str())?;
self.set_xcall_host(deps.storage, validated_address)?;
Ok(Response::new())
}
ExecuteMsg::ConfigureConnection {
connection_id,
counterparty_port_id,
counterparty_nid,
client_id,
timeout_height,
} => {
self.ensure_admin(deps.as_ref().storage, info.sender)?;
self.configure_connection(
deps,
connection_id,
counterparty_port_id,
counterparty_nid,
client_id,
timeout_height,
)?;
Ok(Response::new())
}
ExecuteMsg::OverrideConnection {
connection_id,
counterparty_port_id,
counterparty_nid,
client_id,
timeout_height,
} => {
self.ensure_owner(deps.as_ref().storage, &info)?;
self.override_connection(
deps.storage,
connection_id,
counterparty_port_id,
counterparty_nid,
client_id,
timeout_height,
)?;
Ok(Response::new())
}
ExecuteMsg::ClaimFees { nid, address } => {
let fee_msg = self.claim_fees(deps, info, nid, address)?;
Ok(Response::new().add_submessage(fee_msg))
}
ExecuteMsg::SetFees {
nid,
packet_fee,
ack_fee,
} => {
self.ensure_admin(deps.as_ref().storage, info.sender)?;
self.set_fee(deps.storage, nid, packet_fee, ack_fee)
}
#[cfg(not(feature = "native_ibc"))]
ExecuteMsg::IbcChannelOpen { msg } => {
self.ensure_ibc_handler(deps.as_ref().storage, info.sender)?;
Ok(self.on_channel_open(deps, msg)?)
}
#[cfg(not(feature = "native_ibc"))]
ExecuteMsg::IbcChannelConnect { msg } => {
self.ensure_ibc_handler(deps.as_ref().storage, info.sender)?;
Ok(self.on_channel_connect(deps, msg)?)
}
#[cfg(not(feature = "native_ibc"))]
ExecuteMsg::IbcChannelClose { msg } => {
self.ensure_admin(deps.as_ref().storage, info.sender)?;
Ok(self.on_channel_close(msg)?)
}
#[cfg(not(feature = "native_ibc"))]
ExecuteMsg::IbcPacketReceive { msg } => {
self.ensure_ibc_handler(deps.as_ref().storage, info.sender)?;
Ok(self.on_packet_receive(deps, msg)?)
}
#[cfg(not(feature = "native_ibc"))]
ExecuteMsg::IbcPacketAck { msg } => {
self.ensure_ibc_handler(deps.as_ref().storage, info.sender)?;
Ok(self.on_packet_ack(deps, msg)?)
}
#[cfg(not(feature = "native_ibc"))]
ExecuteMsg::IbcPacketTimeout { msg } => {
self.ensure_ibc_handler(deps.as_ref().storage, info.sender)?;
Ok(self.on_packet_timeout(deps, msg)?)
}
#[cfg(feature = "native_ibc")]
_ => Err(ContractError::DecodeFailed {
error: "InvalidMessage Variant".to_string(),
}),
}
}
/// The `query` function takes in dependencies, environment, and a message, and returns a binary
/// result based on the type of message received.
///
/// Arguments:
///
/// * `deps`: `deps` is an object of type `Deps` which contains various dependencies required for
/// executing the contract. These dependencies include the storage, API, and querier. The `deps`
/// object is passed as an argument to the `query` function to access these dependencies.
/// * `_env`: The `_env` parameter is an instance of the `Env` struct, which provides information
/// about the current blockchain environment, such as the block height and time. However, it is not
/// used in this particular `query` function.
/// * `msg`: `msg` is a parameter of type `QueryMsg` which is an enum that represents different types
/// of queries that can be made to the smart contract. The function matches on the variant of
/// `QueryMsg` that is passed in and executes the corresponding logic.
///
/// Returns:
///
/// The `query` function returns a `StdResult<Binary>` which can contain either the binary
/// representation of the result of the query or an error if the query fails. The specific result
/// being returned depends on the type of `QueryMsg` being passed in and the logic of the
/// corresponding match arm.
pub fn query(&self, deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetAdmin {} => match self.query_admin(deps.storage) {
Ok(admin) => Ok(to_binary(&admin)?),
Err(error) => Err(StdError::NotFound {
kind: error.to_string(),
}),
},
QueryMsg::GetTimeoutHeight { channel_id } => {
let config = self.get_channel_config(deps.storage, &channel_id).unwrap();
to_binary(&config.timeout_height)
}
QueryMsg::GetFee { nid, response } => {
let fees = self.get_network_fees(deps.storage, nid);
if response {
return to_binary(&(fees.send_packet_fee + fees.ack_fee));
}
to_binary(&(fees.send_packet_fee))
}
QueryMsg::GetUnclaimedFee { nid, relayer } => {
to_binary(&self.get_unclaimed_fee(deps.storage, nid, relayer))
}
QueryMsg::GetIbcConfig { nid } => {
let ibc_config = self.get_ibc_config(deps.storage, &nid).unwrap();
let channel_config = self
.get_channel_config(deps.storage, &ibc_config.src_endpoint().channel_id)
.unwrap();
to_binary(&to_config_response(ibc_config, channel_config))
}
}
}
/// This function handles different types of reply messages and calls corresponding functions based on
/// the message ID.
///
/// Arguments:
///
/// * `deps`: A mutable reference to the dependencies of the contract, which includes access to the
/// storage, API, and other modules.
/// * `env`: `env` is an environment variable that provides information about the current execution
/// environment of the smart contract. It includes information such as the current block height, the
/// sender address, the contract address, and the current time. This information can be used by the
/// smart contract to make decisions or perform actions based on
/// * `msg`: `msg` is a `Reply` struct that contains the message ID and any associated data that was
/// sent as a reply to a previous message. The `reply` function uses the message ID to determine which
/// specific reply message was received and then calls the appropriate function to handle that message.
///
/// Returns:
///
/// a `Result<Response, ContractError>` where `Response` is the response to the message and
/// `ContractError` is an error type that can be returned if there is an error in processing the
/// message.
pub fn reply(&self, deps: DepsMut, _env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
ACK_FAILURE_ID => self.reply_ack_on_error(msg),
XCALL_HANDLE_MESSAGE_REPLY_ID => self.xcall_handle_message_reply(deps, msg),
XCALL_HANDLE_ERROR_REPLY_ID => self.xcall_handle_error_reply(deps, msg),
HOST_WRITE_ACKNOWLEDGEMENT_REPLY_ID => self.host_write_acknowledgement_reply(deps, msg),
HOST_SEND_MESSAGE_REPLY_ID => self.host_send_message_reply(deps, msg),
_ => Err(ContractError::ReplyError {
code: msg.id,
msg: "Unknown".to_string(),
}),
}
}
pub fn migrate(
&self,
deps: DepsMut,
_env: Env,
_msg: MigrateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)
.map_err(ContractError::Std)?;
Ok(Response::default().add_attribute("migrate", "successful"))
}
}
impl<'a> CwIbcConnection<'a> {
/// This function initializes the contract with default values and sets various parameters such as
/// the timeout height and IBC host.
///
/// Arguments:
///
/// * `store`: A mutable reference to a trait object that implements the `Storage` trait. This is
/// used to store and retrieve data from the contract's storage.
/// * `info`: `info` is a struct that contains information about the message sender, such as their
/// address and the amount of tokens they sent with the message. It is of type `MessageInfo`.
/// * `msg`: InstantiateMsg is a struct that contains the parameters passed during contract
/// instantiation. It is defined somewhere in the code and likely contains fields such as
/// `timeout_height` and `ibc_host`.
///
/// Returns:
///
/// a `Result<Response, ContractError>` where `Response` is a struct representing the response to a
/// contract execution and `ContractError` is an enum representing the possible errors that can
/// occur during contract execution.
fn init(
&self,
store: &mut dyn Storage,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let owner = info.sender;
self.add_owner(store, owner.clone())?;
self.update_admin(store, owner)?;
// self.set_timeout_height(store, msg.timeout_height)?;
self.set_ibc_host(store, msg.ibc_host.clone())?;
self.set_xcall_host(store, msg.xcall_address)?;
let config = Config {
port_id: msg.port_id,
denom: msg.denom,
};
self.store_config(store, &config)?;
Ok(Response::new()
.add_attribute("action", "instantiate")
.add_attribute("method", "init")
.add_attribute("ibc_host", msg.ibc_host))
}
fn xcall_handle_message_reply(
&self,
_deps: DepsMut,
message: Reply,
) -> Result<Response, ContractError> {
println!("{LOG_PREFIX} Reply From Forward XCall");
match message.result {
SubMsgResult::Ok(_) => Ok(Response::new()
.add_attribute("action", "call_message")
.add_attribute("method", "xcall_handle_message_reply")),
SubMsgResult::Err(error) => Err(ContractError::ReplyError {
code: message.id,
msg: error,
}),
}
}
fn xcall_handle_error_reply(
&self,
_deps: DepsMut,
message: Reply,
) -> Result<Response, ContractError> {
println!("{LOG_PREFIX} Reply From Forward XCall");
match message.result {
SubMsgResult::Ok(_) => Ok(Response::new()
.add_attribute("action", "call_message")
.add_attribute("method", "xcall_handle_error_reply")),
SubMsgResult::Err(error) => Err(ContractError::ReplyError {
code: message.id,
msg: error,
}),
}
}
fn host_send_message_reply(
&self,
_deps: DepsMut,
message: Reply,
) -> Result<Response, ContractError> {
println!("{LOG_PREFIX} Reply From Forward Host");
match message.result {
SubMsgResult::Ok(_) => Ok(Response::new()
.add_attribute("action", "call_message")
.add_attribute("method", "reply_forward_host")),
SubMsgResult::Err(error) => Err(ContractError::ReplyError {
code: message.id,
msg: error,
}),
}
}
fn host_write_acknowledgement_reply(
&self,
_deps: DepsMut,
message: Reply,
) -> Result<Response, ContractError> {
println!("{LOG_PREFIX} Reply From Write Acknowledgement Host");
match message.result {
SubMsgResult::Ok(_) => Ok(Response::new()
.add_attribute("action", "call_message")
.add_attribute("method", "reply_write_acknowledgement")),
SubMsgResult::Err(error) => Err(ContractError::ReplyError {
code: message.id,
msg: error,
}),
}
}
#[cfg(feature = "native_ibc")]
fn create_packet_response(&self, deps: Deps, env: Env, data: Binary) -> IbcMsg {
let ibc_config = self.ibc_config().may_load(deps.storage).unwrap().unwrap();
let timeout = IbcTimeout::with_timestamp(env.block.time.plus_seconds(300));
IbcMsg::SendPacket {
channel_id: ibc_config.dst_endpoint().channel_id.clone(),
data,
timeout,
}
}
fn reply_ack_on_error(&self, reply: Reply) -> Result<Response, ContractError> {
match reply.result {
SubMsgResult::Ok(_) => Ok(Response::new()),
SubMsgResult::Err(err) => Ok(Response::new().set_data(make_ack_fail(err))),
}
}
/// This function handles the opening of an IBC channel and returns a response with relevant
/// attributes.
///
/// Arguments:
///
/// * `msg`: The `msg` parameter is of type `IbcChannelOpenMsg`, which is a message that represents
/// the opening of an IBC channel. It contains information about the channel, such as the endpoint
/// and the order of packet delivery.
///
/// Returns:
///
/// The function `on_channel_open` returns a `Result<Response, ContractError>` where `Response` is a
/// struct that contains data and attributes that will be returned to the caller, and
/// `ContractError` is an enum that represents any errors that may occur during the execution of the
/// function.
pub fn on_channel_open(
&mut self,
deps: DepsMut,
msg: CwChannelOpenMsg,
) -> Result<Response, ContractError> {
cw_println!(deps, "[IbcConnection]: Called On channel open");
println!("{msg:?}");
let channel = msg.channel();
let ibc_endpoint = channel.endpoint.clone();
check_order(&channel.order)?;
cw_println!(deps, "[IbcConnection]: check order pass");
self.setup_channel(deps, channel.clone())?;
Ok(Response::new()
.set_data(to_binary(&ibc_endpoint).unwrap())
.add_attribute("method", "on_channel_open")
.add_attribute("version", IBC_VERSION))
}
/// This is a Rust function that handles a channel connection message in an IBC protocol implementation,
/// saving the configuration and returning a response.
///
/// Arguments:
///
/// * `store`: `store` is a mutable reference to a trait object of type `dyn Storage`. It is used to
/// interact with the contract's storage and persist data.
/// * `msg`: The `msg` parameter is of type `IbcChannelConnectMsg`, which is a message that represents a
/// channel connection event in the Inter-Blockchain Communication (IBC) protocol. It contains
/// information about the channel being connected, such as the channel ID, port ID, and endpoints.
///
/// Returns:
///
/// a `Result<Response, ContractError>` where `Response` is a struct that contains data and attributes
/// related to the IBC channel connection, and `ContractError` is an enum that represents any errors
/// that may occur during the execution of the function.
pub fn on_channel_connect(
&mut self,
deps: DepsMut,
msg: CwChannelConnectMsg,
) -> Result<Response, ContractError> {
let channel = msg.channel();
cw_println!(deps, "[IBCConnection]: channel connect called");
check_order(&channel.order)?;
cw_println!(deps, "[IBCConnection]: check order pass");
if let Some(counter_version) = msg.counterparty_version() {
check_version(counter_version)?;
}
cw_println!(deps, "[IBCConnection]: check version passed");
self.setup_channel(deps, channel.clone())?;
Ok(Response::new()
.set_data(to_binary(&channel.endpoint.clone()).unwrap())
.add_attribute("method", "on_channel_connect"))
}
/// This function handles an IBC channel close message and returns a response with relevant attributes
/// and data.
///
/// Arguments:
///
/// * `msg`: The `msg` parameter is of type `IbcChannelCloseMsg`, which is a message that represents
/// the closing of an IBC channel. It can be either a `CloseInit` message or a `CloseConfirm` message,
/// both of which contain information about the channel being closed.
///
/// Returns:
///
/// A `Result` containing a `Response` or a `ContractError`.
pub fn on_channel_close(&self, msg: CwChannelCloseMsg) -> Result<Response, ContractError> {
let ibc_endpoint = match msg {
CwChannelCloseMsg::CloseInit { channel } => channel.endpoint,
CwChannelCloseMsg::CloseConfirm { channel } => channel.endpoint,
};
Ok(Response::new()
.add_attribute("method", "ibc_channel_close")
.set_data(to_binary(&ibc_endpoint).unwrap()))
}
/// This function receives an IBC packet and returns a response with acknowledgement and events or an
/// error message.
///
/// Arguments:
///
/// * `deps`: `deps` is a `DepsMut` object, which provides access to the mutable dependencies of the
/// contract. These dependencies include the storage, querier, and API interfaces.
/// * `msg`: The `msg` parameter is an `IbcPacketReceiveMsg` struct that contains information about the
/// received IBC packet, such as the source and destination chain IDs, the packet data, and the packet
/// sequence number.
///
/// Returns:
///
/// A `Result<Response, ContractError>` is being returned.
pub fn on_packet_receive(
&self,
deps: DepsMut,
msg: CwPacketReceiveMsg,
) -> Result<Response, ContractError> {
match self.do_packet_receive(deps, msg.packet, msg.relayer) {
Ok(ibc_response) => Ok(Response::new()
.add_attributes(ibc_response.attributes.clone())
.add_submessages(ibc_response.messages)
.add_events(ibc_response.events)),
Err(error) => Err(error),
}
}
/// The function handles the acknowledgement of a packet and returns a response with attributes
/// indicating success or failure.
///
/// Arguments:
///
/// * `ack`: The `ack` parameter is an `IbcPacketAckMsg` struct, which represents the acknowledgement
/// message for an Inter-Blockchain Communication (IBC) packet. It contains information about the
/// acknowledgement, including the original packet data and the acknowledgement data.
///
/// Returns:
///
/// a `Result<Response, ContractError>` where `Response` is a struct representing the response to be
/// returned to the caller and `ContractError` is an enum representing the possible errors that can
/// occur during the execution of the function.
pub fn on_packet_ack(
&self,
deps: DepsMut,
ack: CwPacketAckMsg,
) -> Result<Response, ContractError> {
let packet = ack.original_packet;
let acknowledgement = ack.acknowledgement;
let channel = packet.src.channel_id.clone();
let seq = packet.sequence;
let channel_config = self.get_channel_config(deps.as_ref().storage, &channel)?;
let nid = channel_config.counterparty_nid;
let submsg = self.call_xcall_handle_message(deps.storage, &nid, acknowledgement.data.0)?;
let bank_msg = self.settle_unclaimed_ack_fee(
deps.storage,
nid.as_str(),
seq,
ack.relayer.to_string(),
)?;
Ok(Response::new()
.add_messages(bank_msg)
.add_submessage(submsg))
}
/// This function handles a timeout event for an IBC packet and sends a reply message with an error
/// code.
///
/// Arguments:
///
/// * `_msg`: The `_msg` parameter is of type `IbcPacketTimeoutMsg`, which is a struct that contains
/// information about a timed-out IBC packet. This information includes the packet sequence, the port
/// and channel identifiers, and the height at which the packet was sent.
///
/// Returns:
///
/// a `Result` object that contains a `Response` object if the function executes successfully, or a
/// `ContractError` object if an error occurs. The `Response` object contains a submessage and an
/// attribute. The submessage is a reply on error with a `CosmosMsg::Custom` object that contains an
/// `Empty` message. The attribute is a key-value pair
pub fn on_packet_timeout(
&self,
deps: DepsMut,
msg: CwPacketTimeoutMsg,
) -> Result<Response, ContractError> {
let packet = msg.packet;
let n_message: Message = rlp::decode(&packet.data).unwrap();
if n_message.sn.is_none() {
return Ok(Response::new());
}
let channel_id = packet.src.channel_id.clone();
let channel_config = self.get_channel_config(deps.as_ref().storage, &channel_id)?;
let nid = channel_config.counterparty_nid;
self.add_unclaimed_ack_fees(deps.storage, &nid, packet.sequence, n_message.fee)?;
let submsg = self.call_xcall_handle_error(deps.storage, n_message.sn.0.unwrap())?;
let bank_msg = self.settle_unclaimed_ack_fee(
deps.storage,
nid.as_str(),
packet.sequence,
msg.relayer.to_string(),
)?;
Ok(Response::new()
.add_messages(bank_msg)
.add_submessage(submsg))
}
pub fn settle_unclaimed_ack_fee(
&self,
store: &mut dyn Storage,
nid: &str,
seq: u64,
relayer: String,
) -> Result<Vec<BankMsg>, ContractError> {
let ack_fee = self.get_unclaimed_ack_fee(store, nid, seq);
if ack_fee == 0 {
return Ok(vec![]);
}
self.reset_unclaimed_ack_fees(store, nid, seq)?;
let denom = self.get_denom(store)?;
let msg = BankMsg::Send {
to_address: relayer,
amount: coins(ack_fee, denom),
};
Ok(vec![msg])
}
pub fn setup_channel(
&mut self,
deps: DepsMut,
channel: IbcChannel,
) -> Result<(), ContractError> {
let source = channel.endpoint.clone();
let destination = channel.counterparty_endpoint.clone();
let channel_id = source.channel_id.clone();
let our_port = self.get_port(deps.storage)?;
cw_println!(
deps,
"[IBCConnection]: Check if ports match : {:?} vs {:?}",
our_port,
source.port_id
);
if our_port != source.port_id {
return Err(ContractError::InvalidPortId);
}
let nid =
self.get_counterparty_nid(deps.storage, &channel.connection_id, &destination.port_id)?;
let connection_config = self.get_connection_config(deps.storage, &channel.connection_id)?;
let ibc_config = IbcConfig::new(source, destination);
cw_println!(deps, "[IBCConnection]: save ibc config is {:?}", ibc_config);
self.store_ibc_config(deps.storage, &nid, &ibc_config)?;
self.store_channel_config(
deps.storage,
&channel_id,
&ChannelConfig {
timeout_height: connection_config.timeout_height,
client_id: connection_config.client_id,
counterparty_nid: nid,
},
)?;
cw_println!(deps, "[IBCConnection]: Channel Config Stored");
Ok(())
}
pub fn configure_connection(
&self,
deps: DepsMut,
connection_id: String,
counterparty_port_id: String,
counterparty_nid: NetId,
client_id: String,
timeout_height: u64,
) -> Result<(), ContractError> {
let cfg_res = self.get_ibc_config(deps.storage, &counterparty_nid);
if let Ok(cfg) = cfg_res {
let state_res = self.query_channel_state(
deps.as_ref(),
cfg.src_endpoint().port_id.clone(),
cfg.src_endpoint().channel_id.clone(),
);
if let Ok(state) = state_res {
if State::from_i32(state).map_or(false, |s| s.is_open()) {
return Err(ContractError::ConnectionAlreadyConfigured {
connection_id,
port_id: counterparty_port_id,
});
}
}
self.clear_ibc_config(deps.storage, &counterparty_nid)
}
self.store_counterparty_nid(
deps.storage,
&connection_id,
&counterparty_port_id,
&counterparty_nid,
)?;
self.store_connection_config(
deps.storage,
&connection_id,
&ConnectionConfig {
timeout_height,
client_id,
},
)?;
Ok(())
}
pub fn override_connection(
&self,
store: &mut dyn Storage,
connection_id: String,
counterparty_port_id: String,
counterparty_nid: NetId,
client_id: String,
timeout_height: u64,
) -> Result<(), ContractError> {
self.clear_ibc_config(store, &counterparty_nid);
self.store_counterparty_nid(
store,
&connection_id,
&counterparty_port_id,
&counterparty_nid,
)?;
self.store_connection_config(
store,
&connection_id,
&ConnectionConfig {
timeout_height,
client_id,
},
)?;
Ok(())
}
pub fn create_packet<T: common::rlp::Encodable>(
&self,
ibc_config: IbcConfig,
timeout_height: Height,
sequence_no: u64,
data: T,
) -> RawPacket {
let packet = RawPacket {
sequence: sequence_no,
source_port: ibc_config.src_endpoint().port_id.clone(),
source_channel: ibc_config.src_endpoint().channel_id.clone(),
destination_port: ibc_config.dst_endpoint().port_id.clone(),
destination_channel: ibc_config.dst_endpoint().channel_id.clone(),
data: rlp::encode(&data).to_vec(),
timeout_height: Some(timeout_height.into()),
timeout_timestamp: 0,
};
packet
}
}