def setUp(self): self.user = get_user_model().objects.create( username="******", password="******") self.user_2 = get_user_model().objects.create( username="******", password="******") self.currency = Currency.objects.create( token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_1_2 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkuSwJiAs2HdRopJwuaoSFKPbSPFAaXLGjT4Hjthc3UeXeign2w6", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2 = Wallet.objects.create(owner=self.user_2, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkutu49fgbHxV6vdVRBLbvCLpuq7CmSR6pnowxZRFcY7c76wUqHT", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.currency.cashout_wallet = self.wallet_2 self.currency.save() self.key = pytezos.Key.from_encoded_key( settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY) self.wallet_pk = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=self.key.public_key(), currency=self.currency, owner=self.user, state=WALLET_STATES.VERIFIED.value) self.mint_transaction = Transaction.objects.create( to_wallet=self.wallet_pk, amount=100) self.token_transaction = MetaTransaction( from_wallet=self.wallet_pk, to_wallet=self.currency.cashout_wallet, nonce=self.wallet_pk.nonce+1, amount=10) signature = self.key.sign(pack_meta_transaction( self.token_transaction.to_meta_transaction_dictionary())) self.token_transaction.signature = signature self.token_transaction.save()
def setUp(self): self.currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.key1 = pytezos.crypto.Key.generate() self.key2 = pytezos.crypto.Key.generate() self.wallet1 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=self.key1.public_key(), currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet2 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=self.key2.public_key(), currency=self.currency, state=WALLET_STATES.VERIFIED.value)
def pre_save_signal_wallet(sender, instance, **kwargs): if instance.wallet_id is None or len(instance.wallet_id) <= 0: instance.wallet_id = Wallet.generate_wallet_id() while Wallet.objects.filter(wallet_id=instance.wallet_id).exists(): instance.wallet_id = Wallet.generate_wallet_id() if instance.uuid is not None: try: previous = Wallet.objects.get(uuid=instance.uuid) if instance.state != previous.state and instance.state == WALLET_STATES.VERIFIED.value: instance.notify_owner_verified() except Wallet.DoesNotExist: pass
def setUp(self): self.currency = Currency.objects.create(token_id=0, name="TEZ", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.pytezos_client = pytezos.pytezos.using( key=settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY, shell=settings.TEZOS_NODE) self.last_nonce = read_nonce_from_chain( self.pytezos_client.key.public_key_hash()) self.token_contract = self.pytezos_client.contract( settings.TEZOS_TOKEN_CONTRACT_ADDRESS) self.wallet1 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=self.pytezos_client.key.public_key(), currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet2 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="edpku3g7CeTEvSKhxipD4Q2B6EiEP8cR323u8PFmGFgKRVRvCneEmT", currency=self.currency, state=WALLET_STATES.VERIFIED.value) Transaction.objects.create(to_wallet=self.wallet1, amount=10000)
def setUp(self): self.user = get_user_model().objects.create( username="******", password="******") self.user_2 = get_user_model().objects.create( username="******", password="******") self.currency = Currency.objects.create( token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkvMcoG5ASY8JK7CLaMKMQYx4nUhB3KfrurpuvM6VjJ25H4sbKqq", currency=self.currency) self.wallet_1_2 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpku8CQWKpekx9EWYKPF3pPScPeo3acTEKdeA9vdJYU8hSgoFPq53", currency=self.currency) self.wallet_2 = Wallet.objects.create(owner=self.user_2, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkusN6THUuQ5cJV1wWGURe23Mp4G9qFVgh8Pfh8BcMLT9CziPDVx", currency=self.currency)
def setUp(self): self.user = get_user_model().objects.create(username="******", password="******") self.user_2 = get_user_model().objects.create(username="******", password="******") self.currency = Currency.objects.create(token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.currency_2 = Currency.objects.create(token_id=1, name="TEZ2", starting_capital=22, symbol='tez2', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create( owner=self.user, wallet_id=Wallet.generate_wallet_id(), public_key="edpkuWW8CKkKRD7VipUyggFFnUaCumbMKDBLzPRNtbDx9zG2PtMeRS", currency=self.currency) self.wallet_1_1 = Wallet.objects.create( owner=self.user, wallet_id=Wallet.generate_wallet_id(), public_key="edpkuqDMtBwt45prqmLpjUTNNKUkKvy7i1xXvEkkHkDfAq6ihzMGtX", currency=self.currency) self.wallet_1_2 = Wallet.objects.create( owner=self.user, wallet_id=Wallet.generate_wallet_id(), public_key="edpkuUwKji4CWfQchkf2F1X8VKbYXtgjarAmg7pn4Rhydf1YYzrDka", currency=self.currency, category=WALLET_CATEGORIES.COMPANY.value) self.wallet_2 = Wallet.objects.create( owner=self.user_2, wallet_id=Wallet.generate_wallet_id(), public_key=self.pubkey_2, currency=self.currency) Transaction.objects.create(to_wallet=self.currency.owner_wallet, amount=2000) Transaction.objects.create(to_wallet=self.currency_2.owner_wallet, amount=2000)
def setUp(self): self.user = get_user_model().objects.create( username="******", password="******") self.user_2 = get_user_model().objects.create( username="******", password="******") self.currency = Currency.objects.create( token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_1_2 = Wallet.objects.create(owner=self.user, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkusN6THUuQ5cJV1wWGURe23Mp4G9qFVgh8Pfh8BcMLT9CziPDVx", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2 = Wallet.objects.create(owner=self.user_2, wallet_id=Wallet.generate_wallet_id( ), public_key="edpkuqw4KyJAsjSyn7Ca67Mc6GLpQxTMb6CLPQj8H8KZYdKDeBkC2v", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.currency.cashout_wallet = self.wallet_1 self.currency.save()
def test_validation(self): currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') wallet1 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=currency, state=WALLET_STATES.VERIFIED.value) wallet2 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="edpku3g7CeTEvSKhxipD4Q2B6EiEP8cR323u8PFmGFgKRVRvCneEmT", currency=currency, state=WALLET_STATES.VERIFIED.value) Transaction.objects.create(to_wallet=wallet1, amount=100) with self.assertRaises(ValidationError): MetaTransaction.objects.create( from_wallet=None, to_wallet=wallet2, amount=10, nonce=1) with self.assertRaises(ValidationError): MetaTransaction.objects.create( from_wallet=wallet1, to_wallet=wallet2, amount=10, nonce=0) with self.assertRaises(ValidationError): MetaTransaction.objects.create( from_wallet=wallet1, to_wallet=wallet2, amount=0, nonce=1)
def test_balance_calculation(self): currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') key1 = pytezos.crypto.Key.generate() key2 = pytezos.crypto.Key.generate() wallet1 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=key1.public_key(), currency=currency, state=WALLET_STATES.VERIFIED.value) wallet2 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key=key2.public_key(), currency=currency, state=WALLET_STATES.VERIFIED.value) Transaction.objects.create(to_wallet=wallet1, amount=100) meta_transaction1 = MetaTransaction(from_wallet=wallet1, to_wallet=wallet2, amount=10, nonce=1) signature1 = key1.sign(pack_meta_transaction(meta_transaction1.to_meta_transaction_dictionary())) meta_transaction1.signature = signature1 meta_transaction1.save() self.assertEqual( wallet1.balance, 90) self.assertEqual( wallet2.balance, 10) meta_transaction2 = MetaTransaction(from_wallet=wallet2, to_wallet=wallet1, amount=1, nonce=1) signature2 = key2.sign(pack_meta_transaction(meta_transaction2.to_meta_transaction_dictionary())) meta_transaction2.signature = signature2 meta_transaction2.save() self.assertEqual( wallet1.balance, 91) self.assertEqual( wallet2.balance, 9) meta_transaction1 = MetaTransaction(from_wallet=wallet1, to_wallet=wallet2, amount=1, nonce=2) signature1 = key1.sign(pack_meta_transaction(meta_transaction1.to_meta_transaction_dictionary())) meta_transaction1.signature = signature1 meta_transaction1.save() self.assertEqual( wallet1.balance, 90) self.assertEqual( wallet2.balance, 10)
def setUp(self): self.user = get_user_model().objects.create(username="******", password="******") self.user_2 = get_user_model().objects.create(username="******", password="******") self.currency = Currency.objects.create(token_id=0, name="TEZ", symbol='tez', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.currency_2 = Currency.objects.create(token_id=1, name="TEZ2", symbol='tez2', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet_1 = Wallet.objects.create( owner=self.user, wallet_id=Wallet.generate_wallet_id(), public_key="edpku8CQWKpekx9EWYKPF3pPScPeo3acTEKdeA9vdJYU8hSgoFPq53", currency=self.currency) self.wallet_1_2 = Wallet.objects.create( owner=self.user, wallet_id=Wallet.generate_wallet_id(), public_key="edpkusN6THUuQ5cJV1wWGURe23Mp4G9qFVgh8Pfh8BcMLT9CziPDVx", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2 = Wallet.objects.create( owner=self.user_2, wallet_id=Wallet.generate_wallet_id(), public_key=self.pubkey_2, currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2_1_2 = Wallet.objects.create( owner=self.user_2, wallet_id=Wallet.generate_wallet_id(), public_key="edpkuqw4KyJAsjSyn7Ca67Mc6GLpQxTMb6CLPQj8H8KZYdKDeBkC2v", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet_2_2 = Wallet.objects.create( owner=self.user_2, wallet_id=Wallet.generate_wallet_id(), public_key=self.pubkey_1, currency=self.currency_2, state=WALLET_STATES.VERIFIED.value) self.key = pytezos.Key.from_encoded_key( settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY) self.wallet_pk = Wallet.objects.create( wallet_id=Wallet.generate_wallet_id(), public_key=self.key.public_key(), currency=self.currency, owner=self.user)
def sync_to_blockchain(is_dry_run=True, _async=False): print('starting sync') time.sleep(settings.BLOCKCHAIN_SYNC_WAIT_TIME) from apps.wallet.models import Wallet, MetaTransaction, Transaction, WalletPublicKeyTransferRequest, TRANSACTION_STATES pytezos_client = pytezos.using( key=settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY, shell=settings.TEZOS_NODE) token_contract = pytezos_client.contract( settings.TEZOS_TOKEN_CONTRACT_ADDRESS) funding_transactions = {} meta_transactions = [] operation_groups = [] state_update_items = [] for transaction in Transaction.objects.exclude( state=TRANSACTION_STATES.PENDING.value).exclude( state=TRANSACTION_STATES.DONE.value).order_by('created_at'): state_update_items.append(transaction) if not transaction.from_wallet: operation_groups.append( token_contract.mint( address=transaction.to_wallet.address, decimals=transaction.to_wallet.currency.decimals, name=transaction.to_wallet.currency.name, token_id=transaction.to_wallet.currency.token_id, symbol=transaction.to_wallet.currency.symbol, amount=transaction.amount).operation_group.sign()) elif MetaTransaction.objects.filter(pk=transaction.pk).exists(): meta_transactions.append( MetaTransaction.objects.get(pk=transaction)) else: same_from_txs = funding_transactions.get( transaction.from_wallet.address, []) same_from_txs.append({ "to_": transaction.to_wallet.address, "token_id": transaction.to_wallet.currency.token_id, "amount": transaction.amount }) funding_transactions[ transaction.from_wallet.address] = same_from_txs # preparing funding if len(funding_transactions.items()) > 0: funding_transaction_payloads = list( map(lambda item: { "from_": item[0], "txs": item[1] }, funding_transactions.items())) operation_groups.append( token_contract.transfer( funding_transaction_payloads).operation_group.sign()) # preparing meta if len(meta_transactions) > 0: meta_transaction_payloads = list( map( lambda meta_transaction: meta_transaction. to_meta_transaction_dictionary(), meta_transactions)) operation_groups.append( token_contract.meta_transfer( meta_transaction_payloads).operation_group.sign()) # wallet public key transfers wallet_public_key_transfer_payloads = [] wallet_public_key_transfer_requests = [] for wallet_public_key_transfer_request in WalletPublicKeyTransferRequest.objects.exclude( state=TRANSACTION_STATES.PENDING.value).exclude( state=TRANSACTION_STATES.DONE.value).order_by('created_at'): if wallet_public_key_transfer_request.wallet.balance > 0 and wallet_public_key_transfer_request.wallet.public_key != wallet_public_key_transfer_request.new_public_key: new_address = Wallet(public_key=wallet_public_key_transfer_request. new_public_key).address state_update_items.append(wallet_public_key_transfer_request) wallet_public_key_transfer_requests.append( wallet_public_key_transfer_request) wallet_public_key_transfer_payloads.append({ "from_": wallet_public_key_transfer_request.wallet.address, "txs": [{ "to_": new_address, "token_id": wallet_public_key_transfer_request.wallet.currency. token_id, "amount": wallet_public_key_transfer_request.wallet.balance }] }) else: wallet_public_key_transfer_request.old_public_key = wallet_public_key_transfer_request.wallet.public_key wallet_public_key_transfer_request.wallet.public_key = wallet_public_key_transfer_request.new_public_key wallet_public_key_transfer_request.wallet.save() wallet_public_key_transfer_request.state = TRANSACTION_STATES.DONE.value wallet_public_key_transfer_request.notes = "Has no balance or was recovering to same pubkey, transferred offchain" wallet_public_key_transfer_request.save() if len(wallet_public_key_transfer_payloads) > 0: operation_groups.append( token_contract.transfer( wallet_public_key_transfer_payloads).operation_group.sign()) # merging all operations into one single group final_operation_group = None operation_counter = 0 for operation_group in operation_groups: if final_operation_group == None: final_operation_group = operation_group operation_counter = int(operation_group.contents[0]['counter']) else: operation_counter += 1 operation = operation_group.contents[0] operation['counter'] = str(operation_counter) final_operation_group = final_operation_group.operation( operation_group.contents[0]) if final_operation_group != None: # we have stuff to sync print(final_operation_group) operation_result = final_operation_group.sign().preapply() print(operation_result) if is_dry_run: return OperationResult.is_applied(operation_result) elif OperationResult.is_applied(operation_result): def update_sync_state(items, state=TRANSACTION_STATES.PENDING.value, notes='', operation_hash=''): for item in items: type(item).objects.filter(pk=item.pk).update( state=state, notes=notes, operation_hash=operation_hash, submitted_to_chain_at=now()) update_sync_state(state_update_items) try: is_confirmed_in_chain = False try: operation_inject_result = final_operation_group.sign( ).inject(_async=_async, preapply=True, check_result=True, num_blocks_wait=settings.TEZOS_BLOCK_WAIT_TIME) is_operation_applied = OperationResult.is_applied( operation_inject_result) is_confirmed_in_chain = True except AssertionError: # here we assume that the operation was applied even if we know the assertion failed is_operation_applied = True if is_operation_applied: for wallet_public_key_transfer_request in wallet_public_key_transfer_requests: wallet_public_key_transfer_request.old_public_key = wallet_public_key_transfer_request.wallet.public_key wallet_public_key_transfer_request.wallet.public_key = wallet_public_key_transfer_request.new_public_key wallet_public_key_transfer_request.wallet.save() wallet_public_key_transfer_request.state = TRANSACTION_STATES.DONE.value wallet_public_key_transfer_request.save() if is_confirmed_in_chain: update_sync_state(state_update_items, TRANSACTION_STATES.DONE.value, json.dumps(operation_inject_result), operation_inject_result['hash']) else: update_sync_state(state_update_items, TRANSACTION_STATES.DONE.value, json.dumps(operation_result), "*") else: if operation_inject_result is None: update_sync_state( state_update_items, TRANSACTION_STATES.FAILED.value, 'Error during sync: {}'.format( json.dumps(operation_result))) else: update_sync_state( state_update_items, TRANSACTION_STATES.FAILED.value, 'Error during sync: {}'.format( json.dumps(operation_inject_result))) return is_operation_applied except Exception as error: update_sync_state( state_update_items, TRANSACTION_STATES.FAILED.value, 'Exception during sync: {}\nTraceback: {}'.format( repr(error), traceback.format_exc())) return False else: return OperationResult.is_applied(operation_result)
def test_address_calculation(self): currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') wallet = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=currency, state=WALLET_STATES.VERIFIED.value) self.assertEqual( wallet.nonce, 0)
def test_bogus_pubkey(self): currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') with self.assertRaises(ValidationError): wallet = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="bogus", currency=currency, state=WALLET_STATES.VERIFIED.value)
def setUp(self): self.currency = Currency.objects.create(token_id=0, name="test", symbol='test', claim_deadline='2120-01-01', campaign_end='2120-01-01') self.wallet1 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="edpku976gpuAD2bXyx1XGraeKuCo1gUZ3LAJcHM12W1ecxZwoiu22R", currency=self.currency, state=WALLET_STATES.VERIFIED.value) self.wallet2 = Wallet.objects.create(wallet_id=Wallet.generate_wallet_id( ), public_key="edpku3g7CeTEvSKhxipD4Q2B6EiEP8cR323u8PFmGFgKRVRvCneEmT", currency=self.currency, state=WALLET_STATES.VERIFIED.value)