shadowPaySDK 0.2.0.28__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,5 @@
1
1
  import shadowPaySDK
2
2
  from shadowPaySDK.const import __SHADOWPAY_ABI__ERC20__, __ALLOW_CHAINS__, __SHADOWPAY_CONTRACT_ADDRESS__ERC20__
3
- from shadowPaySDK.api import __CREATE__CHEQUE__
4
3
  from web3 import Web3
5
4
  from typing import Optional
6
5
  import httpx
@@ -36,6 +35,7 @@ class Cheque:
36
35
  print(f"Failed to get cheque ID from transaction receipt: {str(e)}")
37
36
  return False
38
37
  def __allow__(self):
38
+ print("Checking if chain is allowed", self.w3.eth.chain_id)
39
39
  for chain in self.allowed_chains:
40
40
 
41
41
  if chain == self.w3.eth.chain_id:
@@ -46,15 +46,25 @@ class Cheque:
46
46
  raise ValueError(f"Chain {str(self.w3.eth.chain_id)} is not allowed. Allowed chains are: {self.allowed_chains}")
47
47
  def get_contract_for_chain(self,chain_id: str):
48
48
  c = None
49
- for chain in __SHADOWPAY_CONTRACT_ADDRESS__ERC20__ :
50
- if str(chain) == str(chain_id):
51
- c = __SHADOWPAY_CONTRACT_ADDRESS__ERC20__[chain_id]
52
- contract_address = Web3.to_checksum_address(c)
53
- contract = self.w3.eth.contract(address=contract_address, abi=__SHADOWPAY_ABI__ERC20__)
54
- self.contract = contract
55
- return contract
49
+ chain_id = int(chain_id)
50
+
51
+ for key,value in __SHADOWPAY_CONTRACT_ADDRESS__ERC20__.items():
52
+ print("Checking address", value, "for chain_id", chain_id)
53
+ if key == chain_id:
54
+ c = value
55
+ contract_address = Web3.to_checksum_address(c)
56
+ contract = self.w3.eth.contract(address=contract_address, abi=__SHADOWPAY_ABI__ERC20__)
57
+ self.contract = contract
58
+ return contract
56
59
  raise ValueError(f"Chain {chain_id} is not supported. Supported chains are: {list(__SHADOWPAY_CONTRACT_ADDRESS__ERC20__.keys())}")
57
-
60
+ async def get_address(self):
61
+ if self.address:
62
+ return self.address
63
+ elif self.w3:
64
+ return self.w3.eth.default_account
65
+ else:
66
+ raise ValueError("No address provided or Web3 instance is not set")
67
+
58
68
  def set_parameters(self,chain_id: Optional[str] = None, w3:Optional[Web3] = None, amount:Optional[int] = None, private_key:Optional[str] = None, token:Optional[str] = None,address:Optional[str] = None):
59
69
  if w3:
60
70
  self.w3 = w3
@@ -63,6 +73,7 @@ class Cheque:
63
73
  self.amount = amount
64
74
  if private_key:
65
75
  self.private_key = private_key
76
+ self.address = Web3.to_checksum_address(self.w3.eth.account.from_key(private_key).address)
66
77
  if token:
67
78
  self.token = token
68
79
  if address:
@@ -70,7 +81,7 @@ class Cheque:
70
81
 
71
82
  def __convert__(self):
72
83
  return self.w3.to_wei(self.amount, 'ether')
73
-
84
+
74
85
  async def InitCheque(self, amount, receiver:list, private_key:Optional[str] = None):
75
86
  if not isinstance(receiver,list):
76
87
  raise ValueError("Receiver must be a list of addresses, [""0x1234...5678", "0x2345...6789""]")
@@ -133,42 +144,55 @@ class Cheque:
133
144
  async def CashOutCheque(
134
145
  self,
135
146
  private_key: str,
136
- cheque_id: str # hex-строка типа "0xabc..."
147
+ cheque_id: str
137
148
  ):
138
149
  if not private_key:
139
150
  private_key = self.private_key
140
151
 
141
-
142
152
  account = self.w3.eth.account.from_key(private_key)
143
153
  sender_address = account.address or self.address
144
-
145
-
146
154
  nonce = self.w3.eth.get_transaction_count(sender_address)
147
155
 
148
- txn = self.contract.functions.CashOutCheque(
149
- Web3.to_bytes(hexstr=cheque_id)).build_transaction({
156
+ latest_block = self.w3.eth.get_block('latest')
157
+ supports_eip1559 = 'baseFeePerGas' in latest_block
158
+
159
+ tx_common = {
150
160
  'from': sender_address,
151
161
  'nonce': nonce,
152
162
  'gas': 300_000,
153
- 'gasPrice': self.w3.to_wei('5', 'gwei'),
154
- })
155
- if self.return_build_tx:
156
- return {
157
- "build_tx": txn
158
- }
163
+ }
159
164
 
160
- signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=private_key)
165
+ if supports_eip1559:
166
+ # EIP-1559 style
167
+ base_fee = latest_block['baseFeePerGas']
168
+ priority_fee = self.w3.to_wei(2, 'gwei') # можно поднять до 5
169
+ max_fee = base_fee + priority_fee * 2
161
170
 
171
+ tx_common.update({
172
+ 'maxFeePerGas': max_fee,
173
+ 'maxPriorityFeePerGas': priority_fee
174
+ })
175
+ else:
176
+ # Legacy gas price style
177
+ tx_common.update({
178
+ 'gasPrice': self.w3.to_wei('5', 'gwei')
179
+ })
162
180
 
163
- tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
181
+ txn = self.contract.functions.CashOutCheque(
182
+ Web3.to_bytes(hexstr=cheque_id)
183
+ ).build_transaction(tx_common)
164
184
 
185
+ if self.return_build_tx:
186
+ return {"build_tx": txn}
187
+
188
+ signed_txn = self.w3.eth.account.sign_transaction(txn, private_key=private_key)
189
+ tx_hash = self.w3.eth.send_raw_transaction(signed_txn.raw_transaction)
165
190
  receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash)
191
+
166
192
  if receipt.status != 1:
167
193
  return False
168
- return {
169
- "hash": tx_hash.hex()
170
- }
171
-
194
+ return {"hash": tx_hash.hex()}
195
+
172
196
  async def InitTokenCheque(self, token_address:str, amount, reciver:str, private_key:Optional[str] = None):
173
197
  key = private_key or self.private_key
174
198
 
@@ -193,7 +217,7 @@ class Cheque:
193
217
  )
194
218
  estimated_gas = self.contract.functions.InitTokenCheque(
195
219
  Web3.to_checksum_address(token_address),
196
- int(amount * (10 ** decimals)),
220
+ amount,
197
221
  Web3.to_checksum_address(reciver)
198
222
  ).estimate_gas({
199
223
  'from': address,
@@ -201,7 +225,7 @@ class Cheque:
201
225
  })
202
226
  txn = self.contract.functions.InitTokenCheque(
203
227
  Web3.to_checksum_address(token_address),
204
- int(amount * (10 ** decimals)),
228
+ amount,
205
229
  Web3.to_checksum_address(reciver)
206
230
  ).build_transaction({
207
231
  'from': address,
@@ -285,9 +309,9 @@ class Cheque:
285
309
  estimated_gas = self.contract.functions.InitSwapCheque(
286
310
  Web3.to_checksum_address(reciver),
287
311
  Web3.to_checksum_address(token_in),
288
- int(amount_in * (10 ** decimals)),
312
+ amount_in,
289
313
  Web3.to_checksum_address(token_out),
290
- int(amount_out * (10 ** token_out_decinals)),
314
+ amount_out,
291
315
  ).estimate_gas({
292
316
  'from': address,
293
317
  'gasPrice': self.w3.eth.gas_price
@@ -295,9 +319,9 @@ class Cheque:
295
319
  txn = self.contract.functions.InitSwapCheque(
296
320
  Web3.to_checksum_address(reciver),
297
321
  Web3.to_checksum_address(token_in),
298
- int(amount_in * (10 ** decimals)),
322
+ amount_in,
299
323
  Web3.to_checksum_address(token_out),
300
- int(amount_out * (10 ** token_out_decinals))
324
+ amount_out
301
325
  ).build_transaction({
302
326
  'from': address,
303
327
  'nonce': self.w3.eth.get_transaction_count(address),
@@ -333,11 +357,20 @@ class Cheque:
333
357
  amount_out = swapDetail["amountOut"]
334
358
  erc20 = shadowPaySDK.ERC20Token(w3=self.w3)
335
359
  erc20.set_params(token_address=token_out)
336
- encure_allowance = erc20.ensure_allowance(
337
- private_key=private_key,
360
+ encure_allowance = erc20.allowance(
338
361
  spender=self.contract.address,
339
- amount=amount_out,
362
+ owner=self.address,
340
363
  )
364
+ if encure_allowance < amount_out:
365
+ approve = erc20.approve(
366
+ spender=self.contract.address,
367
+ amount=amount_out,
368
+ private_key=private_key,
369
+ conveted_amount=False
370
+ )
371
+ if not approve:
372
+ return False
373
+ print(f"contract balance: {erc20.get_balance(wallet_address=self.contract.address)}")
341
374
  estimated_gas = self.contract.functions.CashOutSwapCheque(
342
375
  Web3.to_bytes(hexstr=cheque_id)
343
376
  ).estimate_gas({
@@ -369,21 +402,75 @@ class Cheque:
369
402
 
370
403
 
371
404
  async def getComunityPool(self):
372
- # fee = self.contract.functions.getCollectedFee().call()
373
- fee = 50000000000000
405
+ fee = self.contract.functions.getCollectedFee().call()
374
406
  half_fee_eth = self.w3.from_wei(fee // 2, 'ether')
375
407
  return half_fee_eth
376
408
  async def getOwner(self):
377
409
  return self.contract.functions.getOwner().call()
378
410
  async def getTreasery(self):
379
411
  return self.contract.functions.getTreasery().call()
412
+ def getChequeInfo(self, cheque_id: str, address: Optional[str] = None):
413
+ ###returns cheque info###
414
+ # s = EVMcheque.getChequeInfo(
415
+ # cheque_id=chequeId,
416
+ # address=address
417
+ # )
418
+ ### cheque_amount = s[0] - amount of cheque in wei
419
+ ### cheque_sender = s[1] - list of sender addresses
420
+ ### cheque_status = s[2] - status of cheque (True - claimed, False - not claimed)
421
+ if not cheque_id:
422
+ raise ValueError("Cheque ID is required")
423
+ if address:
424
+ address = Web3.to_checksum_address(address)
425
+
426
+ cheque_id_bytes32 = Web3.to_bytes(hexstr=cheque_id).rjust(32, b'\x00')
427
+ cheque_info = self.contract.functions.getChequeInfo(
428
+ cheque_id_bytes32,
429
+ address or self.address
430
+ ).call()
431
+
432
+ return {
433
+ "sender": cheque_info[0],
434
+ "receiver": cheque_info[1],
435
+ "status": "claimed" if cheque_info[2] else "unclaimed",
436
+ }
437
+ def getTokenChequeInfo(self, cheque_id: str):
438
+ # f = EVMcheque.getTokenChequeInfo(
439
+ # cheque_id=chequeId,
440
+ # )
441
+ ### cheque_sender = s[0] - sender address
442
+ ### cheque_amount = s[1] - receiver address
443
+ ### cheque_status = s[2] - status of cheque (True - claimed, False - not claimed
444
+
445
+ if not cheque_id:
446
+ raise ValueError("Cheque ID is required")
447
+
448
+ cheque_id_bytes32 = Web3.to_bytes(hexstr=cheque_id).rjust(32, b'\x00')
449
+ cheque_info = self.contract.functions.getTokenChequeDetail(cheque_id_bytes32).call()
450
+ return {
451
+ "sender": cheque_info[0],
452
+ "receivers": cheque_info[1],
453
+ "status": "claimed" if cheque_info[2] else "unclaimed",
454
+ }
380
455
  async def getSwaoDetail(self, cheque_id: str):
381
- cheque_id_bytes = Web3.to_bytes(hexstr=cheque_id)
382
- s = self.contract.functions.getSwapDetail(cheque_id_bytes).call()
383
- return{
456
+ # f = EVMcheque.getSwaoDetail(
457
+ # cheque_id=chequeId,
458
+ # )
459
+ # f[0] - tokenOut address,
460
+ # f[1] - amountOut in wei,
461
+ # f[2] - spender,
462
+ # f[3] - receiver,
463
+ # f[4] - claimed
464
+ cheque_id_bytes32 = Web3.to_bytes(hexstr=cheque_id).rjust(32, b'\x00')
465
+ s = self.contract.functions.getSwapDetail(cheque_id_bytes32).call()
466
+ return {
384
467
  "tokenOut": s[0],
385
468
  "amountOut": s[1],
469
+ "spender": s[2],
470
+ "receiver": s[3],
471
+ "status": "claimed" if s[4] else "unclaimed"
386
472
  }
473
+
387
474
  class NFTcheque:
388
475
  def __init__(self, w3:Web3, token:str, amount:int, spender:str):
389
476
  self.w3 = w3