# OCR Set-up

<mark style="color:red;">**OCR configuration, currently is specific for validators involved in participating in the Oracle price feeds.**</mark> \ <mark style="color:red;">**We will send an announcement to validators while changing the current price feeds to OCR protocol based price feeds.**</mark>

Here are the detailed steps to enable Offchain reporting onto the Plugin Node for Peer.&#x20;

1. To enable OCR in the Plugin environment, set the parameters in the `config.toml` file as shown below. Once the changes are made, restart the plugin using `pm2 restart 0`.<br>

<figure><img src="https://1713380242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl1AVRR4cLSYjVp62RmtY%2Fuploads%2F3nez0gQtI8sH8K6b9Uhj%2FScreenshot%202024-06-03%20at%201.35.40%E2%80%AFPM.png?alt=media&#x26;token=9e166a63-d5a2-4542-9b4a-cea613bcb53c" alt=""><figcaption></figcaption></figure>

```
// OCR parameters in config.toml in addition to Plugin parameters

[P2P]
PeerID = '<peer_ID_as shown in the image>'

[P2P.V1]
Enabled = true

[P2P.V2]
Enabled = true
AnnounceAddresses = ['<Public_IP_address_of_the_server>:5001']
DefaultBootstrappers = ['<bootStrapper_peer_ID>@<bootStrapper_node_IP>:5001']
ListenAddresses = ['<Public_IP_address_of_the_server>:5001']

[OCR]
Enabled = true
KeyBundleID = '<key_bundle_ID_as shown in the image>'

[OCR2]
Enabled = true
KeyBundleID =  '<key_bundle_ID_as shown in the image>'
```

2. To set up the foundry repository pull the code base to your local environment git clone <https://github.com/GoPlugin/foundry.git>
3. Now move into the foundry contracts directory
   * &#x20;"cd foundry/contracts/v0.7.6"[ ](https://github.com/GoPlugin/foundry.git)
4. Invoke remix to deploy Offchainaggregator contract&#x20;
   * "remixd -s . -u <https://remix.xinfin.network>"
5. Once the remix is invoked, open the remix in the browser "<https://remix.xinfin.network>" URL.
6. Now select Offchainaggregator.sol contract as given below.&#x20;
7. Parameters for deploying Offchainaggregator.sol

   i) Constructor params: \
   ii) Decimal: 18

   iii) Billing Access Controller: \<wallet\_address>&#x20;

   iv) Request Access Controller: \<wallet\_address>&#x20;

   v) Min Ans : 0&#x20;

   vi) Max Ans : 99999999999999999999&#x20;

   vii) Validator : 0x0000000000000000000000000000000000000000 \
   viii) PLI : 0x33f4212b027E22aF7e6BA21Fc572843C0D701CD1 \
   ix) Max gas price : 3000&#x20;

   x) Reasonable Gas Price: 75&#x20;

   xi) MicroPliPerEth: 305186108&#x20;

   xii) PliWeiPerObservation : 151513582&#x20;

   xiii) PliWeiPerTransmission : 9090766
8. To get the OS instruction set of server, execute the command "uname -m" in CLI.
9. Now execute the command in foundry directory as given below.

{% code overflow="wrap" fullWidth="true" %}

```sh
external/OCRHelper/ocrhelper-linux-<command_7_output> NODE_ADDRESSES="<node_address_peer1>","<node_address_peer2>","<node_address_peer3>","<node_address_peer4>" OFFCHAIN_PUBLIC_KEYS="<peer1_offchain_public_key>,<peer2_offchain_public_key>,<peer3_offchain_public_key>,<peer4_offchain_public_key>" CONFIG_PUBLIC_KEYS="<peer1_config_public_key>,<peer2_config_public_key>,<peer3_config_public_key>,<peer4_config_public_key>" ONCHAIN_SIGNING_ADDRESSES="<peer1_onchain_sign_addr>,<peer2_onchain_sign_addr>,<peer3_onchain_sign_addr>,<peer4_onchain_sign_addr>" PEER_IDS="<Peer1_ID>,<Peer2_ID>,<Peer3_ID>,<Peer4_ID>"
```

{% endcode %}

```
// SAMPLE OUTPUT of the above command execution
[0x4bEDDe1B6464aC385C32E06Db3E3cD7486636BA7,0x336acE60B9247CC39996f15B2788AD7C9df7b2A1,0xeF53A8245De8169c40a2325Bab7D7369a92Fb4bC,0xae51BD92635FCbD683975aEcB778f2CE7d013FA5] 
[0xe2B935C17089b31c457B80A577338AbeDD5e6206,0xF73A7CD3908700F18e424C327c0301583bcEf41d,0x3CE7D10187eFf99DEbDC0923B2d4F447A5b42eB9,0x20A3beCE1147e33F1932AA12245E50858b261fFB] 
1 
1 
00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000826299e0000000000000000000000000000000000000000000000000000000003f5476a0000000000000000000000000000000000000000000000000000000006fc23ac0000000000000000000000000000000000000000000000000000000002cb4178000000000000000000000000000000000000000000000000000000034630b8a00000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000df847580000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004315e5b0930b10e51c9149013ed68fa88c6bc230048212ccb7066cb02f4525e282d1fb82f0b3b1fa7a32531bac38c5cb53b030711165038b1185f5259d3db678317af622227ec7bf7f4e32c5a2b4161d86147c299750b236043622a83af6901719a829bf2db49f3e215c948b8108e4da83cb61ed5c53483e237175fb57d9ec2a700000000000000000000000000000000000000000000000000000000000000e37032705f313244334b6f6f5748596e694c6b76376f7a32726a3374564c4156657231384b373546356b4e736142506277576d6a6d686158432c7032705f313244334b6f6f57506f716a36486d7a6e5738553657436e4a46486a516774486442594a3352425a4770624b46473662316461652c7032705f313244334b6f6f57536165344b624835374d446e48504b476a656739354e444d593435347273334739376e51474d6335356f396a2c7032705f313244334b6f6f57535756386a74774b363666347a435a4237597179595238734a64674b316e547743354c5347313347353472640000000000000000000000000000000000000000000000000000000000d91977e6e91d96dc1577706e4d6b4232bfdd4cdb5a26e3de7c6ba5ecade1c224bc052e956da259990fa5364def4eb76fcee1374aa863d7244db0fc39dee675870000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000467df5168d0cd9e03571d5afeb312e7df00000000000000000000000000000000a278a323a7bcc33f6dcc64fe7ec960c100000000000000000000000000000000fa7aca070b44c477a2eb93f4f17f134a000000000000000000000000000000005b1e4c4b5cd02a4861b707e23ff2c6b900000000000000000000000000000000
```

10. Get the \[5th] parameter field from the above output as the value of hexString
11. Deploy the cast.sol contract in remix to convert the hexString value to solidity bytes&#x20;

````
```remix-solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HexToBytesConverter {
    // Function to convert hexadecimal string to bytes
    function hexStringToBytes(string memory hexa) public pure returns (bytes memory) {
        bytes memory b = bytes(hexa);
        require(b.length % 2 == 0, "Hex string has odd length");

        bytes memory result = new bytes(b.length / 2);

        for (uint256 i = 0; i < bytes(hexa).length / 2; i++) {
            result[i] = bytes1(_fromHexChar(uint8(bytes(hexa)[2 * i])) * 16 + _fromHexChar(uint8(bytes(hexa)[2 * i + 1])));
        }

        return result;
    }

    // Helper function to convert a hex character to its value
    function _fromHexChar(uint8 c) internal pure returns (uint8) {
        if (bytes1(c) >= bytes1('0') && bytes1(c) <= bytes1('9')) {
            return c - uint8(bytes1('0'));
        } else if (bytes1(c) >= bytes1('a') && bytes1(c) <= bytes1('f')) {
            return 10 + c - uint8(bytes1('a'));
        } else if (bytes1(c) >= bytes1('A') && bytes1(c) <= bytes1('F')) {
            return 10 + c - uint8(bytes1('A'));
        } else {
            revert("Invalid hex character");
        }
    }
    
    // Function to simulate the usage of envBytes
    function getEnvBytes() public view returns (bytes memory) {
        // In a real scenario, this would retrieve the environment variable
        // Using the provided long hexadecimal string as an example
        string memory hexStr = "";
        return hexStringToBytes(hexStr);
    }
}
```
````

12. In the 'hexStringToBytes' option pass the hexString value and click to get the solidity converted bytes.

<figure><img src="https://1713380242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl1AVRR4cLSYjVp62RmtY%2Fuploads%2Fp5M3NkezFjktX9cH77Gx%2Fimage.png?alt=media&#x26;token=3e19d1f1-6112-42b5-89a8-edfc1ef3ac47" alt="" width="139"><figcaption></figcaption></figure>

13. Set the payee in Offchainaggregator.sol contract using the below-mentioned values, and click on transact,\
    &#x20;transmitters: \["\<node\_address\_peer1>","\<node\_address\_peer2>","\<node\_address\_peer3>","\<node\_address\_peer4>"] \
    payees: \["\<node\_address\_peer1\_walletaddress>","\<node\_address\_peer2\_walletaddress>","\<node\_address\_peer3\_walletaddress>","\<node\_address\_peer4\_walletaddress>"]

<figure><img src="https://1713380242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl1AVRR4cLSYjVp62RmtY%2Fuploads%2F1XihjmmUtwkX09LTPRpx%2Fimage.png?alt=media&#x26;token=49767395-dcab-4fb3-8e12-a792e21931dd" alt=""><figcaption></figcaption></figure>

14. Now set Offchainaggregator config using setConfig option

```
signers: [1] value from step 9 output 
transmitters: [2] value from step 9 output (Node address)
threshold: [3] value from step 9 output 
_encodedConfigVersion: [4] value from step 9 output 
encoded: bytes value from step 12
```

<figure><img src="https://1713380242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl1AVRR4cLSYjVp62RmtY%2Fuploads%2FPshJKnblSv2YgdvnieJ5%2Fimage.png?alt=media&#x26;token=db59dad9-7bc0-450c-ba96-281abc651c21" alt=""><figcaption></figcaption></figure>

15. It is time to submit Job for OCR. \
    \
    Job spec template for Peer nodes is available in foundry directory @ ./src/sandbox/clroot/jobs/ocr\_job.toml.\
    Kindly, make the required changes in the ocr\_job.toml (mentioned in between '<>')and submit the job in your node, so that it will communicate with the bootstrapper node and participate in generating the off chain report.

```
type = "offchainreporting"
schemaVersion = 1
name = "Get > Uint256 <Offchainaggregator_contract_address> (ocr)"
forwardingAllowed = false
maxTaskDuration = "0s"
blockchainTimeout = "20s"
contractAddress = "<Offchainaggregator_contract_address>"
contractConfigConfirmations = 3
contractConfigTrackerPollInterval = "2m0s"
contractConfigTrackerSubscribeInterval = "5m0s"
evmChainID = "51"
isBootstrapPeer = false
keyBundleID = "<key_bundle_ID_as mentioned in step 1>"
observationTimeout = "20s"
p2pBootstrapPeers = [
  "/dns4/<bootStrapper_node_IP>/tcp/6689/p2p/<bootStrapper_peer_ID>"
]
p2pv2Bootstrappers = [
  "<bootStrapper_peer_ID>@<bootStrapper_node_IP>:6689"
]
transmitterAddress = "<your_node_address>"
observationSource = """
   fetch        [type="http" method=GET url="https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd" allowUnrestrictedNetworkAccess="true"]
   parse        [type="jsonparse" path="ethereum,usd" data="$(fetch)"]
   multiply     [type="multiply" input="$(parse)" times=100]

   fetch -> parse -> multiply
"""
```

16. Job submission output\
    \
    Once the job is submitted you can see the job is running in Plugin UI @ "Jobs" option.

<figure><img src="https://1713380242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl1AVRR4cLSYjVp62RmtY%2Fuploads%2FssN2pbPr4dKHjHHnwdtf%2Fimage.png?alt=media&#x26;token=84444d37-8c3e-438d-a0fd-4ec95b805e01" alt=""><figcaption></figcaption></figure>
