Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Akash leverj pyexchange changes20191206 #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 39 additions & 31 deletions pyexchange/leverj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,7 +142,7 @@ def from_all_list(pair, trade):

class LeverjAPI(PyexAPI):
"""LeverJ API interface.
"""
"""

logger = logging.getLogger()

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -344,6 +344,7 @@ def _result(self, result) -> Optional[dict]:

return data


class LeverJ(Contract):
"""A client for the Leverj proxy exchange contract.

Expand All @@ -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))
Expand All @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want just to return receipt or to check here if transaction completed ok / other details?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to return receipt in it's full form to be checked by inventory service with gluon block to know where we are in the processing

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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, should we just return receipt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to return both, gluon_block_number tells us where we are in the process, the hash in the receipt is eventually sent back into post_pending_tx_hash to let let leverj know we just sent something and to look for that transaction hash in pending transactions on ethereum block chain.

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))
Expand Down Expand Up @@ -442,24 +454,20 @@ 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"]}')
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

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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