From 229be315a88be7cbf0f56c08a15dfd56423f708c Mon Sep 17 00:00:00 2001 From: mitakash Date: Wed, 20 Nov 2019 12:10:12 -0500 Subject: [PATCH 1/6] added leverj-ordersigner to requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 84e9ab8f..506e1770 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,3 +5,4 @@ python-dateutil == 2.6.1 websockets == 6.0.0 kucoin-python == 2.0.5 pyjwt == 1.7.1 +leverj-ordersigner == 0.6 From ce0a63c4631d856c6dc251b191de163bc5293cec Mon Sep 17 00:00:00 2001 From: mitakash Date: Wed, 20 Nov 2019 12:24:30 -0500 Subject: [PATCH 2/6] changed some names for abis --- pyexchange/abi/{TOKEN_ABI.abi => ERC20TOKEN.abi} | 0 pyexchange/leverj.py | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename pyexchange/abi/{TOKEN_ABI.abi => ERC20TOKEN.abi} (100%) 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..ec9b5367 100644 --- a/pyexchange/leverj.py +++ b/pyexchange/leverj.py @@ -355,7 +355,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)) From a9fccb2c4b4680f59e50c12f93e76ade138cf766 Mon Sep 17 00:00:00 2001 From: mitakash Date: Wed, 20 Nov 2019 12:39:23 -0500 Subject: [PATCH 3/6] added a new line to the end of requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 506e1770..d8313799 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ websockets == 6.0.0 kucoin-python == 2.0.5 pyjwt == 1.7.1 leverj-ordersigner == 0.6 + From a34be55254bd29f68744123e13eab670acd1a572 Mon Sep 17 00:00:00 2001 From: mitakash Date: Fri, 6 Dec 2019 10:51:59 -0500 Subject: [PATCH 4/6] made some changes to clean up code added post_pending_tx_hash function --- pyexchange/leverj.py | 80 ++++++++++++++++++++++++++++++-------------- requirements.txt | 2 +- 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/pyexchange/leverj.py b/pyexchange/leverj.py index ec9b5367..3ea288af 100644 --- a/pyexchange/leverj.py +++ b/pyexchange/leverj.py @@ -80,6 +80,7 @@ def from_list(item: list, pair: str): amount=Wad.from_number(item['quantity'])) + class Trade: def __init__(self, trade_id: Optional[id], @@ -131,6 +132,7 @@ def from_our_list(pair, trade): price=Wad.from_number(trade['price']), amount=Wad.from_number(trade['quantity'])) + @staticmethod def from_all_list(pair, trade): return Trade(trade_id=None, @@ -141,9 +143,12 @@ def from_all_list(pair, trade): amount=Wad.from_number(trade['volume'])) + + class LeverjAPI(PyexAPI): """LeverJ API interface. - """ + """ + logger = logging.getLogger() @@ -175,7 +180,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)) @@ -233,12 +238,15 @@ def get_all_trades(self, pair: str, page_number: int = 1) -> List[Trade]: return list(map(lambda item: Trade.from_all_list(pair, item), result)) + + def get_symbol_trades(self, symbol: str): return self._http_authenticated("GET", "/api/v1", f"/instrument/{symbol}/trade", None) def get_orderbook_symbol(self, symbol: str): return self._http_authenticated("GET", "/api/v1", f"/instrument/{symbol}/orderbook", None) + def createNewOrder(self, side: str, price: str, quantity: str, orderInstrument: dict) -> dict: precision = self.get_product(orderInstrument['symbol'])['quoteSignificantDigits'] qty_precision = self.get_product(orderInstrument['symbol'])['baseSignificantDigits'] @@ -255,6 +263,7 @@ def createNewOrder(self, side: str, price: str, quantity: str, orderInstrument: order['signature'] = sign_order(order, orderInstrument, self.api_secret) return order + def place_order(self, pair: str, is_sell: bool, price: Wad, amount: Wad): assert(isinstance(pair, str)) assert(isinstance(is_sell, bool)) @@ -266,6 +275,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: @@ -289,6 +299,8 @@ def cancel_all_orders(self) -> List: return result + + def _http_authenticated(self, method: str, api_path: str, resource: str, body): assert(isinstance(method, str)) assert(isinstance(api_path, str)) @@ -354,6 +366,7 @@ class LeverJ(Contract): logger = logging.getLogger() + abi = Contract._load_abi(__name__, 'abi/GLUON.abi') token_abi = Contract._load_abi(__name__, 'abi/ERC20TOKEN.abi') @@ -365,6 +378,7 @@ def __init__(self, web3: Web3, address: Address): self.address = address self._contract = self._get_contract(web3, self.abi, address) + def approve_token(self, token_address: str, amount: int) -> Transact: assert(isinstance(token_address, str)) assert(isinstance(amount, int)) @@ -372,42 +386,56 @@ def approve_token(self, token_address: str, amount: int) -> Transact: token_contract = self._get_contract(self.web3, self.token_abi, Address(token_address)) return Transact(self, self.web3, self.token_abi, Address(token_address), token_contract, "approve",[self.address.address, int(amount)], {}) + 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)) @@ -431,6 +459,7 @@ def withdraw_token(self, leverjobj: LeverjAPI, token_addr: str, quantity: int) - number_dict = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None) return number_dict['number']+3 + def claim_funds(self, leverjobj: LeverjAPI, asset: str, quantity: int, gluon_block_number): assert(isinstance(leverjobj, LeverjAPI)) assert(isinstance(asset, str)) @@ -442,24 +471,25 @@ 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: + 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] 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"]}') + + 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 d8313799..17f85aaa 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,5 +5,5 @@ python-dateutil == 2.6.1 websockets == 6.0.0 kucoin-python == 2.0.5 pyjwt == 1.7.1 -leverj-ordersigner == 0.6 +leverj-ordersigner == 0.7 From baaff316de962c8a82fc1b84e3f7aefed08d6547 Mon Sep 17 00:00:00 2001 From: mitakash Date: Fri, 6 Dec 2019 11:06:25 -0500 Subject: [PATCH 5/6] removed extra blank lines --- pyexchange/leverj.py | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/pyexchange/leverj.py b/pyexchange/leverj.py index 3ea288af..ff89b8c2 100644 --- a/pyexchange/leverj.py +++ b/pyexchange/leverj.py @@ -80,7 +80,6 @@ def from_list(item: list, pair: str): amount=Wad.from_number(item['quantity'])) - class Trade: def __init__(self, trade_id: Optional[id], @@ -95,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 @@ -132,7 +130,6 @@ def from_our_list(pair, trade): price=Wad.from_number(trade['price']), amount=Wad.from_number(trade['quantity'])) - @staticmethod def from_all_list(pair, trade): return Trade(trade_id=None, @@ -143,12 +140,9 @@ def from_all_list(pair, trade): amount=Wad.from_number(trade['volume'])) - - class LeverjAPI(PyexAPI): """LeverJ API interface. """ - logger = logging.getLogger() @@ -238,15 +232,12 @@ def get_all_trades(self, pair: str, page_number: int = 1) -> List[Trade]: return list(map(lambda item: Trade.from_all_list(pair, item), result)) - - def get_symbol_trades(self, symbol: str): return self._http_authenticated("GET", "/api/v1", f"/instrument/{symbol}/trade", None) def get_orderbook_symbol(self, symbol: str): return self._http_authenticated("GET", "/api/v1", f"/instrument/{symbol}/orderbook", None) - def createNewOrder(self, side: str, price: str, quantity: str, orderInstrument: dict) -> dict: precision = self.get_product(orderInstrument['symbol'])['quoteSignificantDigits'] qty_precision = self.get_product(orderInstrument['symbol'])['baseSignificantDigits'] @@ -263,7 +254,6 @@ def createNewOrder(self, side: str, price: str, quantity: str, orderInstrument: order['signature'] = sign_order(order, orderInstrument, self.api_secret) return order - def place_order(self, pair: str, is_sell: bool, price: Wad, amount: Wad): assert(isinstance(pair, str)) assert(isinstance(is_sell, bool)) @@ -299,8 +289,6 @@ def cancel_all_orders(self) -> List: return result - - def _http_authenticated(self, method: str, api_path: str, resource: str, body): assert(isinstance(method, str)) assert(isinstance(api_path, str)) @@ -356,6 +344,7 @@ def _result(self, result) -> Optional[dict]: return data + class LeverJ(Contract): """A client for the Leverj proxy exchange contract. @@ -366,7 +355,6 @@ class LeverJ(Contract): logger = logging.getLogger() - abi = Contract._load_abi(__name__, 'abi/GLUON.abi') token_abi = Contract._load_abi(__name__, 'abi/ERC20TOKEN.abi') @@ -378,7 +366,6 @@ def __init__(self, web3: Web3, address: Address): self.address = address self._contract = self._get_contract(web3, self.abi, address) - def approve_token(self, token_address: str, amount: int) -> Transact: assert(isinstance(token_address, str)) assert(isinstance(amount, int)) @@ -386,7 +373,6 @@ def approve_token(self, token_address: str, amount: int) -> Transact: token_contract = self._get_contract(self.web3, self.token_abi, Address(token_address)) return Transact(self, self.web3, self.token_abi, Address(token_address), token_contract, "approve",[self.address.address, int(amount)], {}) - def deposit_ether(self, leverjobj: LeverjAPI, amount: Wad, gluon_block_number): assert(isinstance(leverjobj, LeverjAPI)) assert(isinstance(amount, Wad)) @@ -404,7 +390,6 @@ def deposit_ether(self, leverjobj: LeverjAPI, amount: Wad, gluon_block_number): else: 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)) @@ -436,7 +421,6 @@ def post_pending_tx_hash(self, leverjobj: LeverjAPI, tx_hash: str, asset_addr: s 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)) assert(isinstance(token_addr, str)) @@ -459,7 +443,6 @@ def withdraw_token(self, leverjobj: LeverjAPI, token_addr: str, quantity: int) - number_dict = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None) return number_dict['number']+3 - def claim_funds(self, leverjobj: LeverjAPI, asset: str, quantity: int, gluon_block_number): assert(isinstance(leverjobj, LeverjAPI)) assert(isinstance(asset, str)) @@ -488,8 +471,3 @@ def claim_funds(self, leverjobj: LeverjAPI, asset: str, quantity: int, gluon_blo 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 - - - - - From ca6ae8e8674d84f99ec4272a439f39a444c61096 Mon Sep 17 00:00:00 2001 From: mitakash Date: Tue, 10 Dec 2019 12:37:45 -0500 Subject: [PATCH 6/6] removed extra line for defaultAccount --- pyexchange/leverj.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyexchange/leverj.py b/pyexchange/leverj.py index ff89b8c2..aeaee5ae 100644 --- a/pyexchange/leverj.py +++ b/pyexchange/leverj.py @@ -456,7 +456,6 @@ def claim_funds(self, leverjobj: LeverjAPI, asset: str, quantity: int, gluon_blo else: current_block = leverjobj._http_authenticated("GET", "/api/v1", f"/plasma/{app_id}", None)['number'] if current_block >= gluon_block_number: - 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}")