Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Commit 373bd22

Browse files
committed
Completed all methods in currency module
1 parent a6d3898 commit 373bd22

File tree

6 files changed

+140
-21
lines changed

6 files changed

+140
-21
lines changed

main.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,25 @@
22

33
import nftlabs.options
44
from nftlabs import NftlabsSdk
5+
from pprint import pprint
56
from nftlabs.modules.nft_types import MintArg
67
from nftlabs.types import Role
78

89
options = nftlabs.options.SdkOptions()
910
sdk = NftlabsSdk(options, "https://rpc-mumbai.maticvigil.com")
11+
sdk.set_private_key(os.getenv("PKEY"))
1012

1113
currency_module = sdk.get_currency_module("0xd75807e8D122A4DDF3bCCBB67fC33D8d78955726")
1214

13-
print(currency_module.total_supply())
15+
pprint(currency_module.get_value(1))
16+
pprint(currency_module.get_value(100000000000000000))
17+
pprint(currency_module.get_value(1000000000000000000))
18+
pprint(currency_module.get_value(10000000000000000000))
1419

15-
print(currency_module.get())
20+
pprint(currency_module.total_supply())
21+
pprint(currency_module.get())
22+
pprint(currency_module.get_all_role_members())
1623

17-
sdk.set_private_key(os.getenv("PKEY"))
1824

1925
nft_module = sdk.get_nft_module("0xbDfF8fb43688fB4D2184DF8029A7238ac1413A24")
2026
print(nft_module.total_supply())
@@ -34,5 +40,5 @@
3440
# ])
3541
# print(minted_nfts)
3642

37-
nft_module.grant_role(Role.admin, "0x26ec212ba1854F1a0c287e5B8E3e5463122EFb47")
43+
# nft_module.grant_role(Role.admin, "0x26ec212ba1854F1a0c287e5B8E3e5463122EFb47")
3844

nftlabs/errors/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
class NoSignerException(Exception):
3+
def __init__(self):
4+
super().__init__("No signer (private key) supplied to SDK")

nftlabs/modules/currency.py

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
from ..errors import NoSignerException
2+
from ..types import Role
13
from ..abi.coin import Coin
24
from .base import BaseModule
3-
from .currency_types import Currency
5+
from .currency_types import Currency, CurrencyValue
46
from ..abi.erc20 import ERC20
57
from web3 import Web3
8+
from typing import List, Dict
69

710

811
class CurrencyModule(BaseModule):
@@ -20,19 +23,115 @@ def total_supply(self) -> int:
2023
"""
2124
Gets the currency name, symbol, and decimals
2225
"""
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()
3126

32-
return Currency(name, symbol, decimals)
27+
def get(self) -> Currency:
28+
return self.__get_currency_metadata(self.address)
3329

3430
def balance_of(self, address: str) -> int:
3531
return self.__abi_module.balance_of.call(address)
3632

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+
)

nftlabs/modules/currency_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ class Currency:
66
name: str
77
symbol: str
88
decimals: int
9+
10+
@dataclasses.dataclass
11+
class CurrencyValue(Currency):
12+
value: str
13+
display_value: str
14+

nftlabs/sdk.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from typing import Optional
55

6+
from .errors import NoSignerException
67
from .modules import CurrencyModule, NftModule
78
from .options import SdkOptions
89
from .storage import IpfsStorage
@@ -45,14 +46,17 @@ def __init__(self, options: SdkOptions, url: str, private_key=""):
4546
if not self.client.isConnected():
4647
raise Exception("Failed to connect to the web3 provider")
4748

49+
"""
50+
Sets the Private Key used across the entire SDK. Calling this method
51+
will automatically reload the private key across all instantiated
52+
modules, which allows you to operate on behalf of multiple
53+
wallets by calling a single method.
54+
"""
4855
def set_private_key(self, private_key=""):
4956
self.__current_account = Account.from_key(private_key)
5057
self.__private_key = "0x" + private_key.lstrip("0x")
5158
self.signer_address = self.__current_account.address
5259

53-
# self.private_key = private_key
54-
# self.signer_address = address
55-
5660
"""
5761
Returns an instance of the currency module
5862
"""
@@ -88,7 +92,7 @@ def __get_storage(self) -> IpfsStorage:
8892

8993
def __get_signer_address(self) -> str:
9094
if self.signer_address == "":
91-
raise Exception("Trying to execute transaction but there's no private key set")
95+
raise NoSignerException()
9296
return self.signer_address
9397

9498
def __get_private_key(self) -> str:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# This call to setup() does all the work
1111
setup(
1212
name="nftlabs-sdk",
13-
version="0.0.3",
13+
version="0.0.6",
1414
description="Official Nftlabs sdk",
1515
long_description=README,
1616
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)