# Simple Abstract Messenger Example

{% hint style="info" %}
If you have any questions or need assistance, please feel free to use our [contact form](https://entangle.zendesk.com/hc/en-us/requests/new) to connect with an expert on our team.
{% endhint %}

An effective implementation of blockchain interoperability is sending cross-chain messages. Traditionally, without UIP, this would be a challenging task. However, UIP makes this an easy and safe process. This guide walks you through deploying a simple dApp that utilizes UIP to send cross-chain text messages between [Solana](https://solana.com/) and other networks.

For this guide, we won't need to worry about fees as we will deploy the smart contracts on devnet, which is the commonly used Solana testnet network.

## Prerequisites

Before you begin, you'll need to ensure you're familiar with the concepts and have the tools we'll be using for this guide. If you're unsure about anything, we recommend reading more via the provided links.&#x20;

* The [Rust](https://www.rust-lang.org/) programming language.
* The [Anchor](https://www.anchor-lang.com/docs) development environment.
* The [Bun](https://bun.sh/) toolkit.

We will be using the following native token:

* [Solana SOL](https://faucet.solana.com/)

Click on the token to be taken to its public faucet and complete the steps to acquire the necessary funds in your wallet.

## Environment Setup

To begin with, let's setup the working environment.

{% stepper %}
{% step %}
Clone the example project from GitHub.

```bash
git clone https://github.com/Entangle-Protocol/UIP-examples.git
```

{% endstep %}

{% step %}
Change your directory to the project folder.

```bash
cd UIP-examples/other/Messenger/Solana
```

{% endstep %}

{% step %}
Install the JS dependencies.

```bash
bun install
```

{% endstep %}
{% endstepper %}

## Set the Program Address

To deploy your own instance of the messenger, you need to generate a custom keypair for it and put it in `target/deploy`folder.

Run the below command to generate your keypair.

```sh
solana-keygen grind --starts-with mes:1 --ignore-case
```

It will generate a file called `<your program address>.json`, where `<your program address>` is the resulting public key. Move it to the `target/deploy`folder using the below command.

```sh
mv <your program address>.json target/deploy/messenger-keypair.json
```

Next, replace the program address in `programs/messenger/src/lib.rs` and\
`Anchor.toml` with the public key of the generated keypair.

## Whitelist Configuration

The Solana Messenger example employs a whitelist to only allow messages from specific contracts. For messages to be accepted, you will need to specify all the contracts from other chains that you plan on communicating with.

First, put the addresses of the deployed EVM contracts in `programs/messenger/src/addresses.rs`. They have to be padded with 12 zero bytes, as the EVM addresses undergo encoding to bytes.

If you want to change the chains you want to send messages to or chains you want to receive messages from, make changes to `programs/messenger/src/instructions/send_message.rs` or `programs/messenger/src/instructions/execute.rs` accordingly.

Alternatively, you can redesign the contract to add the allowed origins and destinations dynamically.

## Deploying Contracts

Build the Solana program by running the below command.

```sh
anchor build
```

If you are planning to deploy to mainnet, run the following command.

```sh
anchor build --features mainnet
```

After that, you can deploy your program by running the below command.

```sh
solana program deploy --url devnet \
  --upgrade-authority ~/.config/solana/id.json \
  --program-id target/deploy/messenger-keypair.json target/deploy/messenger.so
```

### Building the TypeScript SDK <a href="#registering-the-extension" id="registering-the-extension"></a>

To run the client-side scripts, make sure to build the TypeScript SDK after compiling the program.

```sh
cp target/idl/messenger.json target/types/messenger.ts ts-sdk/src/idl/
bun run build:sdk
```

### Registering the Extension <a href="#registering-the-extension" id="registering-the-extension"></a>

Now, you need to register the protocol extension. Extensions exist to let the UIP transmitters know how to interact with your program. In essence, they are WebAssembly executables that are shared by uploading to the IPFS network. Every Solana contract connected to UIP needs to register its extension to start receiving messages.

Run the command below to compile the messenger extension.

```sh
rustup target add wasm32-wasip1
cargo build --target wasm32-wasip1 --release -p messenger-extension
```

The generated `target/wasm32-wasip1/release/messenger_extension.wasm` file needs to be uploaded to IPFS. It can be done using a service such as [Pinata](https://pinata.cloud/) (it’s free). Upon uploading, copy the CID of the extension file.

You can run the initialization script to set up the messenger config and to register the extension.

```sh
anchor run initialize --provider.cluster devnet -- \
  --extension <extension CID>
```

## Sending and Receiving Messages

In this section, we'll take a look at sending and receiving messages.

### Sending Messages

To send a message from the Solana contract to another chain, use the `send-message` script.

```sh
anchor run send-message --provider.cluster devnet -- \
  --times 1 \
  --dst-chain base-sepolia \
  --fee 100000 \
  --custom-gas-limit 300000 \
  --text "Hi from Solana"
```

Here, we are sending one message, choosing the destination to be Base Sepolia, paying 0.0001 SOL for fees, and setting the gas limit for execution on target chain to be 300k.

### Receiving Messages

To display all the messages sent from a wallet, copy the command below, update the sender address with the EVM address and run it.

{% code overflow="wrap" %}

```sh
anchor run get-messages-by-sender --provider.cluster devnet -- <your sender wallet address>
```

{% endcode %}

## Conclusion

In this guide we created a dApp utilising SOL contracts to facilitate in cross-chain text messaging.


---

# Agent Instructions: 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:

```
GET https://docs.entangle.fi/universal-interoperability-protocol/developer-guides/solana/simple-abstract-messenger-example.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
