Beispiel #1
0
        private async void btnSendVote_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(fromPassword))
            {
                fromPassword = Prompt.ShowDialog("Password", "Enter password...");
            }
            if (!String.IsNullOrEmpty(fromPassword))
            {
                string fromAddress = txtVoteFromAddress.Text;
                int    proposal    = int.Parse(txtVoteForProposal.Text);

                if (web3 == null)
                {
                    web3 = new Web3();
                }
                var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);

                var contract = web3.Eth.GetContract(txtContractAbi.Text, txtContractAddress.Text);
                var fVote    = contract.GetFunction("vote");
                Nethereum.RPC.Eth.DTOs.TransactionInput ti = new Nethereum.RPC.Eth.DTOs.TransactionInput();
                ti.From = fromAddress;
                ti.Gas  = new HexBigInteger(900000);
                ti.To   = contract.Address;
                var txHash = await fVote.SendTransactionAsync(ti, proposal);

                // fromAddress, new HexBigInteger(3000000), null, proposal
                bt.SetLastTxHash(txHash);
                txtTxHash.Text         = txHash;
                txtStartedMessage.Text = DateTime.UtcNow.ToString();
            }
            else
            {
                txtStartedMessage.Text = "No password";
            }
        }
        private async void DrainWalletToAddress(string receipientAddress)
        {
            // calculate

            var balance = await Web3.Eth.GetBalance.SendRequestAsync(Account.Address);

            var gasPrice = await Web3.Eth.GasPrice.SendRequestAsync();

            var gasPriceHex = new Nethereum.Hex.HexTypes.HexBigInteger(gasPrice);

            var gas    = Web3.Eth.TransactionManager.DefaultGas;
            var gasHex = new Nethereum.Hex.HexTypes.HexBigInteger(gas);

            var balanceToTransfer    = BigInteger.Subtract(balance, BigInteger.Multiply(gasPrice, gas));
            var balanceToTransferHex = new Nethereum.Hex.HexTypes.HexBigInteger(balanceToTransfer);

            var transactionInput = new Nethereum.RPC.Eth.DTOs.TransactionInput(null, receipientAddress, Account.Address, gasHex, gasPriceHex, balanceToTransferHex);

            drainTransactionRichTextBox.Text = JsonConvert.SerializeObject(transactionInput);

            drainSignedTransactionRichTextBox.Text = await Web3.TransactionManager.SignTransactionRetrievingNextNonceAsync(transactionInput);

            //var txHash = await Web3.TransactionManager.SignTransactionRetrievingNextNonceAsync(transactionInput);

            var txReceipt = await Web3.TransactionManager.TransactionReceiptService.SendRequestAndWaitForReceiptAsync(() => {
                return(Web3.TransactionManager.SendTransactionAsync(transactionInput));
            });

            drainTransactionReceiptRichTextBox.Text = JsonConvert.SerializeObject(txReceipt);
        }
        public async Task <IActionResult> SendTx([FromBody] TxModel txModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var transacInpunt = new Nethereum.RPC.Eth.DTOs.TransactionInput
                    {
                        From  = txModel.SenderAddress,
                        To    = txModel.ToAddress,
                        Value = new HexBigInteger(txModel.Value)
                    };

                    var tx = await Web3Client.Eth.Transactions.SendTransaction.SendRequestAsync(transacInpunt);

                    return(Json(tx));
                }

                return(BadRequest(ModelState));
            }
            catch (Exception ex)
            {
                return(ReturnError(ex));
            }
        }
        public async Task <IActionResult> SendRbtcOnlyForRegtest(string address)
        {
            try
            {
                UnitConversion unitConversion = new UnitConversion();

                var personalAccounts = await Web3Client.Eth.Accounts.SendRequestAsync();

                if (personalAccounts != null && personalAccounts.Length > 0)
                {
                    var transacInpunt = new Nethereum.RPC.Eth.DTOs.TransactionInput
                    {
                        From  = personalAccounts[0],
                        To    = address,
                        Value = new HexBigInteger(unitConversion.ToWei(0.1, EthUnit.Ether))
                    };

                    var tx = await Web3Client.Eth.Transactions.SendTransaction.SendRequestAsync(transacInpunt);

                    return(Json(tx));
                }

                return(Json(string.Empty));
            }
            catch (Exception ex)
            {
                return(ReturnError(ex));
            }
        }
Beispiel #5
0
        public IEnumerator SendRequest(Nethereum.RPC.Eth.DTOs.TransactionInput input)
        {
            var request = _ethSendTransaction.BuildRequest(input);

            if (this.network == "ganache")
            {
                Debug.Log("Using ganache, just forwarding request on...");
                yield return(base.SendRequest(request));

                yield break;
            }

            var requestFormatted = new RpcModel.RpcRequest(request.Id, request.Method, request.RawParameters);
            var rpcRequestJson   = JsonConvert.SerializeObject(requestFormatted, JsonSerializerSettings);

            Debug.Log(rpcRequestJson);
            this.authProvider.SendTransaction(this.network, request.Method, rpcRequestJson, SendRequestCallback);

            var waitCount = 0;

            while (this.Result == null && this.Exception == null)
            {
                if (waitCount > 100)
                {
                    break;
                }
                waitCount++;
                yield return(new WaitForSeconds((float)0.1));
            }
        }
        static public async Task <bool> InteractWithExistingContractWithEtherAndEventsExample(Web3 web3, string fromAddress, string fromPassword, string contractAddress, string contractAbi, long amountWei)
        {
            Console.WriteLine("InteractWithExistingContractWithEtherAndEventsExample:");

            string extraData = DateTime.Now.ToString() + " extraData contract";

            var extraDataHex = new HexUTF8String(extraData).HexValue;
            var ti           = new Nethereum.RPC.Eth.DTOs.TransactionInput(extraDataHex, contractAddress, fromAddress, new HexBigInteger(900000), new HexBigInteger(amountWei));

            var contract = web3.Eth.GetContract(contractAbi, contractAddress);

            var depositFunction     = contract.GetFunction("deposit");
            var depositReceiptEvent = contract.GetEvent("DepositReceipt");

            var filterAllDepositReceipt = await depositReceiptEvent.CreateFilterAsync();

            var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);

            var txHash2 = await depositFunction.SendTransactionAsync(ti, "1234-5678-9012-3456"); // first transfer

            ti.Value = new HexBigInteger(357);
            txHash2  = await depositFunction.SendTransactionAsync(ti, "1234-5678-9012-3456"); // second transfer

            Console.WriteLine("txHash2:\t" + txHash2.ToString());

            var txReceipt2 = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash2);

            int timeoutCount = 0;

            while (txReceipt2 == null && timeoutCount < MAX_TIMEOUT)
            {
                Console.WriteLine("Sleeping...");
                Thread.Sleep(SLEEP_TIME);
                txReceipt2 = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash2);

                timeoutCount += SLEEP_TIME;
            }
            Console.WriteLine("timeoutCount:\t" + timeoutCount.ToString());
            Console.WriteLine("fromAddress:\t" + fromAddress.ToString());
            Console.WriteLine("contractAddress:\t" + contractAddress.ToString());
            Console.WriteLine("amountWei:\t" + amountWei.ToString());
            Console.WriteLine("extraData:\t" + extraData.ToString());
            Console.WriteLine("extraDataHex:\t" + extraDataHex.ToString());
            Console.WriteLine("txHash2:\t" + txHash2.ToString());

            var logDepositReceipts = await depositReceiptEvent.GetFilterChanges <FunctionOutputHelpers.DepositReceiptArgs>(filterAllDepositReceipt);

            foreach (var dra in logDepositReceipts)
            {
                var dt = Helpers.UnixTimeStampToDateTime((double)dra.Event.timestamp);
                Console.WriteLine("depositReceiptEvent:\t" +
                                  dt.ToString() + " " + dra.Event.from.ToString() + " " + dra.Event.id.ToString() + " " + dra.Event.value.ToString());
            }

            return(true);
        }
Beispiel #7
0
        static async Task <String> TransferRBTC(Web3 web3, String addressFrom, String addressTo, decimal value)
        {
            var transactionInput = new Nethereum.RPC.Eth.DTOs.TransactionInput
            {
                From  = addressFrom,
                To    = addressTo,
                Value = new HexBigInteger(value.ToString())
            };

            return(await web3.Eth.Transactions.SendTransaction.SendRequestAsync(transactionInput));
        }
Beispiel #8
0
        private async void btnSendEther_Click(object sender, EventArgs e)
        {
            txtTxHash.Text   = "";
            txtTxStatus.Text = "";

            if (String.IsNullOrEmpty(fromPassword))
            {
                fromPassword = Prompt.ShowDialog("Password", "Enter password...");
            }
            if (!String.IsNullOrEmpty(fromPassword))
            {
                if (web3 == null)
                {
                    web3 = new Web3();
                }
                string fromAddress = txtFromAddress.Text;
                string toAddress   = txtToAddress.Text;
                ulong  amountWei   = ulong.Parse(txtValue.Text);
                txtStartedMessage.Text = DateTime.UtcNow.ToString(); // set this early so that it doesn't overwrite txtStartedMesssage
                string txHash = "";
                try
                {
                    var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);

                    string extraDataHex = txtDataHex.Text;
                    if (extraDataHex.Length == 0 || extraDataHex == "0x")
                    {
                        txHash = await web3.Eth.TransactionManager.SendTransactionAsync(fromAddress, toAddress, new HexBigInteger(amountWei));
                    }
                    else
                    {
                        Nethereum.RPC.Eth.DTOs.TransactionInput ti = new Nethereum.RPC.Eth.DTOs.TransactionInput();
                        ti.From  = fromAddress;
                        ti.Gas   = new HexBigInteger(900000);
                        ti.To    = toAddress;
                        ti.Value = new HexBigInteger(amountWei);
                        ti.Data  = extraDataHex;
                        txHash   = await web3.Eth.TransactionManager.SendTransactionAsync(ti);
                    }
                }
                catch (Exception ex)
                {
                    txtStartedMessage.Text = ex.Message;
                    fromPassword           = ""; // doesn't cause password reprompt
                }
                bt.SetLastTxHash(txHash);
                txtTxHash.Text = txHash;
            }
            else
            {
                txtStartedMessage.Text = "No password";
            }
        }
        private async void sendEthTransactionButton_Click(object sender, EventArgs e)
        {
            try
            {
                var receipientAddress = receipientAddressTextBox.Text.Trim();

                var ethAmount   = decimal.Parse(ethToSendTextBox.Text.Trim()); // exponent -18
                var ethInWei    = UnitConversion.Convert.ToWeiFromUnit(ethAmount, UnitConversion.Convert.GetEthUnitValue(UnitConversion.EthUnit.Ether));
                var ethInWeiHex = new Nethereum.Hex.HexTypes.HexBigInteger(ethInWei);

                var gasPrice         = decimal.Parse(gasPriceToUseTextBox.Text.Trim()); // exponent -9
                var gasPriceInWei    = UnitConversion.Convert.ToWei(gasPrice, 9);
                var gasPriceInWeiHex = new Nethereum.Hex.HexTypes.HexBigInteger(gasPriceInWei);

                var gas         = decimal.Parse(gasUsedToUseTextBox.Text.Trim()); // exponent 0
                var gasInWei    = new BigInteger(gas);
                var gasInWeiHex = new Nethereum.Hex.HexTypes.HexBigInteger(gasInWei);

                var tx = new Nethereum.RPC.Eth.DTOs.TransactionInput(null, receipientAddress, Account.Address, gasInWeiHex, gasPriceInWeiHex, ethInWeiHex);
                sentTransactionDataTextBox.Text = JsonConvert.SerializeObject(tx);

                var txSigned = await Web3.Eth.TransactionManager.SignTransactionRetrievingNextNonceAsync(tx);

                sentTransactionHashTextBox.Text = $"Signed Tx: {txSigned}";

                // sanity check because Ganache UI was displaying rounded up numbers which was truly killing me inside.
                //{
                //    var gasssss = BigInteger.Subtract(Balance, BigInteger.Multiply(gasInWeiHex, gasPriceInWeiHex));
                //    var final = BigInteger.Subtract(gasssss, ethInWeiHex);

                //    Console.WriteLine($"{Balance.ToString()} = {final.ToString()} + {BigInteger.Multiply(gasInWeiHex, gasPriceInWeiHex).ToString()} + {ethInWeiHex.Value.ToString()}");
                //}

                var txHash = await Web3.Eth.TransactionManager.SendTransactionAsync(tx);

                var txReceipt = await Web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txHash);

                // Give users a receipt

                //hashAndReceiptTextBox.Text = string.Empty;
                //hashAndReceiptTextBox.AppendText($"Transaction Hash: {txHash}{Environment.NewLine}{Environment.NewLine}{Environment.NewLine}");
                hashAndReceiptTextBox.Text = $"Receipt: {JsonConvert.SerializeObject(txReceipt)}";


                InitializeBalance();
            }
            catch (Exception ex)
            {
                MessageBox.Show(JsonConvert.SerializeObject(ex));
            }
        }
Beispiel #10
0
        private async void btn_sendTransfer_Click(object sender, EventArgs e)
        {
            string AccountFrom = this.tb_from.Text.Trim();
            string AccountTo   = this.tb_to.Text;


            if (string.IsNullOrEmpty(AccountFrom) || string.IsNullOrEmpty(AccountTo))
            {
                MessageBox.Show("不能为空");
                return;
            }

            string AccountPwd = InputBox.ShowInputBox("请输入密码!");

            bool unlockres = false;

            try
            {
                unlockres = await web3.Personal.UnlockAccount.SendRequestAsync(AccountFrom, AccountPwd, 1500);
            }
            catch (Exception ex)
            {
                this.tb_logList.Text = "钱包密码错误!";
                return;
            }

            if (!unlockres)
            {
                this.tb_logList.Text = "钱包密码错误!";
                return;
            }



            Nethereum.RPC.Eth.DTOs.TransactionInput input = new Nethereum.RPC.Eth.DTOs.TransactionInput();
            input.From = AccountFrom;
            input.To   = AccountTo;

            HexBigInteger gas = new HexBigInteger(2100000);   //设置转账小号的gas

            input.Gas = gas;

            HexBigInteger price = new HexBigInteger(13000000000000000); //单位:wei

            input.Value = price;

            var Block = await web3.Eth.Transactions.SendTransaction.SendRequestAsync(input);

            this.tb_logList.Text = Block;
        }
        static public async Task <string> SendEtherToAccountWithExtraDataExample(Web3 web3, string fromAddress, string fromPassword, string toAddress, long amountWei, string extraData)
        {
            Console.WriteLine("SendEtherToAccountWithExtraDataExample:");

            var extraDataHex = new HexUTF8String(extraData).HexValue;
            var ti           = new Nethereum.RPC.Eth.DTOs.TransactionInput(extraDataHex, toAddress, fromAddress, new HexBigInteger(900000), new HexBigInteger(amountWei));

            var unlockResult = await web3.Personal.UnlockAccount.SendRequestAsync(fromAddress, fromPassword, UNLOCK_TIMEOUT);

            var sendTxHash = await web3.Eth.TransactionManager.SendTransactionAsync(ti);

            Console.WriteLine("fromAddress:\t" + fromAddress.ToString());
            Console.WriteLine("toAddress:\t" + toAddress.ToString());
            Console.WriteLine("amountWei:\t" + amountWei.ToString());
            Console.WriteLine("extraData:\t" + extraData.ToString());
            Console.WriteLine("extraDataHex:\t" + extraDataHex.ToString());
            Console.WriteLine("sendTxHash:\t" + sendTxHash.ToString());
            return(sendTxHash);
        }
Beispiel #12
0
        public IEnumerator SendRequest(Nethereum.RPC.Eth.DTOs.TransactionInput input)
        {
            var request = _ethSendTransaction.BuildRequest(input);

            yield return(SendRequest(request));
        }
Beispiel #13
0
        public IEnumerator SendRequest(Nethereum.RPC.Eth.DTOs.TransactionInput txn, System.String password)
        {
            var request = _personalSignAndSendTransaction.BuildRequest(txn, password);

            yield return(SendRequest(request));
        }
Beispiel #14
0
        async void OnSendTranClicked(object sender, EventArgs e)
        {
            try
            {
                if (addfrom.Text == null || addfrom.Text.Trim() == "" || !addfrom.Text.StartsWith("0x"))
                {
                    await DisplayAlert("Error!", "Wallet address is error.", "OK");

                    return;
                }
                if (addto.Text == null || addto.Text.Trim() == "" || !addto.Text.StartsWith("0x"))
                {
                    await DisplayAlert("Error!", "Wallet address is error.", "OK");

                    return;
                }
                if (addpwd.Text == null || addpwd.Text.Trim() == "")
                {
                    await DisplayAlert("Error!", "Input password.", "OK");

                    return;
                }
                if (decimal.Parse(tranamount.Text.Trim()) <= 0)
                {
                    await DisplayAlert("Error!", "Wrong transaction amount.", "OK");

                    return;
                }
                sendbut.IsEnabled        = false;
                tranloading.IsRefreshing = true;
                var winfo = App.Wdb.GetWalletAsyncByAddress(addfrom.Text.Trim()).Result;
                try
                {
                    Account account = Account.LoadFromKeyStore(winfo.FJson, addpwd.Text.Trim());
                    if (account != null)
                    {
                        Web3 web3 = new Web3(account, App.ethapiurl);

                        if (selcionflag.ToLower() == "eth")
                        {
                            try
                            {
                                var gasprice = decimal.Parse(Nethereum.Util.UnitConversion.Convert.FromWei(System.Numerics.BigInteger.Parse(tranprice.Text)
                                                                                                           , UnitConversion.EthUnit.Gwei).ToString());

                                var hash = await web3.Eth.GetEtherTransferService().TransferEtherAsync(addto.Text.Trim(),
                                                                                                       decimal.Parse(tranamount.Text.Trim())
                                                                                                       , gasprice
                                                                                                       , System.Numerics.BigInteger.Parse(traegas.Text));

                                if (hash != null && hash.Length > 20 && !hash.StartsWith("0x0000000000"))
                                {
                                    tranamount.Text = "";
                                    await DisplayAlert("Tips", "Transaction hash:" + hash + " , Please wait a few minutes.", "OK");
                                }
                                else
                                {
                                    await DisplayAlert("Tips", "Transaction error", "OK");
                                }
                            }
                            catch (Exception ex)
                            {
                                await DisplayAlert("Exception", ex.Message, "OK");
                            }
                        }
                        else
                        {
                            try
                            {
                                var toamount = Nethereum.Util.UnitConversion.Convert.ToWei(decimal.Parse(tranamount.Text), int.Parse(decimals.ToString()));

                                string data = EthHelper.CallContractFunData("transfer(address,uint256)", new List <object> {
                                    addto.Text.Trim(), toamount
                                });
                                Nethereum.RPC.Eth.DTOs.TransactionInput traninput = new Nethereum.RPC.Eth.DTOs.TransactionInput(data, contractAddress, account.Address
                                                                                                                                , new HexBigInteger(traegas.Text), new HexBigInteger("0"));


                                var hash = await web3.TransactionManager.SendTransactionAsync(traninput);

                                if (hash != null && hash.Length > 20 && !hash.StartsWith("0x0000000000"))
                                {
                                    tranamount.Text = "";
                                    await DisplayAlert("Tips", "Transaction hash:" + hash + " , Please wait a few minutes.", "OK");
                                }
                                else
                                {
                                    await DisplayAlert("Tips", "Transaction error", "OK");
                                }
                            }
                            catch (Exception ex)
                            {
                                await DisplayAlert("Exception", ex.Message, "OK");
                            }
                        }
                    }
                    else
                    {
                        await DisplayAlert("Error", "Password is error", "OK");
                    }
                }
                catch { await DisplayAlert("Error", "Password is error", "OK"); }
            }
            catch (Exception ex)
            {
                await DisplayAlert("Exception!", "Try again later " + ex.Message, "OK");
            }
            tranloading.IsRefreshing = false;
            sendbut.IsEnabled        = true;
        }