async def post(self, user): """Add new relayer""" relayer = self.request_body name = relayer.get('name', None) verify_user(user, relayer['owner']) normalized_relayer = self.validator.normalized(relayer) b = Blockchain() coinbase = b.web3.toChecksumAddress(relayer['coinbase']) r = b.getRelayerByCoinbase(coinbase) if r[1].lower() != user.lower(): raise InvalidValueException('owner required') b.updateRelayer(coinbase) query = (Relayer.update(**normalized_relayer).where( Relayer.coinbase == coinbase).returning(Relayer)) cursor = query.execute() db_relayer = Relayer.select().where(Relayer.coinbase == coinbase).get() db_relayer = model_to_dict(db_relayer) try: requests.put( urljoin(settings['tomodex'], '/api/relayer') + '?relayerAddress=' + coinbase + '&relayerName=' + name + '&relayerUrl=' + db_relayer['link'] + '&authKey=' + settings['tomodex_auth']) except: logger.error('Update tomodex failed') self.json_response(model_to_dict(cursor[0]))
async def patch(self, user): """Update existing relayer""" relayer = self.request_body relayer_coinbase = relayer.get('coinbase', None) relayer_owner = relayer.get('owner', None) name = relayer.get('name', None) verify_user(user, relayer_owner) if relayer_coinbase == None: raise MissingArgumentException('missing relayer coinbase') del relayer['id'] if relayer.get('new_owner', None): relayer['owner'] = relayer['new_owner'] del relayer['new_owner'] normalized_relayer = self.validator.normalized(relayer) try: b = Blockchain() coinbase = b.web3.toChecksumAddress(relayer_coinbase) db_relayer = Relayer.select().where( Relayer.coinbase == coinbase).get() db_relayer = model_to_dict(db_relayer) if user.lower() != db_relayer['owner'].lower(): raise MissingArgumentException('wrong owner') r = b.getRelayerByCoinbase(coinbase) if r[1].lower() != relayer['owner'].lower(): raise InvalidValueException('owner required') query = (Relayer.update(**normalized_relayer).where( Relayer.coinbase == coinbase).returning(Relayer)) cursor = query.execute() b.updateRelayer(coinbase) try: requests.put( urljoin(settings['tomodex'], '/api/relayer') + '?relayerAddress=' + coinbase + '&relayerName=' + name + '&relayerUrl=' + relayer['link'] + '&authKey=' + settings['tomodex_auth']) except: logger.error('Update tomodex failed') self.json_response(model_to_dict(cursor[0])) except IndexError: raise InvalidValueException( 'relayer id={param} does not exist'.format( param=str(relayer_coinbase)))
async def patch(self, user): """Update existing relayer""" relayer = self.request_body relayer_id = relayer.get('id', None) relayer_owner = relayer.get('owner', None) verify_user(user, relayer_owner) if not relayer_id: raise MissingArgumentException('missing relayer id') del relayer['id'] if relayer.get('new_owner', None): relayer['owner'] = relayer['new_owner'] del relayer['new_owner'] if not self.validator.validate(relayer): raise InvalidValueException(str( self.validator.document_error_tree)) normalized_relayer = self.validator.normalized(relayer) try: query = (Relayer.update(**normalized_relayer).where( Relayer.id == relayer_id).returning(Relayer)) cursor = query.execute() self.json_response(model_to_dict(cursor[0])) except IndexError: raise InvalidValueException( 'relayer id={param} does not exist'.format( param=str(relayer_id)))
def createDomain(self, coinbase, resigning): used = not resigning b = False try: try: db_relayer = Relayer.select().where( Relayer.coinbase == coinbase).get() except Relayer.DoesNotExist: b = True db_relayer = model_to_dict(db_relayer) Domain.update(used=used, coinbase=coinbase).where( Domain.domain == db_relayer['domain']).execute() except: pass try: db_domain = Domain.select().where(Domain.used == False).order_by( Domain.id.asc()).get() db_domain = model_to_dict(db_domain) if b == True: Domain.update(used=True, coinbase=coinbase).where( Domain.domain == db_domain['domain']).execute() return db_domain['domain'] except: countDomain = Domain.select().where(Domain.used == True).count() domain = 'https://' + format( countDomain, '03d') + '.' + settings['domain_suffix'] Domain.insert( domain=domain, used=True, coinbase=coinbase).on_conflict_ignore(True).execute() return domain
async def get(self): redis = self.application.redis keys = await redis.hlen('public_res') logger.debug('Length = %d', keys) if keys: cached = await redis.hgetall('public_res', encoding='utf-8') return self.json_response( {k: json.loads(v) for k, v in cached.items()}) relayers = [ model_to_dict(relayer or {}) for relayer in Relayer.select() ] contracts = [ model_to_dict(c or {}) for c in Contract.select().where(Contract.obsolete == False) ] tokens = [model_to_dict(token or {}) for token in Token.select()] self.json_response({ 'Relayers': relayers, 'Contracts': contracts, 'Tokens': tokens }) await redis.hmset_dict('public_res', Relayers=json.dumps(relayers), Contracts=json.dumps(contracts), Tokens=json.dumps(tokens))
async def get(self): relayers = [model_to_dict(relayer or {}) for relayer in Relayer.select()] tokens = [model_to_dict(token or {}) for token in Token.select()] domains = [model_to_dict(domain or {}) for domain in Domain.select()] contracts = Blockchain.contracts self.json_response({ 'Relayers': relayers, 'Contracts': contracts, 'Tokens': tokens, 'Domains': domains })
def updateRelayer(self, coinbase): coinbase = self.web3.toChecksumAddress(coinbase) c = self.web3.eth.contract(address=self.web3.toChecksumAddress( settings['relayerregistration_addr']), abi=self.RegistrationABI) logger.info('UpdateRelayer coinbase %s', coinbase) try: relayer = c.functions.getRelayerByCoinbase(coinbase).call() name = 'Relayer' + str(relayer[0]) lock_time = c.functions.RESIGN_REQUESTS(coinbase).call() resigning = True if (lock_time) else False domain = self.createDomain(coinbase, resigning) Relayer.insert(idx=relayer[0], coinbase=coinbase, owner=relayer[1], name=name, deposit=relayer[2], trade_fee=relayer[3], from_tokens=relayer[4], to_tokens=relayer[5], link=domain, domain=domain, resigning=resigning, lock_time=lock_time).on_conflict( conflict_target=(Relayer.coinbase, ), update={ Relayer.owner: relayer[1], Relayer.deposit: relayer[2], Relayer.trade_fee: relayer[3], Relayer.from_tokens: relayer[4], Relayer.to_tokens: relayer[5], Relayer.resigning: resigning, Relayer.lock_time: lock_time }).execute() except: logger.error('UpdateRelayer failed')
async def get(self): relayers = [ model_to_dict(relayer or {}) for relayer in Relayer.select() ] contracts = [ model_to_dict(c or {}) for c in Contract.select().where(Contract.obsolete == False) ] tokens = [model_to_dict(token or {}) for token in Token.select()] self.json_response({ 'Relayers': relayers, 'Contracts': contracts, 'Tokens': tokens })
async def delete(self, user): """Delete a relayer""" relayer_id = self.get_argument('id', None) if not relayer_id: raise MissingArgumentException('missing relayer id') try: relayer = Relayer.select().where(Relayer.owner == user, Relayer.id == relayer_id).get() relayer.delete_instance() self.json_response({}) except Exception: raise InvalidValueException( 'invalid relayer: relayer with id={} or owner={} does not exist' .format(relayer_id, user))
def updateRelayers(self): c = self.web3.eth.contract(address=self.web3.toChecksumAddress( settings['relayerregistration_addr']), abi=self.RegistrationABI) relayer_count = c.functions.RelayerCount().call() logger.info('Relayer count %s', relayer_count) for n in range(relayer_count): coinbase = c.functions.RELAYER_COINBASES(n).call() logger.info('Relayer coinbase %s', coinbase) relayer = c.functions.getRelayerByCoinbase(coinbase).call() lock_time = c.functions.RESIGN_REQUESTS(coinbase).call() logger.info('Relayer resign request %s', lock_time) resigning = True if (lock_time) else False logger.info('Relayer owner %s', relayer[1]) for t in relayer[4]: self.updateToken(t) for t in relayer[5]: self.updateToken(t) domain = self.createDomain(coinbase, resigning) rl = (Relayer.insert(idx=relayer[0], coinbase=coinbase, owner=relayer[1], name='Relayer' + str(relayer[0]), deposit=relayer[2], trade_fee=relayer[3], from_tokens=relayer[4], to_tokens=relayer[5], link=domain, domain=domain, resigning=resigning, lock_time=lock_time).on_conflict( conflict_target=(Relayer.coinbase, ), update={ Relayer.idx: relayer[0], Relayer.owner: relayer[1], Relayer.deposit: relayer[2], Relayer.trade_fee: relayer[3], Relayer.from_tokens: relayer[4], Relayer.to_tokens: relayer[5], Relayer.resigning: resigning, Relayer.lock_time: lock_time }).execute())
async def delete(self, user): """Delete a relayer""" coinbase = self.get_argument('coinbase', None) b = Blockchain() coinbase = b.web3.toChecksumAddress(coinbase) if not coinbase: raise MissingArgumentException('missing relayer coinbase') try: relayer = Relayer.select().where( Relayer.coinbase == coinbase).get() db_relayer = model_to_dict(relayer) if user.lower() != db_relayer['owner'].lower(): raise MissingArgumentException('wrong owner') relayer.delete_instance() self.json_response({}) except Exception: raise InvalidValueException( 'invalid relayer: relayer with id={} or owner={} does not exist' .format(coinbase, user))
async def get(self, user): relayers = [ model_to_dict(relayer or {}) for relayer in Relayer.select().where(Relayer.owner == user) ] self.json_response(relayers)