def constructor_pub_info(constructor: Constructor) -> Dict: from apps.constructors.views import _process_ctor_schema res = { 'id': constructor.slug, 'name': constructor.name, 'description': constructor.description, 'price': constructor.get_formatted_price(), 'schema': _process_ctor_schema(constructor.blockchain, constructor.get_schema()), 'ui_schema': constructor.get_ui_schema(), 'blockchain': constructor.blockchain, 'image': constructor.image, 'is_public': constructor.is_public, 'user_id': constructor.user_id, 'payment_address': constructor.payment_address, } if hasattr(constructor, 'dapps__count'): res['compilations_count'] = constructor.dapps__count return res
def setUp(self): self.user = User() self.user.save() self.constructor = Constructor.create(price=0) self.constructor.save() self.auth_header = { 'X_ACCESSTOKEN': UsersService().generate_token(self.user, 'ethereum') }
def test_search_dapp(self): constructor = Constructor.create(price=0) constructor.save() dapp_testnet = Dapp.create( address='0x0123456789012345678901234567890123456789', constructor=constructor, abi='[]', function_specs='{}', dashboard_functions='[]', network_id='3', has_public_access=True) dapp_testnet.save() dapp = Dapp.create( address='0x0123456789012345678901234567890123456789', constructor=constructor, abi='[]', function_specs='{}', dashboard_functions='[]', network_id='1', has_public_access=True) dapp.save() resp = self.app.post_json( '/api/search', params={ 'query': '0x0123456789012345678901234567890123456789', 'ethereum_network_id': '1' }) self.assertIn('address', resp.json) self.assertIsInstance(resp.json['address'], dict) self.assertIn('type', resp.json['address']) self.assertEqual(resp.json['address']['type'], 'dapp') self.assertIn('dapp', resp.json['address']) self.assertIn('id', resp.json['address']['dapp']) self.assertEqual(dapp.slug, resp.json['address']['dapp']['id'])
def post(self, request): args = request.data price = str(args.get('price', 0)) if 'payment_address' in args: if not re.findall('^0x[0-9a-fA-F]{40}$', args['payment_address']): return error_response("Invalid payment address") else: args['payment_address'] = '' if float(price) and not args['payment_address']: return error_response( "Payment address must be specified with price >0") user = auth(request) if isinstance(user, HttpResponse): return user # error name = nonempty(args_string(args, 'name')) descr = nonempty(args_string( args, 'description')) if 'description' in args else '' filename = tempfile.mktemp('ctor') if 'constructor_id' in args: try: current_constructor = Constructor.objects.get( slug=args['constructor_id']) except Constructor.DoesNotExist: return error_response('Constructor does not exists') if current_constructor.user_id != user.pk: return error_response('Access denied') if Constructor.objects.filter(name=name).exclude( slug=args['constructor_id']).exists(): return error_response( 'Constructor with this name already exists') else: if Constructor.objects.filter(name=name).exists(): return error_response( 'Constructor with this name already exists') current_constructor = Constructor.create() if 'ctor_file' in args: file_base64 = re.sub('^data:.+;base64,', '', args['ctor_file']) try: file_source = base64.b64decode(file_base64).decode('utf-8') except Exception: return error_response("Invalid input/0") file = open(filename, "w") file.write(file_source) file.close() is_public = False else: return error_response("Constructor file is missing") if float(price): with open(filename) as f: if not '%payment_code%' in f.read( ): # todo check on deploy in source of contract? return error_response( "Payment code must be in contract constructor") current_constructor.name = name current_constructor.description = descr current_constructor.payment_address = args['payment_address'] current_constructor.price = Decimal(price) current_constructor.is_public = is_public current_constructor.user = user version_info = self.constructor_engine.get_constructor_version( file_source) if 'error' == version_info['result']: return error_response(version_info['error_descr']) current_constructor.version = version_info['version'] current_constructor.blockchain = version_info['blockchain'] params = self.constructor_engine.get_constructor_params(file_source) if 'error' == params['result']: return error_response(params['error_descr']) current_constructor.schema = json.dumps(params.get('schema', {})) current_constructor.ui_schema = json.dumps(params.get('ui_schema', {})) current_constructor.save() self.constructor_engine.register_constructor(current_constructor.slug, filename) return JsonResponse({'ok': True})