2023-03-25 17:10:36 +01:00
|
|
|
pragma solidity >=0.8.0;
|
2021-10-04 16:24:51 +02:00
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
2021-10-04 16:24:51 +02:00
|
|
|
|
|
|
|
contract TokenUniqueSymbolIndex {
|
2023-03-25 17:10:36 +01:00
|
|
|
mapping(address => bool) isWriter;
|
|
|
|
mapping ( bytes32 => uint256 ) registry;
|
|
|
|
mapping ( address => bytes32 ) tokenIndex;
|
2021-10-04 16:24:51 +02:00
|
|
|
address[] tokens;
|
|
|
|
|
2023-03-25 17:23:16 +01:00
|
|
|
// Implements EIP173
|
|
|
|
address public owner;
|
|
|
|
|
|
|
|
// Implements Registry
|
2023-03-25 19:00:10 +01:00
|
|
|
bytes32[] public identifierList;
|
2023-03-25 17:23:16 +01:00
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
// Implements EIP173
|
|
|
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
|
|
|
|
|
|
|
|
// Implements AccountsIndex
|
|
|
|
event AddressKey(bytes32 indexed _symbol, address _token);
|
|
|
|
|
|
|
|
// Implements AccountsIndex
|
|
|
|
event AddressAdded(address _token);
|
|
|
|
|
|
|
|
// Implements AccountsIndexMutable
|
|
|
|
event AddressRemoved(address _token);
|
|
|
|
|
|
|
|
// Implements Writer
|
|
|
|
event WriterAdded(address _writer);
|
|
|
|
|
|
|
|
// Implements Writer
|
|
|
|
event WriterDeleted(address _writer);
|
2021-10-04 16:24:51 +02:00
|
|
|
|
2023-03-17 22:46:01 +01:00
|
|
|
constructor() {
|
2021-10-04 16:24:51 +02:00
|
|
|
owner = msg.sender;
|
|
|
|
tokens.push(address(0));
|
2023-03-25 19:00:10 +01:00
|
|
|
identifierList.push(bytes32(0));
|
2021-10-04 16:24:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Implements AccountsIndex
|
|
|
|
function entry(uint256 _idx) public view returns ( address ) {
|
|
|
|
return tokens[_idx + 1];
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:40:52 +01:00
|
|
|
// Implements RegistryClient
|
2021-10-04 16:24:51 +02:00
|
|
|
function addressOf(bytes32 _key) public view returns ( address ) {
|
|
|
|
uint256 idx;
|
|
|
|
|
|
|
|
idx = registry[_key];
|
|
|
|
return tokens[idx];
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
// Attempt to register the token at the given address.
|
|
|
|
// Will revet if symbol cannot be retrieved, or if symbol already exists.
|
2021-10-04 16:24:51 +02:00
|
|
|
function register(address _token) public returns (bool) {
|
2023-03-25 17:10:36 +01:00
|
|
|
require(isWriter[msg.sender]);
|
2021-10-04 16:24:51 +02:00
|
|
|
|
|
|
|
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));
|
2023-03-25 17:10:36 +01:00
|
|
|
require(token_symbol.length <= 32, 'ERR_TOKEN_SYMBOL_TOO_LONG');
|
|
|
|
token_symbol_key = bytes32(token_symbol);
|
2021-10-04 16:24:51 +02:00
|
|
|
|
|
|
|
idx = registry[token_symbol_key];
|
|
|
|
require(idx == 0);
|
|
|
|
|
|
|
|
registry[token_symbol_key] = tokens.length;
|
|
|
|
tokens.push(_token);
|
2023-03-25 19:00:10 +01:00
|
|
|
identifierList.push(token_symbol_key);
|
2023-03-25 17:10:36 +01:00
|
|
|
tokenIndex[_token] = token_symbol_key;
|
2023-03-25 17:23:16 +01:00
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
emit AddressKey(token_symbol_key, _token);
|
|
|
|
emit AddressAdded(_token);
|
2021-10-04 16:24:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements AccountsIndex
|
|
|
|
function add(address _token) public returns (bool) {
|
|
|
|
return register(_token);
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
// Implements AccountsIndexMutable
|
|
|
|
function remove(address _token) external returns (bool) {
|
|
|
|
uint256 i;
|
|
|
|
uint256 l;
|
|
|
|
|
2023-03-25 19:00:10 +01:00
|
|
|
require(isWriter[msg.sender] || msg.sender == owner, 'ERR_AXX');
|
2023-03-25 17:10:36 +01:00
|
|
|
require(tokenIndex[_token] != bytes32(0), 'ERR_NOT_FOUND');
|
|
|
|
|
|
|
|
l = tokens.length - 1;
|
|
|
|
i = registry[tokenIndex[_token]];
|
|
|
|
if (i < l) {
|
|
|
|
tokens[i] = tokens[l];
|
2023-03-25 19:00:10 +01:00
|
|
|
identifierList[i] = identifierList[l];
|
2023-03-25 17:10:36 +01:00
|
|
|
}
|
2023-03-25 19:00:10 +01:00
|
|
|
registry[tokenIndex[tokens[i]]] = i;
|
2023-03-25 17:10:36 +01:00
|
|
|
tokens.pop();
|
2023-03-25 19:00:10 +01:00
|
|
|
identifierList.pop();
|
2023-03-25 17:10:36 +01:00
|
|
|
registry[tokenIndex[_token]] = 0;
|
2023-03-25 19:00:10 +01:00
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
emit AddressRemoved(_token);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements AccountsIndexMutable
|
|
|
|
function activate(address _token) public pure returns(bool) {
|
|
|
|
_token;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements AccountsIndexMutable
|
|
|
|
function deactivate(address _token) public pure returns(bool) {
|
|
|
|
_token;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-04 16:24:51 +02:00
|
|
|
|
|
|
|
// Implements AccountsIndex
|
|
|
|
function entryCount() public view returns ( uint256 ) {
|
|
|
|
return tokens.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements EIP173
|
|
|
|
function transferOwnership(address _newOwner) public returns (bool) {
|
|
|
|
address oldOwner;
|
|
|
|
|
2023-03-25 17:10:36 +01:00
|
|
|
require(msg.sender == owner);
|
|
|
|
oldOwner = owner;
|
|
|
|
owner = _newOwner;
|
|
|
|
|
2021-10-04 16:24:51 +02:00
|
|
|
emit OwnershipTransferred(oldOwner, owner);
|
2023-03-25 17:10:36 +01:00
|
|
|
|
2023-03-17 22:46:01 +01:00
|
|
|
return true;
|
2021-10-04 16:24:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 18:57:45 +02:00
|
|
|
// Implements Writer
|
|
|
|
function addWriter(address _writer) public returns (bool) {
|
|
|
|
require(owner == msg.sender);
|
2023-03-25 17:10:36 +01:00
|
|
|
isWriter[_writer] = true;
|
|
|
|
|
|
|
|
emit WriterAdded(_writer);
|
|
|
|
|
2021-10-23 18:57:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements Writer
|
|
|
|
function deleteWriter(address _writer) public returns (bool) {
|
|
|
|
require(owner == msg.sender);
|
2023-03-25 17:10:36 +01:00
|
|
|
delete isWriter[_writer];
|
|
|
|
|
|
|
|
emit WriterDeleted(_writer);
|
|
|
|
|
2021-10-23 18:57:45 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-04 16:24:51 +02:00
|
|
|
|
2023-03-25 19:00:10 +01:00
|
|
|
// Implements Registry
|
|
|
|
function identifier(uint256 _idx) public view returns(bytes32) {
|
|
|
|
return identifierList[_idx + 1];
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:23:16 +01:00
|
|
|
// Implements Registry
|
|
|
|
function identifierCount() public view returns(uint256) {
|
2023-03-25 19:00:10 +01:00
|
|
|
return identifierList.length - 1;
|
2023-03-25 17:23:16 +01:00
|
|
|
}
|
|
|
|
|
2021-10-04 16:24:51 +02:00
|
|
|
// Implements EIP165
|
|
|
|
function supportsInterface(bytes4 _sum) public pure returns (bool) {
|
2023-03-25 19:00:10 +01:00
|
|
|
if (_sum == 0xeffbf671) { // Registry
|
2021-10-04 16:24:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-25 17:10:36 +01:00
|
|
|
if (_sum == 0xb7bca625) { // AccountsIndex
|
2021-10-04 16:24:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-25 17:10:36 +01:00
|
|
|
if (_sum == 0x9479f0ae) { // AccountsIndexMutable
|
2021-10-04 16:24:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-25 17:10:36 +01:00
|
|
|
if (_sum == 0x01ffc9a7) { // EIP165
|
2021-10-04 16:24:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-25 17:10:36 +01:00
|
|
|
if (_sum == 0x9493f8b2) { // EIP173
|
2021-10-04 16:24:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-10-23 18:57:45 +02:00
|
|
|
if (_sum == 0x80c84bd6) { // Writer
|
|
|
|
return true;
|
|
|
|
}
|
2021-10-04 16:24:51 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|