-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstory_client.py
More file actions
185 lines (150 loc) · 5.9 KB
/
story_client.py
File metadata and controls
185 lines (150 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# src/story_protocol_python_sdk/story_client.py
import os
import sys
from story_protocol_python_sdk.resources.Dispute import Dispute
from story_protocol_python_sdk.resources.Group import Group
from story_protocol_python_sdk.resources.IPAccount import IPAccount
from story_protocol_python_sdk.resources.IPAsset import IPAsset
from story_protocol_python_sdk.resources.License import License
from story_protocol_python_sdk.resources.NFTClient import NFTClient
from story_protocol_python_sdk.resources.Permission import Permission
from story_protocol_python_sdk.resources.Royalty import Royalty
from story_protocol_python_sdk.resources.WIP import WIP
# Ensure the src directory is in the Python path
current_dir = os.path.dirname(__file__)
src_path = os.path.abspath(os.path.join(current_dir, ".."))
if src_path not in sys.path:
sys.path.append(src_path)
class StoryClient:
"""
A client for interacting with Story Protocol, providing access to IPAsset, License, and Royalty resources.
:param web3 Web3: An instance of Web3.
:param account: The account to use for transactions.
:param chain_id int: The ID of the blockchain network.
"""
def __init__(self, web3, account, chain_id: int):
"""
Initialize the StoryClient with the given web3 instance, account, and chain ID.
:param web3 Web3: An instance of Web3.
:param account: The account to use for transactions.
:param chain_id int: The ID of the blockchain network.
:raises ValueError: If web3 or account is not provided.
"""
if not web3 or not account:
raise ValueError("web3 and account must be provided")
if chain_id not in [1315, 1514]:
raise ValueError("only support story devnet")
self.web3 = web3
self.account = account
self.chain_id = chain_id
self._ip_asset: IPAsset | None = None
self._license: License | None = None
self._royalty: Royalty | None = None
self._ip_account: IPAccount | None = None
self._permission: Permission | None = None
self._nft_client: NFTClient | None = None
self._dispute: Dispute | None = None
self._wip: WIP | None = None
self._group: Group | None = None
@property
def IPAsset(self) -> IPAsset:
"""
Access the IPAsset resource.
:return IPAsset: An instance of IPAsset.
"""
if self._ip_asset is None:
self._ip_asset = IPAsset(self.web3, self.account, self.chain_id)
return self._ip_asset
@property
def License(self) -> License:
"""
Access the License resource.
:return License: An instance of License.
"""
if self._license is None:
self._license = License(self.web3, self.account, self.chain_id)
return self._license
@property
def Royalty(self) -> Royalty:
"""
Access the Royalty resource.
:return Royalty: An instance of Royalty.
"""
if self._royalty is None:
self._royalty = Royalty(self.web3, self.account, self.chain_id)
return self._royalty
@property
def IPAccount(self) -> IPAccount:
"""
Access the IPAccount resource.
:return IPAccount: An instance of IPAccount.
"""
if self._ip_account is None:
self._ip_account = IPAccount(self.web3, self.account, self.chain_id)
return self._ip_account
@property
def Permission(self) -> Permission:
"""
Access the Permission resource.
:return Permission: An instance of Permission.
"""
if self._permission is None:
self._permission = Permission(self.web3, self.account, self.chain_id)
return self._permission
@property
def NFTClient(self) -> NFTClient:
"""
Access the NFTClient resource.
:return NFTClient: An instance of NFTClient.
"""
if self._nft_client is None:
self._nft_client = NFTClient(self.web3, self.account, self.chain_id)
return self._nft_client
@property
def Dispute(self) -> Dispute:
"""
Access the Dispute resource.
:return Dispute: An instance of Dispute.
"""
if self._dispute is None:
self._dispute = Dispute(self.web3, self.account, self.chain_id)
return self._dispute
@property
def WIP(self) -> WIP:
"""
Access the WIP resource.
:return WIP: An instance of WIP.
"""
if self._wip is None:
self._wip = WIP(self.web3, self.account, self.chain_id)
return self._wip
@property
def Group(self) -> Group:
"""
Access the Group resource.
"""
if self._group is None:
self._group = Group(self.web3, self.account, self.chain_id)
return self._group
def get_balance(self, address: str) -> int:
"""
Get the native token (IP) balance of the specified address.
:param address str: The address to query the balance for.
:return int: The native token balance of the specified address in wei.
:raises ValueError: If the address is invalid.
"""
if not address:
raise ValueError("Address must be provided")
if not self.web3.is_address(address):
raise ValueError(f"Invalid address format: {address}")
checksum_address = self.web3.to_checksum_address(address)
return self.web3.eth.get_balance(checksum_address)
def get_wallet_balance(self) -> int:
"""
Get the native token (IP) balance of the current wallet.
:return int: The native token balance of the current wallet in wei.
:raises ValueError: If no account is found.
"""
if not self.account or not hasattr(self.account, "address"):
raise ValueError("No account found in wallet")
return self.get_balance(self.account.address)