日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

合约实战,代币合约,DAPP开发

發布時間:2024/8/26 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 合约实战,代币合约,DAPP开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. ERC20標準

https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md

pragma solidity ^0.4.4;//定義接口 contract ERC20Interface{string public name;string public symbol;uint8 public decimals;uint256 public totalSupply;/* function balanceOf(address _owner) view returns (uint256 balance); */function transfer(address _to, uint256 _value) returns (bool success);function transferFrom(address _from, address _to, uint256 _value) returns (bool success);function approve(address _spender, uint256 _value) returns (bool success);function allowance(address _owner, address _spender) view returns (uint256 remaining);event Transfer(address indexed _from, address indexed _to, uint256 _value);event Approval(address indexed _owner, address indexed _spender, uint256 _value);}contract ERC20 is ERC20Interface{mapping(address => uint256) public balanceOf;//委托,允許他人操作的金額數mapping(address => mapping(address => uint256)) allowed;//構造方法constructor() public {name = "MyToken";symbol = "weixuexi";decimals = 0;totalSupply = 10000;balanceOf[msg.sender] = totalSupply;}/* function balanceOf(address _owner) view returns (uint256 balance){return balanceOf[_owner];} */function transfer(address _to, uint256 _value) returns (bool success){require(_to != address(0));require(balanceOf[msg.sender] >= _value);require(balanceOf[_to] + _value > balanceOf[_to]);balanceOf[msg.sender] -= _value;balanceOf[_to] += _value;emit Transfer(msg.sender, _to, _value);}function transferFrom(address _from, address _to, uint256 _value) returns (bool success){require(_from != address(0));require(_to != address(0));require(balanceOf[_from] >= _value);require(allowed[msg.sender][_from] >= _value); //當前合約能操作_from的錢數要大于_valuerequire(balanceOf[_to] + _value > balanceOf[_to]);balanceOf[_from] -= _value;balanceOf[_to] += _value;emit Transfer(_from, _to, _value);success = true;}function approve(address _spender, uint256 _value) returns (bool success){//當前賬戶允許—_spender操作的金額數allowed[msg.sender][_spender] = _value;emit Approval(msg.sender, _spender, _value);success = true;}function allowance(address _owner, address _spender) view returns (uint256 remaining){return allowed[_owner][_spender];} } View Code

?

?2.DAPP去中心化應用開發【web項目】

  web3.js

  solidity

  truffle??https://truffleframework.com/docs/truffle/getting-started/installation

  ganache

開發流程:

  • 新建項目【初始化】
  • 編寫合約
  • 合約編譯、部署、測試
  • 與合約交互

?

安裝truffle:

npm install -g truffle
查看版本:truffle.cmd version

創建項目:

  下載基本文件:

    ? cd pet-shop

    truffle init? 【安裝項目】

?

  下載pet-shop包文件:

    cd pet-shop

  truffle unbox pet-shop

?編寫智能合約:Adoption.sol

pragma solidity ^0.4.23;contract Adoption {address[16] public adoptors; //領養這地址 function adopt(uint petId) public returns(uint) {adoptors[petId] = msg.sender;return petId;}function getAdoptors() public view returns (address[16]) {return adoptors;} }

?

編譯: truffle.cmd compile【方式一,結合使用ganache】

  truffle develop 【方式二,truffle集成了testrpc】; compile;

?

?

?

部署合約:migrate

  部署合約腳本: 1_migration.js? ?改文件用于監聽合約文件的動向,是否更新,變化

var MyContract = artifacts.require("MyContract");module.exports = function(deployer) {// deployment steps deployer.deploy(MyContract); };

?  truffle.js:

module.exports = {// See <http://truffleframework.com/docs/advanced/configuration>// to customize your Truffle configuration! networks: {development: {host: "127.0.0.1", //主機port: 7545, //端口,和ganache對應network_id: "*" // Match any network id }} };

?

  部署合約:[truffle] migrate

輸出結果:Adoption.json? [編譯出的文件做出了更新]

?

?這時,ganache中可以發現,已經出來了4個區塊:

還有交易信息:

?

?獲取合約實例,調用函數:

pragma solidity ^0.4.23;contract Hello {//如果使用pure修飾,可以通過合約對象直接調用,否則需要使用call方法調用function test() pure public returns (string) {return "hello world";} } Hello.sol

?

/migrations/3_deploy_hello.js

var Hello = artifacts.require("./Hello.sol");module.exports = function(deployer) {deployer.deploy(Hello); };

?

  使用web3獲取合約實例: 

>truffle: let contract;? ?//聲明contract變量

    contract = Hello.deployed().then(instance => contract=instance)? ? ? ? ? ? ?//Hello 是上面遷移文件的合約實例

pragma solidity ^0.4.23;import "truffle/Assert.sol"; import "truffle/DeployedAddresses.sol"; import "../contracts/Adoption.sol";contract TestAdoption {Adoption adoption = Adoption(DeployedAddresses.Adoption());function testUserCatAdoptPet() public {uint returnId = adoption.adopt(8);uint expect = 8;Assert.equal(returnId, expect, "Adoption of pet Id 8 should be recorded");}function testGetAdoptorAddByPetId() public {address expect = this;address adoptor = adoption.adoptors(8);Assert.equal(expect, adoptor, "get adopt by pet id");} } View Code

?

  注意:修改完合約文件后,重新編譯【compile】后,重新部署命令【migrate --reset】

?

  調用合約方法:contract.test();

?

?  使用斷言測試:Assert

同時會多出很多新的區塊和交易

?

?3.web3.js api的使用

官網

  進入truffle開發環境:truffle.cmd develop

  獲取賬戶余額:web3.eth.getBalace("").toString();

truffle(develop)> web3.eth.getBalance("0x627306090abab3a6e1400e9345bc60c78a8bef57").toString()'100000000000000000000'

?

?

  獲取所有賬戶:web3.eth.accounts;

truffle(develop)> null [ '0x627306090abab3a6e1400e9345bc60c78a8bef57','0xf17f52151ebef6c7334fad080c5704d77216b732','0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef','0x821aea9a577a9b44299b9c15c88cf3087f3b5544','0x0d1d4e623d10f9fba5db95830f7d3839406c6af2','0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e','0x2191ef87e392377ec08e7c08eb105ef5448eced5','0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5','0x6330a553fc93768f612722bb8c2ec78ac90b3bbc','0x5aeda56215b167893e80b4fe645ba6d5bab767de' ]

?

  獲取當前賬號:【數組中的第0個賬號】

truffle(develop)> web3.eth.coinbase;'0x627306090abab3a6e1400e9345bc60c78a8bef57'

?

?  web3.fromWei(num, 單位);

truffle(develop)> web3.fromWei(5, 'ether')'0.000000000000000005'

  web3.toWei(num,單位)

  默認的區塊:

truffle(develop)> web3.eth.defaultBlock'latest'

?

?

  交易:

truffle(develop)> web3.eth.sendTransaction({from:account1, to:account2, value:100000000000000000})'0xb5065a9a03d75f08d7e704d352d08de5c8c4f8f19cc654cb1261d254f6194d90' truffle(develop)> web3.eth.getBalance(account1)BigNumber { s: 1, e: 19, c: [ 998999, 99999999979000 ] } truffle(develop)> web3.eth.getBalance(account2)BigNumber { s: 1, e: 20, c: [ 1001000 ] } truffle(develop)> web3.eth.getBalance(account1).toString()'99899999999999979000' truffle(develop)> web3.eth.getBalance(account2).toString()'100100000000000000000' truffle(develop)> web3.fromWei(web3.eth.getBalance(account1).toString(), 'ether')'99.899999999999979' truffle(develop)> web3.fromWei(web3.eth.getBalance(account2).toString(), 'ether')'100.1' truffle(develop)> web3.fromWei(web3.eth.getBalance(web3.eth.accounts[2]).toString(), 'ether')'100'

?

?

?

?

?

  

4. 代幣合約的概念

pragma solidity ^0.4.4;contract EncryptedToken {uint256 INITAL_SUPPLY = 10000;mapping(address => uint) public balanceOf;constructor() {balanceOf[msg.sender] = INITAL_SUPPLY;}function transfer(address _to, uint256 _value) public {require(_to != address(0));require(balanceOf[msg.sender] >= _value);require(balanceOf[_to] + _value > balanceOf[_to]);balanceOf[msg.sender] -= _value;balanceOf[_to] += _value;} } EncryptedToken.sol

?

?

var EncryptedToken = artifacts.require("./EncryptedToken.sol");module.exports = function(deployer) {deployer.deploy(EncryptedToken); }; 1_deploy_encryptedToken.js

?

?

部署:

  truffle.cmd develop

  compile

  migate

  let c;

  EncryptedToken.deployed().then(inc => c=inc);

  c.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57") ;? ->10000? ?//錢包地址

默認部署到第一個測試錢包中:

轉賬:c.transfer("0xf17f52151ebef6c7334fad080c5704d77216b732",1000)

truffle(develop)> c.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57")
       BigNumber { s: 1, e: 3, c: [ 9000 ] }

5. web端

4.1 使用npm管理項目

  npm init

  安裝web server:npm install lite-server

配置文件:bs-config.json

{"server" : {"baseDir":["./src", "./build/contracts"]} }

?

package.json:

?

啟動服務:npm run dev

?

轉載于:https://www.cnblogs.com/zhuxiang1633/p/9486914.html

總結

以上是生活随笔為你收集整理的合约实战,代币合约,DAPP开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。