pragma solidity ^0.4.23;
contract Hello{
uint private money = 1000;
function echoStr() public pure returns(string){
return 'str';
}
function echoExternal() external pure returns(string){
return 'external';
}
function externalTest() public view returns(string){
return this.echoExternal();
}
}
contract Hello2 is Hello{
function test() public view returns(string){
return echoStr();
}
}
internal 只能在继承的合约内部或合约内部使用,不能在合约的外部使用
external 只能用this用来外部调用 或者 Hello h = new Hello();引入调用
private 只能被自己使用,不能被继承
pragma solidity ^0.4.23;
contract Hello{
function echoStr() internal pure returns(string){
return 'str';
}
}
contract Hello2 is Hello{
function test() public view returns(string){
return echoStr();
}
function test2() public view returns(string){
return '22222';
}
function test3() public view returns(string){
return this. echoExternal();
}
}
contract Hello3{
Hello h = new Hello();
function externalEcho() view returns(string){
return h.echoExternal() ;
}
}
pragma solidity ^0.4.23;
contract Hello{
uint public num;
string public name;
function setData(uint _num,string _name){
num = _num;
name = _name;
}
function test(){
setData({_name:"zy",_num:5});
}
}
函数的重载
函数的重载
1,函数的名字相同
2,函数的类型数量不同。
参数数量不同
function func(uint num){}
function func(){}
参数类型不同
function func(uint num){}
function func(uint8 num){}
3,不考虑函数的返回值是否相同
address 和 uint160 要注意,虽然能通过编译,但是不能够被调用( address = uint160 无法判断该调用谁)
function func(uint160 num){}
function func(address num){}
一些语法
pragma solidity ^0.4.0;
contract Hello{
string Myname = "zy"; // string 没有 length 属性 需要 bytes(_name).length;强转获得长度
bool _a ;
// int 可正可负 int256 uint 只能正数 uint256
int _num1 = 1 ;
int _num2 = 200 ;
uint _num3 = 2;
/**
* 权限 private internal external public
* 标识符 pure constant view payable
* view 可以自由调用,因为它只是“查看”区块链的状态而不改变它。不消耗燃料
* pure 也可以自由调用,不写入区块链 不消耗燃料
* payable 交易操作需要使用
**/
function getName() public view returns(string)
{
return Myname;
}
// 修改 消耗燃料
function changeMyName(string _newName) public{
Myname = _newName;
}
function pureTest(string _name) pure public returns(string){
return _name;
}
function getBool() view public returns(bool){
return _a;
}
function getBool2() view public returns(bool){
return !_a;
}
function panduan() view public returns(bool){
return _num1 == _num2;
}
function panduan2() view public returns(bool){
return _num1 != _num2;
}
function yu() view public returns(bool){
return (_num1 == _num2) && true;
}
function huo() view public returns(bool){
return (_num1 == _num2)||true;
}
function add(uint a,uint b)pure public returns(uint){
return a+b;
}
function fan() view public returns(uint){
return ~_num3;
}
function lyu() view public returns(int){
return _num1<<1;
}
function flow() view public returns(uint){
uint8 mm = 255;
mm++;
return mm;
}
function flow2() view public returns(uint){
uint8 mm = 0;
mm--;
return mm;
}
function interTest() returns(uint){
return 2/4*1000;
}
bytes1 public bynum1 = 0x7a; // 定义的时候加public会默认加个公共方法 1个字节 8位 bytes 一旦固定长度不得修改长度
function getLength() returns(uint){
return bynum1.length;
}
bytes public name = new bytes(2);
function IniName(){
name[0] = 0x7a;
name[1] = 0x68;
}
function getnameLength() view returns(uint){
return name.length;
}
function changeName(){
name[0] = 0x88;
}
function changeLength(){
name.length = 5; //设置了长度后值会右边填充0
}
// 数组后面添加元素
function pushtest(){
name.push(0x99);
}
}
payable
pragma solidity ^0.4.0;
contract Hello{
address public account; // 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 uint160
// payable 关键字用来给合约地址充值转账
function pay() payable{
}
function getBalance() view returns(uint){
return this.balance;
}
// 获取合约地址
function getThis() view returns(address){
return this;
}
function getOneBalance() view returns(uint){
address account = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
return account.balance;
}
// transfer转账 到某个账户
function transfer() payable{
address account = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
account.transfer(msg.value);
}
// transfer转账 到合约中
function transfer2() payable{
this.transfer(msg.value);
}
//转账 到合约中 需要一个回滚函数,不然会报错
function () payable{
}
function transfer3() payable{
address account = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
account.transfer(10 ether);
}
//send 不会报错 指会返回 true 或 false
function sends() payable{
address account = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
account.send(10 ether);
}
}
solstring
pragma solidity ^0.4.0;
contract Hello{
string name = "zy"; // string 没有 length 属性 需要 bytes(_name).length;强转获得长度
string name2 = "!$%^&#@$ada";
function getLenght() view returns(uint){
return bytes(name).length;
}
function changeName() view returns (bytes1){
return bytes(name)[1];
}
function getName() view returns(bytes){
return bytes(name);
}
function changeName2(){
bytes(name)[0] = 'C';
}
function getLenght2() view returns(uint){
return bytes(name2).length;
}
}
发布测试代币
pragma solidity ^0.4.24;
// ----------------------------------------------------------------------------
// 'FIXED' 'Example Fixed Supply Token' token contract
//
// Symbol : USDT
// Name : USDT
// Total supply: 1,000,000,000.0000 发型代币数量
// Decimals : 4 小数点
//
// Enjoy.
//
// (c) BokkyPooBah / Bok Consulting Pty Ltd 2018. The MIT Licence.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Safe maths
// ----------------------------------------------------------------------------
library SafeMath {
function add(uint a, uint b) internal pure returns (uint c) {
c = a + b;
require(c >= a);
}
function sub(uint a, uint b) internal pure returns (uint c) {
require(b <= a);
c = a - b;
}
function mul(uint a, uint b) internal pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}
function div(uint a, uint b) internal pure returns (uint c) {
require(b > 0);
c = a / b;
}
}
// ----------------------------------------------------------------------------
// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
// ----------------------------------------------------------------------------
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
// ----------------------------------------------------------------------------
// Contract function to receive approval and execute function in one call
//
// Borrowed from MiniMeToken
// ----------------------------------------------------------------------------
contract ApproveAndCallFallBack {
function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}
// ----------------------------------------------------------------------------
// Owned contract
// ----------------------------------------------------------------------------
contract Owned {
address public owner;
address public newOwner;
event OwnershipTransferred(address indexed _from, address indexed _to);
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
function acceptOwnership() public {
require(msg.sender == newOwner);
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
newOwner = address(0);
}
}
// ----------------------------------------------------------------------------
// ERC20 Token, with the addition of symbol, name and decimals and a
// fixed supply
// ----------------------------------------------------------------------------
contract FixedSupplyToken is ERC20Interface, Owned {
using SafeMath for uint;
string public symbol;
string public name;
uint8 public decimals;
uint _totalSupply;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
constructor() public {
symbol = "USDT";
name = "USDT"; //放代币名称
decimals = 4;
_totalSupply = (1*10**9) * 10**uint(decimals);
balances[owner] = _totalSupply;
emit Transfer(address(0), owner, _totalSupply);
}
// ------------------------------------------------------------------------
// Total supply
// ------------------------------------------------------------------------
function totalSupply() public view returns (uint) {
return _totalSupply.sub(balances[address(0)]);
}
// ------------------------------------------------------------------------
// Get the token balance for account `tokenOwner`
// ------------------------------------------------------------------------
function balanceOf(address tokenOwner) public view returns (uint balance) {
return balances[tokenOwner];
}
// ------------------------------------------------------------------------
// Transfer the balance from token owner's account to `to` account
// - Owner's account must have sufficient balance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] = balances[msg.sender].sub(tokens);
balances[to] = balances[to].add(tokens);
emit Transfer(msg.sender, to, tokens);
return true;
}
// ------------------------------------------------------------------------
// Token owner can approve for `spender` to transferFrom(...) `tokens`
// from the token owner's account
//
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// recommends that there are no checks for the approval double-spend attack
// as this should be implemented in user interfaces
// ------------------------------------------------------------------------
function approve(address spender, uint tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
// ------------------------------------------------------------------------
// Transfer `tokens` from the `from` account to the `to` account
//
// The calling account must already have sufficient tokens approve(...)-d
// for spending from the `from` account and
// - From account must have sufficient balance to transfer
// - Spender must have sufficient allowance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = balances[from].sub(tokens);
allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
balances[to] = balances[to].add(tokens);
emit Transfer(from, to, tokens);
return true;
}
// ------------------------------------------------------------------------
// Returns the amount of tokens approved by the owner that can be
// transferred to the spender's account
// ------------------------------------------------------------------------
function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
return allowed[tokenOwner][spender];
}
// ------------------------------------------------------------------------
// Token owner can approve for `spender` to transferFrom(...) `tokens`
// from the token owner's account. The `spender` contract function
// `receiveApproval(...)` is then executed
// ------------------------------------------------------------------------
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
return true;
}
// ------------------------------------------------------------------------
// Don't accept ETH
// ------------------------------------------------------------------------
function () public payable {
revert();
}
// ------------------------------------------------------------------------
// Owner can transfer out any accidentally sent ERC20 tokens
// ------------------------------------------------------------------------
function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
return ERC20Interface(tokenAddress).transfer(owner, tokens);
}
}