|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +title: Fee transfers |
| 4 | +description: Understanding fee transfer transactions in the Mina protocol |
| 5 | +slug: /developers/transactions/fee-transfers |
| 6 | +--- |
| 7 | + |
| 8 | +# Fee transfers |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +Fee transfers are protocol-generated transactions that distribute collected |
| 13 | +transaction fees to block producers. Unlike user commands, fee transfers are |
| 14 | +created automatically during block production and do not require user |
| 15 | +signatures. |
| 16 | + |
| 17 | +A fee transfer consists of: |
| 18 | + |
| 19 | +- **Receiver(s)**: One or two block producers receiving fees |
| 20 | +- **Fee amount(s)**: The fee amount(s) to distribute |
| 21 | +- **Token**: Must be the default token (MINA) |
| 22 | + |
| 23 | +## Transaction structure |
| 24 | + |
| 25 | +Fee transfers use a specialized structure that supports one or two receivers: |
| 26 | + |
| 27 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L357-L357 --> |
| 28 | + |
| 29 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 30 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L357-L357 |
| 31 | +``` |
| 32 | + |
| 33 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L330-L334 --> |
| 34 | + |
| 35 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 36 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L330-L334 |
| 37 | +``` |
| 38 | + |
| 39 | +### Creating fee transfers |
| 40 | + |
| 41 | +Fee transfers are constructed using the `of_singles` method which validates |
| 42 | +token compatibility: |
| 43 | + |
| 44 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L396-L412 --> |
| 45 | + |
| 46 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 47 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L396-L412 |
| 48 | +``` |
| 49 | + |
| 50 | +## Transaction application |
| 51 | + |
| 52 | +### First pass |
| 53 | + |
| 54 | +During the first pass (`apply_transaction_first_pass`), the fee transfer: |
| 55 | + |
| 56 | +1. **Validates token compatibility** |
| 57 | + - All fees must be in the default token (MINA) |
| 58 | + - Rejects non-default tokens |
| 59 | + |
| 60 | +2. **Processes single or double transfers** |
| 61 | + - For single transfers: adds fee to receiver's balance |
| 62 | + - For double transfers: adds respective fees to both receivers |
| 63 | + - Creates receiver accounts if they don't exist |
| 64 | + |
| 65 | +3. **Deducts account creation fees** |
| 66 | + - If receiver account doesn't exist, charges account creation fee |
| 67 | + - Receiver gets: `fee_amount - account_creation_fee` |
| 68 | + |
| 69 | +**Implementation:** |
| 70 | +`ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs` |
| 71 | + |
| 72 | +### No fee payment |
| 73 | + |
| 74 | +Fee transfers do not pay fees themselves - they are protocol transactions: |
| 75 | + |
| 76 | +- No nonce increment |
| 77 | +- No fee payer |
| 78 | +- No signature required |
| 79 | +- Cannot be submitted by users |
| 80 | + |
| 81 | +## Account creation |
| 82 | + |
| 83 | +If a receiver account doesn't exist, it is created automatically. The account |
| 84 | +creation fee (1 MINA by default) is deducted from the fee being transferred. |
| 85 | + |
| 86 | +## Single vs. double transfers |
| 87 | + |
| 88 | +### Single transfer |
| 89 | + |
| 90 | +Distributes fees to one block producer. See |
| 91 | +[test_apply_single_fee_transfer_success](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L94-L95) |
| 92 | +for an example: |
| 93 | + |
| 94 | +<!-- CODE_REFERENCE: ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L94-L95 --> |
| 95 | + |
| 96 | +```rust reference title="ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs" |
| 97 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L94-L95 |
| 98 | +``` |
| 99 | + |
| 100 | +### Double transfer |
| 101 | + |
| 102 | +Distributes fees to two block producers (e.g., coinbase producer and snark work |
| 103 | +producer). See |
| 104 | +[test_apply_double_fee_transfer_success](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L169-L174) |
| 105 | +for an example: |
| 106 | + |
| 107 | +<!-- CODE_REFERENCE: ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L169-L174 --> |
| 108 | + |
| 109 | +```rust reference title="ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs" |
| 110 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L169-L174 |
| 111 | +``` |
| 112 | + |
| 113 | +## Token constraints |
| 114 | + |
| 115 | +Fee transfers must use the default token. For double transfers, both tokens must |
| 116 | +match. The validation is implemented in |
| 117 | +[FeeTransfer::of_singles](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L400): |
| 118 | + |
| 119 | +<!-- CODE_REFERENCE: ledger/src/scan_state/transaction_logic/mod.rs#L400-L408 --> |
| 120 | + |
| 121 | +```rust reference title="ledger/src/scan_state/transaction_logic/mod.rs" |
| 122 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs#L400-L408 |
| 123 | +``` |
| 124 | + |
| 125 | +See |
| 126 | +[test_apply_fee_transfer_incompatible_tokens](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L333-L337) |
| 127 | +for an example creating fee transfers with incompatible tokens: |
| 128 | + |
| 129 | +<!-- CODE_REFERENCE: ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L333-L337 --> |
| 130 | + |
| 131 | +```rust reference title="ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs" |
| 132 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L333-L337 |
| 133 | +``` |
| 134 | + |
| 135 | +The validation fails with an error: |
| 136 | + |
| 137 | +<!-- CODE_REFERENCE: ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L344-L346 --> |
| 138 | + |
| 139 | +```rust reference title="ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs" |
| 140 | +https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L344-L346 |
| 141 | +``` |
| 142 | + |
| 143 | +## Testing |
| 144 | + |
| 145 | +Comprehensive tests are available in |
| 146 | +[`ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs): |
| 147 | + |
| 148 | +- [`test_apply_single_fee_transfer_success`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L75) - |
| 149 | + Single fee transfer to existing account |
| 150 | +- [`test_apply_double_fee_transfer_success`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L137) - |
| 151 | + Double fee transfer to two receivers |
| 152 | +- [`test_apply_fee_transfer_creates_account`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L237) - |
| 153 | + Fee transfer creating new account |
| 154 | +- [`test_apply_fee_transfer_incompatible_tokens`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs#L316) - |
| 155 | + Token compatibility validation |
| 156 | + |
| 157 | +## Related files |
| 158 | + |
| 159 | +- [`ledger/src/scan_state/transaction_logic/mod.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/mod.rs) - |
| 160 | + Fee transfer type definitions |
| 161 | +- [`ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/src/scan_state/transaction_logic/transaction_partially_applied.rs) - |
| 162 | + Fee transfer application logic |
| 163 | +- [`ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs`](https://github.com/o1-labs/mina-rust/blob/develop/ledger/tests/test_transaction_logic_first_pass_fee_transfer.rs) - |
| 164 | + Fee transfer tests |
0 commit comments