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);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章