Toc
  1. 一、Token
  2. 二、TokenSale
Toc
0 results found
bbcfive
Filx智能合约源码解析

一、Token

token.sol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// 使用solidity0.5.0及以上版本
pragma solidity ^0.5.0;

contract FilxToken {
string public name = "FILX Token"; //Optional
string public symbol = "FILX"; //Optional
uint256 public totalSupply;

// 注册Transfer和Approval两个交易事件
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(
address indexed _owner,
address indexed _spender,
uint256 _value
);

// 创建balanceOf和allowance两个键值对
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;

constructor(uint256 _initialSupply) public {
// 令当前交易地址的余额等于_initialSupply
balanceOf[msg.sender] = _initialSupply;
totalSupply = _initialSupply;
}

// 创建转账函数
function transfer(address _to, uint256 _value)
public
returns (bool success)
{
// 判断当前交易地址的余额是否大于等于_value,否则抛出错误"Not enough balance"
require(balanceOf[msg.sender] >= _value, "Not enough balance");
// 是则当前交易地址的余额减去_value
balanceOf[msg.sender] -= _value;
// 转出地址的余额加_value
balanceOf[_to] += _value;
// 触发Transfer事件
emit Transfer(msg.sender, _to, _value);
// 返回true
return true;
}

// 创建准许函数
function approve(address _spender, uint256 _value)
public
returns (bool success)
{
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}

// 创建自定义转出账户的转账函数
function transferFrom(
address _from,
address _to,
uint256 _value
) public returns (bool success) {
// 判断当前转出账户的余额是否大于等于_value
require(
balanceOf[_from] >= _value,
"_from does not have enough tokens"
);
// 判断当前津贴转出账户的余额是否大于等于_value
require(
allowance[_from][msg.sender] >= _value,
"Spender limit exceeded"
);
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}

二、TokenSale

tokenSale.sol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pragma solidity ^0.5.0;

// 引入文件
import "./token.sol";

contract FILXTokenSale {
address payable admin;
FilxToken public tokenContract;

constructor(FilxToken _tokenContract) public {
admin = msg.sender;
tokenContract = _tokenContract;
}

function buyTokens(uint256 _numberOfTokens) public payable{

require(
// 判断_numberOfTokens与msg.value匹配
_numberOfTokens == msg.value / 10**14,
"Number of tokens does not match with the value"
);

require(
// 判断当前地址的余额大于等于_numberOfTokens
tokenContract.balanceOf(address(this)) >= _numberOfTokens,
"Contact does not have enough tokens"
);
require(
// 购买token
tokenContract.transfer(msg.sender, _numberOfTokens),
"Some problem with token transfer"
);
}

function endSale() public {
require(msg.sender == admin, "Only the admin can call this function");
require(
tokenContract.transfer(
address(0),
tokenContract.balanceOf(address(this))
),
"Unable to transfer tokens to 0x0000"
);
// destroy contract
selfdestruct(admin);
}
}

源码链接:https://filxcoin.com/#/sol

本文作者:bbcfive
版权声明:本文首发于bbcfive的博客,转载请注明出处!