-
Notifications
You must be signed in to change notification settings - Fork 80
/
send_transfer.rs
193 lines (169 loc) · 6.26 KB
/
send_transfer.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
use crate::applications::transfer::context::{
TokenTransferExecutionContext, TokenTransferValidationContext,
};
use crate::applications::transfer::error::TokenTransferError;
use crate::applications::transfer::events::TransferEvent;
use crate::applications::transfer::msgs::transfer::MsgTransfer;
use crate::applications::transfer::packet::PacketData;
use crate::applications::transfer::{is_sender_chain_source, Coin, PrefixedCoin};
use crate::core::ics04_channel::handler::send_packet::{send_packet_execute, send_packet_validate};
use crate::core::ics04_channel::packet::Packet;
use crate::core::ics24_host::path::{ChannelEndPath, SeqSendPath};
use crate::events::ModuleEvent;
use crate::prelude::*;
/// This function handles the transfer sending logic.
/// If this method returns an error, the runtime is expected to rollback all state modifications to
/// the `Ctx` caused by all messages from the transaction that this `msg` is a part of.
pub fn send_transfer<C, Ctx>(ctx_a: &mut Ctx, msg: MsgTransfer<C>) -> Result<(), TokenTransferError>
where
C: TryInto<PrefixedCoin> + Clone,
Ctx: TokenTransferExecutionContext,
{
send_transfer_validate(ctx_a, msg.clone())?;
send_transfer_execute(ctx_a, msg)
}
pub fn send_transfer_validate<C, Ctx>(
ctx_a: &Ctx,
msg: MsgTransfer<C>,
) -> Result<(), TokenTransferError>
where
C: TryInto<PrefixedCoin>,
Ctx: TokenTransferValidationContext,
{
ctx_a.can_send_coins()?;
let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let chan_end_on_a = ctx_a
.channel_end(&chan_end_path_on_a)
.map_err(TokenTransferError::ContextError)?;
let port_id_on_b = chan_end_on_a.counterparty().port_id().clone();
let chan_id_on_b = chan_end_on_a
.counterparty()
.channel_id()
.ok_or_else(|| TokenTransferError::DestinationChannelNotFound {
port_id: msg.port_id_on_a.clone(),
channel_id: msg.chan_id_on_a.clone(),
})?
.clone();
let seq_send_path_on_a = SeqSendPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let sequence = ctx_a
.get_next_sequence_send(&seq_send_path_on_a)
.map_err(TokenTransferError::ContextError)?;
let token = msg
.token
.try_into()
.map_err(|_| TokenTransferError::InvalidToken)?;
let denom = token.denom.clone();
let coin = Coin {
denom: denom.clone(),
amount: token.amount,
};
let sender: Ctx::AccountId = msg
.sender
.clone()
.try_into()
.map_err(|_| TokenTransferError::ParseAccountFailure)?;
if is_sender_chain_source(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone(), &denom) {
let escrow_address = ctx_a.get_escrow_account(&msg.port_id_on_a, &msg.chan_id_on_a)?;
ctx_a.send_coins_validate(&sender, &escrow_address, &coin)?;
} else {
ctx_a.burn_coins_validate(&sender, &coin)?;
}
let data = {
let data = PacketData {
token: coin,
sender: msg.sender.clone(),
receiver: msg.receiver.clone(),
};
serde_json::to_vec(&data).expect("PacketData's infallible Serialize impl failed")
};
let packet = Packet {
seq_on_a: sequence,
port_id_on_a: msg.port_id_on_a,
chan_id_on_a: msg.chan_id_on_a,
port_id_on_b,
chan_id_on_b,
data,
timeout_height_on_b: msg.timeout_height_on_b,
timeout_timestamp_on_b: msg.timeout_timestamp_on_b,
};
send_packet_validate(ctx_a, &packet).map_err(TokenTransferError::ContextError)?;
Ok(())
}
pub fn send_transfer_execute<C, Ctx>(
ctx_a: &mut Ctx,
msg: MsgTransfer<C>,
) -> Result<(), TokenTransferError>
where
C: TryInto<PrefixedCoin>,
Ctx: TokenTransferExecutionContext,
{
let chan_end_path_on_a = ChannelEndPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let chan_end_on_a = ctx_a
.channel_end(&chan_end_path_on_a)
.map_err(TokenTransferError::ContextError)?;
let port_on_b = chan_end_on_a.counterparty().port_id().clone();
let chan_on_b = chan_end_on_a
.counterparty()
.channel_id()
.ok_or_else(|| TokenTransferError::DestinationChannelNotFound {
port_id: msg.port_id_on_a.clone(),
channel_id: msg.chan_id_on_a.clone(),
})?
.clone();
// get the next sequence
let seq_send_path_on_a = SeqSendPath::new(&msg.port_id_on_a, &msg.chan_id_on_a);
let sequence = ctx_a
.get_next_sequence_send(&seq_send_path_on_a)
.map_err(TokenTransferError::ContextError)?;
let token = msg
.token
.try_into()
.map_err(|_| TokenTransferError::InvalidToken)?;
let denom = token.denom.clone();
let coin = Coin {
denom: denom.clone(),
amount: token.amount,
};
let sender = msg
.sender
.clone()
.try_into()
.map_err(|_| TokenTransferError::ParseAccountFailure)?;
if is_sender_chain_source(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone(), &denom) {
let escrow_address = ctx_a.get_escrow_account(&msg.port_id_on_a, &msg.chan_id_on_a)?;
ctx_a.send_coins_execute(&sender, &escrow_address, &coin)?;
} else {
ctx_a.burn_coins_execute(&sender, &coin)?;
}
let data = {
let data = PacketData {
token: coin,
sender: msg.sender.clone(),
receiver: msg.receiver.clone(),
};
serde_json::to_vec(&data).expect("PacketData's infallible Serialize impl failed")
};
let packet = Packet {
seq_on_a: sequence,
port_id_on_a: msg.port_id_on_a,
chan_id_on_a: msg.chan_id_on_a,
port_id_on_b: port_on_b,
chan_id_on_b: chan_on_b,
data,
timeout_height_on_b: msg.timeout_height_on_b,
timeout_timestamp_on_b: msg.timeout_timestamp_on_b,
};
send_packet_execute(ctx_a, packet).map_err(TokenTransferError::ContextError)?;
{
ctx_a.log_message(format!(
"IBC fungible token transfer: {} --({})--> {}",
msg.sender, token, msg.receiver
));
let transfer_event = TransferEvent {
sender: msg.sender,
receiver: msg.receiver,
};
ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into());
}
Ok(())
}