public override Task <CashSwapOperationResponse> CashSwap(CashSwapOperation request, ServerCallContext context) { using var activity = MyTelemetry.StartActivity("CashSwap"); activity?.AddTag("operationId", request.Id) .AddTag("brokerId", request.BrokerId) .AddTag("accountId1", request.AccountId1) .AddTag("WalletId1", request.WalletId1) .AddTag("assetId1", request.AssetId1) .AddTag("volume1", request.Volume1) .AddTag("accountId2", request.AccountId2) .AddTag("WalletId2", request.WalletId2) .AddTag("assetId2", request.AssetId2) .AddTag("volume2", request.Volume2); return(_cashServiceClient.CashSwapAsync(request, cancellationToken: context.CancellationToken) .ResponseAsync); }
public async Task CashSwap() { AccountEntity testAccount1 = (AccountEntity)await this.AccountRepository.TryGetAsync(this.TestAccountId1); Assert.NotNull(testAccount1); BalanceDTO accountBalance1Asset1 = testAccount1.BalancesParsed.Where(b => b.Asset == this.TestAsset1).FirstOrDefault(); Assert.NotNull(accountBalance1Asset1); BalanceDTO accountBalance1Asset2 = testAccount1.BalancesParsed.Where(b => b.Asset == this.TestAsset2).FirstOrDefault(); Assert.NotNull(accountBalance1Asset2); AccountEntity testAccount2 = (AccountEntity)await this.AccountRepository.TryGetAsync(this.TestAccountId2); Assert.NotNull(testAccount2); BalanceDTO accountBalance2Asset1 = testAccount2.BalancesParsed.Where(b => b.Asset == this.TestAsset1).FirstOrDefault(); Assert.NotNull(accountBalance2Asset1); BalanceDTO accountBalance2Asset2 = testAccount2.BalancesParsed.Where(b => b.Asset == this.TestAsset2).FirstOrDefault(); Assert.NotNull(accountBalance2Asset2); //Attempt invalid swap double badSwapAmount1 = accountBalance1Asset1.Balance + 0.7; double badSwapAmount2 = accountBalance2Asset2.Balance - 0.2; // this one isn't bad, but the transaction should still fail string badSwapId = Guid.NewGuid().ToString(); MeResponseModel badSwapResponse = await this.Consumer.Client.SwapAsync(badSwapId, testAccount1.Id, this.TestAsset1, badSwapAmount1, testAccount2.Id, this.TestAsset2, badSwapAmount2); Assert.True(badSwapResponse.Status == MeStatusCodes.LowBalance); //Attempt a valid swap double swapAmount1 = Math.Round(accountBalance1Asset1.Balance / 10, this.AssetPrecission); double swapAmount2 = Math.Round(accountBalance2Asset2.Balance / 10, this.AssetPrecission); string swapId = Guid.NewGuid().ToString(); MeResponseModel swapReseponse = await this.Consumer.Client.SwapAsync(swapId, testAccount1.Id, this.TestAsset1, swapAmount1, testAccount2.Id, this.TestAsset2, swapAmount2); Assert.True(swapReseponse.Status == MeStatusCodes.Ok); CashSwapOperation message = (CashSwapOperation)await this.WaitForRabbitMQ <CashSwapOperation>(o => o.id == swapId); Assert.True(message.asset1 == this.TestAsset1); Assert.True(message.asset2 == this.TestAsset2); Assert.True(message.clientId1 == testAccount1.Id); Assert.True(message.clientId2 == testAccount2.Id); if (Double.TryParse(message.volume1, NumberStyles.Float, CultureInfo.InvariantCulture, out double parsedVolume)) { Assert.True(parsedVolume == swapAmount1); } if (Double.TryParse(message.volume2, NumberStyles.Float, CultureInfo.InvariantCulture, out parsedVolume)) { Assert.True(parsedVolume == swapAmount2); } CashSwapEntity checkCashSwapOperation = (CashSwapEntity)await this.CashSwapRepository.TryGetAsync(c => c.ExternalId == swapId); Assert.True(checkCashSwapOperation.AssetId1 == this.TestAsset1); Assert.True(checkCashSwapOperation.AssetId2 == this.TestAsset2); Assert.True(checkCashSwapOperation.ClientId1 == testAccount1.Id); Assert.True(checkCashSwapOperation.ClientId2 == testAccount2.Id); Assert.True(checkCashSwapOperation.Amount1 == swapAmount1); Assert.True(checkCashSwapOperation.Amount2 == swapAmount2); AccountEntity checkTestAccount1 = (AccountEntity)await this.AccountRepository.TryGetAsync(this.TestAccountId1); BalanceDTO checkAccountBalance1Asset1 = checkTestAccount1.BalancesParsed.Where(b => b.Asset == this.TestAsset1).FirstOrDefault(); BalanceDTO checkAccountBalance1Asset2 = checkTestAccount1.BalancesParsed.Where(b => b.Asset == this.TestAsset2).FirstOrDefault(); AccountEntity checkTestAccount2 = (AccountEntity)await this.AccountRepository.TryGetAsync(this.TestAccountId2); BalanceDTO checkAccountBalance2Asset1 = checkTestAccount2.BalancesParsed.Where(b => b.Asset == this.TestAsset1).FirstOrDefault(); BalanceDTO checkAccountBalance2Asset2 = checkTestAccount2.BalancesParsed.Where(b => b.Asset == this.TestAsset2).FirstOrDefault(); Assert.True(Math.Round(checkAccountBalance1Asset1.Balance, this.AssetPrecission) == Math.Round(accountBalance1Asset1.Balance - swapAmount1, this.AssetPrecission)); Assert.True(Math.Round(checkAccountBalance2Asset1.Balance, this.AssetPrecission) == Math.Round(accountBalance2Asset1.Balance + swapAmount1, this.AssetPrecission)); Assert.True(Math.Round(checkAccountBalance1Asset2.Balance, this.AssetPrecission) == Math.Round(accountBalance1Asset2.Balance + swapAmount2, this.AssetPrecission)); Assert.True(Math.Round(checkAccountBalance2Asset2.Balance, this.AssetPrecission) == Math.Round(accountBalance2Asset2.Balance - swapAmount2, this.AssetPrecission)); // Attempt swap back string swapBackId = Guid.NewGuid().ToString(); MeResponseModel swapBackReseponse = await this.Consumer.Client.SwapAsync(swapBackId, testAccount2.Id, this.TestAsset1, swapAmount1, testAccount1.Id, this.TestAsset2, swapAmount2); Assert.True(swapBackReseponse.Status == MeStatusCodes.Ok); message = (CashSwapOperation)await this.WaitForRabbitMQ <CashSwapOperation>(o => o.id == swapBackId); Assert.True(message.asset1 == this.TestAsset1); Assert.True(message.asset2 == this.TestAsset2); Assert.True(message.clientId1 == testAccount2.Id); Assert.True(message.clientId2 == testAccount1.Id); if (Double.TryParse(message.volume1, NumberStyles.Float, CultureInfo.InvariantCulture, out parsedVolume)) { Assert.True(parsedVolume == swapAmount1); } if (Double.TryParse(message.volume2, NumberStyles.Float, CultureInfo.InvariantCulture, out parsedVolume)) { Assert.True(parsedVolume == swapAmount2); } checkCashSwapOperation = (CashSwapEntity)await this.CashSwapRepository.TryGetAsync(c => c.ExternalId == swapBackId); Assert.True(checkCashSwapOperation.AssetId1 == this.TestAsset1); Assert.True(checkCashSwapOperation.AssetId2 == this.TestAsset2); Assert.True(checkCashSwapOperation.ClientId1 == testAccount2.Id); Assert.True(checkCashSwapOperation.ClientId2 == testAccount1.Id); Assert.True(checkCashSwapOperation.Amount1 == swapAmount1); Assert.True(checkCashSwapOperation.Amount2 == swapAmount2); checkTestAccount1 = (AccountEntity)await this.AccountRepository.TryGetAsync(this.TestAccountId1); checkAccountBalance1Asset1 = checkTestAccount1.BalancesParsed.Where(b => b.Asset == this.TestAsset1).FirstOrDefault(); checkAccountBalance1Asset2 = checkTestAccount1.BalancesParsed.Where(b => b.Asset == this.TestAsset2).FirstOrDefault(); checkTestAccount2 = (AccountEntity)await this.AccountRepository.TryGetAsync(this.TestAccountId2); checkAccountBalance2Asset1 = checkTestAccount2.BalancesParsed.Where(b => b.Asset == this.TestAsset1).FirstOrDefault(); checkAccountBalance2Asset2 = checkTestAccount2.BalancesParsed.Where(b => b.Asset == this.TestAsset2).FirstOrDefault(); // balances should be back to their initial state Assert.True(accountBalance1Asset1.Balance == checkAccountBalance1Asset1.Balance); Assert.True(accountBalance1Asset2.Balance == checkAccountBalance1Asset2.Balance); Assert.True(accountBalance2Asset1.Balance == checkAccountBalance2Asset1.Balance); Assert.True(accountBalance2Asset2.Balance == checkAccountBalance2Asset2.Balance); }
CashSwapOperationResponse ICashServiceClient.CashSwap(CashSwapOperation request, CancellationToken cancellationToken) { return(CashSwap(request, cancellationToken: cancellationToken)); }
async Task <CashSwapOperationResponse> ICashServiceClient.CashSwapAsync(CashSwapOperation request, CancellationToken cancellationToken) { return(await CashSwapAsync(request, cancellationToken : cancellationToken)); }
public Task <CashSwapOperationResponse> CashSwapAsync(CashSwapOperation request, CancellationToken cancellationToken = new CancellationToken()) { throw new System.NotImplementedException(); }