def __init__(self, dapp, height, width, token_address, action='', sell_amount='', buy_amount='', **kwargs): self.token = Erc20(dapp.node, address=token_address) self._action = action self._buy_amount = buy_amount self._sell_amount = sell_amount try: self.token_symbol = self.token.symbol() except OverflowError: self.token = Erc20(dapp.node, address=token_address, provided_abi=Erc20.DS_TOKEN_ABI) self.token_symbol = self.token.symbol().rstrip(b'\x00').decode() except Exception as e: self.token_symbol = '???' self.exchange = Exchange(dapp.node, token_address) # Dirty flags for updating token and eth text fields #self.token_value_dirty_flag = False #self.eth_value_dirty_flag = False super(UniswapFrame, self).__init__(dapp, height, width, title="Uniswap Exchange", **kwargs)
def _update(self): if self._credstick: self._wei_balance = self._w3.eth.getBalance(self._credstick.addressStr()) # Trying to catch a wily web3.py bug. # Sometimes when using the websockets middleware, # strange responses start coming back. if self._wei_balance.__class__ != int: logging.debug("w3.eth.getBalance returned something other than an int! = {}".format(self._wei_balance)) self._erc20_balances = Erc20.balances(self, self.credstick.address) if self._network == '1': try: self._ens_domain = self.ens.name(self._credstick.addressStr()) self._eth_usd = self.w3.fromWei(self._sai_pip.eth_price(), 'ether') except BadFunctionCallOutput: self._ens_domain = 'Unknown' else: self._ens_domain = 'Unknown' self._best_block = str(self._w3.eth.blockNumber) self._syncing = self._w3.eth.syncing if self._syncing not in (None, False): self._blocks_behind = self._syncing['highestBlock'] - self._syncing['currentBlock'] else: self._blocks_behind = None
def _update(self): w3 = self.w3_getter() # semaphore only allows one thread to wait on update self.update_sem.acquire(blocking=False) with self.update_lock: if self.credstick: self._wei_balance = w3.eth.getBalance( self.credstick.addressStr()) self.erc20_balances = Erc20.balances(self, self.credstick.address) if self.network == '1': try: self.ens = ENS.fromWeb3(_w3) self._ens_domain = self.ens.name( self.credstick.addressStr()) self.eth_price = w3.fromWei(self._sai_pip.eth_price(), 'ether') except BadFunctionCallOutput: self._ens_domain = 'Unknown' else: self._ens_domain = 'Unknown' #self.best_block = str(self.w3.eth.blockNumber) self.best_block = str(w3.eth.blockNumber) self.syncing_hash = w3.eth.syncing if self.syncing_hash not in (None, False): self.blocks_behind = self.syncing_hash[ 'highestBlock'] - self.syncing_hash['currentBlock'] else: self.blocks_behind = None self.update_sem.release()
def validate(self, address): try: contract = Erc20(self.dapp.node, address=address) contract.functions.balanceOf(address).call() except (BadFunctionCallOutput, ContractConfigError): return False return True
def __init__(self, node, erc20_address): factory = Factory(node) exchange_address = factory.getExchange(erc20_address) if exchange_address == '0x0000000000000000000000000000000000000000': raise ExchangeNotFound self.token_contract = Erc20(node, address=erc20_address) super(Exchange, self).__init__(node, address=exchange_address) self._eth_reserve = Decimal(self.w3.eth.getBalance(self.address)) self._token_reserve = Decimal( self.token_contract.balanceOf(self.address))
def _ok(self, gas_price_wei, nonce=None): address_text = self.find_widget('address') amount_text = self.find_widget('amount') if not self._validations(address_text._value, amount_text._value): return if self.everything_checkbox._value == True: if self.currency_listbox.value['name'] != 'ETH': amount = [ x['balance'] for x in self._interface.node.erc20_balances if x['name'] == self.currency_listbox.value['name'] ][0] else: #21000 gas to send eth, times gas price amount = (Decimal(self._interface.node._wei_balance) - (Decimal(21000) * Decimal(gas_price_wei))) / Decimal( 10**18) #debug(); pdb.set_trace() else: amount = amount_text._value try: if self.currency_listbox.value['name'] == 'ETH': rx = self._interface.node.send_ether(address_text._value, Decimal(amount), gas_price_wei, nonce) else: token = Erc20.factory(self._interface.node, self.currency_listbox.value['name']) rx = self._interface.node.send_erc20(token, address_text._value, amount, gas_price_wei, nonce) #pyperclip.copy(rx) #self._scene.add_effect( MessageDialog(self._screen,"Tx submitted.", width = 20)) #self._scene.add_effect(AskClipboardFrame, height=3, width=65, title="Tx Submitted. Copy TxHash to clipboard?") ) except SignTxError: self._scene.add_effect( MessageDialog(self._screen, "Credstick refused to sign Tx")) except ValueError as e: self._scene.add_effect( MessageDialog(self._screen, str(e), width=70)) self._scene.remove_effect(self) raise NextScene