> For the complete documentation index, see [llms.txt](https://docs.yarchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yarchain.org/dev-docs/hello-world.md).

# Hello world!

{% hint style="info" %}
More examples can be found here: [https://github.com/YARlabs/YAR-v2-contracts/](https://github.com/YARlabs/YAR-v2-contracts/tree/main)
{% endhint %}

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.20;

import { YarLib } from "../../YarLib.sol";
import { YarRequest } from "../../YarRequest.sol";
import { YarResponse } from "../../YarResponse.sol";

contract YarBridgeMessage {
    address public owner;
    uint256 public chainId;
     
    address public yarRequest;
    address public yarResponse;
    
    mapping(uint256 chainId => address peer) public peers;
    function setPeer(uint256 newChainId, address newPeer) external {
        require(msg.sender == owner, "only owner!");
        peers[newChainId] = newPeer;
    }

    function getPeer(uint256 _chainId) public view returns (address) {
        address peer = peers[_chainId];
        return peer == address(0) ? address(this) : peer;
    }
 
    constructor(
        address intialYarRequest,
        address intialYarResponse
    )
    {
        yarRequest = intialYarRequest;
        yarResponse = intialYarResponse;
        chainId = block.chainid;
        owner = msg.sender;
    }
 
    function sendTo(
        uint256 targetChainId,
        address receiver,
        string memory message
    )
    
    external returns (YarLib.YarTX memory) {
        bytes memory targetTx = abi.encodeWithSelector(
            YarBridgeMessage.sendFrom.selector,
            msg.sender,
            receiver,
            message
        );

        YarLib.YarTX memory yarTx = YarLib.YarTX(
            chainId,
            address(this),
            msg.sender,
            targetChainId,
            getPeer(targetChainId),
            0,
            targetTx,
            0
        );

        YarRequest(yarRequest).send(yarTx);

        return yarTx;
    }

    struct Message {
        address sender;
        address receiver;
        string message;
        uint256 timestamp;
    }
 
    Message[] public messages;
    uint256 public messageCount;
 
    function getMessages(address receiver, address sender, uint offset, uint limit) public view returns (Message[] memory) {
        require(offset < messageCount, "Offset out of range");
        
        uint end = offset + limit;
        if (end > messageCount) {
            end = messageCount;
        }

        uint resultSize = end - offset;
        Message[] memory result = new Message[](resultSize);

        uint index = 0;
        for (uint i = messageCount - offset; i > messageCount - end; i--) {
            if (messages[i - 1].receiver == receiver) {
                result[index] = messages[i - 1];
                index++;
            } else if (messages[i - 1].sender == sender) {
                result[index] = messages[i - 1];
                index++;
            }
        }

        return result;
    }
 
    function sendFrom(
        address sender,
        address receiver,
        string calldata message
    ) external {
        require(msg.sender == yarResponse, "Only YarResponse!");

        YarLib.YarTX memory trustedYarTx = YarResponse(yarResponse).trustedYarTx();
        require(getPeer(trustedYarTx.initialChainId) == trustedYarTx.sender, "not peer!");

        messages.push(Message(
            sender,
            receiver,
            message,
            block.timestamp
        ));
        messageCount++;
    }
}
```


---

# 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.yarchain.org/dev-docs/hello-world.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.
