Skip to content

Commit

Permalink
[PAY-2364] Fix nft gated tracks (#7228)
Browse files Browse the repository at this point in the history
Co-authored-by: Saliou Diallo <saliou@audius.co>
  • Loading branch information
sddioulde and Saliou Diallo authored Jan 18, 2024
1 parent 12af10e commit eab69ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/discovery-provider/src/api/v1/tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def get(self, track_id):
description="""Optional - data which was used to generate the optional signature argument.""",
type=str,
)
stream_parser.add_argument(
download_parser.add_argument(
"nft_access_signature",
description="""Optional - nft access signature for this track which was previously generated by a registered DN.
We perform checks on it and pass it through to CN.""",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import base64
import concurrent.futures
import json
import logging
Expand Down Expand Up @@ -299,20 +298,22 @@ def _get_metadata_account(mint_address: str):


def _get_token_account_info(token_account):
return token_account["account"]["data"]["parsed"]["info"]
return token_account.account.data.parsed["info"]


def _decode_metadata_account(metadata_account):
account_info = solana_client_manager.get_account_info(metadata_account)
if (
(not account_info)
or ("value" not in account_info)
or (not account_info["value"])
or ("data" not in account_info["value"])
or (not account_info["value"]["data"])
):
try:
account_info = solana_client_manager.get_account_info_json_parsed(
metadata_account
)
if not account_info:
return None
return account_info.data
except Exception as e:
logger.error(
f"Could not decode metadata account {metadata_account}. Error: {e}"
)
return None
return base64.b64decode(account_info["value"]["data"][0])


async def _wrap_decode_metadata_account(metadata_account):
Expand Down Expand Up @@ -344,8 +345,11 @@ def _does_user_own_sol_nft_collection(

for wallet in user_sol_wallets:
try:
result = solana_client_manager.get_token_accounts_by_owner(wallet)
token_accounts = result["value"]
token_accounts = (
solana_client_manager.get_token_accounts_by_owner_json_parsed(
Pubkey.from_string(wallet)
)
)
nft_token_accounts = list(
filter(
lambda item: _get_token_account_info(item)["tokenAmount"]["amount"]
Expand Down
14 changes: 13 additions & 1 deletion packages/discovery-provider/src/queries/get_track_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,19 @@ def get_track_download_signature(args: GetTrackDownloadSignature):
authed_user_wallet = authed_user["user_wallet"].lower()
if signature_user_wallet != authed_user_wallet:
return None
return {"signature": signature_obj, "cid": cid, "filename": filename}
# todo: use commented line below once logic is added to pass in correct signature
# for nft gated tracks i.e. for original cid or for mp3 cid.
# for now, we are returning the signature for mp3 cid
# ==============
# return {"signature": signature_obj, "cid": cid, "filename": filename}
# ==============
if not track.get("track_cid"):
return None
return {
"signature": signature_obj,
"cid": track.get("track_cid"),
"filename": filename,
}

# build a track instance from the track dict
track_entity = Track(
Expand Down
46 changes: 10 additions & 36 deletions packages/discovery-provider/src/solana/solana_client_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,15 @@ def _get_slot(client: Client, index):
"solana_client_manager.py | get_slot | All requests failed to fetch",
)

def get_token_accounts_by_owner(self, owner: Pubkey, retries=DEFAULT_MAX_RETRIES):
def _get_token_accounts_by_owner(client: Client, index):
def get_token_accounts_by_owner_json_parsed(
self, owner: Pubkey, retries=DEFAULT_MAX_RETRIES
):
def _get_token_accounts_by_owner_json_parsed(client: Client, index):
endpoint = self.endpoints[index]
num_retries = retries
while num_retries > 0:
try:
response = client.get_token_accounts_by_owner(
response = client.get_token_accounts_by_owner_json_parsed(
owner,
TokenAccountOpts(
program_id=SPL_TOKEN_ID_PK, encoding="jsonParsed"
Expand All @@ -178,50 +180,22 @@ def _get_token_accounts_by_owner(client: Client, index):
return response.value
except Exception as e:
logger.error(
f"solana_client_manager.py | get_token_accounts_by_owner, {e}",
exc_info=True,
)
num_retries -= 1
time.sleep(DELAY_SECONDS)
logger.error(
f"solana_client_manager.py | get_token_accounts_by_owner | Retrying with endpoint {endpoint}"
)
raise Exception(
f"solana_client_manager.py | get_token_accounts_by_owner | Failed with endpoint {endpoint}"
)

return _try_all(
self.clients,
_get_token_accounts_by_owner,
"solana_client_manager.py | get_token_accounts_by_owner | All requests failed to fetch",
)

def get_account_info(self, account: Pubkey, retries=DEFAULT_MAX_RETRIES):
def _get_account_info(client: Client, index):
endpoint = self.endpoints[index]
num_retries = retries
while num_retries > 0:
try:
response = client.get_account_info(account)
return response.value
except Exception as e:
logger.error(
f"solana_client_manager.py | get_account_info, {e}",
f"solana_client_manager.py | get_token_accounts_by_owner_json_parsed, {e}",
exc_info=True,
)
num_retries -= 1
time.sleep(DELAY_SECONDS)
logger.error(
f"solana_client_manager.py | get_account_info | Retrying with endpoint {endpoint}"
f"solana_client_manager.py | get_token_accounts_by_owner_json_parsed | Retrying with endpoint {endpoint}"
)
raise Exception(
f"solana_client_manager.py | get_account_info | Failed with endpoint {endpoint}"
f"solana_client_manager.py | get_token_accounts_by_owner_json_parsed | Failed with endpoint {endpoint}"
)

return _try_all(
self.clients,
_get_account_info,
"solana_client_manager.py | get_account_info | All requests failed to fetch",
_get_token_accounts_by_owner_json_parsed,
"solana_client_manager.py | get_token_accounts_by_owner_json_parsed | All requests failed to fetch",
)

def get_account_info_json_parsed(
Expand Down

0 comments on commit eab69ec

Please sign in to comment.