智能合约使用solidity语言编写的一个代码块形式上像其他开发语言当中的一个类,其实就是我们在区块链上设定的一些规则,这些规则可以被区块链节点调用,完成对应的功能。
Solidity是一种智能合约高级语言,运行在Ethereum虚拟机(EVM)之上
remix是编写与编译solidity智能合约的一个web ide 工具,使用该工具用户没有必要在本地部署智能合约的编辑工具
ABI是Application Binary Interface的缩写,字面意思 应用二进制接口,可以通俗的理解为 合约的接口说明。当合约被编译后,那么它的abi也就确定了。
bytecode可以认为是智能合约的字节码,可以简单认为是只能合约的二进制字节码
pragma solidity ^0.4.20;
contract MyToken {
/* This creates an array with all balances */
mapping (address => uint256) public balanceOf;
/* Initializes contract with initial supply tokens to the creator of the contract */
function MyToken(
uint256 initialSupply
) {
balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
}
/* Send coins */
function transfer(address _to, uint256 _value) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
balanceOf[msg.sender] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
}
function getBalance(address addr) returns(uint) {
return balanceOf[addr];
}
}
打开下面网址
http://remix.ethereum.org/
将合约代码复制到网站中,点击compile进行编译
如果代码没有错误可以成功编译点击detail查看详细信息,在detail中复制智能合约的api与bytecode,并进行存储
复制WEB3DEPLOY代码并在geth客户端中,需要把对应的变量替换为对应的变量值
编译效果如下图所示
将合约部署到区块链时需要开启挖矿功能,因为智能合约的部署需要算力,算力需要区块链上的矿机来提供
下一篇文章来探讨使用web3.js访问节点并进行交互
区块链 智能合约 代币 合约编译部署