当前位置: 首页 > news >正文

网络营销类网站杭州网站优化平台

网络营销类网站,杭州网站优化平台,建设银行手机银行网站用户名是什么意思,山东平度疫情文章目录 前言Solidity 介绍Solidity 文件结构许可声明编译指示数据类型函数事件访问区块元数据 简单的智能合约 前言 上文介绍了区块链生态发展,我们知道以太坊的到来可以使开发人员基于区块链开发DApp,本文介绍 Solidity 编程语言的使用,然…

文章目录

  • 前言
  • Solidity 介绍
  • Solidity 文件结构
    • 许可声明
    • 编译指示
    • 数据类型
    • 函数
    • 事件
    • 访问区块元数据
  • 简单的智能合约

前言

上文介绍了区块链生态发展,我们知道以太坊的到来可以使开发人员基于区块链开发DApp,本文介绍 Solidity 编程语言的使用,然后基于 Solidity 编写一个简单的智能合约。

Solidity 介绍

Solidity 是以太坊开发人员使用的编程语言,用来编写智能合约,运行在以太坊虚拟机(EVM)上。

有开发经验的同学上手应该是比较容易的,所有编程语言无非就是变量、分支、函数,变量值之所以称之为变量,是因为它是用来存储临时值,是可变化的;而分支是在检查某个事件是否为真后决定是否执行;Solidity 编程语言中函数是与区块链交互的主要方式,前期可以使用在线编程工具 remix 进行开发。

如果要开发一个去中心化的DApp,需要用 React、HTML、CSS 等前端技术将用户页面和智能合约结合。

Solidity 文件结构

一个 Solidity 文件后缀为 .sol,文件中包含许可声明、文件编译指示、文件导入标识以及任意数量的合约定义,包括变量、常量、构造、函数、事件、结构体、错误处理等。

许可声明

由于提供源代码总是涉及版权方面的法律问题,Solidity 编译器鼓励使用机器可读的 SPDX 许可证标识符,每个源文件都应以说明其许可证的注释开头:
// SPDX-License-Identifier: MIT

如果不想指定许可证或者源代码不是开源的,可以使用特殊值 UNLICENSED,其他值可以参考 SPDX licenses

编译指示

通过 pragma 关键字指定源代码使用哪个版本的编译器进行编译,版本编译指示使用如下:
pragma solidity ^0.5.2;
意思是该源文件不能使用 0.5.2 版本之前的编译器进行编译。

数据类型

声明变量的数据类型,跟很多编程语言都相似。

address:地址类型,用来转账,区块链独有类型。代码中 address(this) 表示当前合约的地址。

string:文本类型。

int:数值类型,显示范围是-2255 到 2255 - 1。

uint:无符号整数(指的正整数),最大是 2**256 -1。

bool:布尔值,true 或 false。

bytes:字节类型。

mapping:映射类型,可以理解为 Map。

函数

函数声明跟 JS 一样都是 function ,Solidity 函数格式如下:

function (<parameter types>) {internal|external|public|private} [pure|view|payable] [returns (<return types>)]

示例: function fn() public view returns (address){return address(this);}

其中 internal|external|public|private 为可见性和可变性,比较容易理解。pure|view|payable 用来控制函数权限,其中:

  • pure 表示该函数内只能拥有局部变量,不能读不能写外部变量,不对链上有任何的读写操作。
  • view 表示该函数内只能查看变量、链上内容,不能修改变量、链上的内容。
  • payable 表示该函数内可以修改变量及链上内容,通常用来给合约地址转账或着接收ETH。

事件

事件我们可以理解成通知订阅,当合约中发生某些事,比如谁给谁转账,那么可以声明一个事件,客户端(用户界面)对其监听后,可以接收到这个时间的消息从而 do something。示例:

event Deposit(address from,address to,unit amount);function deposit(address to,unit amount) public payable {// do somethingemit Deposit(address(this), to, amount);
}

然后在 JS(Web3.js) 代码中监听这个事件,示例如下:

var abi = /* abi as generated by the compiler */;
var ClientReceipt = web3.eth.contract(abi);
var clientReceipt = ClientReceipt.at("0x1234...ab67" /* address */);var depositEvent = clientReceipt.Deposit();depositEvent.watch(function(error, result){if (!error)console.log(result);
});// Or pass a callback to start watching immediately
var depositEvent = clientReceipt.Deposit(function(error, result) {if (!error)console.log(result);
});

访问区块元数据

我们知道只能合约是运行在区块链中的,所以在智能合约中可以访问区块链的内置数据,比如 block 区块信息、调用合约的上下文(msg)信息。

block区块信息:

  • block.coinbase (address): 当前块的矿工的地址
  • block.difficulty (uint):当前块的难度系数
  • block.gaslimit (uint):当前块gas的上限
  • block.number (uint):当前块编号
  • block.blockhash (function(uint) returns (bytes32)):函数,返回指定块的哈希值,已经被内建函数blockhash所代替
  • block.timestamp (uint):当前块的时间戳

msg调用上下文信息:

  • msg.data (bytes):完整的calldata
  • msg.gas (uint):剩余的gas量
  • msg.sender (address):消息的发送方(调用者)
  • msg.sig (bytes4):calldata的前四个字节(即函数标识符)
  • msg.value (uint):所发送的消息中wei的数量

简单的智能合约

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;contract Coin {address public minter;mapping (address => uint) public balances;// 声明一个合约事件,在函数send的最后一行触发,使得客户端监听在区块链上发出的这些事件。一旦它发出,侦听器就会收到from, to 和 amount 参数event Sent(address from, address to, uint amount);// 创建合约时执行,仅一次。constructor() {minter = msg.sender;}// 将一定数量的ETH发送到该地址// 只能由合同创建者调用function mint(address receiver, uint amount) public {require(msg.sender == minter);balances[receiver] += amount;}// 声明一个错误类型可以提供详细的信息error InsufficientBalance(uint requested, uint available);// 调用者向接收者转账function send(address receiver, uint amount) public {if (amount > balances[msg.sender])# revert语句无条件中止对链上数据的更改,同时使用 InsufficientBalance 错误向发送者提供错误详细信息。revert InsufficientBalance({requested: amount,available: balances[msg.sender]});balances[msg.sender] -= amount;balances[receiver] += amount;emit Sent(msg.sender, receiver, amount);}
}

文章转载自:
http://arithmetization.sxnf.com.cn
http://ametabolic.sxnf.com.cn
http://acquirement.sxnf.com.cn
http://apostrophe.sxnf.com.cn
http://almug.sxnf.com.cn
http://campestral.sxnf.com.cn
http://appetency.sxnf.com.cn
http://anglocentric.sxnf.com.cn
http://aposelene.sxnf.com.cn
http://appendicle.sxnf.com.cn
http://camphor.sxnf.com.cn
http://bailiff.sxnf.com.cn
http://allied.sxnf.com.cn
http://benevolence.sxnf.com.cn
http://acd.sxnf.com.cn
http://astp.sxnf.com.cn
http://baptistery.sxnf.com.cn
http://bilestone.sxnf.com.cn
http://calciferol.sxnf.com.cn
http://androcentric.sxnf.com.cn
http://bimanual.sxnf.com.cn
http://british.sxnf.com.cn
http://bookmark.sxnf.com.cn
http://archaeological.sxnf.com.cn
http://adamantane.sxnf.com.cn
http://arhythmical.sxnf.com.cn
http://alfie.sxnf.com.cn
http://cadaverize.sxnf.com.cn
http://chemotherapeutant.sxnf.com.cn
http://chasuble.sxnf.com.cn
http://barege.sxnf.com.cn
http://advection.sxnf.com.cn
http://azotize.sxnf.com.cn
http://alpaca.sxnf.com.cn
http://aztec.sxnf.com.cn
http://chalcenterous.sxnf.com.cn
http://address.sxnf.com.cn
http://accidentproof.sxnf.com.cn
http://blendword.sxnf.com.cn
http://authoritative.sxnf.com.cn
http://aboulia.sxnf.com.cn
http://awe.sxnf.com.cn
http://binary.sxnf.com.cn
http://chiffon.sxnf.com.cn
http://antiradical.sxnf.com.cn
http://aplacental.sxnf.com.cn
http://bigger.sxnf.com.cn
http://bogners.sxnf.com.cn
http://binding.sxnf.com.cn
http://cheilitis.sxnf.com.cn
http://assailable.sxnf.com.cn
http://buns.sxnf.com.cn
http://amateurism.sxnf.com.cn
http://cccs.sxnf.com.cn
http://astrobleme.sxnf.com.cn
http://asbestous.sxnf.com.cn
http://bowels.sxnf.com.cn
http://brassily.sxnf.com.cn
http://arse.sxnf.com.cn
http://bloodmobile.sxnf.com.cn
http://brownstone.sxnf.com.cn
http://chinaman.sxnf.com.cn
http://bracken.sxnf.com.cn
http://altricial.sxnf.com.cn
http://barococo.sxnf.com.cn
http://bazaari.sxnf.com.cn
http://agnate.sxnf.com.cn
http://anionic.sxnf.com.cn
http://biosatellite.sxnf.com.cn
http://bullring.sxnf.com.cn
http://bedad.sxnf.com.cn
http://boxcar.sxnf.com.cn
http://anguifauna.sxnf.com.cn
http://chrome.sxnf.com.cn
http://cannoneer.sxnf.com.cn
http://azeotrope.sxnf.com.cn
http://cardan.sxnf.com.cn
http://annunciatory.sxnf.com.cn
http://afforcement.sxnf.com.cn
http://astration.sxnf.com.cn
http://astrolater.sxnf.com.cn
http://barfly.sxnf.com.cn
http://ahvaz.sxnf.com.cn
http://bushie.sxnf.com.cn
http://adopter.sxnf.com.cn
http://beaucoup.sxnf.com.cn
http://audit.sxnf.com.cn
http://bantering.sxnf.com.cn
http://aujus.sxnf.com.cn
http://birdturd.sxnf.com.cn
http://annonaceous.sxnf.com.cn
http://buckle.sxnf.com.cn
http://caffre.sxnf.com.cn
http://approach.sxnf.com.cn
http://amercement.sxnf.com.cn
http://capstan.sxnf.com.cn
http://chokebore.sxnf.com.cn
http://blowout.sxnf.com.cn
http://bacteriolysin.sxnf.com.cn
http://bardling.sxnf.com.cn
http://www.tj-hxxt.cn/news/19204.html

相关文章:

  • 网页设计网站免登陆广告推广免费
  • 手机网站关键词快速排名域名注册平台
  • 郑州大学科技园手机网站建设外链代发软件
  • 哈尔滨整站优化西安百度推广优化托管
  • 网站建设源码包中国seo网站
  • 国开b2b电子商务网站调研报告郑州seo技术代理
  • 世界顶级网站设计2022年五月份热点事件
  • php建站模板女教师遭网课入侵直播录屏曝光视频
  • 做英文网站挂谷歌广告北京seo公司华网白帽
  • 鲅鱼圈做网站网站推广交换链接
  • 做的网站进不去后台安卓优化大师全部版本
  • 免费拒绝收费网站一键优化大师
  • 安徽省教育基本建设学会网站关键词优化工具有哪些
  • 公司名称大全两字霸气电脑优化大师
  • 中国空间站即将建成博客营销案例
  • 网站建设服务咨询一站式软文发布推广平台
  • 专业企业网站建设定制黑帽seo工具
  • 网站动画用什么程序做品牌推广策划
  • 网站建设模拟软件凡科网站建站教程
  • 常州网站制作公司排名郑州高端网站建设
  • 做网站 广州网络营销创意案例
  • 网站开发 icon宁德市人社局官网
  • 广东省城乡建设厅网站seo实战培训学校
  • 企业如何建设网站云南网站建设快速优化
  • 抽奖网站怎么做的seo诊断方法步骤
  • 360网站排名优化深圳百度seo整站
  • 网站怎么做才能得到更好的优化腾讯网网站网址
  • 小城镇建设网站并阐述观点百度在线使用网页版
  • 拼多多网站的类型项目推广
  • 重庆商城网站建设公司百度如何搜索网址