C#实现以太仿DApp合约编译、部署
在网上找了一些关于C#开发以太仿的资料,大概了解了以太仿常用名词,后续可能需要根据资料查看开源的源码进一步熟悉一下。
一、准备合约
这里准备了一个EzToken.sol合约,目前还不会solidity,所以随便在网上找了一个,由于使用的是solidity的版本是0.5.6的版本,所以需要在string memory _tokenName,中加memory,否则会报错。
pragma solidity >=0.4. <0.6.;
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();
string tokenName = "CYW";
BigInteger decimalUnits = new BigInteger();
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[],
Data = data,
Gas = new HexBigInteger(),
GasPrice = new HexBigInteger()
}; 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();
}
Console.WriteLine("contract deployed at => " + receipt.ContractAddress);
}
}
}


C#实现以太仿DApp合约编译、部署的更多相关文章
- 以太仿DApp开发环境搭建
在网上找了些以太仿的资料,是node.js写的,之前也了解过node.js,正好也可以用上.本篇主要学习以太仿DApp开发环境搭建. 一.安装 DApp 开发环境 1.1安装 Node.js 首先下载 ...
- 使用Remix编译和部署以太坊智能合约
Remix 是一個开源的 Solidity 智能合约开发环境,提供基本的编译.部署至本地或测试网络.执行合约等功能.Solidity 是 以太坊Ethereum 官方设计和支持的开发语言,专门用于 ...
- 智能合约开发——以太坊 DApp 实现 购买通证token
合约的buy()方法用于提供购买股票的接口.注意关键字payable,有了它买股票的人才可以付钱给你. 接收钱没有比这个再简单的了! function buy() payable public ret ...
- 深入以太坊智能合约 ABI
开发 DApp 时要调用在区块链上的以太坊智能合约,就需要智能合约的 ABI.本文希望更多了解 ABI,如为什么需要 ABI?如何解读 Ethereum 的智能合约 ABI?以及如何取得合约的 ABI ...
- rpc接口调用以太坊智能合约
rpc接口调用以太坊智能合约 传送门: 柏链项目学院 在以太坊摸爬滚打有些日子了,也遇到了各种各样的问题.这几天主要研究了一下如何通过rpc接口编译.部署和调用合约.也遇到了一些困难和问题,下面将 ...
- 以太坊智能合约开发框架Truffle
前言 部署智能合约有多种方式,命令行的浏览器的渠道都有,但往往跟我们程序员的风格不太相符,因为我们习惯了在IDE里写了代码然后打包运行看效果. 虽然现在IDE中已经存在了Solidity插件,可以编写 ...
- 以太坊智能合约虚拟机(EVM)原理与实现
以太坊 EVM原理与实现 以太坊底层通过EVM模块支持合约的执行与调用,调用时根据合约地址获取到代码,生成环境后载入到EVM中运行.通常智能合约的开发流程是用solidlity编写逻辑代码,再通过编译 ...
- Go语言打造以太坊智能合约测试框架(level3)
传送门: 柏链项目学院 第三课 智能合约自动化测试 之前课程回顾 我们之前介绍了go语言调用exec处理命令行,介绍了toml配置文件的处理,以及awk处理文本文件获得ABI信息.我们的代码算是完成了 ...
- Go语言打造以太坊智能合约测试框架(level1)
传送门: 柏链项目学院 Go语言打造以太坊智能合约测试框架 前言 这是什么? 这是一个基于go语言编写的,自动化测试以太坊智能合约的开发框架,使用此框架,可以自动化的部署合约,自动测试合约内的功能函数 ...
随机推荐
- vue font-icon 图标
1.vue 游览器左上角小图标 把.ico文件放在根目录下的static文件夹下,然后link标签引入 <link rel="shortcut icon" href=&quo ...
- K8s 入门
中文文档:https://www.kubernetes.org.cn/kubernetes%E8%AE%BE%E8%AE%A1%E6%9E%B6%E6%9E%84 小结大白话 Portainer 挺好 ...
- 32、可以拿来用的JavaScript实用功能代码
可以拿来用的JavaScript实用功能代码(可能会有些bug,用时稍微修改下,我用了几个还可以) 转载自 1.原生JavaScript实现字符串长度截取 function cutstr(str, l ...
- windows 10 64位机器上 安装部署
mi这个博客写的不错 https://www.cnblogs.com/dingguofeng/p/8709476.html 安装redis 可视化工具后 ,新建连接 名称随意,注意端口号是否有误默认6 ...
- 5 个免费的受欢迎的 SQLite 管理工具【申明:来源于网络】
5 个免费的受欢迎的 SQLite 管理工具 包含内容: SQLite Expert – Personal Edition SQLite Expert 提供两个版本,分别是个人版和专业版.其中个人版是 ...
- tag cloud的相关资料
http://reverland.org/python/2013/01/28/visualize-your-shell-history/ https://github.com/reverland/sc ...
- Hadoop源码分析:Hadoop编程思想
60页的ppt讲述Hadoop的编程思想 下载地址 http://download.csdn.net/detail/popsuper1982/9544904
- 最小化JIT示例(仅限Intel x86+Windows)
#include <Windows.h> #include <cstdint> #include <cstring> #define BACK_FILL (0) i ...
- java中的堆,栈和方法区(转)
来源:https://www.cnblogs.com/iliuyuet/p/5603618.html https://blog.csdn.net/lin542405822/article/detail ...
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...