eth-address-index/solidity/TokenUniqueSymbolIndex.sol

106 lines
2.3 KiB
Solidity
Raw Normal View History

2021-01-10 22:03:26 +01:00
pragma solidity >0.6.11;
2020-12-29 19:44:14 +01:00
// SPDX-License-Identifier: GPL-3.0-or-later
contract TokenUniqueSymbolIndex {
// EIP 173
address public owner;
2021-04-30 16:15:25 +02:00
address newOwner;
2020-12-29 19:44:14 +01:00
mapping ( bytes32 => uint256 ) public registry;
2020-12-29 21:19:12 +01:00
address[] tokens;
2020-12-29 19:44:14 +01:00
2021-04-30 17:22:32 +02:00
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
event AddressAdded(address indexed addedAccount, uint256 indexed accountIndex); // AccountsIndex
2021-01-10 22:03:26 +01:00
constructor() public {
2020-12-29 19:44:14 +01:00
owner = msg.sender;
tokens.push(address(0));
}
2021-04-30 16:15:25 +02:00
// Implements AccountsIndex
2020-12-30 08:58:37 +01:00
function entry(uint256 _idx) public view returns ( address ) {
return tokens[_idx + 1];
}
2021-04-30 16:15:25 +02:00
// Implements Registry
2020-12-29 19:44:14 +01:00
function addressOf(bytes32 _key) public view returns ( address ) {
uint256 idx;
idx = registry[_key];
return tokens[idx];
}
2021-03-21 10:21:23 +01:00
function register(address _token) public returns (bool) {
2020-12-29 19:44:14 +01:00
require(msg.sender == owner);
bytes memory token_symbol;
bytes32 token_symbol_key;
uint256 idx;
(bool _ok, bytes memory _r) = _token.call(abi.encodeWithSignature('symbol()'));
require(_ok);
token_symbol = abi.decode(_r, (bytes));
token_symbol_key = sha256(token_symbol);
idx = registry[token_symbol_key];
require(idx == 0);
registry[token_symbol_key] = tokens.length;
tokens.push(_token);
2021-04-30 17:22:32 +02:00
emit AddressAdded(_token, tokens.length - 1);
2020-12-29 19:44:14 +01:00
return true;
}
2021-04-30 16:15:25 +02:00
// Implements AccountsIndex
function add(address _token) public returns (bool) {
return register(_token);
}
// Implements AccountsIndex
2021-03-21 10:21:23 +01:00
function entryCount() public view returns ( uint256 ) {
2020-12-29 21:19:12 +01:00
return tokens.length - 1;
2020-12-29 19:44:14 +01:00
}
2021-04-30 17:22:32 +02:00
// Implements EIP173
function transferOwnership(address _newOwner) public returns (bool) {
require(msg.sender == owner);
newOwner = _newOwner;
}
// Implements OwnedAccepter
function acceptOwnership() public returns (bool) {
address oldOwner;
require(msg.sender == newOwner);
oldOwner = owner;
owner = newOwner;
newOwner = address(0);
emit OwnershipTransferred(oldOwner, owner);
}
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0xcbdb05c7) { // AccountsIndex
return true;
}
if (_sum == 0xbb34534c) { // Registry
return true;
}
2021-04-30 17:22:32 +02:00
if (_sum == 0x01ffc9a7) { // EIP165
return true;
}
if (_sum == 0x9493f8b2) { // EIP173
return true;
}
if (_sum == 0x37a47be4) { // OwnedAccepter
return true;
}
return false;
}
2020-12-29 19:44:14 +01:00
}