> For the complete documentation index, see [llms.txt](https://docs.multichain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.multichain.org/developer-guide/anycall-v6/anyfallback.md).

# anyFallback

If the `anyExec` contract execution failed on Chain B, it can call `_fallback` function to send messages back to Chain A.&#x20;

&#x20;`anyExecute` <mark style="color:red;">**DAPP receiver contract(Chain B failed)**</mark>**&#x20;-> anyCall**(Chain B) **->** *SMPC Network* **-> anyExec**(Chain A) **->** `anyExecute` **(**<mark style="color:green;">**DAPP sender contract(Chain A)**</mark>**)**

**->**`anyFallback`

**Format of the fallback function needs to be**:&#x20;

`function anyFallback(address _to, bytes calldata _data) external;`

### Logic

When `anyExecute` fails on Chain B, the anycall contract would issue another call back to chain A with data including the `anyFallback` selector.

Hence the `anyExecute` execute normal function or fallback function based on the selector.

```
emit LogAnyCall(
                _from,
                _fallback,
                abi.encodeWithSelector(IApp.anyFallback.selector, _to, _data),
                address(0),
                _ctx.fromChainID,
                0, // pay fee on dest chain
                _appID,
                nonce);
```

### Example

Example Contract Code:<https://ftmscan.com/address/0x2b6fbe2a08491130dcc2fe13f0e60b3540d92a55#code>

1. Chain A First call: <https://ftmscan.com/tx/0xE45DCF2F196982D5F2B4F84AD0926B9D1A439F62B9E950AE5BBA0460B775C389>

&#x20; 2\. Chain B issued callback: <https://bscscan.com/tx/0x3ccb1f91ad66348ec5dd8676d1dd59372fd37bca4e202982b582cd8d4e5c582c#eventlog>

&#x20; 3\. anyFallback executing: <https://ftmscan.com/tx/0x215002fb282c630dada2ebeb56ea605ae6c24df8a52db46b977e9c081893d061#eventlog>

`anyExecute` function:

When executing, anyExecute first look at the selector of the calldata passed in to determine if it should execute normal flow or fallback function.

```
function anyExecute(bytes calldata data)
        external
        
        returns (bool success, bytes memory result)
    {
        bytes4 selector = bytes4(data[:4]);
        if (selector == this.anyExecute.selector) {
            (
                string memory message
            ) = abi.decode(
                data[4:],
                (string)
            );

            if (compareStrings(message,"fail")){
                return (false, "fail on purpose");
            }

            emit NewMsg(message);
        } else if (selector == this.anyFallback.selector) {

            // original data with selector would be passed here if thats the case
            (address _to, bytes memory _data) = abi.decode(data[4:], (address, bytes));
            this.anyFallback(_to, _data);
        } else {
            return (false, "unknown selector");
        }
        return (true, "");
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.multichain.org/developer-guide/anycall-v6/anyfallback.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
