def test_token_to_wei(self): assert utils.token_to_wei(11, consts.MARKET_WETH) == \ 11 * (10 ** 18) assert utils.token_to_wei(22, consts.MARKET_DAI) == \ 22 * (10 ** 18) assert utils.token_to_wei(33, consts.MARKET_USDC) == \ 33 * (10 ** 6)
def place_order(self, pair: str, is_sell: bool, price: float, amount: float) -> str: assert (isinstance(pair, str)) assert (isinstance(is_sell, bool)) assert (isinstance(price, float)) assert (isinstance(amount, float)) side = 'SELL' if is_sell else 'BUY' self.logger.info(f"Placing order ({side}, amount {amount} of {pair}," f" price {price})...") tick_size = abs(Decimal(self.market_info[pair]['minimumTickSize']).as_tuple().exponent) # As market_id is used for amount, use baseCurrency instead of quoteCurrency market_id = self.market_info[pair]['baseCurrency']['soloMarketId'] # Convert tokens with different decimals to standard wei units decimal_exponent = (18 - int(self.market_info[pair]['quoteCurrency']['decimals'])) * -1 unformatted_price = price unformatted_amount = amount price = round(Decimal(unformatted_price * (10 ** decimal_exponent)), tick_size) amount = utils.token_to_wei(amount, market_id) created_order = self.client.place_order( market=pair, # structured as <MAJOR>-<Minor> side=side, price=price, amount=amount, fillOrKill=False, postOnly=False )['order'] order_id = created_order['id'] self.logger.info( f"Placed {side} order #{order_id} with amount {unformatted_amount}, at price {unformatted_price}") return order_id
def withdraw_funds(self, token: str, amount: float) -> TxReceipt: assert (isinstance(token, str)) assert (isinstance(amount, float)) market_id = self._get_market_id(token) tx_hash = self.client.eth.solo.withdraw(market=market_id, wei=utils.token_to_wei( amount, market_id)) receipt = self.client.eth.get_receipt(tx_hash) logging.info(f"Withdrew {amount} of {token} from DYDX") return receipt
def deposit_funds(self, token, amount: float): assert (isinstance(amount, float)) market_id = consts.MARKET_ETH # determine if 6 or 18 decimals are needed for wei conversion if token == 'USDC': market_id = consts.MARKET_USDC tx_hash = self.client.eth.deposit(market=market_id, wei=utils.token_to_wei( amount, market_id)) receipt = self.client.eth.get_receipt(tx_hash) return receipt
def deposit_funds(self, token: str, amount: float) -> TxReceipt: assert (isinstance(token, str)) assert (isinstance(amount, float)) market_id = self._get_market_id(token) try: deposit_tx_hash = self.client.eth.solo.deposit( market=market_id, wei=utils.token_to_wei(amount, market_id)) receipt = self.client.eth.get_receipt(deposit_tx_hash) logging.info(f"Deposited {amount} of {token} into DYDX") return receipt except (Timeout, TypeError): raise RuntimeError( 'Unable to deposit funds. Try calling set_allowances() prior to deposit.' )