public async Task <Result <decimal> > GetERC20AllowanceAsync(
            EthereumTokens.ERC20 erc20,
            string tokenAddress,
            FunctionMessage allowanceMessage,
            CancellationToken cancellationToken = default)
        {
            try
            {
                var web3 = new Web3(_uri);

                if (!(allowanceMessage is ERC20AllowanceFunctionMessage allowMes))
                {
                    throw new NotSupportedException("Not supported message type");
                }

                //var tokenService = new StandardTokenService(web3, contractAddress);
                //var allowance = await tokenService.AllowanceQueryAsync(allowMes.Owner, allowMes.Spender)
                //    .ConfigureAwait(false);

                var contractHandler = web3.Eth.GetContractHandler(tokenAddress);

                var result = await contractHandler
                             .QueryAsync <ERC20AllowanceFunctionMessage, BigInteger>(allowMes)
                             .ConfigureAwait(false);

                return(result != null
                    ? erc20.TokenDigitsToTokens(result)
                    : 0);
            }
            catch (Exception e)
            {
                return(new Error(Errors.RequestError, e.Message));
            }
        }
예제 #2
0
        public async Task <Result <decimal> > GetERC20AllowanceAsync(
            EthereumTokens.ERC20 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));
            }
        }
예제 #3
0
        public static EthereumTransaction TransformTransferEvent(
            this EtherScanApi.ContractEvent contractEvent,
            string address,
            EthereumTokens.ERC20 erc20,
            long lastBlockNumber)
        {
            if (!contractEvent.IsERC20TransferEvent())
            {
                return(null);
            }

            var transferEvent = contractEvent.ParseERC20TransferEvent();

            var tx = new EthereumTransaction() //todo: make a refactoring
            {
                Currency = erc20,
                Id       = contractEvent.HexTransactionHash,

                Type = transferEvent.From == address
                    ? transferEvent.To == erc20.SwapContractAddress.ToLowerInvariant()   //todo: change to erc20.SwapContractAddress after currencies.json update
                        ? BlockchainTransactionType.Output | BlockchainTransactionType.SwapPayment
                        : BlockchainTransactionType.Output
                    : BlockchainTransactionType.Input,               //todo: recognize redeem&refund
                State        = BlockchainTransactionState.Confirmed, //todo: check if true in 100% cases
                CreationTime = contractEvent.HexTimeStamp.Substring(PrefixOffset).FromHexString(),

                From   = transferEvent.From,
                To     = transferEvent.To,
                Amount = transferEvent.Value.ToHexBigInteger(),
                ////Nonce
                GasPrice = new HexBigInteger(contractEvent.HexGasPrice).Value,
                ////GasLimit
                GasLimit      = new HexBigInteger(contractEvent.HexGasUsed).Value,
                ReceiptStatus = true,
                IsInternal    = transferEvent.From == erc20.SwapContractAddress.ToLowerInvariant() ||
                                transferEvent.To == erc20.SwapContractAddress.ToLowerInvariant(),
                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);
        }
 public async Task <Result <decimal> > GetERC20AllowanceAsync(
     EthereumTokens.ERC20 erc20,
     string tokenAddress,
     FunctionMessage allowanceMessage,
     CancellationToken cancellationToken = default)
 {
     return(await _web3.GetERC20AllowanceAsync(
                erc20 : erc20,
                tokenAddress : tokenAddress,
                allowanceMessage : allowanceMessage,
                cancellationToken : cancellationToken)
            .ConfigureAwait(false));
 }
예제 #5
0
        public static EthereumTransaction TransformApprovalEvent(
            this EtherScanApi.ContractEvent contractEvent,
            EthereumTokens.ERC20 erc20,
            long lastBlockNumber)
        {
            if (!contractEvent.IsERC20ApprovalEvent())
            {
                return(null);
            }

            var approvalEvent = contractEvent.ParseERC20ApprovalEvent();

            var tx = new EthereumTransaction() //todo: make a refactoring
            {
                Currency     = erc20,
                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);
        }