polytone_evm/ack.rs
1use cosmwasm_std::{from_json, to_json_binary, Binary, IbcAcknowledgement};
2
3pub use crate::callbacks::Callback;
4
5/// wasmd 0.32+ will not return a hardcoded ICS-20 ACK if
6/// ibc_packet_receive errors [1] so we can safely use an ACK format
7/// that is not ICS-20 error-variant compatible.
8///
9/// [1]: https://github.com/CosmWasm/wasmd/issues/1305#issuecomment-1489871618
10pub type Ack = Callback;
11
12/// Serializes an ACK-SUCCESS containing the provided data.
13///
14// pub fn ack_query_success(result: Vec<Binary>) -> Binary {
15// to_json_binary(&Callback::Query(Ok(result))).unwrap()
16// }
17
18/// Serializes an ACK-SUCCESS for a query that failed.
19// pub fn ack_query_fail(message_index: Uint64, error: String) -> Binary {
20// to_json_binary(&Callback::Query(Err(ErrorResponse {
21// message_index,
22// error,
23// })))
24// .unwrap()
25// }
26
27/// Serializes an ACK-SUCCESS for execution that succeeded.
28// pub fn ack_execute_success(result: Vec<HexBinary>, executed_by: String) -> Binary {
29// to_json_binary(&Callback::Execute(Ok(ExecutionResponse {
30// result: result.into_iter().map(|data| ExecuteResult{ success: true, data }).collect(),
31// executed_by,
32// })))
33// .unwrap()
34// }
35
36/// Serializes an ACK-SUCCESS for execution that failed.
37pub fn ack_execute_fail(error: String) -> Binary {
38 to_json_binary(&Callback::Execute(Err(error))).unwrap()
39}
40
41/// Serializes an ACK-FAIL containing the provided error.
42pub fn ack_fail(err: String) -> Binary {
43 to_json_binary(&Callback::FatalError(err)).unwrap()
44}
45
46/// Unmarshals an ACK from an acknowledgement returned by the SDK. If
47/// the returned acknowledgement can not be parsed into an ACK,
48/// err(base64(ack)) is returned.
49pub fn unmarshal_ack(ack: &IbcAcknowledgement) -> Ack {
50 from_json(&ack.data).unwrap_or_else(|e| {
51 Callback::FatalError(format!(
52 "error unmarshalling ack ({}): {}",
53 ack.data.to_base64(),
54 e
55 ))
56 })
57}