From ab24471d7bc395b4f055e572e18b45bcec77f655 Mon Sep 17 00:00:00 2001 From: Reed <3893871+dharit-tan@users.noreply.github.com> Date: Mon, 11 Dec 2023 18:25:41 -0500 Subject: [PATCH] Add memo for purchaser user id (#6846) Co-authored-by: Raymond Jacobson --- packages/commands/src/purchase-track.mjs | 5 ++++- packages/common/src/services/audius-backend/solana.ts | 5 +++-- packages/common/src/store/purchase-content/sagas.ts | 11 ++++++++--- .../libs/src/services/solana/SolanaWeb3Manager.ts | 9 +++++++-- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/commands/src/purchase-track.mjs b/packages/commands/src/purchase-track.mjs index 44d8e01e104..9be15f2e721 100644 --- a/packages/commands/src/purchase-track.mjs +++ b/packages/commands/src/purchase-track.mjs @@ -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') @@ -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)) diff --git a/packages/common/src/services/audius-backend/solana.ts b/packages/common/src/services/audius-backend/solana.ts index 15c09ebe546..069a373d7b7 100644 --- a/packages/common/src/services/audius-backend/solana.ts +++ b/packages/common/src/services/audius-backend/solana.ts @@ -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' @@ -351,11 +351,12 @@ export const pollForBalanceChange = async ( } export type PurchaseContentArgs = { - id: number + id: ID blocknumber: number extraAmount?: number | BN type: 'track' splits: Record + purchaserUserId: ID } export const purchaseContent = async ( audiusBackendInstance: AudiusBackend, diff --git a/packages/common/src/store/purchase-content/sagas.ts b/packages/common/src/store/purchase-content/sagas.ts index c905331b568..1c63778b11c 100644 --- a/packages/common/src/store/purchase-content/sagas.ts +++ b/packages/common/src/store/purchase-content/sagas.ts @@ -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, @@ -316,7 +320,8 @@ function* doStartPurchaseContentFlow({ blocknumber, extraAmount: extraAmountBN, splits, - type: 'track' + type: 'track', + purchaserUserId }) yield* put(purchaseSucceeded()) diff --git a/packages/libs/src/services/solana/SolanaWeb3Manager.ts b/packages/libs/src/services/solana/SolanaWeb3Manager.ts index da94a5ffff6..9f1810e9815 100644 --- a/packages/libs/src/services/solana/SolanaWeb3Manager.ts +++ b/packages/libs/src/services/solana/SolanaWeb3Manager.ts @@ -512,6 +512,7 @@ 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({ @@ -519,13 +520,15 @@ export class SolanaWeb3Manager { type, blocknumber, extraAmount = 0, - splits + splits, + purchaserUserId }: { id: number type: 'track' splits: Record extraAmount?: number | BN blocknumber: number + purchaserUserId: number }) { if (!this.web3Manager) { throw new Error( @@ -565,6 +568,8 @@ export class SolanaWeb3Manager { mintKey: this.mints.usdc }) + const data = `${type}:${id}:${blocknumber}:${purchaserUserId}` + const memoInstruction = new TransactionInstruction({ keys: [ { @@ -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],