Skip to content

Commit d4d83f4

Browse files
authored
Use new format in stream/download conditions for usdc_purchase (#13724)
updates splits format
1 parent e2caf27 commit d4d83f4

13 files changed

Lines changed: 135 additions & 106 deletions

File tree

packages/common/src/adapters/accessConditionsFromSDK.ts

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AccessGate } from '@audius/sdk'
22
import {
3-
instanceOfExtendedPurchaseGate,
43
instanceOfFollowGate,
4+
instanceOfPurchaseGate,
55
instanceOfTokenGate
66
} from '@audius/sdk'
77

@@ -13,35 +13,23 @@ export const accessConditionsFromSDK = (
1313
): AccessConditions | null => {
1414
if (instanceOfFollowGate(input)) {
1515
return { follow_user_id: input.followUserId }
16-
} else if (instanceOfExtendedPurchaseGate(input)) {
17-
const purchase = input.usdcPurchase
18-
const splits = Array.isArray(purchase.splits)
19-
? purchase.splits.map((s) => ({
20-
user_id: s.userId,
21-
percentage: s.percentage,
22-
payout_wallet: s.payoutWallet,
23-
amount: s.amount,
24-
...(s.ethWallet != null && { eth_wallet: s.ethWallet })
25-
}))
26-
: []
27-
const albumTrackPrice = (purchase as { albumTrackPrice?: number })
28-
.albumTrackPrice
29-
return {
30-
usdc_purchase: {
31-
price: purchase.price,
32-
...(albumTrackPrice != null && { albumTrackPrice }),
33-
splits
34-
}
35-
}
3616
} else if (instanceOfTokenGate(input)) {
3717
return {
3818
token_gate: {
3919
token_mint: input.tokenGate.tokenMint,
4020
token_amount: input.tokenGate.tokenAmount
4121
}
4222
}
43-
} else if ('nftCollection' in input && input.nftCollection != null) {
44-
return null
23+
} else if (instanceOfPurchaseGate(input)) {
24+
return {
25+
usdc_purchase: {
26+
price: input.usdcPurchase.price,
27+
splits: input.usdcPurchase.splits.map((s) => ({
28+
user_id: s.userId,
29+
percentage: s.percentage
30+
}))
31+
}
32+
}
4533
} else {
4634
throw new Error(`Unsupported access gate type: ${JSON.stringify(input)}`)
4735
}

packages/common/src/adapters/accessConditionsToSDK.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const usdcPurchaseConditionsToSDK = (
2828
ethWallet?: string
2929
}
3030
) => ({
31-
userId: s.user_id ?? s.userId,
31+
userId: s.user_id,
3232
percentage: s.percentage,
3333
payoutWallet: s.payout_wallet ?? s.payoutWallet ?? '',
3434
amount: s.amount,

packages/common/src/api/tan-query/upload/usePublishCollection.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ type PublishCollectionContext = Pick<
3636
'audiusSdk' | 'analytics' | 'dispatch' | 'reportToSentry'
3737
> & {
3838
userId: number
39-
wallet: string
4039
}
4140

4241
type PublishCollectionParams = {
@@ -52,15 +51,11 @@ type PublishCollectionParams = {
5251
const getPublishCollectionOptions = (context: PublishCollectionContext) =>
5352
mutationOptions({
5453
mutationFn: async (params: PublishCollectionParams) => {
55-
const { audiusSdk, userId, wallet } = context
54+
const { audiusSdk, userId } = context
5655
const sdk = await audiusSdk()
57-
if (!userId || !wallet) {
56+
if (!userId) {
5857
throw new Error('User ID and wallet are required to publish collection')
5958
}
60-
const userBank = await sdk.services.claimableTokensClient.deriveUserBank({
61-
ethWallet: wallet,
62-
mint: 'USDC'
63-
})
6459

6560
// If the collection is a premium album, this will populate the premium metadata (price/splits/etc)
6661
let albumTrackPrice: number | undefined
@@ -73,15 +68,15 @@ const getPublishCollectionOptions = (context: PublishCollectionContext) =>
7368
params.collectionMetadata.stream_conditions?.usdc_purchase
7469
.albumTrackPrice ?? undefined
7570
params.collectionMetadata.stream_conditions = getUSDCMetadata(
76-
userBank.toString(),
71+
userId,
7772
params.collectionMetadata.stream_conditions
7873
)
7974
}
8075

8176
// Combine collection metadata into each track's metadata
8277
for (const track of params.tracks) {
8378
track.metadata = combineMetadata(
84-
userBank.toString(),
79+
userId,
8580
track.metadata,
8681
params.collectionMetadata,
8782
albumTrackPrice
@@ -145,14 +140,12 @@ export const usePublishCollection = (
145140
const { data: account = null } = useCurrentAccount()
146141
const { data: accountUser } = useCurrentAccountUser()
147142
const userId = account?.userId ?? undefined
148-
const wallet = account?.walletAddresses.currentUser ?? undefined
149143

150144
return useMutation({
151145
...options,
152146
...getPublishCollectionOptions({
153147
audiusSdk,
154148
userId: userId!,
155-
wallet: wallet!,
156149
dispatch,
157150
analytics,
158151
reportToSentry
@@ -214,7 +207,7 @@ export const usePublishCollection = (
214207
* taking the metadata from the playlist when the track is missing it.
215208
*/
216209
function combineMetadata(
217-
userBank: string,
210+
userId: number,
218211
trackMetadata: TrackMetadataForUpload,
219212
collectionMetadata: CollectionValues,
220213
albumTrackPrice?: number
@@ -269,7 +262,7 @@ function combineMetadata(
269262
usdc_purchase: { price: albumTrackPrice, splits: [] }
270263
}
271264
// Add splits to stream & download conditions
272-
addPremiumMetadata(userBank, metadata)
265+
addPremiumMetadata(userId, metadata)
273266
}
274267
return metadata
275268
}

packages/common/src/api/tan-query/upload/usePublishTracks.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { USDC } from '@audius/fixed-decimal'
21
import { HashId, Id, type UploadResponse } from '@audius/sdk'
32
import { useMutation, useQueryClient } from '@tanstack/react-query'
43

@@ -29,7 +28,6 @@ type PublishTracksContext = Pick<
2928
'audiusSdk' | 'analytics' | 'dispatch' | 'reportToSentry'
3029
> & {
3130
userId: number
32-
wallet: string
3331
kind?: 'tracks' | 'album' | 'playlist'
3432
}
3533

@@ -47,29 +45,22 @@ export const publishTracks = async (
4745
) => {
4846
const {
4947
userId,
50-
wallet,
5148
kind,
5249
audiusSdk,
5350
dispatch,
5451
reportToSentry,
5552
analytics: { make, track }
5653
} = context
5754

58-
if (!context.userId || !context.wallet) {
55+
if (!context.userId) {
5956
throw new Error('User ID and wallet are required to publish tracks')
6057
}
6158

6259
const sdk = await audiusSdk()
63-
const userBank = await sdk.services.claimableTokensClient.deriveUserBank({
64-
ethWallet: wallet,
65-
mint: 'USDC'
66-
})
60+
6761
return await Promise.all(
6862
params.map(async (param) => {
69-
const snakeMetadata = addPremiumMetadata(
70-
userBank.toString(),
71-
param.metadata
72-
)
63+
const snakeMetadata = addPremiumMetadata(userId, param.metadata)
7364

7465
const trackId = await sdk.tracks.generateTrackId()
7566
const camelMetadata = trackMetadataForUploadToSdk({
@@ -191,15 +182,13 @@ export const usePublishTracks = (
191182
const { data: account } = useCurrentAccount()
192183
const { data: accountUser } = useCurrentAccountUser()
193184
const userId = account?.userId ?? undefined
194-
const wallet = account?.walletAddresses.currentUser ?? undefined
195185
const kind = options?.kind ?? 'tracks'
196186

197187
return useMutation({
198188
...options,
199189
...getPublishTracksOptions({
200190
...queryContext,
201191
userId: userId!,
202-
wallet: wallet!,
203192
kind
204193
}),
205194
onSuccess: async (data) => {
@@ -235,11 +224,10 @@ export const usePublishTracks = (
235224
* returns updated conditions with price in WEI and splits in the new array format.
236225
*/
237226
export function getUSDCMetadata(
238-
userBank: string,
227+
userId: number,
239228
stream_conditions: USDCPurchaseConditions
240229
): USDCPurchaseConditions {
241230
const priceCents = stream_conditions.usdc_purchase.price
242-
const priceWei = Number(USDC(priceCents / 100).value.toString())
243231
return {
244232
usdc_purchase: {
245233
price: priceCents,
@@ -248,9 +236,8 @@ export function getUSDCMetadata(
248236
}),
249237
splits: [
250238
{
251-
payout_wallet: userBank?.toString() ?? '',
252-
percentage: 100,
253-
amount: priceWei
239+
user_id: userId,
240+
percentage: 100
254241
}
255242
]
256243
}
@@ -262,24 +249,21 @@ export function getUSDCMetadata(
262249
* Converts prices to WEI and adds splits for USDC purchasable content.
263250
*/
264251
export function addPremiumMetadata<T extends TrackMetadataForUpload>(
265-
userBank: string,
252+
userId: number,
266253
track: T
267254
) {
268255
// download_conditions could be set separately from stream_conditions, so we check for them first
269256
if (isContentUSDCPurchaseGated(track.download_conditions)) {
270257
track.download_conditions = getUSDCMetadata(
271-
userBank,
258+
userId,
272259
track.download_conditions
273260
)
274261
}
275262

276263
if (isContentUSDCPurchaseGated(track.stream_conditions)) {
277-
track.stream_conditions = getUSDCMetadata(userBank, track.stream_conditions)
264+
track.stream_conditions = getUSDCMetadata(userId, track.stream_conditions)
278265
// If stream_conditions are set, download_conditions should always match
279-
track.download_conditions = getUSDCMetadata(
280-
userBank,
281-
track.stream_conditions
282-
)
266+
track.download_conditions = getUSDCMetadata(userId, track.stream_conditions)
283267
}
284268

285269
return track

packages/common/src/models/Track.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ export type NftGatedConditions = {
6666

6767
/** Single split in USDC purchase conditions (matches API extended_payment_split). */
6868
export type PaymentSplit = {
69-
user_id?: number
69+
user_id: number
7070
percentage: number
71-
payout_wallet: string
72-
amount: number
71+
payout_wallet?: string
72+
amount?: number
7373
eth_wallet?: string
7474
}
7575

packages/common/src/schemas/upload/uploadFormSchema.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ const TokenGatedConditionsSchema = z
3737

3838
/** Same as API extended_payment_split (snake-cased) */
3939
const PaymentSplitSchema = z.object({
40-
user_id: z.optional(z.number()),
40+
user_id: z.number(),
4141
percentage: z.number().min(0).max(100),
42-
payout_wallet: z.string(),
43-
amount: z.number().nonnegative(),
42+
payout_wallet: z.string().optional(),
43+
amount: z.number().nonnegative().optional(),
4444
eth_wallet: z.optional(z.string())
4545
})
4646

packages/sdk/src/sdk/api/generated/default/.openapi-generator/FILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ models/NftGate.ts
209209
models/Notification.ts
210210
models/Notifications.ts
211211
models/NotificationsResponse.ts
212+
models/PaymentSplit.ts
212213
models/PinCommentRequestBody.ts
213214
models/Playlist.ts
214215
models/PlaylistAddedTimestamp.ts

packages/sdk/src/sdk/api/generated/default/models/AccessGate.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ import {
2020
FollowGateFromJSONTyped,
2121
FollowGateToJSON,
2222
} from './FollowGate';
23-
import {
24-
NftGate,
25-
instanceOfNftGate,
26-
NftGateFromJSON,
27-
NftGateFromJSONTyped,
28-
NftGateToJSON,
29-
} from './NftGate';
3023
import {
3124
PurchaseGate,
3225
instanceOfPurchaseGate,
@@ -54,7 +47,7 @@ import {
5447
*
5548
* @export
5649
*/
57-
export type AccessGate = FollowGate | NftGate | PurchaseGate | TipGate | TokenGate;
50+
export type AccessGate = FollowGate | PurchaseGate | TipGate | TokenGate;
5851

5952
export function AccessGateFromJSON(json: any): AccessGate {
6053
return AccessGateFromJSONTyped(json, false);
@@ -64,7 +57,7 @@ export function AccessGateFromJSONTyped(json: any, ignoreDiscriminator: boolean)
6457
if ((json === undefined) || (json === null)) {
6558
return json;
6659
}
67-
return { ...FollowGateFromJSONTyped(json, true), ...NftGateFromJSONTyped(json, true), ...PurchaseGateFromJSONTyped(json, true), ...TipGateFromJSONTyped(json, true), ...TokenGateFromJSONTyped(json, true) };
60+
return { ...FollowGateFromJSONTyped(json, true), ...PurchaseGateFromJSONTyped(json, true), ...TipGateFromJSONTyped(json, true), ...TokenGateFromJSONTyped(json, true) };
6861
}
6962

7063
export function AccessGateToJSON(value?: AccessGate | null): any {
@@ -78,9 +71,6 @@ export function AccessGateToJSON(value?: AccessGate | null): any {
7871
if (instanceOfFollowGate(value)) {
7972
return FollowGateToJSON(value as FollowGate);
8073
}
81-
if (instanceOfNftGate(value)) {
82-
return NftGateToJSON(value as NftGate);
83-
}
8474
if (instanceOfPurchaseGate(value)) {
8575
return PurchaseGateToJSON(value as PurchaseGate);
8676
}

0 commit comments

Comments
 (0)