Example #1
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
                    }));
                }
            }
        }