예제 #1
0
        public async Task <double> GetBalanceAsync()
        {
            string response;

            using (var client = HttpClientFactory.CreateFirefox(m_EtherscanUri))
                using (var getResponse = await client.GetAsync(
                           new Uri(m_EtherscanUri, $"/api?module=account&action=balance&address={m_WalletAddress}&tag=latest")))
                    response = await getResponse.Content.ReadAsStringAsync();

            dynamic responseJson = JsonConvert.DeserializeObject(response);

            if ((int)responseJson.status != 1)
            {
                return(0);
            }
            return((double)responseJson.result / WeisInEth);
        }
예제 #2
0
        public async Task <string[]> GetSupportedCompilersAsync()
        {
            var document = new HtmlDocument();

            using (var client = HttpClientFactory.CreateFirefox(m_ServiceUri))
                using (var getResponse = await client.GetAsync(m_ServiceUri))
                    document.LoadHtml(await getResponse.Content.ReadAsStringAsync());

            var options = document.DocumentNode.SelectNodes(
                "//select[@name='ctl00$ContentPlaceHolder1$ddlCompilerVersions']/option");

            if (options == null)
            {
                return(new string[0]);
            }
            return(options
                   .Select(x => x.GetAttributeValue("value", null))
                   .Where(x => !string.IsNullOrWhiteSpace(x))
                   .Select(x => x.TrimStart('v'))
                   .ToArray());
        }
예제 #3
0
        public async Task VerifyAsync(VerifyRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var document = new HtmlDocument();

            using (var client = HttpClientFactory.CreateFirefox(m_ServiceUri))
                using (var getResponse = await client.GetAsync(m_ServiceUri))
                    document.LoadHtml(await getResponse.Content.ReadAsStringAsync());
            var hiddenFields = document.DocumentNode
                               .SelectNodes("//input[@type='hidden']");
            var postValues = hiddenFields
                             .Select(x => new
            {
                Key   = x.GetAttributeValue("id", null),
                Value = x.GetAttributeValue("value", "")
            })
                             .Where(x => !string.IsNullOrEmpty(x.Key))
                             .GroupBy(x => x.Key)
                             .ToDictionary(x => x.Key, x => HtmlEntity.DeEntitize(x.First().Value));

            postValues["ctl00$ContentPlaceHolder1$txtContractAddress"]       = request.ContractAddress;
            postValues["ctl00$ContentPlaceHolder1$txtContractName"]          = request.Name;
            postValues["ctl00$ContentPlaceHolder1$ddlCompilerVersions"]      = $"v{request.Compiler}";
            postValues["ctl00$ContentPlaceHolder1$ddlOptimization"]          = "1";
            postValues["ctl00$ContentPlaceHolder1$txtSourceCode"]            = request.Source;
            postValues["ctl00$ContentPlaceHolder1$txtConstructorArguements"] = HexHelper.ToHex(request.AbiEncodedConstructorArgs);
            //postValues["ctl00$ContentPlaceHolder1$txtRuns"] = "200";
            for (var i = 1; i <= 5; i++)
            {
                postValues["ctl00$ContentPlaceHolder1$txtLibraryName" + i]    = string.Empty;
                postValues["ctl00$ContentPlaceHolder1$txtLibraryAddress" + i] = "0x";
            }
            postValues["ctl00$ContentPlaceHolder1$btnSubmit"]      = "Verify And Publish";
            postValues["ctl00$ContentPlaceHolder1$txtGithubGist"]  = string.Empty;
            postValues["ctl00$ContentPlaceHolder1$hdnSelectedTab"] = "'bytecode'";
            postValues["ctl00$ContentPlaceHolder1$libcounter"]     = "1";

            using (var client = HttpClientFactory.CreateFirefox(m_ServiceUri))
                using (var postResponse = await client.PostAsync(m_ServiceUri, new FormUrlEncodedContent(postValues)))
                {
                    postResponse.EnsureSuccessStatusCode();
                    document.LoadHtml(await postResponse.Content.ReadAsStringAsync());
                }
            if (document.DocumentNode.SelectSingleNode(
                    "//font[@color='green' and contains(.,'Successfully generated ByteCode and ABI')]") != null)
            {
                return;
            }
            var errorText = document.DocumentNode.SelectSingleNode(
                "//span[@id='ContentPlaceHolder1_lblErrorStatus']");

            if (string.IsNullOrWhiteSpace(errorText?.InnerText))
            {
                errorText = document.DocumentNode.SelectSingleNode(
                    "//span[@id='ContentPlaceHolder1_lblResult']");
            }
            throw new ContractVerificationException("Verification failed: " + errorText?.InnerText);
        }