## Solidity 智能智能合约编写 ### Solidity 代码说明 ![](https://box.kancloud.cn/550cd35d5866db258f27061dc3bafcdc_2270x774.png) ### Solidity 数据类型 ![](https://box.kancloud.cn/bdddcb9d1cea1917c8be57cf8b06eb2a_2300x1254.png) ![](https://box.kancloud.cn/0564eaff8597e1a41419d5d42034d21d_2298x1238.png) ### Solidity 函数/方法 ![](https://box.kancloud.cn/d1b33d5c5a48067050a9c75cc749e614_2298x1168.png) ![](https://box.kancloud.cn/a8ccd75fadefaa8577caedbf10b32938_2300x1266.png) ### Solidity 事件 ![](https://box.kancloud.cn/4130dc31642668f2ea1230a932a98a46_2300x960.png) ### Solidity 合约内部调用方法 ![](https://box.kancloud.cn/ebfe1f92d68b86b939351fd4c5729410_2298x1128.png) ### 合约示例 示例1: ``` pragma solidity ^0.5.0; contract SimpleStorage { uint balance; function save() public payable { balance += msg.value; } function query() public view returns (uint) { return balance; } function take(uint value) public { require(value <= balance, "balance is not enough"); balance -= value; msg.sender.transfer(value); } } ``` 示例2: ``` pragma solidity ^0.5.0; library SafeMath { function sub(uint256 a, uint256 b) internal pure returns (uint256) { assert(b <= a); return a - b; } function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; assert(c >= a); return c; } } contract TestERC20Token { using SafeMath for uint256; string public constant name = "Test ERC20 Token"; string public constant symbol = "TET"; uint256 public constant decimals = 6; uint256 public totalSupply = 100000000 * 10 ** decimals; mapping (address => uint256) private balances; mapping (address => mapping (address => uint256)) allowed; event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); function balanceOf(address _owner) public view returns (uint256 balance) { return balances[_owner]; } function transfer(address _to, uint256 _value) public returns (bool success) { require (_to != address(0), ""); require((balances[msg.sender] >= _value), ""); balances[msg.sender] = balances[msg.sender].sub(_value); balances[_to] = balances[_to].add(_value); emit Transfer(msg.sender, _to, _value); return true; } function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require (_to != address(0), ""); require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value, ""); balances[_from] = balances[_from].sub(_value); balances[_to] = balances[_to].add(_value); emit Transfer(_from, _to, _value); return true; } 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]; } } ``` ### 合约转账与主网币转账的区别 ![](https://box.kancloud.cn/42e28876085c60bfd57b33949c49c1f4_2278x1256.png) ### 编写合约安全注意事项 ![](https://box.kancloud.cn/e813a6a83c0dfd6aeba522bc64d1ee87_2300x1238.png)