# Usage

### Setting Up

To setup the library, the configuration settings for the Discv5 protocol must first be created:

<pre class="language-csharp"><code class="lang-csharp">// Must provide an array of Ethereum Node Records (ENRs) in string format.
// These are required for finding the relevant nodes. 
var bootstrapEnrs = new[]
{
    "enr:-Ku4QImhMc1z8yC...", 
};

// Configurable settings used by the Discovery protocol.
var tableOptions = new TableOptions(bootstrapEnrs);
var connectionOptions = new ConnectionOptions();
var sessionOptions = SessionOptions.Default;

// Create an instance of ENR (Ethereum Node Record).
// Must provide the 'v4' identity scheme and Secp256k1 public key as entries.
var enr = new EnrBuilder()
        .WithIdentityScheme(sessionOptions.Verifier, sessionOptions.Signer)
        .WithEntry(EnrEntryKey.Id, new EntryId("v4"))
        .WithEntry(EnrEntryKey.Secp256K1, new EntrySecp256K1(sessionOptions.Signer.PublicKey));
<strong>
</strong>// Create a logger factory for the discovery protocol.
// Below's example sets the log level to none, however, if needed, other levels can be used.
<strong>var discv5LoggerFactory = LoggerFactory.Create(builder => builder.SetMinimumLevel(LogLevel.None));
</strong></code></pre>

Next, the configuration settings need to be created for the Beacon client:

```csharp
// The following settings must be provided for sync protocol.
// The trusted block root can be retrieved from any Ethereum explorer.
var syncProtocolOptions = new SyncProtocolOptions()
{
    Preset = SizePreset.MainnetPreset,
    GenesisTime = 1606824023,
    GenesisValidatorsRoot = Convert.FromHexString("4b363db94e286120d76eb905340fdd4e54bfe9f06bf33ff6cf5ad27f511bfe95"),
    TrustedBlockRoot = Convert.FromHexString("9344d07abccaa481cb301e804c99d9b3102fb70c0d950354c8d22f6f0f389ef2"),
    Network = NetworkType.Mainnet;
};

// Configurable options for the beacon client.
// Note that providing bootnodes is optional for beacon client options.
var beaconClientOptions = new BeaconClientOptions()
{
    TcpPort = 9005,
    Bootnodes = ["/ip4/135.148.103.80/tcp/9000/p2p/16Uiu2HAm1iCnKSNGhee2RKa1EYbazz4JJ8CDVVCPLyXS9PFYPG1A"]
};

// Create a logger factory for the Beacon client.
// Below is an example that can be used.
var loggerFactory = LoggerFactory.Create(builder =>
{
    builder
        .SetMinimumLevel(LogLevel.Information)
        .AddSimpleConsole(l =>
        {
            l.SingleLine = true;
            l.TimestampFormat = "[HH:mm:ss] ";
            l.ColorBehavior = LoggerColorBehavior.Default;
            l.IncludeScopes = false;
            l.UseUtcTimestamp = true;
        });
});
```

Lastly, the configurations that have been setup in the previous steps must be added to `ServiceCollection`:

<pre class="language-csharp"><code class="lang-csharp"><strong>// Create a new instance of service collection
</strong><strong>var services = new ServiceCollection();
</strong>
// Add the configurations to the service collection
services.AddBeaconClient(beaconClientBuilder =>
{
    beaconClientBuilder.AddDiscoveryProtocol(discv5Builder =>
    {
        discv5Builder.WithConnectionOptions(connectionOptions)
            .WithTableOptions(tableOptions)
            .WithEnrBuilder(enr)
            .WithSessionOptions(sessionOptions)
            .WithLoggerFactory(discv5LoggerFactory);
    });
    beaconClientBuilder.WithBeaconClientOptions(beaconClientOptions);
    beaconClientBuilder.WithSyncProtocolOptions(syncProtocolOptions);
    beaconClientBuilder.AddLibp2pProtocol(libp2pBuilder => libp2pBuilder);
    beaconClientBuilder.WithLoggerFactory(loggerFactory);
});
</code></pre>

Now that all the required steps have been completed for setting up the Beacon client, the service for it can be created:&#x20;

```csharp
var serviceProvider = services.BuildServiceProvider();
var beaconClient = serviceProvider.GetRequiredService<IBeaconClient>();
```

### How To Use

For running the beacon client, it is first necessary to call the `InitAsync` method in an async manner:

```csharp
await beaconClient.InitAsync();
```

This will initialize the necessary dependencies for the client to run correctly. This function must be called and awaited before starting the beacon client.

The beacon client can then be started by calling:

```csharp
await beaconClient.StartAsync();
```
