async Task <TransactionBlock> SendTokenFromDaoToOrderAsync(DagSystem sys, SendTransferBlock send)
        {
            var order = JsonConvert.DeserializeObject <OTCOrder>(send.Tags["data"]);

            var lastblock = await sys.Storage.FindLatestBlockAsync(order.daoId) as TransactionBlock;

            var keyStr    = $"{send.Hash.Substring(0, 16)},{order.crypto},{send.AccountID}";
            var AccountId = Base58Encoding.EncodeAccountId(Encoding.ASCII.GetBytes(keyStr).Take(64).ToArray());

            var sb = await sys.Storage.GetLastServiceBlockAsync();

            var sendToOrderBlock = new DaoSendBlock
            {
                // block
                ServiceHash = sb.Hash,

                // trans
                Fee       = 0,
                FeeCode   = LyraGlobal.OFFICIALTICKERCODE,
                FeeType   = AuthorizationFeeTypes.NoFee,
                AccountID = lastblock.AccountID,

                // send
                DestinationAccountId = AccountId,

                // broker
                Name           = ((IBrokerAccount)lastblock).Name,
                OwnerAccountId = ((IBrokerAccount)lastblock).OwnerAccountId,
                RelatedTx      = send.Hash,

                // profiting
                PType     = ((IProfiting)lastblock).PType,
                ShareRito = ((IProfiting)lastblock).ShareRito,
                Seats     = ((IProfiting)lastblock).Seats,

                // dao
                SellerFeeRatio = ((IDao)lastblock).SellerFeeRatio,
                BuyerFeeRatio  = ((IDao)lastblock).BuyerFeeRatio,
                SellerPar      = ((IDao)lastblock).SellerPar,
                BuyerPar       = ((IDao)lastblock).BuyerPar,
                Description    = ((IDao)lastblock).Description,
                Treasure       = ((IDao)lastblock).Treasure.ToDecimalDict().ToLongDict(),
            };

            // calculate balance
            var dict = lastblock.Balances.ToDecimalDict();

            dict[order.crypto] -= order.amount;
            dict[LyraGlobal.OFFICIALTICKERCODE] -= 2;   // for delist and close use later
            sendToOrderBlock.Balances            = dict.ToLongDict();

            sendToOrderBlock.AddTag(Block.MANAGEDTAG, "");   // value is always ignored

            sendToOrderBlock.InitializeBlock(lastblock, NodeService.Dag.PosWallet.PrivateKey, AccountId: NodeService.Dag.PosWallet.AccountId);
            return(sendToOrderBlock);
        }
        protected async Task <TransactionBlock> SendCollateralToBuyerAsync(DagSystem sys, SendTransferBlock sendBlock)
        {
            var tradelatest = await sys.Storage.FindLatestBlockAsync(sendBlock.DestinationAccountId) as TransactionBlock;

            var trade        = (tradelatest as IOtcTrade).Trade;
            var daolastblock = await sys.Storage.FindLatestBlockAsync(trade.daoId) as TransactionBlock;

            // get dao for order genesis
            var odrgen = await sys.Storage.FindFirstBlockAsync(trade.orderId) as ReceiveTransferBlock;

            var daoforodr = await sys.Storage.FindBlockByHashAsync(odrgen.SourceHash) as IDao;

            // buyer fee calculated as LYR
            var buyerFee       = Math.Round(((trade.pay * (odrgen as IOtcOrder).Order.fiatPrice) * daoforodr.BuyerFeeRatio) / (odrgen as IOtcOrder).Order.collateralPrice, 8);
            var amountToSeller = trade.collateral - buyerFee;

            var sb = await sys.Storage.GetLastServiceBlockAsync();

            var sendCollateral = new DaoSendBlock
            {
                // block
                ServiceHash = sb.Hash,

                // trans
                Fee       = 0,
                FeeCode   = LyraGlobal.OFFICIALTICKERCODE,
                FeeType   = AuthorizationFeeTypes.NoFee,
                AccountID = daolastblock.AccountID,

                // send
                DestinationAccountId = (tradelatest as IBrokerAccount).OwnerAccountId,

                // broker
                Name           = ((IBrokerAccount)daolastblock).Name,
                OwnerAccountId = ((IBrokerAccount)daolastblock).OwnerAccountId,
                RelatedTx      = sendBlock.Hash,

                // profiting
                PType     = ((IProfiting)daolastblock).PType,
                ShareRito = ((IProfiting)daolastblock).ShareRito,
                Seats     = ((IProfiting)daolastblock).Seats,

                // dao
                SellerFeeRatio = ((IDao)daolastblock).SellerFeeRatio,
                BuyerFeeRatio  = ((IDao)daolastblock).BuyerFeeRatio,
                SellerPar      = ((IDao)daolastblock).SellerPar,
                BuyerPar       = ((IDao)daolastblock).BuyerPar,
                Description    = ((IDao)daolastblock).Description,
                Treasure       = ((IDao)daolastblock).Treasure.ToDecimalDict().ToLongDict(),
            };

            // calculate balance
            var dict = daolastblock.Balances.ToDecimalDict();

            dict[LyraGlobal.OFFICIALTICKERCODE] -= amountToSeller;
            sendCollateral.Balances              = dict.ToLongDict();

            sendCollateral.AddTag(Block.MANAGEDTAG, "");   // value is always ignored

            sendCollateral.InitializeBlock(daolastblock, NodeService.Dag.PosWallet.PrivateKey, AccountId: NodeService.Dag.PosWallet.AccountId);
            return(sendCollateral);
        }
Example #3
0
        protected async Task <TransactionBlock> SendCollateralToSellerAsync(DagSystem sys, SendTransferBlock send)
        {
            var daoid       = send.Tags["daoid"];
            var orderid     = send.Tags["orderid"];
            var orderlatest = await sys.Storage.FindLatestBlockAsync(orderid) as TransactionBlock;

            var daolastblock = await sys.Storage.FindLatestBlockAsync(daoid) as TransactionBlock;

            // get dao for order genesis
            var odrgen = await sys.Storage.FindFirstBlockAsync(orderid) as ReceiveTransferBlock;

            var daoforodr = await sys.Storage.FindBlockByHashAsync(odrgen.SourceHash) as IDao;

            // calculate fees
            // dao fee + network fee

            var allTrades = await sys.Storage.FindOtcTradeForOrderAsync(orderid);

            var totalAmount = allTrades.Cast <IOtcTrade>()
                              .Where(a => a.OTStatus == OTCTradeStatus.CryptoReleased)
                              .Sum(a => a.Trade.amount);

            // seller fee calculated as LYR
            var order          = (odrgen as IOtcOrder).Order;
            var sellerFee      = Math.Round((((totalAmount * order.price) * order.fiatPrice) * daoforodr.SellerFeeRatio) / order.collateralPrice, 8);
            var networkFee     = Math.Round((((totalAmount * order.price) * order.fiatPrice) * 0.002m) / order.collateralPrice, 8);
            var amountToSeller = order.collateral - sellerFee - networkFee;

            var sb = await sys.Storage.GetLastServiceBlockAsync();

            var sendCollateral = new DaoSendBlock
            {
                // block
                ServiceHash = sb.Hash,

                // trans
                Fee       = networkFee,
                FeeCode   = LyraGlobal.OFFICIALTICKERCODE,
                FeeType   = AuthorizationFeeTypes.Dynamic,
                AccountID = daolastblock.AccountID,

                // send
                DestinationAccountId = (orderlatest as IBrokerAccount).OwnerAccountId,

                // broker
                Name           = ((IBrokerAccount)daolastblock).Name,
                OwnerAccountId = ((IBrokerAccount)daolastblock).OwnerAccountId,
                RelatedTx      = send.Hash,

                // profiting
                PType     = ((IProfiting)daolastblock).PType,
                ShareRito = ((IProfiting)daolastblock).ShareRito,
                Seats     = ((IProfiting)daolastblock).Seats,

                // dao
                SellerFeeRatio = ((IDao)daolastblock).SellerFeeRatio,
                BuyerFeeRatio  = ((IDao)daolastblock).BuyerFeeRatio,
                SellerPar      = ((IDao)daolastblock).SellerPar,
                BuyerPar       = ((IDao)daolastblock).BuyerPar,
                Description    = ((IDao)daolastblock).Description,
                Treasure       = ((IDao)daolastblock).Treasure.ToDecimalDict().ToLongDict(),
            };

            // calculate balance
            var dict = daolastblock.Balances.ToDecimalDict();

            dict[LyraGlobal.OFFICIALTICKERCODE] -= amountToSeller + networkFee;
            sendCollateral.Balances              = dict.ToLongDict();

            sendCollateral.AddTag(Block.MANAGEDTAG, "");   // value is always ignored

            sendCollateral.InitializeBlock(daolastblock, NodeService.Dag.PosWallet.PrivateKey, AccountId: NodeService.Dag.PosWallet.AccountId);
            return(sendCollateral);
        }