Skip to content

Commit

Permalink
Merge pull request #23 from The-Poolz/lomet-refactor
Browse files Browse the repository at this point in the history
add linq
  • Loading branch information
ArdenHide authored Sep 21, 2023
2 parents c75d31b + c233fe6 commit bade337
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 96 deletions.
87 changes: 22 additions & 65 deletions src/KYC.API.Proxy/LambdaFunction.cs
Original file line number Diff line number Diff line change
@@ -1,84 +1,41 @@
using Amazon.Lambda.Core;
using KYC.API.Proxy.Utils;
using KYC.API.Proxy.Models;
using KYC.API.Proxy.Models.HttpResponse;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace KYC.API.Proxy;

public class LambdaFunction
namespace KYC.API.Proxy
{
public const string ZeroAddress = "0x0000000000000000000000000000000000000000";

private readonly HttpCall httpCall;
private readonly DynamoDb dynamoDb;

public LambdaFunction()
: this(new HttpCall(), new DynamoDb())
{ }

public LambdaFunction(HttpCall httpCall, DynamoDb dynamoDb)
{
this.httpCall = httpCall;
this.dynamoDb = dynamoDb;
}

public async Task<OutputData> RunAsync(InputData request)
public class LambdaFunction : LambdaFunctionScenarios
{
if (string.IsNullOrWhiteSpace(request.Address) || request.Address == ZeroAddress)
{
return new OutputData
{
RequestStatus = RequestStatus.error
};
}
var response = httpCall.GetBlockPassResponse(request.Address);
public LambdaFunction()
: this(new HttpCall(), new DynamoDb())
{ }

if (response.Status != RequestStatus.error)
{
return BuildOutputData(response);
}
public LambdaFunction(HttpCall httpCall, DynamoDb dynamoDb)
: base(httpCall, dynamoDb)
{ }

var proxy = dynamoDb.GetProxyAddress(request.Address);
if (proxy != null)
public OutputData Run(InputData request)
{
response = httpCall.GetBlockPassResponse(proxy);
if (!request.Valid)
return OutputData.Error;

if (response.Status != RequestStatus.error)
var scenarios = new List<Func<InputData, OutputData?>>
{
return BuildOutputData(response, proxy);
}
}
HandleBlockPassResponse,
HandleProxyAddress,
HandleValidWallet
};

var wallets = dynamoDb.GetWallets(request.Address);
foreach (var wallet in wallets)
{
response = httpCall.GetBlockPassResponse(wallet);
if (response.Status != RequestStatus.error)
foreach (var scenario in scenarios)
{
if (proxy != wallet)
{
await dynamoDb.UpdateItemAsync(request.Address, wallet);
}
return BuildOutputData(response, wallet);
var result = scenario(request);
if (result != null)
return result;
}
}

return new OutputData
{
RequestStatus = RequestStatus.error
};
}

private static OutputData BuildOutputData(Response response, string? proxy = null)
{
return new OutputData
{
RequestStatus = response.Status,
Status = response.Data.Status,
Name = response.Data.Identities.GivenName.Value,
Proxy = proxy
};
return OutputData.Error;
}
}
}
46 changes: 46 additions & 0 deletions src/KYC.API.Proxy/LambdaFunctionScenarios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using KYC.API.Proxy.Models;
using KYC.API.Proxy.Utils;

namespace KYC.API.Proxy;

public class LambdaFunctionScenarios
{
internal LambdaFunctionScenarios(HttpCall httpCall, DynamoDb dynamoDb)
{
_httpCall = httpCall;
_dynamoDb = dynamoDb;
}

private readonly HttpCall _httpCall;
private readonly DynamoDb _dynamoDb;
internal OutputData? HandleBlockPassResponse(InputData request)
{
var response = _httpCall.GetBlockPassResponse(request.Address);
return response.Status == RequestStatus.success ? new OutputData(response) : null;
}

internal OutputData? HandleProxyAddress(InputData request)
{
var proxy = _dynamoDb.GetProxyAddress(request.Address);
if (proxy == null) return null;

var response = _httpCall.GetBlockPassResponse(proxy);
return response.Status == RequestStatus.success ? new OutputData(response, proxy) : null;
}

internal OutputData? HandleValidWallet(InputData request)
{
var proxy = _dynamoDb.GetProxyAddress(request.Address);

var validWallet = _dynamoDb.GetWallets(request.Address)
.Select(wallet => new { Wallet = wallet, Response = _httpCall.GetBlockPassResponse(wallet) })
.FirstOrDefault(w => w.Response.Status == RequestStatus.success);

if (validWallet == null) return null;

if (proxy != validWallet.Wallet)
_dynamoDb.UpdateItem(request.Address, validWallet.Wallet);

return new OutputData(validWallet.Response, validWallet.Wallet);
}
}
2 changes: 2 additions & 0 deletions src/KYC.API.Proxy/Models/InputData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

public class InputData
{
public const string ZeroAddress = "0x0000000000000000000000000000000000000000";
public string Address { get; set; } = null!;
public bool Valid => !string.IsNullOrWhiteSpace(Address) && Address != ZeroAddress;
}
15 changes: 14 additions & 1 deletion src/KYC.API.Proxy/Models/OutputData.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using Newtonsoft.Json;
using KYC.API.Proxy.Models.HttpResponse;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace KYC.API.Proxy.Models;

public class OutputData
{
internal OutputData() { }
public OutputData(Response response, string? proxy = null)
{
RequestStatus = response.Status;
Status = response.Data.Status;
Name = response.Data.Identities.GivenName.Value;
Proxy = proxy;
}
[JsonConverter(typeof(StringEnumConverter))]
public RequestStatus RequestStatus { get; set; }
public string? Status { get; set; }
public string? Name { get; set; }
public string? Proxy { get; set; }
public static OutputData Error => new()
{
RequestStatus = RequestStatus.error
};
}
23 changes: 8 additions & 15 deletions src/KYC.API.Proxy/Utils/DynamoDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,19 @@ public DynamoDb(IAmazonDynamoDB client)
this.client = client;
}

public virtual string[] GetWallets(string wallet)
public virtual IEnumerable<string> GetWallets(string wallet)
{
var user = GetItem(wallet);
if (user == null || !user.ContainsKey("EvmWallets"))
return Array.Empty<string>();

var associatedWallets = user["EvmWallets"].L.Select(x => x.S).ToArray();

var wallets = new List<string>();
foreach (var associatedWallet in associatedWallets)
{
var associatedUser = GetItem(associatedWallet);
if (associatedUser == null || !associatedUser.ContainsKey("EvmWallets"))
continue;

if (associatedUser["EvmWallets"].L.Exists(x => x.S == wallet))
{
wallets.Add(associatedUser["EvmWallet"].S);
}
}

return wallets.ToArray();
return associatedWallets
.Select(associatedWallet => GetItem(associatedWallet))
.Where(associatedUser => associatedUser != null && associatedUser.ContainsKey("EvmWallets"))
.Where(associatedUser => associatedUser!["EvmWallets"].L.Exists(x => x.S == wallet))
.Select(associatedUser => associatedUser!["EvmWallet"].S);
}

public virtual string? GetProxyAddress(string wallet)
Expand All @@ -51,6 +42,8 @@ public virtual string[] GetWallets(string wallet)
return user.TryGetValue("Proxy", out var proxy) ? proxy.S : null;
}

public void UpdateItem(string primaryKey, string proxyAddress) => UpdateItemAsync(primaryKey, proxyAddress).GetAwaiter().GetResult();

public virtual async Task UpdateItemAsync(string primaryKey, string proxyAddress)
{
var request = new UpdateItemRequest
Expand Down
30 changes: 15 additions & 15 deletions tests/KYC.API.Proxy.Tests/LambdaFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,46 @@ internal void Ctor_Default()
}

[Fact]
internal async Task RunAsync_ShouldReturnForbidden_WhenAddressIsInvalid()
internal void RunAsync_ShouldReturnForbidden_WhenAddressIsInvalid()
{
var request = new InputData
{
Address = LambdaFunction.ZeroAddress
Address = InputData.ZeroAddress
};
var lambdaFunction = MockLambdaFunction();

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.error, result.RequestStatus);
}

[Fact]
internal async Task RunAsync_ShouldReturnForbidden_WhenAddressIsMissing()
internal void RunAsync_ShouldReturnForbidden_WhenAddressIsMissing()
{
var request = new InputData();
var lambdaFunction = MockLambdaFunction();

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.error, result.RequestStatus);
}

[Fact]
internal async Task RunAsync_ShouldReturnExpectedResponse_WhenAddressIsValid()
internal void RunAsync_ShouldReturnExpectedResponse_WhenAddressIsValid()
{
var request = new InputData
{
Address = TestAddress
};
var lambdaFunction = MockLambdaFunction();

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.success, result.RequestStatus);
}

[Fact]
internal async Task RunAsync_ReceiveAddressFromProxyAddress()
internal void RunAsync_ReceiveAddressFromProxyAddress()
{
var mockDynamoDb = new Mock<DynamoDb>();
mockDynamoDb.Setup(x => x.GetProxyAddress(TestAddress))
Expand All @@ -85,13 +85,13 @@ internal async Task RunAsync_ReceiveAddressFromProxyAddress()
};
var lambdaFunction = MockLambdaFunction(mockHttpCall, mockDynamoDb);

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.success, result.RequestStatus);
}

[Fact]
internal async Task RunAsync_BadResponseInProxyAddress()
internal void RunAsync_BadResponseInProxyAddress()
{
var mockDynamoDb = new Mock<DynamoDb>();
mockDynamoDb.Setup(x => x.GetProxyAddress(TestAddress))
Expand All @@ -112,13 +112,13 @@ internal async Task RunAsync_BadResponseInProxyAddress()
};
var lambdaFunction = MockLambdaFunction(mockHttpCall, mockDynamoDb);

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.error, result.RequestStatus);
}

[Fact]
internal async Task RunAsync_ReceiveAddressFromAssociatedWallets()
internal void RunAsync_ReceiveAddressFromAssociatedWallets()
{
var mockDynamoDb = new Mock<DynamoDb>();
mockDynamoDb.Setup(x => x.GetWallets(TestAddress))
Expand All @@ -139,13 +139,13 @@ internal async Task RunAsync_ReceiveAddressFromAssociatedWallets()
};
var lambdaFunction = MockLambdaFunction(mockHttpCall, mockDynamoDb);

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.success, result.RequestStatus);
}

[Fact]
internal async Task RunAsync_ReceiveErrorResponse()
internal void RunAsync_ReceiveErrorResponse()
{
var mockDynamoDb = new Mock<DynamoDb>();
mockDynamoDb.Setup(x => x.GetWallets(TestAddress))
Expand All @@ -164,7 +164,7 @@ internal async Task RunAsync_ReceiveErrorResponse()

var lambdaFunction = MockLambdaFunction(mockHttpCall, mockDynamoDb);

var result = await lambdaFunction.RunAsync(request);
var result = lambdaFunction.Run(request);

Assert.Equal(RequestStatus.error, result.RequestStatus);
}
Expand Down

0 comments on commit bade337

Please sign in to comment.