以太坊区块链开发-私链

 

 

 1.首先查看我们两个私链钱包的以太坊wei余额,后续我们写转账到区块链地址

2.然后我们启动私链 geth --identity "secbro etherum" --rpc --rpccorsdomain "*" --datadir "私链数据文件夹绝对路径" --port "30303" --rpcapi "db,eth,net,web3" --networkid 95518 console

然后我们开始码代码,我们这里使用Nethereum以太坊开发框架

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Nethereum.Contracts;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Web3.Accounts.Managed;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ShowWei();


            Task.Run(async () =>
            {
                var password = "123456";//私钥密码
                var accountFilePath = @"D:\Geth\EthDBSpace\MyPrivChain\keystore\UTC--2020-08-08T17-14-07.485273900Z--9c086b477028ef6bc7c23efc0ad008982e622f61";//keystore
                var account = Account.LoadFromKeyStoreFile(accountFilePath, password);//加载私钥并签名
                var web3 = new Web3(account);//实例化
                await web3.TransactionManager.SendTransactionAsync(account.Address, "0xd8f74f49b638fe0072bad0a177a7b6ff9063368c", new HexBigInteger(1));//发送转账到指定区块链地址单位WeiHexBigInteger(wei))
            }).Wait();

            Console.WriteLine("main end");
            Console.ReadLine();
        }
        public static void ShowWei()
        {//查看钱余额
            var web3 = new Web3();
            var balance = web3.Eth.GetBalance.SendRequestAsync("0x9c086b477028ef6bc7c23efc0ad008982e622f61").Result.Value;
            Console.WriteLine($"Balance in Wei: {balance}");
            var etherAmount = Web3.Convert.FromWei(balance);
            Console.WriteLine($"Balance in Ether: {etherAmount}");
        }
    }
}

为了转账变化明显,测试转账1wei

 

 

 发送转账记录运行完毕,那么我们查看下余额

 

 此时会发现一个奇怪现象,前面的转账请求已经提交,为什么两个账户的余额没有发生任何变化?,以太坊使用POW共识激励矿工记账,而由于我们创建的是私链目前只有我们一个节点,所以此时并没有其它节点参与记账。所以我们需要通过挖矿把这笔转账记录到块中。

启动挖矿

 

并立马停止

 

 现在我们在查询下钱包以太坊Wei余额

 

 到账成功,COOL!

posted @ 2020-08-10 22:13  昊天888  阅读(501)  评论(0)    收藏  举报