Encoding the Function Name
Work with multiple functions in a single Cross-Chain dApp
Great work. We can now pack multiple values into a message. In this section, we will learn how to encode the function name and, depending on it, its parameters in the message. Let's imagine a more advanced calculator that not only has a single add function but also a concatenate function, that lets you concatenate two strings.
For the add function we need to encode two numbers. For the concatenate function we need to encode two strings. How can we go about this? The concept is easy: It's like packing an envelope into another envelope:

Ecoding the Function Name and Parameters
The first step is to create a CalculatorAction enum that specifies the different functions that can be called on the calculator.
In the next step we can add this to our encode helpers in the sender contract:
As you can see here we are calling abi.encode twice in the encode helpers. The first time we encode the function parameters and the second time we encode the function name with the byte array containing parameters.
Decode the Function Name and Parameters
Let's now look at the receiver:
You can see that we first decode the CalculatorAction enum:
Then based on the function name we decide how to unpack the parameters
For the add function we decode two numbers and for the concatenate function we decode two strings. After the decoding we call the appropriate internal function.
Try it Out
Deploy the sender and receiver contracts and try out the add and concatenate functions.
Call the Functions
Now you can call the sendAddMessage and sendConcatenateMessage functions on the sender contract and see the results on the receiver contract.
Is this guide helpful?



