コード例 #1
0
 private async Task <Token> GetToken(Web3.Models.DTOs.Transaction transaction, MongoRepository <Token> tokenRepository)
 {
     return(await tokenRepository.GetAsync(x => x.Address == transaction.To));
 }
コード例 #2
0
        public async Task <TokenTransaction> GetTokenData(Web3.Models.DTOs.Transaction transaction, MongoRepository <Token> tokenRepository, MongoRepository <Price> priceRepository)
        {
            var input = transaction.Input;

            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            if (input.Length < 10)
            {
                return(null);
            }

            var token = await GetToken(transaction, tokenRepository);

            if (token == null)
            {
                return(null);
            }

            var price = await GetPriceAsync(token, priceRepository);

            var method = GetMethod(input.Substring(0, 10));

            if (string.IsNullOrEmpty(method))
            {
                return(null);
            }

            var parameters = GetParameters(input.Substring(10));

            if (parameters.Length == 0)
            {
                throw new Exception("No parameters found in the input.");
            }

            TokenTransaction tokenTransaction = null;

            switch (method)
            {
            case "approve":
                tokenTransaction = new TokenTransaction()
                {
                    Method          = method,
                    ContractAddress = token.Address,
                    Name            = token.Name,
                    Symbol          = token.Symbol,
                    From            = transaction.From,
                    To       = GetAddressFromInputParameter(parameters[0]),
                    Value    = parameters[1],
                    Decimals = token.Decimals,
                    PriceBtc = price != null ? price.PriceBtc : 0,
                    PriceUsd = price != null ? price.PriceUsd : 0,
                    PriceEur = price != null ? price.PriceEur : 0,
                };
                break;

            case "transfer":
                tokenTransaction = new TokenTransaction()
                {
                    Method          = method,
                    ContractAddress = token.Address,
                    Name            = token.Name,
                    Symbol          = token.Symbol,
                    From            = transaction.From,
                    To       = GetAddressFromInputParameter(parameters[0]),
                    Value    = parameters[1],
                    Decimals = token.Decimals,
                    PriceBtc = price != null ? price.PriceBtc : 0,
                    PriceUsd = price != null ? price.PriceUsd : 0,
                    PriceEur = price != null ? price.PriceEur : 0,
                };
                break;

            case "transferFrom":
                tokenTransaction = new TokenTransaction()
                {
                    Method          = method,
                    ContractAddress = token.Address,
                    Name            = token.Name,
                    Symbol          = token.Symbol,
                    From            = GetAddressFromInputParameter(parameters[0]),
                    To       = GetAddressFromInputParameter(parameters[1]),
                    Value    = parameters[2],
                    Decimals = token.Decimals,
                    PriceBtc = price != null ? price.PriceBtc : 0,
                    PriceUsd = price != null ? price.PriceUsd : 0,
                    PriceEur = price != null ? price.PriceEur : 0,
                };
                break;

            case "sweep":
                tokenTransaction = new TokenTransaction()
                {
                    Method          = method,
                    ContractAddress = GetAddressFromInputParameter(parameters[0]),
                    Name            = token.Name,
                    Symbol          = token.Symbol,
                    From            = transaction.To,
                    To       = transaction.From,
                    Value    = parameters[1],
                    Decimals = token.Decimals,
                    PriceBtc = price != null ? price.PriceBtc : 0,
                    PriceUsd = price != null ? price.PriceUsd : 0,
                    PriceEur = price != null ? price.PriceEur : 0,
                };
                break;

            case "mint":
                tokenTransaction = new TokenTransaction()
                {
                    Method          = method,
                    ContractAddress = token.Address,
                    Name            = token.Name,
                    Symbol          = token.Symbol,
                    From            = token.Address,
                    To       = GetAddressFromInputParameter(parameters[0]),
                    Value    = parameters[1],
                    Decimals = token.Decimals,
                    PriceBtc = price != null ? price.PriceBtc : 0,
                    PriceUsd = price != null ? price.PriceUsd : 0,
                    PriceEur = price != null ? price.PriceEur : 0,
                };
                break;
            }

            return(tokenTransaction);
        }