|
| 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 |
0 commit comments