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
use ibc_relayer_types::signer::SignerError;
use std::ops::Add;
use std::str::FromStr;

use core::time::Duration;

use flex_error::{define_error, DetailOnly};
use ibc_proto::cosmos::base::v1beta1::Coin;
use ibc_proto::google::protobuf::Any;
use ibc_relayer_types::applications::transfer::error::Error as Ics20Error;
use ibc_relayer_types::applications::transfer::msgs::transfer::MsgTransfer;
use ibc_relayer_types::applications::transfer::Amount;
use ibc_relayer_types::core::ics04_channel::timeout::TimeoutHeight;
use ibc_relayer_types::core::ics24_host::identifier::{ChainId, ChannelId, PortId};
use ibc_relayer_types::events::IbcEvent;
use ibc_relayer_types::signer::Signer;
use ibc_relayer_types::timestamp::{Timestamp, TimestampOverflowError};
use ibc_relayer_types::tx_msg::Msg;

use crate::chain::endpoint::ChainStatus;
use crate::chain::handle::ChainHandle;
use crate::chain::tracking::TrackedMsgs;
use crate::error::Error;
use crate::event::IbcEventWithHeight;

define_error! {
    TransferError {
        ReceiverAddress
            [ SignerError ]
            |_| { "receiver address error "},
        Relayer
            [ Error ]
            |_| { "relayer error" },

        Key
            [ Error ]
            |_| { "key error" },

        Submit
            { chain_id: ChainId }
            [ Error ]
            |e| {
                format!("failed while submitting the Transfer message to chain {0}",
                    e.chain_id)
            },

        TimestampOverflow
            [ DetailOnly<TimestampOverflowError> ]
            |_| { "timestamp overflow" },

        TxResponse
            { event: String }
            |e| {
                format!("tx response event consists of an error: {}",
                    e.event)
            },

        UnexpectedEvent
            { event: IbcEvent }
            |e| {
                format!("internal error, expected IBCEvent::ChainError, got {:?}",
                    e.event)
            },

        TokenTransfer
            [ Ics20Error ]
            |_| { "token transfer error" },

        ZeroTimeout
            | _ | { "packet timeout height and packet timeout timestamp cannot both be 0" },
    }
}

#[derive(Copy, Clone)]
pub struct TransferTimeout {
    pub timeout_height: TimeoutHeight,
    pub timeout_timestamp: Timestamp,
}

impl TransferTimeout {
    /**
       Construct the transfer timeout parameters from the given timeout
       height offset, timeout duration, and the latest chain status
       containing the latest time of the destination chain.

       The height offset and duration are optional, with zero indicating
       that the packet do not get expired at the given height or time.
       If both height offset and duration are zero, then the packet will
       never expire.
    */
    pub fn new(
        timeout_height_offset: u64,
        timeout_duration: Duration,
        destination_chain_status: &ChainStatus,
    ) -> Result<Self, TransferError> {
        let timeout_height = if timeout_height_offset == 0 {
            TimeoutHeight::no_timeout()
        } else {
            destination_chain_status
                .height
                .add(timeout_height_offset)
                .into()
        };

        let timeout_timestamp = if timeout_duration == Duration::ZERO {
            Timestamp::none()
        } else {
            (destination_chain_status.timestamp + timeout_duration)
                .map_err(TransferError::timestamp_overflow)?
        };

        Ok(TransferTimeout {
            timeout_height,
            timeout_timestamp,
        })
    }
}

#[derive(Clone, Debug)]
pub struct TransferOptions {
    pub src_port_id: PortId,
    pub src_channel_id: ChannelId,
    pub amount: Amount,
    pub denom: String,
    pub receiver: Option<String>,
    pub timeout_height_offset: u64,
    pub timeout_duration: Duration,
    pub number_msgs: usize,
    pub memo: Option<String>,
}

pub fn build_transfer_message(
    src_port_id: PortId,
    src_channel_id: ChannelId,
    amount: Amount,
    denom: String,
    sender: Signer,
    receiver: Signer,
    timeout_height: TimeoutHeight,
    timeout_timestamp: Timestamp,
    memo: Option<String>,
) -> Any {
    let msg = MsgTransfer {
        source_port: src_port_id,
        source_channel: src_channel_id,
        token: Coin {
            denom,
            amount: amount.to_string(),
        },
        sender,
        receiver,
        timeout_height,
        timeout_timestamp,
        memo,
    };

    msg.to_any()
}

pub fn build_transfer_messages<SrcChain: ChainHandle, DstChain: ChainHandle>(
    src_chain: &SrcChain, // the chain whose account is debited
    dst_chain: &DstChain, // the chain whose account eventually gets credited
    opts: &TransferOptions,
) -> Result<Vec<Any>, TransferError> {
    let receiver = match &opts.receiver {
        Some(receiver) => Signer::from_str(receiver).map_err(TransferError::receiver_address)?,
        None => dst_chain.get_signer().map_err(TransferError::key)?,
    };

    let sender = src_chain.get_signer().map_err(TransferError::key)?;

    let destination_chain_status = dst_chain
        .query_application_status()
        .map_err(TransferError::relayer)?;

    let timeout = TransferTimeout::new(
        opts.timeout_height_offset,
        opts.timeout_duration,
        &destination_chain_status,
    )?;

    let message = build_transfer_message(
        opts.src_port_id.clone(),
        opts.src_channel_id.clone(),
        opts.amount,
        opts.denom.clone(),
        sender,
        receiver,
        timeout.timeout_height,
        timeout.timeout_timestamp,
        opts.memo.clone(),
    );

    let msgs = vec![message; opts.number_msgs];

    Ok(msgs)
}

pub fn send_messages<Chain: ChainHandle>(
    chain: &Chain,
    msgs: Vec<Any>,
) -> Result<Vec<IbcEventWithHeight>, TransferError> {
    let events_with_heights = chain
        .send_messages_and_wait_commit(TrackedMsgs::new_static(msgs, "ft-transfer"))
        .map_err(|e| TransferError::submit(chain.id(), e))?;

    // Check if the chain rejected the transaction
    let result = events_with_heights
        .iter()
        .find(|event| matches!(event.event, IbcEvent::ChainError(_)));

    match result {
        None => Ok(events_with_heights),
        Some(err) => {
            if let IbcEvent::ChainError(ref err) = err.event {
                Err(TransferError::tx_response(err.clone()))
            } else {
                panic!("internal error, expected IBCEvent::ChainError, got {err:?}")
            }
        }
    }
}

pub fn build_and_send_transfer_messages<SrcChain: ChainHandle, DstChain: ChainHandle>(
    // the chain whose account is debited
    src_chain: &SrcChain,
    // the chain whose account eventually gets credited
    dst_chain: &DstChain,
    // options describing the transfer
    opts: &TransferOptions,
) -> Result<Vec<IbcEventWithHeight>, TransferError> {
    let msgs = build_transfer_messages(src_chain, dst_chain, opts)?;
    send_messages(src_chain, msgs)
}