ibc_app_transfer/
context.rs

1//! Defines the main context traits and IBC module callbacks
2
3use ibc_app_transfer_types::{Memo, PrefixedCoin, PrefixedDenom};
4use ibc_core::host::types::error::HostError;
5use ibc_core::host::types::identifiers::{ChannelId, PortId};
6use ibc_core::primitives::prelude::*;
7use ibc_core::primitives::Signer;
8
9/// Methods required in token transfer validation, to be implemented by the host
10pub trait TokenTransferValidationContext {
11    /// Native chain account id.
12    type AccountId;
13
14    /// Attempt to convert a [`Signer`] to a native chain sender account.
15    fn sender_account(&self, sender: &Signer) -> Result<Self::AccountId, HostError>;
16
17    /// Attempt to convert a [`Signer`] to a native chain receiver account.
18    fn receiver_account(&self, receiver: &Signer) -> Result<Self::AccountId, HostError>;
19
20    /// get_port returns the portID for the transfer module.
21    fn get_port(&self) -> Result<PortId, HostError>;
22
23    /// Returns Ok() if the host chain supports sending coins.
24    fn can_send_coins(&self) -> Result<(), HostError>;
25
26    /// Returns Ok() if the host chain supports receiving coins.
27    fn can_receive_coins(&self) -> Result<(), HostError>;
28
29    /// Validates that the tokens can be escrowed successfully.
30    ///
31    /// `memo` field allows incorporating additional contextual details in the
32    /// escrow validation.
33    fn escrow_coins_validate(
34        &self,
35        from_account: &Self::AccountId,
36        port_id: &PortId,
37        channel_id: &ChannelId,
38        coin: &PrefixedCoin,
39        memo: &Memo,
40    ) -> Result<(), HostError>;
41
42    /// Validates that the tokens can be unescrowed successfully.
43    fn unescrow_coins_validate(
44        &self,
45        to_account: &Self::AccountId,
46        port_id: &PortId,
47        channel_id: &ChannelId,
48        coin: &PrefixedCoin,
49    ) -> Result<(), HostError>;
50
51    /// Validates the receiver account and the coin input
52    fn mint_coins_validate(
53        &self,
54        account: &Self::AccountId,
55        coin: &PrefixedCoin,
56    ) -> Result<(), HostError>;
57
58    /// Validates the sender account and the coin input before burning.
59    ///
60    /// `memo` field allows incorporating additional contextual details in the
61    /// burn validation.
62    fn burn_coins_validate(
63        &self,
64        account: &Self::AccountId,
65        coin: &PrefixedCoin,
66        memo: &Memo,
67    ) -> Result<(), HostError>;
68
69    /// Returns a hash of the prefixed denom.
70    /// Implement only if the host chain supports hashed denominations.
71    fn denom_hash_string(&self, _denom: &PrefixedDenom) -> Option<String> {
72        None
73    }
74}
75
76/// Methods required in token transfer execution, to be implemented by the host.
77pub trait TokenTransferExecutionContext: TokenTransferValidationContext {
78    /// Executes the escrow of the tokens in a user account.
79    ///
80    /// `memo` field allows incorporating additional contextual details in the
81    /// escrow execution.
82    fn escrow_coins_execute(
83        &mut self,
84        from_account: &Self::AccountId,
85        port_id: &PortId,
86        channel_id: &ChannelId,
87        coin: &PrefixedCoin,
88        memo: &Memo,
89    ) -> Result<(), HostError>;
90
91    /// Executes the unescrow of the tokens in a user account.
92    fn unescrow_coins_execute(
93        &mut self,
94        to_account: &Self::AccountId,
95        port_id: &PortId,
96        channel_id: &ChannelId,
97        coin: &PrefixedCoin,
98    ) -> Result<(), HostError>;
99
100    /// Executes minting of the tokens in a user account.
101    fn mint_coins_execute(
102        &mut self,
103        account: &Self::AccountId,
104        coin: &PrefixedCoin,
105    ) -> Result<(), HostError>;
106
107    /// Executes burning of the tokens in a user account.
108    ///
109    /// `memo` field allows incorporating additional contextual details in the
110    /// burn execution.
111    fn burn_coins_execute(
112        &mut self,
113        account: &Self::AccountId,
114        coin: &PrefixedCoin,
115        memo: &Memo,
116    ) -> Result<(), HostError>;
117}