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(
            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 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));
 }
 public void LogGivenSendTransaction(FunctionMessage transactionMessage)
 {
     _writer.WriteLine("Given I Send Tranasction");
     _writer.WriteLine(Stateprinter.PrintObject(transactionMessage));
 }
 public void LogWhenQueryFunction(FunctionMessage queryFunction)
 {
     _writer.WriteLine("When Querying Function:");
     _writer.WriteLine(Stateprinter.PrintObject(queryFunction));
 }
 public void LogWhenQueryFunctionThen(FunctionMessage queryFunction, object outputDTO)
 {
     LogWhenQueryFunction(queryFunction);
     LogExpectedQueryResult(outputDTO);
 }