planetarium / NineChronicles.DataProvider

4 stars 15 forks source link

NineChronicles DataProvider

Table of Contents

Pre-requisite

Run

Development Guide

1. Setup Database

2. Create Model


- In [NineChronicles.DataProvider/NineChronicles.DataProvider/Store/NineChroniclesContext.cs](https://github.com/planetarium/NineChronicles.DataProvider/blob/development/NineChronicles.DataProvider/Store/NineChroniclesContext.cs), add a DbSet called `TransferAssets` and its description for reference.

// Table for storing TransferAsset actions public DbSet TransferAssets => Set();


### 2. Create Store Method

- In [NineChronicles.DataProvider/NineChronicles.DataProvider/Store/MySqlStore.cs](https://github.com/planetarium/NineChronicles.DataProvider/blob/development/NineChronicles.DataProvider/Store/MySqlStore.cs), add a following method that stores the `TransferAsset` data into MySQL.

public void StoreTransferAsset(TransferAssetModel model) { using NineChroniclesContext ctx = _dbContextFactory.CreateDbContext(); TransferAssetModel? prevModel = ctx.TransferAssets.FirstOrDefault(r => r.TxId == model.TxId); if (prevModel is null) { ctx.TransferAssets.Add(model); } else { prevModel.BlockIndex = model.BlockIndex; prevModel.Sender = model.Sender; prevModel.Recipient = model.Recipient; prevModel.Amount = model..Amount; prevModel.Date = model.Date; prevModel.TimeStamp = model.TimeStamp; ctx.TransferAssets.Update(prevModel); }

ctx.SaveChanges();

}


### 3. (Optional) Add Data Getter
In some cases, you need to handle state to get data to make model.  
To do this easily, you can make your own data getter inside [NineChronicles.DataProvider/DataRendering/](https://github.com/planetarium/NineChronicles.DataProvider/tree/development/NineChronicles.DataProvider/DataRendering).  

### 4. Render & Store Action Data

- In [NineChronicles.DataProvider/NineChronicles.DataProvider/RenderSubscriber.cs](https://github.com/planetarium/NineChronicles.DataProvider/blob/development/NineChronicles.DataProvider/RenderSubscriber.cs), add a following render code

_actionRenderer.EveryRender() .Subscribe(ev => { try { if (ev.Exception is null && ev.Action is { } transferAsset) { var model = new TransferAssetModel() { TxId = transferAsset.TxId, BlockIndex = transferAsset.BlockIndex, Sender = transferAsset.Sender, Recipient = transferAsset.Recipient, Amount = Convert.ToDecimal(transferAsset.Amount.GetQuantityString()), Date = DateOnly.FromDateTime(_blockTimeOffset.DateTime), TimeStamp = _blockTimeOffset, }; MySqlStore.StoreTransferAsset(model); } } catch (Exception e) { Console.WriteLine(e); }


### 5. Add Database Migration

- Navigate to [NineChronicles.DataProvider/NineChronicles.DataProvider.Executable](https://github.com/planetarium/NineChronicles.DataProvider/tree/development/NineChronicles.DataProvider.Executable) directory on your terminal and run the following migration command

dotnet ef migrations add AddTransferAsset -- [Connection String]

Current Table Descriptions

Migrating Past Chain Data to MySQL Database

Migrate All Action Data

Migrate action data in rocksdb store to mysql db.

Options: -o, --store-path Rocksdb path to migrate. (Required) --mysql-server A hostname of MySQL server. (Required) --mysql-port A port of MySQL server. (Required) --mysql-username The name of MySQL user. (Required) --mysql-password The password of MySQL user. (Required) --mysql-database The name of MySQL database to use. (Required) --offset offset of block index (no entry will migrate from the genesis block). --limit limit of block count (no entry will migrate to the chain tip). -h, --help Show help message


### Migrate Battle Arena Ranking Data
- This command calculates the `battle arena` ranking of all participants at a specific block index (`migration-block-index`) and inserts the data to a designated mysql database.

Usage: NineChronicles.DataProvider.Executable battle-arena-ranking-migration [--store-path ] [--mysql-server ] [--mysql-port ] [--mysql-username ] [--mysql-password ] [--mysql-database <Stri ng>] [--migration-block-index ] [--help]

Migrate battle arena ranking data at a specific block index to a mysql database.

Options: -o, --store-path RocksDB store path to migrate. (Required) --mysql-server Hostname of MySQL server. (Required) --mysql-port Port of MySQL server. (Required) --mysql-username Name of MySQL user. (Required) --mysql-password Password of MySQL user. (Required) --mysql-database Name of MySQL database. (Required) --migration-block-index Block index to migrate. -h, --help Show help message


### Migrate User Staking Data
- This command calculates the `NCG staking amount` of all participants at a specific block index (`migration-block-index`) and inserts the data to a designated mysql database.
- Currently, the `slack-token` and `slack-channel` options for sending the data in `csv` format are required, however, these will be changed to optional in the near future.

Usage: NineChronicles.DataProvider.Executable user-staking-migration [--store-path ] [--mysql-server ] [--mysql-port ] [--mysql-username ] [--mysql-password ] [--mysql-database ] [-- slack-token ] [--slack-channel ] [--migration-block-index ] [--help]

Migrate staking amounts of users at a specific block index to a mysql database.

Options: -o, --store-path Rocksdb store path to migrate. (Required) --mysql-server Hostname of MySQL server. (Required) --mysql-port Port of MySQL server. (Required) --mysql-username Name of MySQL user. (Required) --mysql-password Password of MySQL user. (Required) --mysql-database Name of MySQL database to use. (Required) --slack-token slack token to send the migration data. (Required) --slack-channel slack channel that receives the migration data. (Required) --migration-block-index Block index to migrate. -h, --help Show help message