# Fee Delegation

Fee Delegation is a mechanism applied to Applepie HF in WEMIX3.0. This is a feature that FeePayer pays a fee for the transaction that Sender wants to execute instead of Sender.

Fee Delegation Transaction is accomplished by sending the existing Transaction signed by the Sender, including the FeePayer information, by adding the FeePayer signed signature information.

<figure><img src="https://content.gitbook.com/content/Hf9f4FuSvei1yoMuW1wT/blobs/URTfBtEri1Ti8n6qDMEe/Frame%202.png" alt=""><figcaption><p>Fee Delegation</p></figcaption></figure>

#### Fee Delegation Transaction

{% code overflow="wrap" %}

```
DynamicFeeTxType Transaction(with signature of Sender) + FeePayer address + signature of FeePayer
```

{% endcode %}

Fee Delegation Transaction only supports  DynamicFeeTxType among existing Transaction and does not support LegacyTxType or AccessListTxType.

<figure><img src="https://content.gitbook.com/content/Hf9f4FuSvei1yoMuW1wT/blobs/44f5D05LnT7s9l21x3hI/9.png" alt=""><figcaption></figcaption></figure>

Important changes to implementation of Fee Delegation Transaction are as follows, and please refer to the open source for further information.

#### Fee Delegation Transaction Type (transaction.go)

```go
const (
  LegacyTxType = iota
  AccessListTxType
  DynamicFeeTxType
  FeeDelegateDynamicFeeTxType = 22 //fee delegation
)
```

#### Fee Delegation Transaction Structure(transaction.go)

```go
type FeeDelegateDynamicFeeTx struct {
  SenderTx DynamicFeeTx
  FeePayer *common.Address rlp:"nil"

  // Signature values
  FV *big.Int json:"fv" gencodec:"required" // feePayer V
  FR *big.Int json:"fr" gencodec:"required" // feePayer R
  FS *big.Int json:"fs" gencodec:"required" // feePayer S
}
```

#### Fee Delegation Transaction Hash Function for Signature(transaction\_signing.go)

```go
func (s feeDelegateSigner) Hash(tx *Transaction) common.Hash {
  senderV, senderR, senderS := tx.RawSignatureValues()
  return prefixedRlpHash(
    tx.Type(),
    []interface{}{
      []interface{}{
        s.chainId,
        tx.Nonce(),
        tx.GasTipCap(),
        tx.GasFeeCap(),
        tx.Gas(),
        tx.To(),
        tx.Value(),
        tx.Data(),
        tx.AccessList(),
        senderV,
        senderR,
        senderS,
      },
      tx.FeePayer(),
    })
}
```

{% hint style="danger" %}
**Caution**

Fee Delegation Transaction pays fees according to the fee settings (gas, maxFeePerGas, maxPriorityFeePerGas) of DynamicFeeTxType Transaction generated by the Sender. Therefore, FeePayer must have a procedure to verify that the transaction fee is appropriate.
{% endhint %}

{% hint style="danger" %}
**Caution**

It is important to note that the nonce of the transaction requested by the user is greater than the nonce of the transaction requested by the current sender, because Fee Delegation Transaction signed by FeePayer may not be used immediately, depending on the nonce of the sender.
{% endhint %}
