eth-address-index/solidity/TokenUniqueSymbolIndexAddressDeclarator.sol

116 lines
2.7 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 TokenUniqueSymbolIndexAddressDeclarator {
2020-12-29 19:44:14 +01:00
// EIP 173
address public owner;
2021-04-30 16:15:25 +02:00
address newOwner;
address public addressDeclaratorAddress;
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
constructor(address _addressDeclaratorAddress) public {
2020-12-29 19:44:14 +01:00
owner = msg.sender;
addressDeclaratorAddress = _addressDeclaratorAddress;
2020-12-29 19:44:14 +01:00
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);
bool ok;
bytes memory r;
2020-12-29 19:44:14 +01:00
bytes memory token_symbol;
bytes32 token_symbol_key;
uint256 idx;
(ok, r) = _token.call(abi.encodeWithSignature('symbol()'));
require(ok);
2020-12-29 19:44:14 +01:00
token_symbol = abi.decode(r, (bytes));
2020-12-29 19:44:14 +01:00
token_symbol_key = sha256(token_symbol);
(ok, r) = addressDeclaratorAddress.call(abi.encodeWithSignature("addDeclaration(address,bytes32)", _token, token_symbol_key));
require(ok);
require(r[31] == 0x01);
2020-12-29 19:44:14 +01:00
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
}