public static async Task ProcessOfferFinalised(MySqlConnection connection, int blockchainID, Web3 cl,
                                                       string contractAddress, EventLog <List <ParameterOutput> > eventLog)
        {
            using (await LockManager.GetLock(LockType.OfferFinalised).Lock())
            {
                var offerId =
                    HexHelper.ByteArrayToString((byte[])eventLog.Event
                                                .First(e => e.Parameter.Name == "offerId").Result);

                if (!OTContract_Holding_OfferCreated.Exists(connection, offerId, blockchainID))
                {
                    //Lets get this via syncing later on as we've missed the creation of the job
                    return;
                }

                if (OTContract_Holding_OfferFinalized.Exists(connection, offerId, blockchainID))
                {
                    return;
                }

                var block = await BlockHelper.GetBlock(connection, eventLog.Log.BlockHash, eventLog.Log.BlockNumber,
                                                       cl, blockchainID);


                var holder1 = (string)eventLog.Event.FirstOrDefault(e => e.Parameter.Name == "holder1")
                              .Result;
                var holder2 = (string)eventLog.Event.FirstOrDefault(e => e.Parameter.Name == "holder2")
                              .Result;
                var holder3 = (string)eventLog.Event.FirstOrDefault(e => e.Parameter.Name == "holder3")
                              .Result;

                var receipt = cl.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(eventLog.Log.TransactionHash);

                var transaction =
                    cl.Eth.Transactions.GetTransactionByHash.SendRequestAsync(eventLog.Log.TransactionHash);

                await transaction;
                await receipt;

                var row = new OTContract_Holding_OfferFinalized
                {
                    TransactionHash = eventLog.Log.TransactionHash,
                    BlockNumber     = (UInt64)eventLog.Log.BlockNumber.Value,
                    Timestamp       = block.Timestamp,
                    OfferID         = offerId,
                    Holder1         = holder1,
                    Holder2         = holder2,
                    Holder3         = holder3,
                    ContractAddress = contractAddress,
                    GasUsed         = (UInt64)receipt.Result.GasUsed.Value,
                    Data            = eventLog.Log.Data,
                    GasPrice        = (UInt64)transaction.Result.GasPrice.Value,
                    BlockchainID    = blockchainID
                };

                OTContract_Holding_OfferFinalized.InsertIfNotExist(connection, row);
            }
        }
Example #2
0
        public static async Task Execute(MySqlConnection connection, int blockchainID, BlockchainType blockchain, BlockchainNetwork network)
        {
            using (await LockManager.GetLock(LockType.ProcessJobs).Lock())
            {
                if (blockchain == BlockchainType.xDai || blockchain == BlockchainType.Polygon)
                {
                    OTContract_Holding_OfferCreated[] offersToCalcPriceFactor =
                        OTContract_Holding_OfferCreated.GetWithoutEstimatedPriceFactor(connection, blockchainID);

                    foreach (OTContract_Holding_OfferCreated offer in offersToCalcPriceFactor)
                    {
                        (decimal lambda, int confidence)priceFactor = GetPriceFactor(offer.HoldingTimeInMinutes, offer.TokenAmountPerHolder, offer.GasPrice, offer.DataSetSizeInBytes);

                        await OTOffer.UpdateLambda(connection, offer.BlockchainID, offer.OfferID, priceFactor.lambda, priceFactor.confidence);
                    }
                }

                OTContract_Holding_OfferCreated[] offersToAdd =
                    OTContract_Holding_OfferCreated.GetUnprocessed(connection, blockchainID);

                if (offersToAdd.Any())
                {
                    Console.WriteLine("Found " + offersToAdd.Length + " unprocessed offer created events.");
                }

                foreach (var offerToAdd in offersToAdd)
                {
                    OTOffer offer = new OTOffer
                    {
                        CreatedTimestamp            = offerToAdd.Timestamp,
                        OfferID                     = offerToAdd.OfferID,
                        CreatedTransactionHash      = offerToAdd.TransactionHash,
                        CreatedBlockNumber          = offerToAdd.BlockNumber,
                        DCNodeId                    = offerToAdd.DCNodeId,
                        DataSetId                   = offerToAdd.DataSetId,
                        DataSetSizeInBytes          = offerToAdd.DataSetSizeInBytes,
                        HoldingTimeInMinutes        = offerToAdd.HoldingTimeInMinutes,
                        IsFinalized                 = false,
                        LitigationIntervalInMinutes = offerToAdd.LitigationIntervalInMinutes,
                        TransactionIndex            = offerToAdd.TransactionIndex,
                        TokenAmountPerHolder        = offerToAdd.TokenAmountPerHolder,
                        BlockchainID                = blockchainID
                    };


                    //ETH has dynamic tracInBaseCurrency which is more annoying to implement
                    if (blockchain == BlockchainType.xDai || blockchain == BlockchainType.Polygon)
                    {
                        (decimal lambda, int confidence)priceFactor = GetPriceFactor(offer.HoldingTimeInMinutes, offer.TokenAmountPerHolder, offerToAdd.GasPrice, offer.DataSetSizeInBytes);

                        offer.EstimatedLambda           = priceFactor.lambda;
                        offer.EstimatedLambdaConfidence = priceFactor.confidence;
                    }

                    OTOffer.InsertIfNotExist(connection, offer);

                    OTContract_Holding_OfferCreated.SetProcessed(connection, offerToAdd);
                }

                OTContract_Holding_OfferFinalized[] offersToFinalize =
                    OTContract_Holding_OfferFinalized.GetUnprocessed(connection, blockchainID);

                if (offersToFinalize.Any())
                {
                    Console.WriteLine("Found " + offersToFinalize.Length + " unprocessed offer finalized events.");
                }

                foreach (OTContract_Holding_OfferFinalized offerToFinalize in offersToFinalize)
                {
                    await OTOffer.FinalizeOffer(connection, offerToFinalize.OfferID, offerToFinalize.BlockNumber,
                                                offerToFinalize.TransactionHash, offerToFinalize.Holder1, offerToFinalize.Holder2,
                                                offerToFinalize.Holder3, offerToFinalize.Timestamp, blockchainID);

                    OTContract_Holding_OfferFinalized.SetProcessed(connection, offerToFinalize);
                }

                if (offersToFinalize.Any())
                {
                    RabbitMqService.OfferFinalized(offersToFinalize.Select(offerToFinalize =>
                                                                           new OfferFinalizedMessage
                    {
                        OfferID      = offerToFinalize.OfferID,
                        BlockchainID = blockchainID,
                        Timestamp    = offerToFinalize.Timestamp,
                        Holder1      = offerToFinalize.Holder1,
                        Holder2      = offerToFinalize.Holder2,
                        Holder3      = offerToFinalize.Holder3
                    }));
                }
            }
        }