Esempio n. 1
0
 public async Task <AuthorizationAPIResult> CreateTokenAsync(TokenGenesisBlock tokenBlock)
 {
     if (!CheckServiceStatus())
     {
         return(null);
     }
     return(await _trans.CreateTokenAsync(tokenBlock));
 }
        public async Task <SimpleJsonAPIResult> AuthorizeAsync(string blockType, string jsonBlock)
        {
            BlockTypes types;

            try
            {
                types = (BlockTypes)Enum.Parse(typeof(BlockTypes), blockType);
            }
            catch (Exception)
            {
                return(new SimpleJsonAPIResult {
                    ResultCode = APIResultCodes.InvalidBlockType
                });
            }

            var br = new BlockAPIResult
            {
                BlockData       = jsonBlock,
                ResultBlockType = types
            };

            var block = br.GetBlock();

            if (block == null)
            {
                return(new SimpleJsonAPIResult {
                    ResultCode = APIResultCodes.InvalidBlockData
                });
            }

            // block is valid. send it to consensus network
            AuthorizationAPIResult result;

            switch (types)
            {
            case BlockTypes.SendTransfer:
                result = await _trans.SendTransferAsync(block as SendTransferBlock);

                break;

            case BlockTypes.ReceiveTransfer:
                result = await _trans.ReceiveTransferAsync(block as ReceiveTransferBlock);

                break;

            case BlockTypes.OpenAccountWithReceiveTransfer:
                result = await _trans.ReceiveTransferAndOpenAccountAsync(block as OpenWithReceiveTransferBlock);

                break;

            case BlockTypes.TokenGenesis:
                result = await _trans.CreateTokenAsync(block as TokenGenesisBlock);

                break;

            default:
                result = null;
                break;
            }

            if (result == null)
            {
                return new SimpleJsonAPIResult {
                           ResultCode = APIResultCodes.UnsupportedBlockType
                }
            }
            ;

            return(new SimpleJsonAPIResult {
                ResultCode = result.ResultCode
            });
        }
        public async Task <AuthorizationAPIResult> CreateTokenAsync(
            string tokenName,
            string domainName,
            string description,
            sbyte precision,
            decimal supply,
            bool isFinalSupply,
            string owner,               // shop name
            string address,             // shop URL
            string currency,            // USD
            ContractTypes contractType, // reward or discount or custom
            Dictionary <string, string> tags)
        {
            if (string.IsNullOrWhiteSpace(domainName))
            {
                domainName = "Custom";
            }

            string ticker = domainName + "/" + tokenName;

            var blockresult = await _node.GetLastServiceBlockAsync();

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

            ServiceBlock lastServiceBlock = blockresult.GetBlock() as ServiceBlock;

            TransactionBlock latestBlock = await GetLatestBlockAsync();

            if (latestBlock == null || latestBlock.Balances[LyraGlobal.OFFICIALTICKERCODE] < lastServiceBlock.TokenGenerationFee.ToBalanceLong())
            {
                //throw new Exception("Insufficent funds");
                return(new AuthorizationAPIResult()
                {
                    ResultCode = APIResultCodes.InsufficientFunds
                });
            }

            var svcBlockResult = await _node.GetLastServiceBlockAsync();

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

            // initiate test coins
            TokenGenesisBlock tokenBlock = new TokenGenesisBlock
            {
                Ticker        = ticker,
                DomainName    = domainName,
                Description   = description,
                Precision     = precision,
                IsFinalSupply = isFinalSupply,
                //CustomFee = 0,
                //CustomFeeAccountId = string.Empty,
                AccountID    = AccountId,
                Balances     = new Dictionary <string, long>(),
                ServiceHash  = svcBlockResult.GetBlock().Hash,
                Fee          = lastServiceBlock.TokenGenerationFee,
                FeeCode      = LyraGlobal.OFFICIALTICKERCODE,
                FeeType      = AuthorizationFeeTypes.Regular,
                Owner        = owner,
                Address      = address,
                Currency     = currency,
                Tags         = tags,
                RenewalDate  = DateTime.UtcNow.Add(TimeSpan.FromDays(3650)),
                ContractType = contractType,
                VoteFor      = latestBlock.VoteFor
            };
            // TO DO - set service hash

            //var transaction = new TransactionInfo() { TokenCode = ticker, Amount = supply * (long)Math.Pow(10, precision) };
            var transaction = new TransactionInfo()
            {
                TokenCode = ticker, Amount = supply
            };

            tokenBlock.Balances.Add(transaction.TokenCode, transaction.Amount.ToBalanceLong()); // This is current supply in atomic units (1,000,000.00)
            tokenBlock.Balances.Add(LyraGlobal.OFFICIALTICKERCODE, latestBlock.Balances[LyraGlobal.OFFICIALTICKERCODE] - lastServiceBlock.TokenGenerationFee.ToBalanceLong());
            //tokenBlock.Transaction = transaction;
            // transfer unchanged token balances from the previous block
            foreach (var balance in latestBlock.Balances)
            {
                if (!(tokenBlock.Balances.ContainsKey(balance.Key)))
                {
                    tokenBlock.Balances.Add(balance.Key, balance.Value);
                }
            }

            await tokenBlock.InitializeBlockAsync(latestBlock, _signer);

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

            var result = await _trans.CreateTokenAsync(tokenBlock);

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

            return(result);
        }