Skip to content

Commit 37a3b08

Browse files
committed
fix lint
1 parent 49a1171 commit 37a3b08

File tree

6 files changed

+183
-9
lines changed

6 files changed

+183
-9
lines changed

convex_api/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"""
66

77
from convex_api.account import Account # noqa: F401
8-
from convex_api.api import API # noqa: F401
8+
from convex_api.api import API # noqa: F401
9+
from convex_api.contract import Contract # noqa: F401
910
from convex_api.key_pair import KeyPair # noqa: F401
1011

11-
1212
__version__ = "__version__ = '0.2.2'"

convex_api/api.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,13 @@ def resolve_name(self, name):
660660
"""
661661
return self._registry.resolve_address(name)
662662

663+
def load_contract(self, name):
664+
from convex_api.contract import Contract
665+
666+
contract = Contract(self)
667+
if contract.load(name=name):
668+
return contract
669+
663670
def _transaction_post(self, url, data, sequence_retry_count=20):
664671
max_sleep_time_seconds = 1
665672
while sequence_retry_count >= 0:

convex_api/contract.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
3+
4+
Convex Contract
5+
6+
"""
7+
8+
from convex_api.utils import to_address
9+
10+
11+
class Contract:
12+
def __init__(self, convex):
13+
self._convex = convex
14+
self._name = None
15+
self._address = None
16+
self._owner_address = None
17+
18+
def load(self, name=None, address=None, owner_address=None):
19+
20+
if name:
21+
address = self.resolve_address(name)
22+
owner_address = self.resolve_owner_address(name)
23+
self._name = name
24+
25+
if address is None:
26+
raise ValueError('no contract found')
27+
28+
if owner_address is None:
29+
owner_address = address
30+
31+
self._address = address
32+
self._owner_address = owner_address
33+
return self._address
34+
35+
def deploy(self, account, text=None, filename=None, name=None, owner_account=None):
36+
if filename:
37+
with open(filename, 'r') as fp:
38+
text = fp.read()
39+
if text is None:
40+
raise ValueError('You need to provide a contract filename or text to deploy')
41+
deploy_line = f"""
42+
(deploy
43+
(quote
44+
(do
45+
{text}
46+
)
47+
)
48+
)
49+
"""
50+
result = self._convex.send(deploy_line, account)
51+
if result and 'value' in result:
52+
address = to_address(result["value"])
53+
if name:
54+
if owner_account is None:
55+
owner_account = account
56+
self._convex.registry.register(name, address, owner_account)
57+
return address
58+
59+
def register(self, name, address, account):
60+
return self._convex.registry.register(name, address, account)
61+
62+
def send(self, transaction, account):
63+
if not self._address:
64+
raise ValueError(f'No contract address found for {self._name}')
65+
return self._convex.send(f'(call #{self._address} {transaction})', account)
66+
67+
def query(self, transaction, account_address=None):
68+
if not self._address:
69+
raise ValueError(f'No contract address found for {self._name}')
70+
if account_address is None:
71+
account_address = to_address(account_address)
72+
if account_address is None:
73+
account_address = self._address
74+
return self._convex.query(f'(call #{self._address} {transaction})', account_address)
75+
76+
def resolve_address(self, name):
77+
return self._convex.registry.resolve_address(name)
78+
79+
def resolve_owner_address(self, name):
80+
return self._convex.registry.resolve_owner(name)
81+
82+
@property
83+
def is_registered(self):
84+
return self._address is not None
85+
86+
@property
87+
def address(self):
88+
return self._address
89+
90+
@property
91+
def owner_address(self):
92+
return self._owner_address
93+
94+
@property
95+
def name(self):
96+
return self._name

convex_api/utils.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import binascii
77
import re
88

9+
910
from cryptography.hazmat.backends.openssl.backend import backend
1011
from cryptography.hazmat.primitives import hashes
1112

1213

13-
def to_address(text):
14+
def to_address(value):
1415
"""
1516
Convert address text with possible leading '#' to an interger address value.
1617
@@ -19,15 +20,16 @@ def to_address(text):
1920
:returns: Integer address or None if not a valid address
2021
2122
"""
22-
if isinstance(text, int):
23-
return int(text)
24-
if isinstance(text, str):
23+
if isinstance(value, int):
24+
return int(value)
25+
elif is_account(value):
26+
return value.address
27+
elif isinstance(value, str):
2528
try:
26-
value = int(re.sub(r'^#', '', text.strip()))
29+
address = int(re.sub(r'^#', '', value.strip()))
2730
except ValueError:
2831
return None
29-
return value
30-
return None
32+
return address
3133

3234

3335
def is_address(text):
@@ -178,3 +180,8 @@ def to_hex(value):
178180
:returns: Returns a hex string with a preappended '0x'
179181
"""
180182
return add_0x_prefix(binascii.hexlify(value).decode())
183+
184+
185+
def is_account(value):
186+
from convex_api import Account
187+
return isinstance(value, Account)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
3+
Test contract
4+
5+
"""
6+
import pytest
7+
8+
from convex_api import (
9+
Account,
10+
API,
11+
Contract
12+
)
13+
14+
15+
TEST_FUNDING_AMOUNT = 8888888
16+
17+
TEST_CONTRACT_FILENAME = './tests/resources/test_contract.cvx'
18+
TEST_CONTRACT_NAME = 'test_contract_starfish'
19+
20+
21+
def test_convex_api_deploy_contract(convex_url, test_account):
22+
convex = API(convex_url)
23+
24+
# create a contract object
25+
contract = Contract(convex)
26+
27+
# set the default owner to the test_account
28+
owner_account = test_account
29+
30+
# see if it has already been created by another test account
31+
owner_address = contract.resolve_owner_address(TEST_CONTRACT_NAME)
32+
if owner_address:
33+
# if so then rebuild the owner account using the same key_pair
34+
owner_account = Account(test_account.key_pair, owner_address)
35+
else:
36+
owner_address = test_account.address
37+
38+
# deploy the contract
39+
contract_address = contract.deploy(owner_account, filename=TEST_CONTRACT_FILENAME, name=TEST_CONTRACT_NAME)
40+
assert(contract_address)
41+
# load the contract - should be the same address
42+
new_address = contract.load(TEST_CONTRACT_NAME)
43+
assert(contract_address == new_address)
44+
assert(contract_address == contract.address)
45+
assert(owner_address == contract.owner_address)
46+
47+
48+
contract = convex.load_contract(TEST_CONTRACT_NAME)
49+
assert(contract)
50+
assert(contract_address == contract.address)

tests/resources/test_contract.cvx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(def stored-data
2+
^{:private? true}
3+
nil
4+
)
5+
(defn get
6+
^{:callable? true}
7+
[]
8+
stored-data
9+
)
10+
(defn set
11+
^{:callable? true}
12+
[x]
13+
( def stored-data x)
14+
)

0 commit comments

Comments
 (0)