diff --git a/pyexchange/abi/TOKEN_ABI.abi b/pyexchange/abi/ERC20TOKEN.abi similarity index 100% rename from pyexchange/abi/TOKEN_ABI.abi rename to pyexchange/abi/ERC20TOKEN.abi diff --git a/pyexchange/leverj.py b/pyexchange/leverj.py index 57988239..aeaee5ae 100644 --- a/pyexchange/leverj.py +++ b/pyexchange/leverj.py @@ -94,7 +94,6 @@ def __init__(self, assert(isinstance(is_sell, bool)) assert(isinstance(price, Wad)) assert(isinstance(amount, Wad)) - self.trade_id = trade_id self.timestamp = timestamp self.pair = pair @@ -143,7 +142,7 @@ def from_all_list(pair, trade): class LeverjAPI(PyexAPI): """LeverJ API interface. - """ + """ logger = logging.getLogger() @@ -175,7 +174,7 @@ def get_balance(self, coin: str): balances = self.get_balances() for key in balances: if balances[key]['symbol'] == coin: - return balances[key]['plasma'] + return balances[key]['available'] def get_pending(self, coin: str): assert(isinstance(coin, str)) @@ -266,6 +265,7 @@ def place_order(self, pair: str, is_sell: bool, price: Wad, amount: Wad): price = str(price) quantity = str(amount) order = self.createNewOrder(side, price, quantity, orderInstrument) + self.logger.info(f'order is {order}') return self._http_authenticated("POST", "/api/v1", "/order", [order])[0]['uuid'] def cancel_order(self, order_id: str) -> bool: @@ -344,6 +344,7 @@ def _result(self, result) -> Optional[dict]: return data + class LeverJ(Contract): """A client for the Leverj proxy exchange contract. @@ -355,7 +356,7 @@ class LeverJ(Contract): logger = logging.getLogger() abi = Contract._load_abi(__name__, 'abi/GLUON.abi') - token_abi = Contract._load_abi(__name__, 'abi/TOKEN_ABI.abi') + token_abi = Contract._load_abi(__name__, 'abi/ERC20TOKEN.abi') def __init__(self, web3: Web3, address: Address): assert(isinstance(web3, Web3)) @@ -375,39 +376,50 @@ def approve_token(self, token_address: str, amount: int) -> Transact: def deposit_ether(self, leverjobj: LeverjAPI, amount: Wad, gluon_block_number): assert(isinstance(leverjobj, LeverjAPI)) assert(isinstance(amount, Wad)) - assert(isinstance(gluon_block_number, None) or isinstance(gluon_block_number, int)) custodian_account = self.address app_id = leverjobj.get_spot_exchange_id() if gluon_block_number is None: - gluon_block_number = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] +2 - Transact(self, self.web3, self.abi, self.address, self._contract, "depositEther",[app_id], {'value': int(amount.value)}).transact() - return gluon_block_number + gluon_block_number = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] + 2 + receipt = Transact(self, self.web3, self.abi, self.address, self._contract, "depositEther",[app_id], {'value': int(amount.value)}).transact() + return (gluon_block_number, receipt) else: current_gluon_block = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] if (current_gluon_block < gluon_block_number): - return gluon_block_number + return (gluon_block_number, None) else: - return None + return (None, None) def deposit_token(self, leverjobj: LeverjAPI, token_address: str, amount: int, gluon_block_number): assert(isinstance(leverjobj, LeverjAPI)) assert(isinstance(token_address, str)) assert(isinstance(amount, int)) - assert(isinstance(gluon_block_number, None) or isinstance(gluon_block_number, int)) custodian_account = self.address app_id = leverjobj.get_spot_exchange_id() if gluon_block_number is None: - gluon_block_number = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] +2 - Transact(self, self.web3, self.abi, self.address, self._contract, "depositToken",[app_id, token_address, amount], {}).transact() - return gluon_block_number + gluon_block_number = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] + 2 + receipt = Transact(self, self.web3, self.abi, self.address, self._contract, "depositToken",[app_id, token_address, amount], {}).transact() + return (gluon_block_number, receipt) else: current_gluon_block = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] if (current_gluon_block < gluon_block_number): - return gluon_block_number + return (gluon_block_number, None) else: - return None + return (None, None) + + def post_pending_tx_hash(self, leverjobj: LeverjAPI, tx_hash: str, asset_addr: str, quantity: str): + assert(isinstance(tx_hash, str)) + assert(isinstance(asset_addr, str)) + assert(isinstance(quantity, str)) + + payload = { + "txid": tx_hash, + "asset": asset_addr, + "quantity": quantity + } + balance_dict = leverjobj._http_authenticated("POST", "/api/v1", "/account/deposit", payload) + return balance_dict def withdraw_token(self, leverjobj: LeverjAPI, token_addr: str, quantity: int) -> int: assert(isinstance(leverjobj, LeverjAPI)) @@ -442,24 +454,19 @@ def claim_funds(self, leverjobj: LeverjAPI, asset: str, quantity: int, gluon_blo return self.withdraw_token(leverjobj, asset, int(quantity)) else: - leverjobj.web3.eth.defaultAccount = leverjobj.account_id - ethereum_account = leverjobj.account_id - custodian_account = self.address - self.logger.info(f"ethereum_account: {ethereum_account}, custodian_account: {custodian_account}, asset: {asset}") - response = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}/evmparams/withdrawals/account/{ethereum_account}/asset/{asset}", None) - response_app_id = int(response[0]) - response_bytes = response[1] current_block = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] - - if (leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] >= gluon_block_number): + if current_block >= gluon_block_number: + ethereum_account = leverjobj.account_id + custodian_account = self.address + self.logger.info(f"ethereum_account: {ethereum_account}, custodian_account: {custodian_account}, asset: {asset}") + response = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}/evmparams/withdrawals/account/{ethereum_account}/asset/{asset}", None) + response_app_id = int(response[0]) + response_bytes = response[1] self.logger.info(f"finally gluon_block_number reached {gluon_block_number} and we are running final transact") Transact(self, self.web3, self.abi, self.address, self._contract, "withdraw",[response_app_id, response_bytes], {}).transact() return None - - self.logger.info(f'does not look like gluon_block_number reached {gluon_block_number} and we are currently at {leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)["number"]}') - return gluon_block_number - - - + current_block = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] + self.logger.info(f'does not look like gluon_block_number reached {gluon_block_number} and we are currently at {current_block}') + return gluon_block_number diff --git a/requirements.txt b/requirements.txt index 84e9ab8f..17f85aaa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,5 @@ python-dateutil == 2.6.1 websockets == 6.0.0 kucoin-python == 2.0.5 pyjwt == 1.7.1 +leverj-ordersigner == 0.7 +