C#实现以太仿DApp合约编译、部署
在网上找了一些关于C#开发以太仿的资料,大概了解了以太仿常用名词,后续可能需要根据资料查看开源的源码进一步熟悉一下。
一、准备合约
这里准备了一个EzToken.sol合约,目前还不会solidity,所以随便在网上找了一个,由于使用的是solidity的版本是0.5.6的版本,所以需要在string memory _tokenName,中加memory,否则会报错。
pragma solidity >=0.4.21 <0.6.0; contract EzToken { uint256 public totalSupply; mapping (address => uint256) public balances; mapping (address => mapping (address => uint256)) public allowed; string public name; uint8 public decimals; string public symbol; event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); constructor( uint256 _initialAmount, string memory _tokenName, uint8 _decimalUnits, string memory _tokenSymbol ) public { balances[msg.sender] = _initialAmount; totalSupply = _initialAmount; name = _tokenName; decimals = _decimalUnits; symbol = _tokenSymbol; } function transfer(address _to, uint256 _value) public returns (bool success) { require(balances[msg.sender] >= _value); balances[msg.sender] -= _value; balances[_to] += _value; emit Transfer(msg.sender, _to, _value); return true; } function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { uint256 allowance = allowed[_from][msg.sender]; require(balances[_from] >= _value && allowance >= _value); balances[_to] += _value; balances[_from] -= _value; allowed[_from][msg.sender] -= _value; emit Transfer(_from, _to, _value); return true; } function balanceOf(address _owner) public view returns (uint256 balance) { return balances[_owner]; } function approve(address _spender, uint256 _value) public returns (bool success) { allowed[msg.sender][_spender] = _value; emit Approval(msg.sender, _spender, _value); return true; } function allowance(address _owner, address _spender) public view returns (uint256 remaining) { return allowed[_owner][_spender]; } }
二、编译合约
编译合约我这边下载了window版本的solidity编译器https://github.com/ethereum/solidity/releases,这里我下载的是window的0.5.6版本。
然后在solidity-windows目录下cmd命令输入下面命令,之后会编译出一个abi文件、一个bin文件,关于solidity后续还需要进一步学习。
solc ../EzToken.sol --bin --abi --optimize --overwrite -o ../
三、部署
部署合约也是一个交易,只是与普通的交易相比,部署交易 需要把合约的字节码和编码后构造函数参数放在请求报文参数中的data字段里, 然后通过eth_sendTransaction或eth_sendRawTransaction调用提交给节点。这里发行了CYW的代币。
using Nethereum.Hex.HexConvertors.Extensions; using Nethereum.Hex.HexTypes; using Nethereum.RPC.Eth.DTOs; using Nethereum.Web3; using System; using System.IO; using System.Numerics; using System.Threading; using System.Threading.Tasks; using Nethereum.ABI.FunctionEncoding; using Nethereum.ABI.Model; namespace DeployContractTheory { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Run().Wait(); Console.ReadLine(); } public static async Task Run() { string abi = File.ReadAllText("EzToken.abi"); string bin = File.ReadAllText("EzToken.bin"); ParametersEncoder encoder = new ParametersEncoder(); Parameter[] parameters = new Parameter[] { new Parameter("uint256"), new Parameter("string"), new Parameter("uint8"), new Parameter("string") }; BigInteger initialAmount = new BigInteger(1000000000); string tokenName = "CYW"; BigInteger decimalUnits = new BigInteger(0); string tokenSymbol = "CuiYW"; byte[] encodedParams = encoder.EncodeParameters(parameters, initialAmount, tokenName, decimalUnits, tokenSymbol); string data = "0x" + bin + encodedParams.ToHex(false); Web3 web3 = new Web3("http://localhost:7545"); string[] accounts = await web3.Eth.Accounts.SendRequestAsync(); TransactionInput txinput = new TransactionInput { From = accounts[0], Data = data, Gas = new HexBigInteger(1000000), GasPrice = new HexBigInteger(20000000000) }; string txhash = await web3.TransactionManager.SendTransactionAsync(txinput); TransactionReceipt receipt = null; while (true) { receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(txhash); if (receipt != null) break; Thread.Sleep(1000); } Console.WriteLine("contract deployed at => " + receipt.ContractAddress); } } }
---------------我是有底线的--------------------
作者:社会主义接班人
出处:http://www.cnblogs.com/5ishare/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
作者:社会主义接班人
出处:http://www.cnblogs.com/5ishare/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?