Esempio n. 1
0
        public async Task <Result <decimal> > GetERC20AllowanceAsync(
            Erc20Config erc20,
            string tokenAddress,
            FunctionMessage allowanceMessage,
            CancellationToken cancellationToken = default)
        {
            try
            {
                var callData = (allowanceMessage as ERC20AllowanceFunctionMessage)
                               .GetCallData()
                               .ToHex(prefix: true);

                return(await HttpHelper.GetAsyncResult <decimal>(
                           baseUri : BaseUrl,
                           requestUri : $"api?module=proxy&action=eth_call&to={tokenAddress}&data={callData}&tag=latest&apikey={ApiKey}",
                           responseHandler : (response, content) =>
                {
                    var result = JsonConvert.DeserializeObject <JObject>(content);

                    var allowanceHex = result?["result"]?.Value <string>();

                    return !string.IsNullOrEmpty(allowanceHex)
                            ? erc20.TokenDigitsToTokens(new HexBigInteger(allowanceHex).Value)
                            : 0;
                },
                           cancellationToken : cancellationToken)
                       .ConfigureAwait(false));
            }
            catch (Exception e)
            {
                return(new Error(Errors.RequestError, e.Message));
            }
        }
        public static decimal GetAmount(
            EthereumTransaction tx,
            Erc20Config erc20Config)
        {
            var result = 0m;

            if (tx.Type.HasFlag(BlockchainTransactionType.SwapRedeem) ||
                tx.Type.HasFlag(BlockchainTransactionType.SwapRefund))
            {
                result += erc20Config.TokenDigitsToTokens(tx.Amount);
            }
            else
            {
                if (tx.Type.HasFlag(BlockchainTransactionType.Input))
                {
                    result += erc20Config.TokenDigitsToTokens(tx.Amount);
                }
                if (tx.Type.HasFlag(BlockchainTransactionType.Output))
                {
                    result += -erc20Config.TokenDigitsToTokens(tx.Amount);
                }
            }

            tx.InternalTxs?.ForEach(t => result += GetAmount(t, erc20Config));

            return(result);
        }
Esempio n. 3
0
        public static ICurrencySwap Create(
            CurrencyConfig currency,
            IAccount account)
        {
            return(currency switch
            {
                BitcoinBasedConfig _ => new BitcoinBasedSwap(
                    account: account.GetCurrencyAccount <BitcoinBasedAccount>(currency.Name),
                    currencies: account.Currencies),

                Erc20Config _ => new Erc20Swap(
                    account: account.GetCurrencyAccount <Erc20Account>(currency.Name),
                    ethereumAccount: account.GetCurrencyAccount <EthereumAccount>("ETH"),
                    currencies: account.Currencies),

                EthereumConfig _ => new EthereumSwap(
                    account: account.GetCurrencyAccount <EthereumAccount>(currency.Name),
                    currencies: account.Currencies),

                Fa12Config _ => new Fa12Swap(
                    account: account.GetCurrencyAccount <Fa12Account>(currency.Name),
                    tezosAccount: account.GetCurrencyAccount <TezosAccount>(TezosConfig.Xtz),
                    currencies: account.Currencies),

                TezosConfig _ => new TezosSwap(
                    account:   account.GetCurrencyAccount <TezosAccount>(currency.Name),
                    currencies: account.Currencies),

                _ => throw new NotSupportedException($"Not supported currency {currency.Name}")
            });
Esempio n. 4
0
 public static SendViewModel CreateViewModel(IAtomexApp app, CurrencyViewModel currencyViewModel)
 {
     return(currencyViewModel.Currency switch
     {
         BitcoinBasedConfig _ => (SendViewModel) new BitcoinBasedSendViewModel(app, currencyViewModel),
         Erc20Config _ => (SendViewModel) new Erc20SendViewModel(app, currencyViewModel),
         EthereumConfig _ => (SendViewModel) new EthereumSendViewModel(app, currencyViewModel),
         Fa12Config _ => (SendViewModel) new Fa12SendViewModel(app, currencyViewModel),
         TezosConfig _ => (SendViewModel) new TezosSendViewModel(app, currencyViewModel),
         _ => throw new NotSupportedException($"Can't create send view model for {currencyViewModel.Currency.Name}. This currency is not supported."),
     });
 public EthereumERC20TransactionViewModel(
     EthereumTransaction tx,
     Erc20Config erc20Config)
     : base(tx, erc20Config, GetAmount(tx, erc20Config), 0)
 {
     From       = tx.From;
     To         = tx.To;
     GasPrice   = EthereumConfig.WeiToGwei((decimal)tx.GasPrice);
     GasLimit   = (decimal)tx.GasLimit;
     GasUsed    = (decimal)tx.GasUsed;
     IsInternal = tx.IsInternal;
 }
Esempio n. 6
0
        private decimal RequiredAmountInTokens(Swap swap, Erc20Config erc20)
        {
            var requiredAmountInERC20 = AmountHelper.QtyToSellAmount(swap.Side, swap.Qty, swap.Price, erc20.DigitsMultiplier);

            // maker network fee
            if (swap.MakerNetworkFee > 0 && swap.MakerNetworkFee < requiredAmountInERC20) // network fee size check
            {
                requiredAmountInERC20 += AmountHelper.RoundDown(swap.MakerNetworkFee, erc20.DigitsMultiplier);
            }

            return(requiredAmountInERC20);
        }
Esempio n. 7
0
        public static EthereumTransaction TransformApprovalEvent(
            this EtherScanApi.ContractEvent contractEvent,
            Erc20Config erc20,
            long lastBlockNumber)
        {
            if (!contractEvent.IsERC20ApprovalEvent())
            {
                return(null);
            }

            var approvalEvent = contractEvent.ParseERC20ApprovalEvent();

            var tx = new EthereumTransaction() //todo: make a refactoring
            {
                Currency     = erc20.Name,
                Id           = contractEvent.HexTransactionHash,
                Type         = BlockchainTransactionType.Output | BlockchainTransactionType.TokenApprove,
                State        = BlockchainTransactionState.Confirmed, //todo: check if true in 100% cases
                CreationTime = contractEvent.HexTimeStamp.Substring(PrefixOffset).FromHexString(),

                From   = approvalEvent.Owner,
                To     = approvalEvent.Spender,
                Amount = 0,
                ////Nonce
                GasPrice = new HexBigInteger(contractEvent.HexGasPrice).Value,
                ////GasLimit
                GasLimit      = new HexBigInteger(contractEvent.HexGasUsed).Value,
                ReceiptStatus = true,
                IsInternal    = false,
                InternalIndex = 0,
                BlockInfo     = new BlockInfo
                {
                    Confirmations = 1 + (int)(lastBlockNumber - long.Parse(contractEvent.HexBlockNumber.Substring(PrefixOffset), System.Globalization.NumberStyles.HexNumber)),
                    //    //Confirmations = txReceipt.Status != null
                    //    //? (int)txReceipt.Status.Value
                    //    //: 0,
                    //    //BlockHash = tx.BlockHash,
                    BlockHeight = long.Parse(contractEvent.HexBlockNumber.Substring(PrefixOffset), System.Globalization.NumberStyles.HexNumber),
                    //    //BlockTime = blockTimeStamp,
                    //    //FirstSeen = blockTimeStamp
                }
            };

            return(tx);
        }