1
+ from ..errors import NoSignerException
2
+ from ..types import Role
1
3
from ..abi .coin import Coin
2
4
from .base import BaseModule
3
- from .currency_types import Currency
5
+ from .currency_types import Currency , CurrencyValue
4
6
from ..abi .erc20 import ERC20
5
7
from web3 import Web3
8
+ from typing import List , Dict
6
9
7
10
8
11
class CurrencyModule (BaseModule ):
@@ -20,19 +23,115 @@ def total_supply(self) -> int:
20
23
"""
21
24
Gets the currency name, symbol, and decimals
22
25
"""
23
- def get (self ) -> Currency :
24
- return self .__get_currency_metadata ()
25
-
26
- def __get_currency_metadata (self ):
27
- erc20_client = ERC20 (self .get_client (), self .address )
28
- name = erc20_client .name .call ()
29
- symbol = erc20_client .symbol .call ()
30
- decimals = erc20_client .decimals .call ()
31
26
32
- return Currency (name , symbol , decimals )
27
+ def get (self ) -> Currency :
28
+ return self .__get_currency_metadata (self .address )
33
29
34
30
def balance_of (self , address : str ) -> int :
35
31
return self .__abi_module .balance_of .call (address )
36
32
37
- # def balance(self):
38
- # return self.__abi_module.balance_of.call(self.get_client().)
33
+ def balance (self ):
34
+ return self .__abi_module .balance_of .call (self .get_signer_address ())
35
+
36
+ def allowance (self , spender : str ) -> int :
37
+ return self .__abi_module .allowance .call (self .get_signer_address (), spender )
38
+
39
+ def allowance_of (self , owner : str , spender : str ) -> int :
40
+ return self .__abi_module .allowance .call (owner , spender )
41
+
42
+ def set_allowance (self , spender : str , amount : int ):
43
+ return self .execute_tx (self .__abi_module .approve .build_transaction (
44
+ spender , amount , self .get_transact_opts ()
45
+ ))
46
+
47
+ def mint_to (self , to : str , amount : int ):
48
+ return self .execute_tx (self .__abi_module .mint .build_transaction (
49
+ to , amount , self .get_transact_opts ()
50
+ ))
51
+
52
+ def mint (self , amount : int ):
53
+ return self .execute_tx (self .__abi_module .mint .build_transaction (
54
+ self .get_signer_address (), amount , self .get_transact_opts ()
55
+ ))
56
+
57
+ def burn (self , amount : int ):
58
+ return self .execute_tx (self .__abi_module .burn .build_transaction (
59
+ amount , self .get_transact_opts ()
60
+ ))
61
+
62
+ def burn_from (self , from_address : str , amount : int ):
63
+ return self .execute_tx (self .__abi_module .burn_from .build_transaction (
64
+ from_address , amount , self .get_transact_opts ()
65
+ ))
66
+
67
+ def transfer_from (self , from_address : str , to_address : str , amount : int ):
68
+ return self .execute_tx (self .__abi_module .transfer_from .build_transaction (
69
+ from_address , to_address , amount , self .get_transact_opts ()
70
+ ))
71
+
72
+ def grant_role (self , role : Role , address : str ):
73
+ role_hash = role .get_hash ()
74
+ self .execute_tx (self .__abi_module .grant_role .build_transaction (
75
+ role_hash , address , self .get_transact_opts ()
76
+ ))
77
+
78
+ def revoke_role (self , role : Role , address : str ):
79
+ role_hash = role .get_hash ()
80
+
81
+ try :
82
+ signer_address = self .get_signer_address ()
83
+ if signer_address .lower () != address .lower ():
84
+ pass
85
+ self .execute_tx (self .__abi_module .renounce_role .build_transaction (
86
+ role_hash , address , self .get_transact_opts ()
87
+ ))
88
+ return
89
+ except NoSignerException :
90
+ pass
91
+
92
+ self .execute_tx (self .__abi_module .revoke_role .build_transaction (
93
+ role_hash , address , self .get_transact_opts ()
94
+ ))
95
+
96
+ def get_role_members (self , role : Role ) -> List [str ]:
97
+ role_hash = role .get_hash ()
98
+ count = self .__abi_module .get_role_member_count .call (role_hash )
99
+ return [self .__abi_module .get_role_member .call (role_hash , i ) for i in range (count )]
100
+
101
+ def get_all_role_members (self ) -> Dict [Role , List [str ]]:
102
+ return {
103
+ Role .admin .name : self .get_role_members (Role .admin ),
104
+ Role .minter .name : self .get_role_members (Role .minter ),
105
+ Role .transfer .name : self .get_role_members (Role .transfer ),
106
+ Role .pauser .name : self .get_role_members (Role .pauser )
107
+ }
108
+
109
+ def get_value (self , value : int ) -> Currency :
110
+ return self .__get_currency_value (self .address , value )
111
+
112
+ def __get_currency_value (self , asset_address : str , price : int ) -> CurrencyValue :
113
+ metadata = self .__get_currency_metadata (asset_address )
114
+ return CurrencyValue (
115
+ name = metadata .name ,
116
+ decimals = metadata .decimals ,
117
+ symbol = metadata .symbol ,
118
+ value = str (price ),
119
+ display_value = self .format_units (price , metadata .decimals )
120
+ )
121
+
122
+ @staticmethod
123
+ def format_units (value : int , decimals : int ) -> str :
124
+ decimal_transformer = float (10 ** decimals )
125
+ val = float (value ) / decimal_transformer
126
+ return f'{ val :.{decimals }f} '
127
+
128
+ def __get_currency_metadata (self , asset_address : str ) -> Currency :
129
+ if asset_address .lower ().startswith ("0x0000000000000000000000000000000000000000" ):
130
+ return Currency (name = "" , symbol = "" , decimals = 0 )
131
+
132
+ erc20_module = ERC20 (self .get_client (), asset_address )
133
+ return Currency (
134
+ name = erc20_module .name .call (),
135
+ symbol = erc20_module .symbol .call (),
136
+ decimals = erc20_module .decimals .call ()
137
+ )
0 commit comments