public async Task AddOrReplaceAsync(GasPriceEntity entity) { entity.PartitionKey = GetPartitionKey(); entity.RowKey = GetRowKey(); await _table.InsertOrReplaceAsync(entity); }
public async Task <BigInteger> CalculateGasPriceAsync(string to, BigInteger amount) { var estimatedGasPrice = await _ethereum.EstimateGasPriceAsync(to, amount); var minMaxGasPrice = await _gasPriceRepository.TryGetAsync(); if (minMaxGasPrice == null) { minMaxGasPrice = new GasPriceEntity { Max = _defaultMaxGasPrice, Min = _defaultMinGasPrice }; await _gasPriceRepository.AddOrReplaceAsync(minMaxGasPrice); } if (estimatedGasPrice <= minMaxGasPrice.Min) { return(minMaxGasPrice.Min); } if (estimatedGasPrice >= minMaxGasPrice.Max) { return(minMaxGasPrice.Max); } return(estimatedGasPrice); }
public void CalculateGasPriceAsync__ExpectedResultReturned( string defaultMinGasPrice, string defaultMaxGasPrice, string estimatedResultStr, string expectedResultStr, bool isSettingsStored) { string to = "0x83F0726180Cf3964b69f62AC063C5Cb9A66B3bE5"; BigInteger amount = 1000000000; BigInteger estimatedResult = BigInteger.Parse(estimatedResultStr); BigInteger expectedResult = BigInteger.Parse(expectedResultStr); GasPriceEntity newGasPriceDto = new GasPriceEntity() { Max = BigInteger.Parse(defaultMaxGasPrice), Min = BigInteger.Parse(defaultMinGasPrice), }; GasPriceEntity gasPriceDto = isSettingsStored ? newGasPriceDto : null; #region Mock EthereumClassicApiSettings settings = new EthereumClassicApiSettings(); Mock <IEthereum> ethereum = new Mock <IEthereum>(); Mock <IGasPriceRepository> gasPriceRepository = new Mock <IGasPriceRepository>(); settings.DefaultMinGasPrice = defaultMinGasPrice; settings.DefaultMaxGasPrice = defaultMaxGasPrice; ethereum.Setup(x => x.EstimateGasPriceAsync(to, amount)).Returns(Task.FromResult(estimatedResult)); gasPriceRepository.Setup(x => x.TryGetAsync()).Returns(Task.FromResult(gasPriceDto)); gasPriceRepository.Setup(x => x.AddOrReplaceAsync(It.IsAny <GasPriceEntity>())) .Returns(Task.FromResult(0)).Verifiable(); #endregion GasPriceOracleService service = new GasPriceOracleService(settings, ethereum.Object, gasPriceRepository.Object); //ACT var actualCalculated = service.CalculateGasPriceAsync(to, amount).Result; //ASSERT Assert.AreEqual(expectedResult, actualCalculated); if (gasPriceDto == null) { gasPriceRepository.Verify(x => x.AddOrReplaceAsync(It.IsAny <GasPriceEntity>()), Times.Once()); } }