示例#1
0
        private static IEnumerable <string> CreateAssets(IEnumerable <ContractMetadata> contracts, bool skipTransferCheck)
        {
            var assetIds = new ConcurrentBag <string>();

            ResetOutputCounter();

            Console.WriteLine("Creating assets...");

            Parallel.ForEach(contracts, contract =>
            {
                var operationResult    = "";
                var operationSucceeded = true;

                try
                {
                    var tokenAddress = contract.Address.ToLowerInvariant();
                    var erc20Token   = _assetsService.Erc20TokenGetByAddress(tokenAddress);

                    if (erc20Token == null)
                    {
                        erc20Token = new Erc20Token
                        {
                            Address = tokenAddress
                        };

                        if (!skipTransferCheck)
                        {
                            if (!ContractHasOrCallsTransferMethod(contract))
                            {
                                operationResult    = "transfer method not detected";
                                operationSucceeded = false;
                            }
                        }

                        if (operationSucceeded)
                        {
                            if (TryGetTokenDecimals(contract, out var tokenDecimals))
                            {
                                erc20Token.TokenDecimals = tokenDecimals;
                            }
                            else
                            {
                                operationResult    = "can not get token decimals";
                                operationSucceeded = false;
                            }
                        }

                        if (operationSucceeded)
                        {
                            if (TryGetTokenSymbol(contract, out var tokenSymbol))
                            {
                                erc20Token.TokenName   = GetTokenName(contract, tokenSymbol);
                                erc20Token.TokenSymbol = tokenSymbol;
                            }
                            else
                            {
                                operationResult    = "can not get token symbol";
                                operationSucceeded = false;
                            }
                        }

                        if (operationSucceeded)
                        {
                            erc20Token.TokenTotalSupply = GetTokenTotalSupply(contract);
                        }

                        // Bug workaround
                        try
                        {
                            _assetsService.Erc20TokenAdd(erc20Token);
                        }
                        catch (HttpOperationException e) when(e.Response.StatusCode == HttpStatusCode.OK)
                        {
                        }
                    }

                    if (operationSucceeded)
                    {
                        var assetId      = contract.AssetId;
                        Asset asset      = null;
                        bool updateAsset = !string.IsNullOrEmpty(contract.AssetId);

                        if (updateAsset)
                        {
                            asset = _assetsService.AssetGet(contract.AssetId);

                            if (asset == null)
                            {
                                Console.WriteLine($"Wrong assetId {contract.AssetId}");

                                return;
                            }
                        }

                        if (!updateAsset && string.IsNullOrEmpty(assetId))
                        {
                            asset = _assetsService.Erc20TokenCreateAsset(contract.Address);

                            assetId = asset.Id;

                            operationResult = $"asset {assetId} created";
                        }
                        else
                        {
                            operationResult = "asset already exists";
                        }

                        if (updateAsset)
                        {
                            var oldTokens = _assetsService.Erc20TokenGetBySpecification(new Erc20TokenSpecification()
                            {
                                Ids = new List <string>()
                                {
                                    assetId
                                }
                            });

                            var oldToken = oldTokens.Items?.FirstOrDefault();
                            if (oldToken != null)
                            {
                                oldToken.AssetId = null;
                                _assetsService.Erc20TokenUpdate(oldToken);
                            }

                            asset.BlockChainAssetId = tokenAddress;
                            erc20Token         = erc20Token ?? _assetsService.Erc20TokenGetByAddress(tokenAddress);
                            erc20Token.AssetId = assetId;
                            asset.Blockchain   = Blockchain.Ethereum;
                            asset.Type         = AssetType.Erc20Token;
                            _assetsService.AssetUpdate(asset);
                            _assetsService.Erc20TokenUpdate(erc20Token);
                        }
                        else
                        {
                            assetIds.Add(assetId);
                        }
                    }
                }
                catch (Exception e)
                {
                    operationResult    = e.ToOperationResult();
                    operationSucceeded = false;
                }

                OutputOperationResult(contract.Address, operationResult, operationSucceeded);
            });

            return(assetIds);
        }