From 8ad2dee67c5a9cbe6d1e6855eb91fdb0948157b1 Mon Sep 17 00:00:00 2001 From: benbierens Date: Sun, 22 Oct 2023 09:32:03 +0200 Subject: [PATCH] Adds user repo. --- Tools/BiblioTech/Configuration.cs | 3 + Tools/BiblioTech/Program.cs | 1 + .../TokenCommands/GetBalanceCommand.cs | 2 + Tools/BiblioTech/UserRepo.cs | 128 ++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 Tools/BiblioTech/UserRepo.cs diff --git a/Tools/BiblioTech/Configuration.cs b/Tools/BiblioTech/Configuration.cs index 94a8ec3a..00b79796 100644 --- a/Tools/BiblioTech/Configuration.cs +++ b/Tools/BiblioTech/Configuration.cs @@ -12,5 +12,8 @@ namespace BiblioTech [Uniform("endpoints", "e", "ENDPOINTS", false, "Path where endpoint JSONs are located. Also accepts codex-deployment JSONs.")] public string EndpointsPath { get; set; } = "endpoints"; + + [Uniform("userdata", "u", "USERDATA", false, "Path where user data files will be saved.")] + public string UserDataPath { get; set; } = "userdata"; } } diff --git a/Tools/BiblioTech/Program.cs b/Tools/BiblioTech/Program.cs index fbf7880a..395111fd 100644 --- a/Tools/BiblioTech/Program.cs +++ b/Tools/BiblioTech/Program.cs @@ -13,6 +13,7 @@ namespace BiblioTech public static Configuration Config { get; private set; } = null!; public static DeploymentsFilesMonitor DeploymentFilesMonitor { get; } = new DeploymentsFilesMonitor(); + public static UserRepo UserRepo { get; } = new UserRepo(); public static Task Main(string[] args) { diff --git a/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs b/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs index cec306f9..add4c4a7 100644 --- a/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs +++ b/Tools/BiblioTech/TokenCommands/GetBalanceCommand.cs @@ -15,6 +15,8 @@ namespace BiblioTech.TokenCommands { } + // connect users to eth address! + public override string Name => "balance"; public override string StartingMessage => "Fetching balance..."; public override string Description => "Shows Eth and TestToken balance of an eth address."; diff --git a/Tools/BiblioTech/UserRepo.cs b/Tools/BiblioTech/UserRepo.cs new file mode 100644 index 00000000..e4cfa2c3 --- /dev/null +++ b/Tools/BiblioTech/UserRepo.cs @@ -0,0 +1,128 @@ +using CodexContractsPlugin; +using GethPlugin; +using Newtonsoft.Json; + +namespace BiblioTech +{ + public class UserRepo + { + private readonly object repoLock = new object(); + + public void AssociateUserWithAddress(ulong discordId, EthAddress address) + { + lock (repoLock) + { + SetUserAddress(discordId, address); + } + } + + public void ClearUserAssociatedAddress(ulong discordId) + { + lock (repoLock) + { + SetUserAddress(discordId, null); + } + } + + public void AddMintEventForUser(ulong discordId, EthAddress usedAddress, Ether eth, TestToken tokens) + { + lock (repoLock) + { + var user = GetOrCreate(discordId); + user.MintEvents.Add(new UserMintEvent(DateTime.UtcNow, usedAddress, eth, tokens)); + SaveUser(user); + } + } + + public EthAddress? GetCurrentAddressForUser(ulong discordId) + { + lock (repoLock) + { + return GetOrCreate(discordId).CurrentAddress; + } + } + + private void SetUserAddress(ulong discordId, EthAddress? address) + { + var user = GetOrCreate(discordId); + user.CurrentAddress = address; + user.AssociateEvents.Add(new UserAssociateAddressEvent(DateTime.UtcNow, address)); + SaveUser(user); + } + + private User GetOrCreate(ulong discordId) + { + var filename = GetFilename(discordId); + if (!File.Exists(filename)) + { + return CreateAndSaveNewUser(discordId); + } + return JsonConvert.DeserializeObject(File.ReadAllText(filename))!; + } + + private User CreateAndSaveNewUser(ulong discordId) + { + var newUser = new User(discordId, DateTime.UtcNow, null, new List(), new List()); + SaveUser(newUser); + return newUser; + } + + private void SaveUser(User user) + { + var filename = GetFilename(user.DiscordId); + if (File.Exists(filename)) File.Delete(filename); + File.WriteAllText(filename, JsonConvert.SerializeObject(user)); + } + + private static string GetFilename(ulong discordId) + { + return Path.Combine(Program.Config.UserDataPath, discordId.ToString() + ".json"); + } + } + + public class User + { + public User(ulong discordId, DateTime createdUtc, EthAddress? currentAddress, List associateEvents, List mintEvents) + { + DiscordId = discordId; + CreatedUtc = createdUtc; + CurrentAddress = currentAddress; + AssociateEvents = associateEvents; + MintEvents = mintEvents; + } + + public ulong DiscordId { get; } + public DateTime CreatedUtc { get; } + public EthAddress? CurrentAddress { get; set; } + public List AssociateEvents { get; } + public List MintEvents { get; } + } + + public class UserAssociateAddressEvent + { + public UserAssociateAddressEvent(DateTime utc, EthAddress? newAddress) + { + Utc = utc; + NewAddress = newAddress; + } + + public DateTime Utc { get; } + public EthAddress? NewAddress { get; } + } + + public class UserMintEvent + { + public UserMintEvent(DateTime utc, EthAddress usedAddress, Ether ethReceived, TestToken testTokensMinted) + { + Utc = utc; + UsedAddress = usedAddress; + EthReceived = ethReceived; + TestTokensMinted = testTokensMinted; + } + + public DateTime Utc { get; } + public EthAddress UsedAddress { get; } + public Ether EthReceived { get; } + public TestToken TestTokensMinted { get; } + } +}