Example #1
0
        private static async Task <NewTransferAPIResult2> GetSendToPftAsync(DagSystem sys, string pftid)
        {
            Console.WriteLine("CR Dividend: GetSendToPftAsync");
            NewTransferAPIResult2 transfer_info = new NewTransferAPIResult2();
            SendTransferBlock     sendBlock     = await sys.Storage.FindUnsettledSendBlockAsync(pftid);

            if (sendBlock != null)
            {
                TransactionBlock previousBlock = await sys.Storage.FindBlockByHashAsync(sendBlock.PreviousHash) as TransactionBlock;

                if (previousBlock == null)
                {
                    transfer_info.ResultCode = APIResultCodes.CouldNotTraceSendBlockChain;
                }
                else
                {
                    transfer_info.Transfer         = sendBlock.GetBalanceChanges(previousBlock); //CalculateTransaction(sendBlock, previousSendBlock);
                    transfer_info.SourceHash       = sendBlock.Hash;
                    transfer_info.NonFungibleToken = sendBlock.NonFungibleToken;
                    transfer_info.ResultCode       = APIResultCodes.Success;
                }
            }
            else
            {
                transfer_info.ResultCode = APIResultCodes.NoNewTransferFound;
            }
            return(transfer_info);
        }
Example #2
0
        private async Task <AuthorizationAPIResult> ReceiveTransferAsync(NewTransferAPIResult2 new_transfer_info)
        {
            if (await GetLocalAccountHeightAsync() == 0) // if this is new account with no blocks
            {
                return(await OpenStandardAccountWithReceiveBlockAsync(new_transfer_info));
            }

            var svcBlockResult = await _rpcClient.GetLastServiceBlockAsync();

            if (svcBlockResult.ResultCode != APIResultCodes.Success)
            {
                throw new Exception("Unable to get latest service block.");
            }

            var receiveBlock = new ReceiveTransferBlock
            {
                AccountID        = _accountId,
                VoteFor          = null,
                ServiceHash      = svcBlockResult.GetBlock().Hash,
                SourceHash       = new_transfer_info.SourceHash,
                Balances         = new Dictionary <string, long>(),
                Fee              = 0,
                FeeType          = AuthorizationFeeTypes.NoFee,
                FeeCode          = LyraGlobal.OFFICIALTICKERCODE,
                NonFungibleToken = new_transfer_info.NonFungibleToken
            };

            TransactionBlock latestBlock = await GetLatestBlockAsync();

            var latestBalances = latestBlock.Balances.ToDecimalDict();
            var recvBalances   = latestBlock.Balances.ToDecimalDict();

            foreach (var chg in new_transfer_info.Transfer.Changes)
            {
                if (recvBalances.ContainsKey(chg.Key))
                {
                    recvBalances[chg.Key] += chg.Value;
                }
                else
                {
                    recvBalances.Add(chg.Key, chg.Value);
                }
            }

            receiveBlock.Balances = recvBalances.ToLongDict();

            await receiveBlock.InitializeBlockAsync(latestBlock, _signer);

            if (!receiveBlock.ValidateTransaction(latestBlock))
            {
                throw new Exception("ValidateTransaction failed");
            }

            //receiveBlock.Signature = Signatures.GetSignature(PrivateKey, receiveBlock.Hash);

            var result = await _rpcClient.ReceiveTransferAsync(receiveBlock);

            return(result);
        }
Example #3
0
        private static async Task <TransactionBlock> CNOReceiveProfitAsync(DagSystem sys, string relatedTx, string pftid, NewTransferAPIResult2 transInfo)
        {
            Console.WriteLine("CR Dividend: CNOReceiveProfitAsync");

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

            var lastPft = await sys.Storage.FindLatestBlockAsync(pftid) as TransactionBlock;

            var pftNext = new ProfitingBlock
            {
                AccountID    = lastPft.AccountID,
                Balances     = new Dictionary <string, long>(),
                PreviousHash = lastPft.Hash,
                ServiceHash  = sb.Hash,
                Fee          = 0,
                FeeCode      = LyraGlobal.OFFICIALTICKERCODE,
                FeeType      = AuthorizationFeeTypes.NoFee,
                SourceHash   = transInfo.SourceHash,

                // profit specified config
                Name           = ((IBrokerAccount)lastPft).Name,
                OwnerAccountId = ((IBrokerAccount)lastPft).OwnerAccountId,
                PType          = ((IProfiting)lastPft).PType,
                ShareRito      = ((IProfiting)lastPft).ShareRito,
                Seats          = ((IProfiting)lastPft).Seats,
                RelatedTx      = relatedTx
            };

            var recvBalances = lastPft.Balances.ToDecimalDict();

            foreach (var chg in transInfo.Transfer.Changes)
            {
                if (recvBalances.ContainsKey(chg.Key))
                {
                    recvBalances[chg.Key] += chg.Value;
                }
                else
                {
                    recvBalances.Add(chg.Key, chg.Value);
                }
            }

            pftNext.Balances = recvBalances.ToLongDict();

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

            // pool blocks are service block so all service block signed by leader node
            pftNext.InitializeBlock(lastPft, sys.PosWallet.PrivateKey, AccountId: sys.PosWallet.AccountId);

            return(pftNext);
        }
        private async Task <AuthorizationAPIResult> OpenStandardAccountWithReceiveBlockAsync(NewTransferAPIResult2 new_transfer_info)
        {
            var svcBlockResult = await _node.GetLastServiceBlockAsync();

            if (svcBlockResult.ResultCode != APIResultCodes.Success)
            {
                throw new Exception("Unable to get latest service block.");
            }

            var openReceiveBlock = new OpenWithReceiveTransferBlock
            {
                AccountType      = AccountTypes.Standard,
                AccountID        = _accountId,
                ServiceHash      = svcBlockResult.GetBlock().Hash,
                SourceHash       = new_transfer_info.SourceHash,
                Balances         = new Dictionary <string, long>(),
                Fee              = 0,
                FeeType          = AuthorizationFeeTypes.NoFee,
                FeeCode          = LyraGlobal.OFFICIALTICKERCODE,
                NonFungibleToken = new_transfer_info.NonFungibleToken,
                VoteFor          = null
            };

            foreach (var chg in new_transfer_info.Transfer.Changes)
            {
                openReceiveBlock.Balances.Add(chg.Key, chg.Value.ToBalanceLong());
            }
            await openReceiveBlock.InitializeBlockAsync(null, _signer);

            //openReceiveBlock.Signature = Signatures.GetSignature(PrivateKey, openReceiveBlock.Hash);

            var result = await _trans.ReceiveTransferAndOpenAccountAsync(openReceiveBlock);

            if (result.ResultCode == APIResultCodes.Success)
            {
                LastBlock = openReceiveBlock;
            }
            else
            {
                LastBlock = null;
            }
            return(result);
        }