Example #1
0
        SendTransferBlock CreateSendDiscountTokenBlock(NonFungibleToken discount_token, TransactionBlock previousBlock)
        {
            decimal TransferFee = serviceAccount.GetLastServiceBlock().TransferFee;

            sbyte precision = _FirstGenesisBlock.Precision;

            //long atomicamount = (long)(50 * Math.Pow(10, precision));

            SendTransferBlock sendBlock = new SendTransferBlock
            {
                AccountID            = AccountId1,
                ServiceHash          = string.Empty,
                DestinationAccountId = AccountId2,
                Balances             = new Dictionary <string, decimal>(),
                Fee     = TransferFee,
                FeeType = AuthorizationFeeTypes.Regular,
                FeeCode = LyraGlobal.OFFICIALTICKERCODE
            };

            sendBlock.Balances.Add(LyraGlobal.OFFICIALTICKERCODE, previousBlock.Balances[LyraGlobal.OFFICIALTICKERCODE] - TransferFee);
            sendBlock.Balances.Add("Custom.DISC", previousBlock.Balances["Custom.DISC"] - 50);

            if (discount_token != null)
            {
                //sendBlock.NonFungibleTokens = new List<INonFungibleToken>();
                //sendBlock.NonFungibleTokens.Add(discount_token);
                sendBlock.NonFungibleToken = discount_token;

                //if (discount_token.OriginHash == null)
                //    discount_token.OriginHash = sendBlock.CalculateHash();
            }


            sendBlock.InitializeBlock(previousBlock, PrivateKey1, AccountId1);

            //sendBlock.Signature = Signatures.GetSignature(PrivateKey1, sendBlock.Hash);

            return(sendBlock);
        }
Example #2
0
        public async Task <APIResultCodes> SendAsync(decimal Amount, string destAccount, string ticker = LyraGlobal.OFFICIALTICKERCODE)
        {
            if (Amount <= 0)
            {
                throw new Exception("Amount must > 0");
            }

            TransactionBlock previousBlock = await GetLatestBlockAsync();

            if (previousBlock == null)
            {
                throw new Exception("No balance");
            }

            var blockresult = await _rpcClient.GetLastServiceBlockAsync();

            if (blockresult.ResultCode != APIResultCodes.Success)
            {
                return(blockresult.ResultCode);
            }

            ServiceBlock lastServiceBlock = blockresult.GetBlock() as ServiceBlock;

            //int precision = await FindTokenPrecision(ticker);
            //if (precision < 0)
            //{

            //    return new AuthorizationAPIResult() { ResultCode = APIResultCodes.TokenGenesisBlockNotFound };
            //}

            //long atomicamount = (long)(Amount * (decimal)Math.Pow(10, precision));
            var balance_change = Amount;

            //var transaction = new TransactionInfo() { TokenCode = ticker, Amount = atomicamount };

            var fee = lastServiceBlock.TransferFee;

            if (ticker == LyraGlobal.OFFICIALTICKERCODE)
            {
                balance_change += fee;
            }

            // see if we have enough tokens
            if (previousBlock.Balances[ticker] < balance_change.ToBalanceLong())
            {
                return(APIResultCodes.InsufficientFunds);
                //throw new ApplicationException("Insufficient funds");
            }

            // see if we have enough LYR to pay the transfer fee
            if (ticker != LyraGlobal.OFFICIALTICKERCODE)
            {
                if (!previousBlock.Balances.ContainsKey(LyraGlobal.OFFICIALTICKERCODE) || previousBlock.Balances[LyraGlobal.OFFICIALTICKERCODE] < fee.ToBalanceLong())
                {
                    //throw new ApplicationException("Insufficient funds to pay transfer fee");
                    return(APIResultCodes.InsufficientFunds);
                }
            }

            //var svcBlockResult = await _rpcClient.GetLastServiceBlock(AccountId, SignAPICallAsync());
            //if (svcBlockResult.ResultCode != APIResultCodes.Success)
            //{
            //    throw new Exception("Unable to get latest service block.");
            //}

            SendTransferBlock sendBlock;

            sendBlock = new SendTransferBlock()
            {
                AccountID            = _accountId,
                VoteFor              = null,
                ServiceHash          = lastServiceBlock.Hash,
                DestinationAccountId = destAccount,
                Balances             = new Dictionary <string, long>(),
                //PaymentID = string.Empty,
                Fee     = fee,
                FeeCode = LyraGlobal.OFFICIALTICKERCODE,
                FeeType = fee == 0m ? AuthorizationFeeTypes.NoFee : AuthorizationFeeTypes.Regular
            };

            sendBlock.Balances.Add(ticker, previousBlock.Balances[ticker] - balance_change.ToBalanceLong());
            //sendBlock.Transaction = transaction;

            // for customer tokens, we pay fee in LYR (unless they are accepted by authorizers as a fee - TO DO)
            if (ticker != LyraGlobal.OFFICIALTICKERCODE)
            {
                sendBlock.Balances.Add(LyraGlobal.OFFICIALTICKERCODE, previousBlock.Balances[LyraGlobal.OFFICIALTICKERCODE] - fee.ToBalanceLong());
            }

            // transfer unchanged token balances from the previous block
            foreach (var balance in previousBlock.Balances)
            {
                if (!(sendBlock.Balances.ContainsKey(balance.Key)))
                {
                    sendBlock.Balances.Add(balance.Key, balance.Value);
                }
            }

            sendBlock.InitializeBlock(previousBlock, _signer);

            if (!sendBlock.ValidateTransaction(previousBlock))
            {
                return(APIResultCodes.SendTransactionValidationFailed);
                //throw new ApplicationException("ValidateTransaction failed");
            }

            AuthorizationAPIResult result;

            result = await _rpcClient.SendTransferAsync(sendBlock);

            if (result.ResultCode == APIResultCodes.Success)
            {
                LastTxHash = sendBlock.Hash;
            }
            else
            {
                LastTxHash = "";
            }

            return(result.ResultCode);
        }