Skip to content

Commit

Permalink
Add memo for purchaser user id (#6846)
Browse files Browse the repository at this point in the history
Co-authored-by: Raymond Jacobson <ray@audius.co>
  • Loading branch information
dharit-tan and raymondjacobson authored Dec 11, 2023
1 parent 68d9f71 commit ab24471
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/commands/src/purchase-track.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ program
)
.action(async (trackId, { from, extraAmount: extraAmountCents }) => {
const audiusLibs = await initializeAudiusLibs(from)
const user = audiusLibs.userStateManager.getCurrentUser();

const track = (await audiusLibs.Track.getTracks(100, 0, [trackId]))[0]
if (!track.premium_conditions || !track.is_premium) {
program.error('Track is not premium')
Expand All @@ -37,7 +39,8 @@ program
extraAmount,
type: 'track',
blocknumber: track.blocknumber,
splits: track.premium_conditions.usdc_purchase.splits
splits: track.premium_conditions.usdc_purchase.splits,
purchaserUserId: user.user_id
})
if (response.error) {
program.error(chalk.red(response.error))
Expand Down
5 changes: 3 additions & 2 deletions packages/common/src/services/audius-backend/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@solana/web3.js'
import BN from 'bn.js'

import { AnalyticsEvent, Name, SolanaWalletAddress } from '../../models'
import { AnalyticsEvent, ID, Name, SolanaWalletAddress } from '../../models'

import { AudiusBackend } from './AudiusBackend'

Expand Down Expand Up @@ -351,11 +351,12 @@ export const pollForBalanceChange = async (
}

export type PurchaseContentArgs = {
id: number
id: ID
blocknumber: number
extraAmount?: number | BN
type: 'track'
splits: Record<string, number | BN>
purchaserUserId: ID
}
export const purchaseContent = async (
audiusBackendInstance: AudiusBackend,
Expand Down
11 changes: 8 additions & 3 deletions packages/common/src/store/purchase-content/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,13 @@ function* doStartPurchaseContentFlow({
)

try {
// get user bank
const userBank = yield* call(getUSDCUserBank)
// get user & user bank
const purchaserUserId = yield* select(getUserId)
if (!purchaserUserId) {
throw new Error('Failed to fetch purchasing user id')
}

const userBank = yield* call(getUSDCUserBank)
const tokenAccountInfo = yield* call(
getTokenAccountInfo,
audiusBackendInstance,
Expand Down Expand Up @@ -316,7 +320,8 @@ function* doStartPurchaseContentFlow({
blocknumber,
extraAmount: extraAmountBN,
splits,
type: 'track'
type: 'track',
purchaserUserId
})
yield* put(purchaseSucceeded())

Expand Down
9 changes: 7 additions & 2 deletions packages/libs/src/services/solana/SolanaWeb3Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,23 @@ export class SolanaWeb3Manager {
* @param params.blocknumber the blocknumber the content was last updated
* @param params.splits map of address to USDC amount, used to split the price amoung several stakeholders
* @param params.extraAmount Extra amount in USDC wei to be distributed to the stakeholders
* @param params.purchaserUserId Id of the user that is purchasing the track
* @returns the transaction signature and/or an error
*/
async purchaseContent({
id,
type,
blocknumber,
extraAmount = 0,
splits
splits,
purchaserUserId
}: {
id: number
type: 'track'
splits: Record<string, number | BN>
extraAmount?: number | BN
blocknumber: number
purchaserUserId: number
}) {
if (!this.web3Manager) {
throw new Error(
Expand Down Expand Up @@ -565,6 +568,8 @@ export class SolanaWeb3Manager {
mintKey: this.mints.usdc
})

const data = `${type}:${id}:${blocknumber}:${purchaserUserId}`

const memoInstruction = new TransactionInstruction({
keys: [
{
Expand All @@ -574,7 +579,7 @@ export class SolanaWeb3Manager {
}
],
programId: MEMO_PROGRAM_ID,
data: Buffer.from(`${type}:${id}:${blocknumber}`)
data: Buffer.from(data)
})
return await this.transactionHandler.handleTransaction({
instructions: [...instructions, memoInstruction],
Expand Down

0 comments on commit ab24471

Please sign in to comment.