eth-address-index/solidity/TokenUniqueSymbolIndex.sol

71 lines
1.5 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;
mapping ( bytes32 => uint256 ) public registry;
2020-12-29 21:19:12 +01:00
address[] tokens;
2020-12-29 19:44:14 +01:00
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));
}
// EIP 165
function supportsInterface(bytes4 _interfaceCode) public pure returns ( bool ) {
if (_interfaceCode == bytes4(0x325d15e2)) {
return true;
}
if (_interfaceCode == bytes4(0x01ffc9a7)) {
return true;
}
return false;
}
2020-12-30 08:58:37 +01:00
function entry(uint256 _idx) public view returns ( address ) {
return tokens[_idx + 1];
}
2021-01-04 16:52:12 +01:00
// EIP 173
2020-12-29 19:44:14 +01:00
function transferOwnership() public {
revert("owner cannot be changed");
}
function addressOf(bytes32 _key) public view returns ( address ) {
uint256 idx;
idx = registry[_key];
return tokens[idx];
}
function register(bytes32 _key, address _token) public returns (bool) {
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);
require(_key == token_symbol_key);
idx = registry[token_symbol_key];
require(idx == 0);
registry[token_symbol_key] = tokens.length;
tokens.push(_token);
return true;
}
function registryCount() public view returns ( uint256 ) {
2020-12-29 21:19:12 +01:00
return tokens.length - 1;
2020-12-29 19:44:14 +01:00
}
}