Skip to content

Commit

Permalink
feat: remove orleans & authorize
Browse files Browse the repository at this point in the history
  • Loading branch information
chaoxkang committed Jan 25, 2025
1 parent 080816e commit 0fe3be7
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 132 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EoaServer.Token.Dto;
using EoaServer.Token.Request;
using EoaServer.UserToken.Dto;
using EoaServer.UserToken.Request;
using Volo.Abp.Application.Dtos;
Expand Down
10 changes: 1 addition & 9 deletions src/EoaServer.Application/Provider/GraphQLProvider.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EoaServer.Options;
using EoaServer.Provider.Dto.Indexer;
using EoaServer.Token;
using EoaServer.Token.Dto;
using GraphQL;
using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft;
using Microsoft.Extensions.Options;
using Orleans;
using Serilog;
using Volo.Abp.DependencyInjection;

Expand All @@ -20,23 +17,18 @@ public class GraphQLProvider : IGraphQLProvider, ISingletonDependency
private readonly GraphQLOptions _graphQLOptions;
private readonly GraphQLHttpClient _blockChainIndexerClient;
private readonly GraphQLHttpClient _tokenIndexerClient;
private readonly IClusterClient _clusterClient;
private readonly ILogger _logger;
private readonly ITokenAppService _tokenAppService;

public const string TokenIndexer = "TokenIndexer";
public const string BlockChainIndexer = "BlockChainIndexer";

public GraphQLProvider(IClusterClient clusterClient,
ITokenAppService tokenAppService,
public GraphQLProvider(
IOptionsSnapshot<GraphQLOptions> graphQLOptions)
{
_logger = Log.ForContext<GraphQLProvider>();
_clusterClient = clusterClient;
_graphQLOptions = graphQLOptions.Value;
_blockChainIndexerClient = new GraphQLHttpClient(_graphQLOptions.IndexerOptions[BlockChainIndexer].BaseUrl, new NewtonsoftJsonSerializer());
_tokenIndexerClient = new GraphQLHttpClient(_graphQLOptions.IndexerOptions[TokenIndexer].BaseUrl, new NewtonsoftJsonSerializer());
_tokenAppService = tokenAppService;
}

public async Task<IndexerTokenTransferListDto> GetTokenTransferInfoAsync(GetTokenTransferRequestDto requestDto)
Expand Down
71 changes: 34 additions & 37 deletions src/EoaServer.Application/UserToken/UserTokenAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class UserTokenAppService : EoaServerBaseService, IUserTokenAppService
private readonly ILogger<UserTokenAppService> _logger;
private readonly IUserTokenProvider _userTokenProvider;
private readonly NftToFtOptions _nftToFtOptions;
private readonly IClusterClient _clusterClient;
private readonly IDistributedEventBus _distributedEventBus;

public UserTokenAppService(
Expand All @@ -45,15 +44,13 @@ public UserTokenAppService(
ILogger<UserTokenAppService> logger,
IUserTokenProvider userTokenProvider,
IOptionsSnapshot<NftToFtOptions> nftToFtOptions,
IClusterClient clusterClient,
IDistributedEventBus distributedEventBus)
{
_tokenListOptions = tokenListOptions.Value;
_tokenInfoProvider = tokenInfoProvider;
_logger = logger;
_userTokenProvider = userTokenProvider;
_nftToFtOptions = nftToFtOptions.Value;
_clusterClient = clusterClient;
_distributedEventBus = distributedEventBus;
}

Expand All @@ -64,44 +61,44 @@ public UserTokenAppService(

public async Task ChangeTokenDisplayAsync(string id, bool isDisplay)
{
var (chainId, symbol) = GetTokenInfoFromId(id);
var userId = CurrentUser.GetId();
var grainId = GrainIdHelper.GenerateGrainId(id, userId);
var grain = _clusterClient.GetGrain<IUserTokenGrain>(grainId);
var userTokenGrainResultDto = await grain.GetAsync();
if (!userTokenGrainResultDto.Success())
{
var tokenInfo = await _tokenInfoProvider.GetAsync(chainId, symbol);
if (tokenInfo == null)
{
_logger.LogError($"can't get token info, chain: {chainId}, symbol: {symbol}");
return;
}
var addResult = await grain.AddAsync(userId, new UserTokenGrainDto
{
UserId = userId,
SortWeight = 0,
Token = new Dto.Token()
{
Id = id,
ChainId = chainId,
Address = tokenInfo.Address,
Symbol = symbol,
Decimals = tokenInfo.Decimals
}
});
_logger.LogInformation($"Add user token: {JsonConvert.SerializeObject(addResult)}");
}

var tokenResult = await grain.ChangeDisplayAsync(userId, isDisplay, false);
_logger.LogInformation($"Change user token: {tokenResult}");

await _distributedEventBus.PublishAsync(ObjectMapper.Map<UserTokenGrainDto, UserTokenEto>(tokenResult.Data));
// var (chainId, symbol) = GetTokenInfoFromId(id);
// var userId = CurrentUser.GetId();
// var grainId = GrainIdHelper.GenerateGrainId(id, userId);
// var grain = _clusterClient.GetGrain<IUserTokenGrain>(grainId);
// var userTokenGrainResultDto = await grain.GetAsync();
// if (!userTokenGrainResultDto.Success())
// {
// var tokenInfo = await _tokenInfoProvider.GetAsync(chainId, symbol);
// if (tokenInfo == null)
// {
// _logger.LogError($"can't get token info, chain: {chainId}, symbol: {symbol}");
// return;
// }
// var addResult = await grain.AddAsync(userId, new UserTokenGrainDto
// {
// UserId = userId,
// SortWeight = 0,
// Token = new Dto.Token()
// {
// Id = id,
// ChainId = chainId,
// Address = tokenInfo.Address,
// Symbol = symbol,
// Decimals = tokenInfo.Decimals
// }
// });
// _logger.LogInformation($"Add user token: {JsonConvert.SerializeObject(addResult)}");
// }
//
// var tokenResult = await grain.ChangeDisplayAsync(userId, isDisplay, false);
// _logger.LogInformation($"Change user token: {tokenResult}");
//
// await _distributedEventBus.PublishAsync(ObjectMapper.Map<UserTokenGrainDto, UserTokenEto>(tokenResult.Data));
}

public async Task<PagedResultDto<GetUserTokenDto>> GetTokensAsync(GetTokenInfosRequestDto requestDto)
{
var userId = CurrentUser.GetId();
var userId = CurrentUser.IsAuthenticated ? CurrentUser.GetId() : Guid.Empty;
var userTokens =
await _userTokenProvider.GetUserTokenInfoListAsync(userId, string.Empty, string.Empty);

Expand Down
2 changes: 1 addition & 1 deletion src/EoaServer.HttpApi.Host/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static async Task<int> Main(string[] args)
builder.Host.AddAppSettingsSecretsJson()
.UseApolloForConfigureHostBuilder()
.UseAutofac()
.UseOrleansClient()
// .UseOrleansClient()
.UseSerilog();

await builder.AddApplicationAsync<EoaServerHttpApiHostModule>();
Expand Down
2 changes: 1 addition & 1 deletion src/EoaServer.HttpApi/Controllers/UserTokenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task ChangeTokenDisplayAsync(string id, IsTokenDisplayInput input)
await _userTokenAppService.ChangeTokenDisplayAsync(id, input.IsDisplay);
}

[HttpGet, Authorize]
[HttpGet]
public async Task<PagedResultDto<GetUserTokenDto>> GetTokensAsync(GetTokenInfosRequestDto requestDto)
{
return await _userTokenAppService.GetTokensAsync(requestDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,35 +210,35 @@ public async void GetNFTItemsAsyncTest()
}


[Fact]
public async void GetUserTokenAsyncTest()
{
await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-USDC", true);
await _userTokenAppService.ChangeTokenDisplayAsync("AELF-USDC", true);

var result = await _userAssetsAppService.GetTokenAsync(new GetTokenRequestDto()
{
AddressInfos = new List<AddressInfo>()
{
new AddressInfo()
{
Address = EoaServerApplicationTestConstant.User1Address,
ChainId = EoaServerApplicationTestConstant.ChainIdTDVW
},
new AddressInfo()
{
Address = EoaServerApplicationTestConstant.User1Address,
ChainId = EoaServerApplicationTestConstant.ChainIdAELF
}
}
});

result.TotalRecordCount.ShouldBe(4);
// default show
result.Data[2].Symbol.ShouldBe("ETH");
result.Data[2].Tokens.Count.ShouldBe(2);
// user token
result.Data[3].Symbol.ShouldBe("USDC");
result.Data[3].Tokens.Count.ShouldBe(2);
}
// [Fact]
// public async void GetUserTokenAsyncTest()
// {
// await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-USDC", true);
// await _userTokenAppService.ChangeTokenDisplayAsync("AELF-USDC", true);
//
// var result = await _userAssetsAppService.GetTokenAsync(new GetTokenRequestDto()
// {
// AddressInfos = new List<AddressInfo>()
// {
// new AddressInfo()
// {
// Address = EoaServerApplicationTestConstant.User1Address,
// ChainId = EoaServerApplicationTestConstant.ChainIdTDVW
// },
// new AddressInfo()
// {
// Address = EoaServerApplicationTestConstant.User1Address,
// ChainId = EoaServerApplicationTestConstant.ChainIdAELF
// }
// }
// });
//
// result.TotalRecordCount.ShouldBe(4);
// // default show
// result.Data[2].Symbol.ShouldBe("ETH");
// result.Data[2].Tokens.Count.ShouldBe(2);
// // user token
// result.Data[3].Symbol.ShouldBe("USDC");
// result.Data[3].Tokens.Count.ShouldBe(2);
// }
}
100 changes: 50 additions & 50 deletions test/EoaServer.Application.Tests/UserToken/UserTokenAppServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,54 +30,54 @@ protected override void AfterAddApplication(IServiceCollection services)
services.AddSingleton(_currentUser);
}

[Fact]
public async Task ChangeTokenDisplayAsyncTest()
{
await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-ETH", true);
await _userTokenAppService.ChangeTokenDisplayAsync("AELF-ETH", true);
await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-SGR-1", true);
await _userTokenAppService.ChangeTokenDisplayAsync("AELF-SGR-1", true);
Thread.Sleep(2000);

var userTokens = await _userTokenAppService.GetTokensAsync(new GetTokenInfosRequestDto());
userTokens.TotalCount.ShouldBe(6);
userTokens.Items.Count.ShouldBe(6);
userTokens.Items[0].Symbol.ShouldBe("ELF");
userTokens.Items[0].ChainId.ShouldBe("tDVW");
userTokens.Items[1].Symbol.ShouldBe("ELF");
userTokens.Items[1].ChainId.ShouldBe("AELF");
userTokens.Items[4].Symbol.ShouldBe("SGR-1");
userTokens.Items[4].Label.ShouldBe("SGR");
userTokens.Items[4].IsDisplay.ShouldBe(true);
userTokens.Items[4].ChainId.ShouldBe("tDVW");
userTokens.Items[5].Symbol.ShouldBe("SGR-1");
userTokens.Items[5].Label.ShouldBe("SGR");
userTokens.Items[5].IsDisplay.ShouldBe(true);
userTokens.Items[5].ChainId.ShouldBe("AELF");

await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-SGR-1", false);
await _userTokenAppService.ChangeTokenDisplayAsync("AELF-SGR-1", false);
Thread.Sleep(2000);

userTokens = await _userTokenAppService.GetTokensAsync(new GetTokenInfosRequestDto());
userTokens.TotalCount.ShouldBe(6);
userTokens.Items.Count.ShouldBe(6);
userTokens.Items[4].Symbol.ShouldBe("SGR-1");
userTokens.Items[4].IsDisplay.ShouldBe(false);
userTokens.Items[5].Symbol.ShouldBe("SGR-1");
userTokens.Items[5].IsDisplay.ShouldBe(false);

userTokens = await _userTokenAppService.GetTokensAsync(new GetTokenInfosRequestDto()
{
Keyword = "el",
ChainIds = new List<string>()
{
"tDVW"
}
});
userTokens.TotalCount.ShouldBe(1);
userTokens.Items.Count.ShouldBe(1);
userTokens.Items[0].Symbol.ShouldBe("ELF");
userTokens.Items[0].ChainId.ShouldBe("tDVW");
}
// [Fact]
// public async Task ChangeTokenDisplayAsyncTest()
// {
// await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-ETH", true);
// await _userTokenAppService.ChangeTokenDisplayAsync("AELF-ETH", true);
// await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-SGR-1", true);
// await _userTokenAppService.ChangeTokenDisplayAsync("AELF-SGR-1", true);
// Thread.Sleep(2000);
//
// var userTokens = await _userTokenAppService.GetTokensAsync(new GetTokenInfosRequestDto());
// userTokens.TotalCount.ShouldBe(6);
// userTokens.Items.Count.ShouldBe(6);
// userTokens.Items[0].Symbol.ShouldBe("ELF");
// userTokens.Items[0].ChainId.ShouldBe("tDVW");
// userTokens.Items[1].Symbol.ShouldBe("ELF");
// userTokens.Items[1].ChainId.ShouldBe("AELF");
// userTokens.Items[4].Symbol.ShouldBe("SGR-1");
// userTokens.Items[4].Label.ShouldBe("SGR");
// userTokens.Items[4].IsDisplay.ShouldBe(true);
// userTokens.Items[4].ChainId.ShouldBe("tDVW");
// userTokens.Items[5].Symbol.ShouldBe("SGR-1");
// userTokens.Items[5].Label.ShouldBe("SGR");
// userTokens.Items[5].IsDisplay.ShouldBe(true);
// userTokens.Items[5].ChainId.ShouldBe("AELF");
//
// await _userTokenAppService.ChangeTokenDisplayAsync("tDVW-SGR-1", false);
// await _userTokenAppService.ChangeTokenDisplayAsync("AELF-SGR-1", false);
// Thread.Sleep(2000);
//
// userTokens = await _userTokenAppService.GetTokensAsync(new GetTokenInfosRequestDto());
// userTokens.TotalCount.ShouldBe(6);
// userTokens.Items.Count.ShouldBe(6);
// userTokens.Items[4].Symbol.ShouldBe("SGR-1");
// userTokens.Items[4].IsDisplay.ShouldBe(false);
// userTokens.Items[5].Symbol.ShouldBe("SGR-1");
// userTokens.Items[5].IsDisplay.ShouldBe(false);
//
// userTokens = await _userTokenAppService.GetTokensAsync(new GetTokenInfosRequestDto()
// {
// Keyword = "el",
// ChainIds = new List<string>()
// {
// "tDVW"
// }
// });
// userTokens.TotalCount.ShouldBe(1);
// userTokens.Items.Count.ShouldBe(1);
// userTokens.Items[0].Symbol.ShouldBe("ELF");
// userTokens.Items[0].ChainId.ShouldBe("tDVW");
// }
}

0 comments on commit 0fe3be7

Please sign in to comment.