diff --git a/app/kudos/admin.py b/app/kudos/admin.py index 2f42db2420f..359d445a847 100644 --- a/app/kudos/admin.py +++ b/app/kudos/admin.py @@ -40,8 +40,15 @@ class TokenRequestAdmin(admin.ModelAdmin): def response_change(self, request, obj): if "_mint_kudos" in request.POST: - obj.mint() - self.message_user(request, "Mint done") + tx_id = obj.mint() + self.message_user(request, f"Mint submitted to chain: tx {tx_id}. Once this tx clears pls 'sync kudos'.") + if "_sync_kudos" in request.POST: + from kudos.management.commands.mint_all_kudos import sync_latest + sync_latest(0) + sync_latest(1) + sync_latest(2) + sync_latest(3) + self.message_user(request, f"Synced latest 3 kudos from open sea. If there is a new kudos on chain it will appear in the marketplace") return super().response_change(request, obj) diff --git a/app/kudos/management/commands/mint_all_kudos.py b/app/kudos/management/commands/mint_all_kudos.py index 38063bffddc..579b85dbb0f 100644 --- a/app/kudos/management/commands/mint_all_kudos.py +++ b/app/kudos/management/commands/mint_all_kudos.py @@ -35,7 +35,12 @@ formatter = '%(levelname)s:%(name)s.%(funcName)s:%(message)s' -def mint_kudos(kudos_contract, kudos, account, private_key, gas_price_gwei, mint_to=None, live=False, skip_sync=True): +def sync_latest(the_buffer=0, network='mainnet'): + kudos_contract = KudosContract(network=network) + kudos_contract.sync_latest(the_buffer) + + +def mint_kudos(kudos_contract, kudos, account, private_key, gas_price_gwei, mint_to=None, live=False, skip_sync=True, dont_wait_for_kudos_id_return_tx_hash_instead=False): image_path = kudos.get('artwork_url') if not image_path: image_name = urllib.parse.quote(kudos.get('image')) @@ -115,17 +120,19 @@ def mint_kudos(kudos_contract, kudos, account, private_key, gas_price_gwei, mint else: mint_to = kudos_contract._w3.toChecksumAddress(settings.KUDOS_OWNER_ACCOUNT) + response = None is_live = live if is_live: try: token_uri_url = kudos_contract.create_token_uri_url(**metadata) args = (mint_to, kudos['priceFinney'], kudos['numClonesAllowed'], token_uri_url) - kudos_contract.mint( + response = kudos_contract.mint( *args, account=account, private_key=private_key, skip_sync=skip_sync, gas_price_gwei=gas_price_gwei, + dont_wait_for_kudos_id_return_tx_hash_instead=dont_wait_for_kudos_id_return_tx_hash_instead, ) print('Live run - Name: ', readable_name, ' - Account: ', account, 'Minted!') except Exception as e: @@ -133,6 +140,7 @@ def mint_kudos(kudos_contract, kudos, account, private_key, gas_price_gwei, mint else: print('Dry run - Name: ', readable_name, ' - Account: ', account, 'Skipping!') + return response class Command(BaseCommand): diff --git a/app/kudos/models.py b/app/kudos/models.py index 10585a9c27c..8143f6aede8 100644 --- a/app/kudos/models.py +++ b/app/kudos/models.py @@ -614,11 +614,11 @@ def mint(self): } kudos_contract = KudosContract(network=self.network) gas_price_gwei = recommend_min_gas_price_to_confirm_in_time(1) - mint_kudos(kudos_contract, kudos, account, private_key, gas_price_gwei, mint_to=None, live=True) + tx_id = mint_kudos(kudos_contract, kudos, account, private_key, gas_price_gwei, mint_to=None, live=True, dont_wait_for_kudos_id_return_tx_hash_instead=True) self.processed = True self.approved = True self.save() - + return tx_id class TransferEnabledFor(SuperModel): """Model that represents the ability to send a Kudos, i diff --git a/app/kudos/utils.py b/app/kudos/utils.py index 29271102a5b..e98007f8b45 100644 --- a/app/kudos/utils.py +++ b/app/kudos/utils.py @@ -279,8 +279,13 @@ def sync_db_without_txid(self, kudos_id): """ kudos = self.getKudosById(kudos_id, to_dict=True) kudos['owner_address'] = self._contract.functions.ownerOf(kudos_id).call() - kudos['contract_address'] = self._contract.address - kudos['network'] = self.network + contract, created = Contract.objects.get_or_create( + address=self._contract.address, + network=self.network, + defaults=dict(is_latest=True) + ) + kudos['contract'] = contract + try: kudos_token = Token.objects.get(token_id=kudos_id) if kudos_token.suppress_sync: @@ -408,7 +413,7 @@ def _resolve_account(self, account): @log_args @may_require_key - def mint(self, *args, account=None, private_key=None, skip_sync=False, gas_price_gwei=None): + def mint(self, *args, account=None, private_key=None, skip_sync=False, gas_price_gwei=None, dont_wait_for_kudos_id_return_tx_hash_instead=False): """Contract transaction method. Mint a new Gen0 Kudos on the blockchain. Not to be confused with clone. @@ -454,6 +459,9 @@ def mint(self, *args, account=None, private_key=None, skip_sync=False, gas_price logger.debug('No private key provided, using local signing...') tx_hash = self._contract.functions.mint(*args).transact({"from": account}) + if dont_wait_for_kudos_id_return_tx_hash_instead: + return self._w3.toHex(tx_hash) + tx_receipt = self._w3.eth.waitForTransactionReceipt(tx_hash) logger.debug(f'Tx hash: {tx_hash.hex()}') @@ -466,6 +474,17 @@ def mint(self, *args, account=None, private_key=None, skip_sync=False, gas_price return kudos_id + def sync_latest(self, the_buffer=0): + """Contract transaction method. + + Sync the latest kudos from the chain + """ + kudos_id = self._contract.functions.getLatestId().call() - the_buffer + self.sync_db_without_txid(kudos_id=kudos_id) + + return kudos_id + + @may_require_key def clone(self, *args, account=None, private_key=None, skip_sync=False): """Contract transaction method. diff --git a/app/retail/templates/admin/change_form.html b/app/retail/templates/admin/change_form.html index e7c59163eaa..fac362ed3bc 100644 --- a/app/retail/templates/admin/change_form.html +++ b/app/retail/templates/admin/change_form.html @@ -9,6 +9,7 @@ {% endif %} {% if 'kudos/tokenrequest' in request.build_absolute_uri %} + {% endif %} {% endblock %} \ No newline at end of file