-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |