Skip to content

Commit

Permalink
[PAY-2449] USDC purchase access migration (#7460)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharit-tan authored Feb 7, 2024
1 parent e13220b commit c633c27
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 11 deletions.
12 changes: 10 additions & 2 deletions packages/commands/src/upload-track.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ const getStreamConditions = async ({

const getDownloadConditions = async ({
streamConditions,
parsedStreamConditions,
downloadConditions,
downloadPrice: downloadPriceString,
audiusLibs
}) => {
if (streamConditions) {
return JSON.parse(streamConditions)
}
if (parsedStreamConditions) {
return parsedStreamConditions
}
if (downloadConditions) {
return JSON.parse(downloadConditions)
}
Expand Down Expand Up @@ -132,7 +136,10 @@ program
'Manually set a stream conditions object. Cannot be used with -u',
''
)
.option('-o, --is-downloadable <is downloadable>', 'Whether track is downloadable')
.option(
'-o, --is-downloadable <is downloadable>',
'Whether track is downloadable'
)
.option(
'-dp, --download-price <download price>',
'Generate a download conditions object with the given price in cents. Cannot be used with -dc'
Expand Down Expand Up @@ -194,6 +201,7 @@ program
})
const parsedDownloadConditions = await getDownloadConditions({
streamConditions,
parsedStreamConditions,
downloadConditions,
downloadPrice,
audiusLibs
Expand All @@ -212,7 +220,7 @@ program
genre:
genre ||
Genre[
Object.keys(Genre)[randomInt(Object.keys(Genre).length - 1)]
Object.keys(Genre)[randomInt(Object.keys(Genre).length - 1)]
],
mood: mood || `mood ${rand}`,
credits_splits: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
begin;
-- create type
do $$ begin
create type usdc_purchase_access_type as enum ('stream', 'download');
exception
when duplicate_object then null;
end $$;

-- add columns to usdc_purchases
alter table usdc_purchases
add column if not exists access usdc_purchase_access_type not null default 'stream';

-- add columns to track_price_history
alter table track_price_history
add column if not exists access usdc_purchase_access_type not null default 'stream';
commit;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
from integration_tests.utils import populate_mock_db
from src.challenges.challenge_event import ChallengeEvent
from src.challenges.challenge_event_bus import ChallengeEventBus
from src.models.users.usdc_purchase import PurchaseType, USDCPurchase
from src.models.users.usdc_purchase import (
PurchaseAccessType,
PurchaseType,
USDCPurchase,
)
from src.models.users.usdc_transactions_history import (
USDCTransactionMethod,
USDCTransactionsHistory,
Expand Down Expand Up @@ -83,6 +87,7 @@
"track_id": 1,
"splits": {"7gfRGGdp89N9g3mCsZjaGmDDRdcTnZh9u3vYyBab2tRy": 1000000},
"total_price_cents": 100,
"access": PurchaseAccessType.stream,
},
{ # pay $1 each to track owner and third party
"track_id": 2,
Expand All @@ -91,6 +96,7 @@
"7dw7W4Yv7F1uWb9dVH1CFPm39mePyypuCji2zxcFA556": 1000000,
},
"total_price_cents": 200,
"access": PurchaseAccessType.stream,
},
],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
TransactionMethod,
TransactionType,
)
from src.models.users.usdc_purchase import PurchaseType, USDCPurchase
from src.models.users.usdc_purchase import (
PurchaseAccessType,
PurchaseType,
USDCPurchase,
)
from src.models.users.usdc_transactions_history import (
USDCTransactionMethod,
USDCTransactionsHistory,
Expand Down Expand Up @@ -119,6 +123,7 @@
"track_id": 1,
"splits": {RECIPIENT_USDC_USER_BANK_ADDRESS: 1000000},
"total_price_cents": 100,
"access": PurchaseAccessType.stream,
},
{ # pay $1 each to track owner and third party
"track_id": 2,
Expand All @@ -127,6 +132,7 @@
EXTERNAL_ACCOUNT_ADDRESS: 1000000,
},
"total_price_cents": 200,
"access": PurchaseAccessType.stream,
},
],
}
Expand Down
14 changes: 10 additions & 4 deletions packages/discovery-provider/integration_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from src.models.users.aggregate_user import AggregateUser
from src.models.users.associated_wallet import AssociatedWallet, WalletChain
from src.models.users.supporter_rank_up import SupporterRankUp
from src.models.users.usdc_purchase import USDCPurchase
from src.models.users.usdc_purchase import PurchaseAccessType, USDCPurchase
from src.models.users.usdc_transactions_history import (
USDCTransactionMethod,
USDCTransactionsHistory,
Expand Down Expand Up @@ -218,9 +218,11 @@ def populate_mock_db(db, entities, block_offset=None):
remix_of=track_meta.get("remix_of", None),
updated_at=track_meta.get("updated_at", track_created_at),
created_at=track_meta.get("created_at", track_created_at),
release_date=str(track_meta.get("release_date"))
if track_meta.get("release_date")
else None,
release_date=(
str(track_meta.get("release_date"))
if track_meta.get("release_date")
else None
),
is_unlisted=track_meta.get("is_unlisted", False),
is_stream_gated=track_meta.get("is_stream_gated", False),
stream_conditions=track_meta.get("stream_conditions", None),
Expand All @@ -240,6 +242,9 @@ def populate_mock_db(db, entities, block_offset=None):
"block_timestamp", datetime.now()
),
total_price_cents=track_price_history_meta.get("total_price_cents", 0),
access=track_price_history_meta.get(
"access", PurchaseAccessType.stream
),
)
session.add(track_price_history)
for i, playlist_meta in enumerate(playlists):
Expand Down Expand Up @@ -687,6 +692,7 @@ def populate_mock_db(db, entities, block_offset=None):
content_id=usdc_purchase.get("content_id", 3),
created_at=usdc_purchase.get("created_at", datetime.now()),
updated_at=usdc_purchase.get("updated_at", datetime.now()),
access=usdc_purchase.get("access", PurchaseAccessType.stream),
)
session.add(purchase)
for i, cid_data in enumerate(cid_datas):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from sqlalchemy import BigInteger, Column, DateTime, Integer, text
from sqlalchemy import BigInteger, Column, DateTime, Enum, Integer, text
from sqlalchemy.dialects.postgresql import JSONB

from src.models.base import Base
from src.models.model_utils import RepresentableMixin
from src.models.users.usdc_purchase import PurchaseAccessType


class TrackPriceHistory(Base, RepresentableMixin):
Expand All @@ -11,6 +12,7 @@ class TrackPriceHistory(Base, RepresentableMixin):
track_id = Column(Integer, nullable=False, primary_key=True)
splits = Column(JSONB(), nullable=False)
total_price_cents = Column(BigInteger, nullable=False)
access = Column(Enum(PurchaseAccessType), nullable=False)
blocknumber = Column(BigInteger, nullable=False)
block_timestamp = Column(DateTime, nullable=False, primary_key=True)
created_at = Column(
Expand Down
6 changes: 6 additions & 0 deletions packages/discovery-provider/src/models/users/usdc_purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ class PurchaseType(str, enum.Enum):
album = "album"


class PurchaseAccessType(str, enum.Enum):
stream = "stream"
download = "download"


class USDCPurchase(Base, RepresentableMixin):
__tablename__ = "usdc_purchases"

Expand All @@ -23,6 +28,7 @@ class USDCPurchase(Base, RepresentableMixin):
extra_amount = Column(BigInteger, nullable=False, server_default=text("0"))
content_type = Column(Enum(PurchaseType), nullable=False, index=True)
content_id = Column(Integer, nullable=False)
access = Column(Enum(PurchaseAccessType), nullable=False)

created_at = Column(
DateTime, nullable=False, index=True, server_default=text("CURRENT_TIMESTAMP")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from src.models.tracks.track import Track
from src.models.tracks.track_price_history import TrackPriceHistory
from src.models.tracks.track_route import TrackRoute
from src.models.users.usdc_purchase import PurchaseAccessType
from src.models.users.user import User
from src.tasks.entity_manager.utils import (
CHARACTER_LIMIT_DESCRIPTION,
Expand Down Expand Up @@ -95,6 +96,7 @@ def update_track_price_history(
new_record.block_timestamp = timestamp
new_record.blocknumber = blocknumber
new_record.splits = {}
new_record.access = PurchaseAccessType.stream
if "price" in usdc_purchase:
price = usdc_purchase["price"]
if isinstance(price, int):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
from src.models.tracks.track import Track
from src.models.tracks.track_price_history import TrackPriceHistory
from src.models.users.payment_router import PaymentRouterTx
from src.models.users.usdc_purchase import PurchaseType, USDCPurchase
from src.models.users.usdc_purchase import (
PurchaseAccessType,
PurchaseType,
USDCPurchase,
)
from src.models.users.usdc_transactions_history import (
USDCTransactionMethod,
USDCTransactionsHistory,
Expand Down Expand Up @@ -337,6 +341,7 @@ def index_purchase(
extra_amount=extra_amount,
content_type=purchase_metadata["type"],
content_id=purchase_metadata["id"],
access=PurchaseAccessType.stream,
)
logger.debug(
f"index_payment_router.py | tx: {tx_sig} | Creating usdc_purchase for purchase {usdc_purchase}"
Expand Down
7 changes: 6 additions & 1 deletion packages/discovery-provider/src/tasks/index_user_bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
TransactionMethod,
TransactionType,
)
from src.models.users.usdc_purchase import PurchaseType, USDCPurchase
from src.models.users.usdc_purchase import (
PurchaseAccessType,
PurchaseType,
USDCPurchase,
)
from src.models.users.usdc_transactions_history import (
USDCTransactionMethod,
USDCTransactionsHistory,
Expand Down Expand Up @@ -412,6 +416,7 @@ def index_purchase(
extra_amount=extra_amount,
content_type=purchase_metadata["type"],
content_id=purchase_metadata["id"],
access=PurchaseAccessType.stream,
)
logger.debug(
f"index_user_bank.py | Creating usdc_purchase for purchase {usdc_purchase}"
Expand Down

0 comments on commit c633c27

Please sign in to comment.