Exemplo n.º 1
0
def create_eth_token(request):
    '''
    view for create eth token
    :param request: (admin_address, token_name, decimals,
    token_short_name, token_type, network_id)
    :return: ok
    '''
    token = request.META['HTTP_TOKEN']
    if not token:
        raise ValidationError({'result': 'Token not found'}, code=404)
    user = get_user_for_token(token)
    if int(request.data['network_id']) not in [1, 2]:
        raise ValidationError({'result': 'Wrong network id'}, code=404)
    log_action_name = 'create_eth_token'
    log_userinfo(log_action_name, token, user)
    network = Network.objects.get(id=int(request.data['network_id']))
    check.is_address(request.data['admin_address'])
    if int(request.data['decimals']) < 0 or int(request.data['decimals']) > 50:
        raise ValidationError({'result': 'Wrong decimals'}, code=404)
    if request.data['token_type'] not in ['ERC20', 'ERC223']:
        raise ValidationError({'result': 'Wrong token type'}, code=404)
    validate_token_name(request.data['token_name'])
    validate_token_short_name(request.data['token_short_name'])
    if request.data['future_minting'] not in [True, False]:
        raise ValidationError({'result': 'Wrong future minting'}, code=404)
    token_params = {
        'decimals': int(request.data['decimals']),
        'token_name': request.data['token_name'],
        'token_short_name': request.data['token_short_name'],
        'admin_address': request.data['admin_address'],
        'token_type': request.data['token_type'],
        'future_minting': request.data['future_minting'],
        'token_holders': []
    }
    log_additions(log_action_name, token_params)
    Contract.get_details_model(5).calc_cost(token_params, network)
    contract = Contract(state='CREATED',
                        name='Contract',
                        contract_type=5,
                        network=network,
                        cost=0,
                        user=user)
    contract.save()

    contract_details = ContractDetailsTokenSerializer().create(
        contract, token_params)
    answer = {
        'state': contract.state,
        'admin_address': contract_details.admin_address,
        'token_short_name': contract_details.token_short_name,
        'token_name': contract_details.token_name,
        'contract_id': contract.id,
        'created_date': contract.created_date,
        'network': contract.network.name,
        'network_id': contract.network.id,
        'decimals': contract_details.decimals,
        'token_type': contract_details.token_type,
        'future_minting': contract_details.future_minting
    }
    return Response(answer)
Exemplo n.º 2
0
 def validate(self, details):
     assert ('user_address' in details and 'date' in details
             and 'recepient_address' in details)
     check.is_address(details['user_address'])
     check.is_address(details['recepient_address'])
     details.get('recepient_email', None) and check.is_email(
         details['recepient_email'])
     return details
Exemplo n.º 3
0
 def validate(self, details):
     assert ('user_address' in details and 'heirs' in details
             and 'active_to' in details and 'check_interval' in details)
     check.is_address(details['user_address'])
     details['user_address'] = details['user_address'].lower()
     details['active_to'] = datetime.datetime.strptime(
         details['active_to'], '%Y-%m-%d %H:%M')
     for heir_json in details['heirs']:
         heir_json.get('email', None) and check.is_email(heir_json['email'])
         check.is_address(heir_json['address'])
         heir_json['address'] = heir_json['address'].lower()
         check.is_percent(heir_json['percentage'])
         heir_json['percentage'] = int(heir_json['percentage'])
     check.is_sum_eq_100([h['percentage'] for h in details['heirs']])
     return details
Exemplo n.º 4
0
def create_contract(request):
    print(request.data)
    if not check.is_address(request.data['address']):
        raise APIException(code=400, detail='address is not valid')
    for heir in request.data['heirs']:
        if not check.is_address(heir['address']):
            raise APIException(code=400,
                               detail='heir address %s is not valid' %
                               heir['address'])
        if heir['email'] and not check.is_email(heir['email']):
            raise APIException(code=400,
                               detail='heir email %s is not valid' %
                               heir['email'])
        if not check.is_percent(heir['percent']):
            raise APIException(code=400,
                               detail='percent %s is not valid' %
                               heir['percent'])
        heir['percent'] = int(heir['percent'])
    if sum([x['percent'] for x in request.data['heirs']]) != 100:
        raise APIException(code=400, detail='percents sum is not equal to 100')
    with open('../lastwill/contracts/LastWillContractTemplate.sol',
              'r') as contract_file:
        contract = contract_file.read()
        contract = re.sub('{{targetUser}}', request.data['address'], contract)
        contract = re.sub('{{lastWillAccount}}', settings.LASTWILL_ACCOUNT,
                          contract)
        contract = re.sub(
            '{{recipientPercents}}',
            #                'zzz',
            '[{}]'.format(', '.join([
                'RecepientPercent({{recipient: {recipient}, percent: {percent}}})'
                .format(recipient=heir['address'], percent=heir['percent'])
                for heir in request.data['heirs']
            ])),
            contract)

    print(contract)
    # deploy
    return JsonResponse({'contract': contract})
Exemplo n.º 5
0
 def validate(self, details):
     assert ('user_address' in details and 'pizza_cost' in details)
     check.is_address(details['user_address'])
     return details
Exemplo n.º 6
0
def edit_eth_token(request):
    '''
    view for edit params in  eth token
    :param request: contain contract id, editable field
    (admin_address, token_name, decimals, token_short_name, token_type)
    :return:
    '''
    token = request.META['HTTP_TOKEN']
    if not token:
        raise ValidationError({'result': 'Token not found'}, code=404)
    user = get_user_for_token(token)
    log_action_name = 'edit_eth_token'
    log_userinfo(log_action_name, token, user)
    contract = Contract.objects.get(id=int(request.data['contract_id']))
    if contract.invisible:
        raise ValidationError({'result': 'Contract is deleted'}, code=404)
    if contract.state != 'CREATED':
        raise ValidationError({'result': 'Wrong status in contract'}, code=403)
    if contract.user != user:
        raise ValidationError({'result': 'Wrong token'}, code=404)
    contract_details = contract.get_details()
    fields = ('decimals', 'token_type', 'token_short_name', 'admin_address',
              'token_name')
    if not any([key in request.data.keys() for key in fields]):
        raise ValidationError({'result': 'Optional parameters (at least one)'},
                              code=403)
    if 'decimals' in request.data:
        if int(request.data['decimals']) < 0 and int(
                request.data['decimals']) > 50:
            raise ValidationError({'result': 'Wrong decimals'}, code=404)
        contract_details.decimals = int(request.data['decimals'])
    if 'token_type' in request.data:
        if request.data['token_type'] not in ['ERC20', 'ERC223']:
            raise ValidationError({'result': 'Wrong token type'}, code=404)
        contract_details.token_type = request.data['token_type']
    if 'token_short_name' in request.data:
        validate_token_short_name(request.data['token_short_name'])
        contract_details.token_short_name = request.data['token_short_name']
    if 'admin_address' in request.data:
        check.is_address(request.data['admin_address'])
        contract_details.admin_address = request.data['admin_address']
    if 'token_name' in request.data and request.data['token_name'] != '':
        validate_token_name(request.data['token_name'])
        contract_details.token_name = request.data['token_name']
    if 'future_minting' in request.data:
        if request.data['future_minting'] not in [True, False]:
            raise ValidationError({'result': 'Wrong future minting'}, code=404)
        contract_details.future_minting = request.data['future_minting']
    log_additions(log_action_name, request.data)
    contract_details.save()
    answer = {
        'state': contract.state,
        'contract_id': contract.id,
        'created_date': contract.created_date,
        'network': contract.network.name,
        'network_id': contract.network.id,
        'token_name': contract_details.token_name,
        'token_short_name': contract_details.token_short_name,
        'admin_address': contract_details.admin_address,
        'decimals': contract_details.decimals,
        'token_type': contract_details.token_type
    }
    return Response(answer)