def register_token(token_address, name, short_name, register_on_chain): try: Token.objects.get(address=remove_0x_prefix(token_address)) raise ValueError( 'Token {} already registered in local db.'.format(token_address)) except Token.DoesNotExist: pass if register_on_chain: NOCUSTContractInterface().register_ERC20(token_address) print('Registration transaction queued.') token = Token.objects.create(address=remove_0x_prefix(token_address), name=name, short_name=short_name, trail=Token.objects.count(), block=LocalViewInterface.latest_block()) print('Token locally registered.') register_owner_account(token) if same_hex_value(token_address, settings.SLA_TOKEN_ADDRESS): register_sla_recipient_account() return token
def validate(self, attrs): if not settings.SWAPS_ENABLED: raise serializers.ValidationError(detail='', code=ErrorCode.SWAPPING_DISABLED) if len(attrs.get( 'debit_signature')) > settings.SWAPS_PERSISTENCE_LIMIT: raise serializers.ValidationError( detail= 'Too many signatures, swaps are only allowed to persist {} Eons.' .format(settings.SWAPS_PERSISTENCE_LIMIT), code=ErrorCode.TOO_MANY_FUTURE_SIGNATURES) signature_lengths = [ len(attrs.get('debit_signature')), len(attrs.get('debit_balance_signature')), len(attrs.get('credit_signature')), len(attrs.get('credit_balance_signature')), len(attrs.get('credit_fulfillment_signature')) ] if len(set(signature_lengths)) > 1: raise serializers.ValidationError( detail='', code=ErrorCode.WRONG_NUMBER_OF_SIGNATURES) if signature_lengths[0] < 1: raise serializers.ValidationError( detail='There should be at least 1 signature provided.', code=ErrorCode.WRONG_NUMBER_OF_SIGNATURES) attrs['valid_eons'] = signature_lengths[0] wallet = attrs.get('wallet') recipient = attrs.get('recipient') if wallet.registration_operator_authorization is None: raise serializers.ValidationError( detail='', code=ErrorCode.DEBIT_WALLET_NOT_ADMITTED) if recipient.registration_operator_authorization is None: raise serializers.ValidationError( detail='', code=ErrorCode.CREDIT_WALLET_NOT_ADMITTED) if not crypto.same_hex_value(wallet.address, recipient.address): raise serializers.ValidationError( detail='', code=ErrorCode.DEBIT_CREDIT_WALLET_ADDRESS_MISMATCH) elif wallet.token == recipient.token: raise serializers.ValidationError( detail='', code=ErrorCode.DEBIT_CREDIT_TOKEN_ADDRESS_MATCH) return attrs
def deposit(self, token_address, wallet, amount): if same_hex_value(token_address, settings.HUB_LQD_CONTRACT_ADDRESS): return self.contract\ .functions\ .deposit(add_0x_prefix(token_address), add_0x_prefix(wallet), amount)\ .transact({'from': wallet, 'value': amount}) else: token_contract = self.web3.eth.contract( address=add_0x_prefix(token_address), abi=token_contract_abi) token_contract\ .functions\ .approve(settings.HUB_LQD_CONTRACT_ADDRESS, amount)\ .transact({'from': wallet}) return self.contract\ .functions\ .deposit(add_0x_prefix(token_address), add_0x_prefix(wallet), amount)\ .transact({'from': wallet})
def register_sla_recipient_account(): if same_hex_value(settings.SLA_RECIPIENT_ADDRESS, settings.HUB_OWNER_ACCOUNT_ADDRESS): logger.warning('Skipping registration: Hub Owner is SLA recipient.') return token = Token.objects.filter( address__iexact=remove_0x_prefix(settings.SLA_TOKEN_ADDRESS)) if not token.exists(): logger.error('SLA Payment Token not yet registered.') return if Wallet.objects.filter(token=token, address__iexact=remove_0x_prefix( settings.SLA_RECIPIENT_ADDRESS)).exists(): logger.error('Recipient account already registered.') return if not LocalViewInterface.get_contract_parameters(): logger.error('Contract parameters not yet populated.') return latest_eon = LocalViewInterface.latest().eon_number() authorization_digest = Wallet( token=token, address=settings.SLA_RECIPIENT_ADDRESS).get_admission_hash(latest_eon) authorization = sign_message(authorization_digest, settings.SLA_RECIPIENT_KEY) registration = AdmissionRequestSerializer( data={ 'token': token.address, 'address': remove_0x_prefix(settings.SLA_RECIPIENT_ADDRESS), 'authorization': encode_signature(authorization) }) registration.is_valid(raise_exception=True) registration.save() process_admissions()
def is_sla_exempt(self): return same_hex_value(self.address, settings.HUB_OWNER_ACCOUNT_ADDRESS)\ or same_hex_value(self.address, settings.SLA_RECIPIENT_ADDRESS)