async Task <Guid> IMedicineBatchService.Create(
            string batchNumber,
            Guid medicineId,
            Guid manufacturerId,
            DateTime manufactureDate,
            DateTime expiryDate,
            uint quantity,
            string unit,
            string certificates)
        {
            var medicineBatch = new MedicineBatch()
            {
                BatchNumber     = batchNumber,
                MedicineId      = medicineId,
                ManufacturerId  = manufacturerId,
                ManufactureDate = manufactureDate,
                ExpiryDate      = expiryDate,
                Quantity        = quantity,
                Unit            = unit,
                Certificates    = certificates,
                DateCreated     = DateTime.UtcNow
            };
            Guid newMedicineBatchId = medicineBatchRepository.CreateAndReturnId(medicineBatch);

            var function        = ethereumService.GetFunction(EthereumFunctions.AddMedicineBatch);
            var transactionHash = await function.SendTransactionAsync(
                ethereumService.GetEthereumAccount(),
                new HexBigInteger(6000000),
                new HexBigInteger(Nethereum.Web3.Web3.Convert.ToWei(10, UnitConversion.EthUnit.Gwei)),
                new HexBigInteger(0),
                functionInput : new object[] {
                newMedicineBatchId.ToString(),
                medicineBatch.MedicineId.ToString(),
                medicineBatch.BatchNumber,
                medicineBatch.ManufacturerId.ToString()
            });

            medicineBatch.TransactionHash = transactionHash;
            medicineBatchRepository.Update(medicineBatch);

            BackgroundJob.Schedule <IMedicineBatchBackgroundJob>(
                medicineBatchBackgroundJob => medicineBatchBackgroundJob.WaitForTransactionToSuccessThenFinishCreatingMedicineBatch(medicineBatch),
                TimeSpan.FromSeconds(3)
                );

            return(newMedicineBatchId);
        }
Example #2
0
        void IMedicineBatchBackgroundJob.WaitForTransactionToSuccessThenFinishCreatingMedicineBatch(MedicineBatch medicineBatch)
        {
            bool isTransactionSuccess = false;

            do
            {
                var receipt = ethereumService.GetTransactionReceipt(medicineBatch.TransactionHash).Result;
                if (receipt == null)
                {
                    continue;
                }
                if (receipt.Status.Value == (new HexBigInteger(1)).Value)
                {
                    isTransactionSuccess = true;
                    var contractAddress = ethereumService.GetObjectContractAddress(medicineBatch.Id).Result;
                    medicineBatch.ContractAddress   = contractAddress;
                    medicineBatch.TransactionStatus = Models.Database.TransactionStatuses.Success;
                    medicineBatchRepository.Update(medicineBatch);

                    var medicineBatchContract = ethereumService.GetContract(MedicineBatchAbi, medicineBatch.ContractAddress);
                    var updateFunction        = ethereumService.GetFunction(medicineBatchContract, EthereumFunctions.UpdateMedicineBatchInformation);
                    var updateReceipt         = updateFunction.SendTransactionAndWaitForReceiptAsync(
                        ethereumService.GetEthereumAccount(),
                        new HexBigInteger(6000000),
                        new HexBigInteger(Nethereum.Web3.Web3.Convert.ToWei(50, UnitConversion.EthUnit.Gwei)),
                        new HexBigInteger(0),
                        functionInput: new object[] {
                        medicineBatch.MedicineId.ToString(),
                        medicineBatch.BatchNumber,
                        medicineBatch.ManufacturerId.ToString(),
                        medicineBatch.Quantity,
                        medicineBatch.Unit,
                        medicineBatch.ManufactureDate.ToUnixTimestamp(),
                        medicineBatch.ExpiryDate.ToUnixTimestamp()
                    }).Result;
                }
            }while (isTransactionSuccess != true);
        }