void IMedicineBatchTransferBackgroundJob.WaitForTransactionToSuccessThenFinishCreating(MedicineBatchTransfer transfer)
        {
            bool isTransactionSuccess = false;

            do
            {
                var receipt = ethereumService.GetTransactionReceipt(transfer.TransactionHash).Result;
                if (receipt == null)
                {
                    continue;
                }
                if (receipt.Status.Value == (new HexBigInteger(1)).Value)
                {
                    isTransactionSuccess = true;
                    var contractAddress = ethereumService.GetObjectContractAddress(transfer.Id).Result;
                    transfer.ContractAddress   = contractAddress;
                    transfer.TransactionStatus = TransactionStatuses.Success;
                    medicineBatchTransferRepository.Update(transfer);
                }
            }while (isTransactionSuccess != true);
        }
Пример #2
0
        async Task <Guid> IMedicineBatchTransferService.Create(Guid medicineBatchId, Guid fromTenantId, Guid toTenantId, uint quantity)
        {
            var allTransfer = medicineBatchTransferRepository.GetAll();
            var batch       = medicineBatchRepository.Get(medicineBatchId);
            var transfer    = new MedicineBatchTransfer()
            {
                MedicineBatchId = batch.Id,
                FromId          = fromTenantId,
                ToId            = toTenantId,
                Quantity        = quantity,
                IsConfirmed     = false,
                DateCreated     = DateTime.UtcNow
            };

            // TODO: Check inventory of fromTenant

            // Set the tier.
            uint tier = 999;

            if (fromTenantId == batch.ManufacturerId)
            {
                tier = 0;
            }
            else
            {
                var firstTransaction = allTransfer
                                       .Where(t => t.MedicineBatchId == batch.Id && t.FromId == batch.ManufacturerId)
                                       .SingleOrDefault();
                if (fromTenantId == firstTransaction.ToId)
                {
                    tier = 1;
                }
                else
                {
                    // Loop through all parent transactions of FromTenantId
                    var transferPool = allTransfer.Where(t => t.MedicineBatchId == batch.Id).ToList();
                    bool parentMatchCondition(MedicineBatchTransfer t) => t.ToId == transfer.FromId;

                    if (transfer.HasParent(transferPool, parentMatchCondition))
                    {
                        tier = transfer.Parent(transferPool, parentMatchCondition).First().Tier + 1;
                    }
                    // TODO: Some errors occured here.
                }
            }

            transfer.Tier = tier;
            var newTransferId = medicineBatchTransferRepository.CreateAndReturnId(transfer);

            var function        = ethereumService.GetFunction(EthereumFunctions.AddMedicineBatchTransfer);
            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[] {
                newTransferId.ToString(),
                transfer.MedicineBatchId.ToString(),
                transfer.FromId.ToString(),
                transfer.ToId.ToString(),
                transfer.Quantity,
                transfer.DateCreated.ToUnixTimestamp(),
                tier
            });

            transfer.TransactionHash = transactionHash;
            medicineBatchTransferRepository.Update(transfer);


            BackgroundJob.Schedule <IMedicineBatchTransferBackgroundJob>(
                job => job.WaitForTransactionToSuccessThenFinishCreating(transfer),
                TimeSpan.FromSeconds(3)
                );

            return(newTransferId);
        }