|
| 1 | +# Transfer Funds Custom Query |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Transfer Funds Custom Query is a feature within the IBC Hooks module that provides Move smart contracts with access to IBC transfer packet information through a custom query interface. This enables contracts to retrieve details about the actual amount transferred and balance changes that occurred during IBC transfers. |
| 6 | + |
| 7 | +## Purpose |
| 8 | + |
| 9 | +The custom query system provides Move contracts with real-time access to IBC transfer data by: |
| 10 | + |
| 11 | +1. Capturing transfer information during IBC packet processing |
| 12 | +2. Exposing this data through a dedicated custom query interface |
| 13 | +3. Enabling contracts to make informed decisions based on actual transfer amounts |
| 14 | +4. Automatically managing data lifecycle (set, query, clear) |
| 15 | + |
| 16 | +## Architecture |
| 17 | + |
| 18 | +### Data Flow |
| 19 | + |
| 20 | +```plaintext |
| 21 | +IBC Transfer Packet Received |
| 22 | + ↓ |
| 23 | +Record Balance Before Transfer |
| 24 | + ↓ |
| 25 | +Execute Underlying Transfer Logic |
| 26 | + ↓ |
| 27 | +Record Balance After Transfer |
| 28 | + ↓ |
| 29 | +Calculate Balance Change |
| 30 | + ↓ |
| 31 | +Store TransferFunds Data |
| 32 | + ↓ |
| 33 | +Execute Move Contract Hook |
| 34 | + ↓ |
| 35 | +Contract Queries TransferFunds via Custom Query |
| 36 | + ↓ |
| 37 | +Contract Performs Custom Logic |
| 38 | + ↓ |
| 39 | +Clear TransferFunds Data |
| 40 | +``` |
| 41 | + |
| 42 | +### Data Structure |
| 43 | + |
| 44 | +The `TransferFunds` struct contains: |
| 45 | + |
| 46 | +```go |
| 47 | +type TransferFunds struct { |
| 48 | + BalanceChange types.Coin `json:"balance_change"` // Actual balance change |
| 49 | + AmountInPacket types.Coin `json:"amount_in_packet"` // Amount specified in packet |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +- **BalanceChange**: The actual change in balance that occurred during the transfer |
| 54 | +- **AmountInPacket**: The amount that was specified in the original IBC packet |
| 55 | + |
| 56 | +## Implementation Details |
| 57 | + |
| 58 | +### Storage |
| 59 | + |
| 60 | +The transfer funds data is stored in a transient collection, meaning it's only available for the duration of the current transaction and is automatically cleared afterward. This ensures data is only accessible during hook execution. |
| 61 | + |
| 62 | +```go |
| 63 | +transferFunds collections.Item[types.TransferFunds] |
| 64 | +``` |
| 65 | + |
| 66 | +## Query Interface |
| 67 | + |
| 68 | +### Custom Query Name |
| 69 | + |
| 70 | +```plantext |
| 71 | +move_hook_get_transfer_funds |
| 72 | +``` |
| 73 | + |
| 74 | +### Query Parameters |
| 75 | + |
| 76 | +- **Input**: Empty byte array (`[]byte{}`) |
| 77 | +- **Output**: JSON-encoded `TransferFunds` or `null` (`0x1::option::Option<TransferFunds>`) |
| 78 | + |
| 79 | +### Response Format |
| 80 | + |
| 81 | +#### When data is available |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "balance_change": { |
| 86 | + "denom": "uinit", |
| 87 | + "amount": "1000000" |
| 88 | + }, |
| 89 | + "amount_in_packet": { |
| 90 | + "denom": "uinit", |
| 91 | + "amount": "1000000" |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### When no data is available |
| 97 | + |
| 98 | +```json |
| 99 | +null |
| 100 | +``` |
| 101 | + |
| 102 | +## Move Contract Integration |
| 103 | + |
| 104 | +### Example Contract Usage |
| 105 | + |
| 106 | +```move |
| 107 | +module std::hook_sender { |
| 108 | + use initia_std::coin; |
| 109 | + use initia_std::query; |
| 110 | + use initia_std::string::String; |
| 111 | + use initia_std::json; |
| 112 | + use initia_std::option::{Self, Option}; |
| 113 | +
|
| 114 | + struct TransferFunds has copy, drop { |
| 115 | + balance_change: Coin, |
| 116 | + amount_in_packet: Coin, |
| 117 | + } |
| 118 | +
|
| 119 | + struct Coin has copy, drop { |
| 120 | + denom: String, |
| 121 | + amount: u64, |
| 122 | + } |
| 123 | +
|
| 124 | + public entry fun send_funds(sender: &signer, receiver: address) { |
| 125 | + // Execute custom query to get transfer funds data |
| 126 | + let response = query::query_custom(b"move_hook_get_transfer_funds", b""); |
| 127 | + let res = json::unmarshal<Option<TransferFunds>>(response); |
| 128 | + |
| 129 | + // Ensure data is available |
| 130 | + assert!(option::is_some(&res), 1000); |
| 131 | +
|
| 132 | + // Extract the transfer funds data |
| 133 | + let res = option::borrow(&res); |
| 134 | + |
| 135 | + // Get coin metadata for the balance change denom |
| 136 | + let coin_metadata = coin::denom_to_metadata(res.balance_change.denom); |
| 137 | + |
| 138 | + // Transfer the actual balance change amount |
| 139 | + coin::transfer(sender, receiver, coin_metadata, res.balance_change.amount); |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Key Points for Move Contracts |
| 145 | + |
| 146 | +1. **Query Name**: Use `b"move_hook_get_transfer_funds"` as the custom query name |
| 147 | +2. **Empty Parameters**: Pass empty byte array `b""` as parameters |
| 148 | +3. **Optional Response**: The response is wrapped in an `Option<TransferFunds>` |
| 149 | +4. **Null Handling**: Check if the option contains data before using it |
| 150 | +5. **Data Availability**: Data is only available during hook execution |
0 commit comments