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语言编写的,自动化测试以太坊智能合约的开发框架,使用此框架,可以自动化的部署合约,自动测试合约内的功能函数 ...
随机推荐
- docker之使用System.Drawing生成图片缺少Gdiplus.dll错误
1.在docker 上找到并运行需要System.Drawing的镜像 然后退出执行另一语句 docker run -it container01 进入镜像以方便安装gdiplus docker ex ...
- Tensor基本操作
Tensor(张量) 1.Tensor,又名张量,从工程角度来说,可简单地认为它就是一个数组,且支持高效的科学计算.它可以是一个数(标量).一维数组(向量).二维数组(矩阵)或更高维的数组(高阶数组) ...
- tyflow birth节点
0-50帧,持续出生5颗粒子,若范围为0-0,5颗粒子将在第一帧全部出生 每一帧出生5颗粒子,直到50帧结束 连续发射,在0-500帧范围内,每5颗粒子出生后,继续出生5颗 5颗粒子出生后持续50帧, ...
- 29 ArcMap许可服务器点击授权后无法进入下一步
系统描述:Windows server 2008 R2 ArcMap版本:10.6 系统要求各项都满足,包括补丁包都有,没有杀毒软件,ArcMap软件能安装上,但是到授权那步出问题 系统要求http ...
- linux mysql 安装
操作系统 Centos 7.2以上版本 操作系统 centos 7.2以上版本 mysql 版本 mysql-5.7.23-el7-x86_64.tar.gz 1.1 安装准备 1. 创建安装文件存 ...
- node.js搭建Web服务器
Node.js 博客搭建 一. 学习需求 Node 的安装运行 会安装node,搭建node环境 会运行node. 基础模块的使用 Buffer:二进制数据处理模块 Event:事件模块 fs:文件系 ...
- 【安富莱专题教程第3期】开发板搭建Web服务器,利用花生壳让电脑和手机可以外网远程监控
说明:1. 开发板Web服务器的设计可以看我们之前发布的史诗级网络教程:链接.2. 需要复杂些的Web设计模板,可以使用我们V6开发板发布的综合Demo:链接.3. 教程中使用的是花生壳免费版, ...
- 体验一把做黑客的感觉-IPC$入侵之远程控制
前言 一看你就是看标题进来的,我可不是标题党啊,大家往下看吧,本文章主要介绍了利用IPC共享漏洞上传并执行木马. 基础知识 一.什么是IPC 进程间通信(IPC,Inter-Process Commu ...
- [Swift]LeetCode100. 相同的树 | Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- [Swift]LeetCode254.因子组合 $ Factor Combinations
Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4. Write a func ...