Skip to content

Commit

Permalink
feat: add tokens container (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu authored Jan 3, 2022
1 parent 7544caf commit 65ee3bf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ape_tokens/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from ape import plugins

from .converters import TokenConversions
from .managers import TokenManager as _TokenManager


@plugins.register(plugins.ConversionPlugin)
def converters():
yield int, TokenConversions


tokens = _TokenManager()

__all__ = [
"tokens",
]
65 changes: 65 additions & 0 deletions ape_tokens/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from ape.api import AddressAPI
from ape.types import ContractType
from ape.utils import cached_property
from eth_utils import to_checksum_address
from tokenlists import TokenListManager

ERC20 = ContractType.from_dict(
{
"contractName": "ERC20", # type: ignore
"abi": [
{
"type": "function",
"stateMutability": "view",
"name": "name",
"outputs": [{"type": "string"}],
},
{
"type": "function",
"stateMutability": "view",
"name": "symbol",
"outputs": [{"type": "string"}],
},
{
"type": "function",
"stateMutability": "view",
"name": "decimals",
"outputs": [{"type": "uint8"}],
},
{
"type": "function",
"stateMutability": "view",
"name": "totalSupply",
"outputs": [{"type": "uint256"}],
},
{
"type": "function",
"stateMutability": "view",
"name": "balanceOf",
"inputs": [{"name": "holder", "type": "address"}],
"outputs": [{"type": "uint256"}],
},
],
}
)


class TokenManager(dict):
@cached_property
def _manager(self) -> TokenListManager:
return TokenListManager()

@cached_property
def _Contract(self):
from ape import Contract

return Contract

def __getitem__(self, symbol: str) -> AddressAPI:
try:
token_info = self._manager.get_token_info(symbol)

except ValueError as e:
raise KeyError(f"Symbol '{symbol}' is not a known token symbol") from e

return self._Contract(to_checksum_address(token_info.address), contract_type=ERC20)

0 comments on commit 65ee3bf

Please sign in to comment.