Skip to content

Commit

Permalink
- add DbChainManager
Browse files Browse the repository at this point in the history
- use `DbChainManager` as default `IChainManager`
  • Loading branch information
ArdenHide committed Jul 1, 2024
1 parent 768cdab commit 1679db9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/MetaDataAPI/DefaultServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class DefaultServiceProvider
{
var serviceCollection = new ServiceCollection();

serviceCollection.AddSingleton<IChainManager, LocalChainManager>(_ => new LocalChainManager());
serviceCollection.AddSingleton<IChainManager, DbChainManager>(_ => new DbChainManager());
serviceCollection.AddSingleton<IErc20Provider, Erc20Provider>(_ => new Erc20Provider());
serviceCollection.AddSingleton<ITlyContext, TlyContext>(_ => new TlyContext(Environments.TLY_API_KEY.Get()));
serviceCollection.AddSingleton<ILockDealNFTService, LockDealNFTService>(_ => new LockDealNFTService());
Expand Down
1 change: 0 additions & 1 deletion src/MetaDataAPI/LambdaFunction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Nethereum.Web3;
using Amazon.Lambda.Core;
using MetaDataAPI.Request;
using Newtonsoft.Json.Linq;
using MetaDataAPI.Response;
using MetaDataAPI.Providers;
using MetaDataAPI.Extensions;
Expand Down
1 change: 1 addition & 0 deletions src/MetaDataAPI/MetaDataAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.2.0" />
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.2.0" />
<PackageReference Include="CovalentDB" Version="2.0.12" />
<PackageReference Include="FluentValidation" Version="11.9.2" />
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
Expand Down
46 changes: 46 additions & 0 deletions src/MetaDataAPI/Services/ChainsInfo/DbChainManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using CovalentDb;
using System.Numerics;
using CovalentDb.Types;
using Net.Web3.EthereumWallet;

namespace MetaDataAPI.Services.ChainsInfo;

public class DbChainManager : IChainManager
{
private readonly CovalentContext _context;

public DbChainManager() : this(new CovalentContext()) { }

public DbChainManager(CovalentContext context)
{
_context = context;
}

public bool TryFetchChainInfo(BigInteger chainId, out ChainInfo chainInfo)
{
chainInfo = new ChainInfo(-1, string.Empty, EthereumAddress.ZeroAddress);

var address = GetContractAddress(chainId);
if (string.IsNullOrEmpty(address)) return false;

var rpcUrl = GetRpcConnection(chainId);
if (string.IsNullOrEmpty(rpcUrl)) return false;

chainInfo = new ChainInfo(chainId, rpcUrl, address);
return true;
}

private string? GetContractAddress(BigInteger chainId)
{
return _context.DownloaderSettings
.FirstOrDefault(x => x.ChainId == chainId && x.ResponseType == ResponseType.LDNFTContractApproved)
?.ContractAddress;
}

private string? GetRpcConnection(BigInteger chainId)
{
return _context.Chains
.FirstOrDefault(x => x.ChainId == chainId)
?.RpcConnection;
}
}

0 comments on commit 1679db9

Please sign in to comment.