《Netkiller Blockchain 手札》
作者正在找工作,联系方式 13113668890
Mr. Neo Chan, 陈景峯(BG7NYT)
中国广东省深圳市望海路半岛城邦三期 518067 +86 13113668890
<netkiller@msn.com>
版权 © 2018 Netkiller(Neo Chan). All rights reserved.
版权声明
转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明
2.10.5. 资产投资与份额持有
传统艺术品投资门槛非常高,一是用户不知道从哪些渠道可以投资,二是艺术品价值过高,三是艺术品简单难。这导致了投资艺术品门槛过高。 Token 能实现份额化,实现人人参与,人人持有,P2P交易。
例如某机构上链一件艺术品,用户可以投资该艺术品的一定份额,可以转让他持有的权益。且交易去中心化,不受任何机构,管理者的制约。
下面的合约可以展示如何分割艺术品份额,最终达到链上资产的份额分割和持有与交易。
pragma solidity ^0.4.25; /** * @title SafeMath * @dev Math operations with safety checks that revert on error */ library SafeMath { /** * @dev Multiplies two numbers, reverts on overflow. */ function mul(uint256 a, uint256 b) internal pure returns (uint256) { // Gas optimization: this is cheaper than requiring 'a' not being zero, but the // benefit is lost if 'b' is also tested. // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 if (a == 0) { return 0; } uint256 c = a * b; require(c / a == b); return c; } /** * @dev Integer division of two numbers truncating the quotient, reverts on division by zero. */ function div(uint256 a, uint256 b) internal pure returns (uint256) { require(b > 0); // Solidity only automatically asserts when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; } /** * @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend). */ function sub(uint256 a, uint256 b) internal pure returns (uint256) { require(b <= a); uint256 c = a - b; return c; } /** * @dev Adds two numbers, reverts on overflow. */ function add(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a + b; require(c >= a); return c; } /** * @dev Divides two numbers and returns the remainder (unsigned integer modulo), * reverts when dividing by zero. */ function mod(uint256 a, uint256 b) internal pure returns (uint256) { require(b != 0); return a % b; } } contract Ownable { address public owner; event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); constructor() public { owner = msg.sender; } modifier onlyOwner() { require(msg.sender == owner); _; } function transferOwnership(address newOwner) public onlyOwner { require(newOwner != address(0)); emit OwnershipTransferred(owner, newOwner); owner = newOwner; } } contract NetkillerAssetsToken is Ownable { using SafeMath for uint256; string public name; string public symbol; uint public decimals; uint256 public totalSupply; mapping(address => mapping(string => uint256)) internal balances; mapping(string => address) internal tokens; event Transfer(address indexed _from, address indexed _to, string indexed _tokenId); event Burn(address indexed from, string _tokenId); constructor( string tokenName, string tokenSymbol, uint decimalUnits ) public { owner = msg.sender; name = tokenName; symbol = tokenSymbol; decimals = decimalUnits; totalSupply = 0; } function add(address _owner, string _tokenId) onlyOwner returns(bool status){ balances[_owner][_tokenId] = 100 * 10 ** uint256(decimals); tokens[_tokenId] = _owner; totalSupply = totalSupply.add(1); return true; } function balanceOf(address _owner, string _tokenId) constant returns(uint balance){ return balances[_owner][_tokenId]; } function ownerOf(string _tokenId) constant returns (address owner) { return tokens[_tokenId]; } function transfer(address _to, string _tokenId){ address _from = msg.sender; uint256 amount = balances[_from][_tokenId]; transfer(_to, amount, _tokenId); } function transfer(address _to, uint256 _value, string _tokenId){ require(msg.sender == ownerOf(_tokenId)); require(msg.sender != _to); require(_to != address(0)); address _from = msg.sender; uint256 amount = balances[_from][_tokenId]; require(amount >= _value); balances[_from][_tokenId] = balances[_from][_tokenId].sub(_value); balances[_to][_tokenId] = balances[_to][_tokenId].add(_value); tokens[_tokenId] = _to; emit Transfer(_from, _to, _tokenId); } function burn(address _owner, string _tokenId) onlyOwner public returns (bool success) { require(balances[_owner][_tokenId] > 0 && balances[_owner][_tokenId] == 100 * 10 ** uint256(decimals)); balances[_owner][_tokenId] = 0; tokens[_tokenId] = address(0); totalSupply = totalSupply.sub(1); emit Burn(msg.sender, _tokenId); return true; } }
由于 ERC721 不太符合我的需求,所以我结合 ERC20 和 ERC721 写出了我的合约。合约尽量保持了ERC20的使用习惯,函数定义尽量兼容 ERC20。
我们来看下面的构造方法,每个种类的物品一个合约,例如字画,陶瓷,青铜器。
constructor( string tokenName, string tokenSymbol, uint decimalUnits ) public { owner = msg.sender; name = tokenName; symbol = tokenSymbol; decimals = decimalUnits; totalSupply = 0; }
通过下面函数,添加资产到 Token,使链上资产与Token绑定。
function add(address _owner, string _tokenId) onlyOwner returns(bool status){ balances[_owner][_tokenId] = 100 * 10 ** uint256(decimals); tokens[_tokenId] = _owner; totalSupply = totalSupply.add(1); return true; }
balances[_owner][_tokenId] = 100 * 10 ** uint256(decimals); 初始化份额是 100 表示 100%
totalSupply = totalSupply.add(1); 物品件数加一。可以用于统计链上资产的数量。
下面函数是查询资产的持有人
function ownerOf(string _tokenId) constant returns (address owner) { return tokens[_tokenId]; }
下面函数是,权益转让和权益份额转让。
function transfer(address _to, string _tokenId){ address _from = msg.sender; uint256 amount = balances[_from][_tokenId]; transfer(_to, amount, _tokenId); } function transfer(address _to, uint256 _value, string _tokenId){ require(msg.sender == ownerOf(_tokenId)); require(msg.sender != _to); require(_to != address(0)); address _from = msg.sender; uint256 amount = balances[_from][_tokenId]; require(amount >= _value); balances[_from][_tokenId] = balances[_from][_tokenId].sub(_value); balances[_to][_tokenId] = balances[_to][_tokenId].add(_value); tokens[_tokenId] = _to; emit Transfer(_from, _to, _tokenId); }
接下来,我们就是可以开发 Dapp 钱包了,在钱包中实现资产的转移交易。
这个合约可以一直到 EOS 上,Hyperledger Fabric 不可以,因为 Fabric 没有锁的机制,会导致计算出错。