Skip to content

Commit

Permalink
[PAY-2691] Integrate purchase track in SDK (#8409)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyrombo authored May 22, 2024
1 parent cede382 commit 6b77385
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 110 deletions.
7 changes: 4 additions & 3 deletions packages/commands/src/purchase-content.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ program
program.command('purchase-track')
.description('Buys a track using USDC')
.argument('<id>', 'The track ID')
.argument('<price>', 'The expected price of the track', parseFloat)
.option('-f, --from [from]', 'The account purchasing the content (handle)')
.option(
'-e, --extra-amount [amount]',
'Extra amount to pay in addition to the price (in dollars)'
, parseFloat)
.action(async (id, { from, extraAmount }) => {
.action(async (id, price, { from, extraAmount }) => {
const audiusLibs = await initializeAudiusLibs(from)
const userIdNumber = audiusLibs.userStateManager.getCurrentUserId()
const userId = Utils.encodeHashId(userIdNumber)
Expand All @@ -107,8 +108,8 @@ program.command('purchase-track')
const audiusSdk = await initializeAudiusSdk({ apiKey: pubKey, apiSecret: privKey })

try {
console.log('Purchasing track...', { trackId, userId, extraAmount })
const response = await audiusSdk.tracks.purchase({ trackId, userId, extraAmount })
console.log('Purchasing track...', { trackId, userId, price, extraAmount })
const response = await audiusSdk.tracks.purchase({ trackId, userId, price, extraAmount })
console.log(chalk.green('Successfully purchased track'))
console.log(chalk.yellow('Transaction Signature:'), response)
} catch (err) {
Expand Down
68 changes: 42 additions & 26 deletions packages/common/src/hooks/useCoinflowAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { TransactionHandler } from '@audius/sdk/dist/core'
import { Connection, PublicKey, Transaction } from '@solana/web3.js'
import { useSelector } from 'react-redux'

import { useAudiusQueryContext } from '~/audius-query'
import { useAppContext } from '~/context'
import { Name } from '~/models/Analytics'
import { FeatureFlags } from '~/services'
import {
decorateCoinflowWithdrawalTransaction,
relayTransaction,
getRootSolanaAccount
} from '~/services/audius-backend'
import { getFeePayer } from '~/store/solana/selectors'

import { useFeatureFlag } from './useFeatureFlag'

type CoinflowAdapter = {
wallet: {
publicKey: PublicKey
Expand Down Expand Up @@ -103,6 +107,10 @@ export const useCoinflowWithdrawalAdapter = () => {
export const useCoinflowAdapter = () => {
const { audiusBackend } = useAppContext()
const [adapter, setAdapter] = useState<CoinflowAdapter | null>(null)
const { isEnabled: isUseSDKPurchaseTrackEnabled } = useFeatureFlag(
FeatureFlags.USE_SDK_PURCHASE_TRACK
)
const { audiusSdk } = useAudiusQueryContext()

useEffect(() => {
const initWallet = async () => {
Expand All @@ -116,40 +124,48 @@ export const useCoinflowAdapter = () => {
publicKey: wallet.publicKey,
sendTransaction: async (transaction: Transaction) => {
transaction.partialSign(wallet)
const transactionHandler = new TransactionHandler({
connection,
useRelay: false
})
const { res, error, errorCode } =
await transactionHandler.handleTransaction({
instructions: transaction.instructions,
recentBlockhash: transaction.recentBlockhash,
skipPreflight: true,
feePayerOverride: transaction.feePayer,
signatures: transaction.signatures.map((s) => ({
signature: s.signature!, // already completely signed
publicKey: s.publicKey.toBase58()
}))
})
if (!res) {
console.error('Sending Coinflow transaction failed.', {
error,
errorCode,
if (isUseSDKPurchaseTrackEnabled) {
const sdk = await audiusSdk()
const { signature } = await sdk.services.solanaRelay.relay({
transaction
})
throw new Error(
`Sending Coinflow transaction failed: ${
error ?? 'Unknown error'
}`
)
return signature
} else {
const transactionHandler = new TransactionHandler({
connection,
useRelay: false
})
const { res, error, errorCode } =
await transactionHandler.handleTransaction({
instructions: transaction.instructions,
recentBlockhash: transaction.recentBlockhash,
skipPreflight: true,
feePayerOverride: transaction.feePayer,
signatures: transaction.signatures.map((s) => ({
signature: s.signature!, // already completely signed
publicKey: s.publicKey.toBase58()
}))
})
if (!res) {
console.error('Sending Coinflow transaction failed.', {
error,
errorCode,
transaction
})
throw new Error(
`Sending Coinflow transaction failed: ${
error ?? 'Unknown error'
}`
)
}
return res
}
return res
}
}
})
}
initWallet()
}, [audiusBackend])
}, [audiusBackend, isUseSDKPurchaseTrackEnabled, audiusSdk])

return adapter
}
6 changes: 4 additions & 2 deletions packages/common/src/services/remote-config/feature-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export enum FeatureFlags {
USE_SDK_REWARDS = 'use_sdk_rewards',
DISCOVERY_TIP_REACTIONS = 'discovery_tip_reactions',
USE_ADDRESS_LOOKUPS = 'use_address_lookups',
MANAGER_MODE = 'manager_mode'
MANAGER_MODE = 'manager_mode',
USE_SDK_PURCHASE_TRACK = 'use_sdk_purchase_track'
}

type FlagDefaults = Record<FeatureFlags, boolean>
Expand Down Expand Up @@ -140,5 +141,6 @@ export const flagDefaults: FlagDefaults = {
[FeatureFlags.USE_SDK_REWARDS]: false,
[FeatureFlags.DISCOVERY_TIP_REACTIONS]: false,
[FeatureFlags.USE_ADDRESS_LOOKUPS]: false,
[FeatureFlags.MANAGER_MODE]: false
[FeatureFlags.MANAGER_MODE]: false,
[FeatureFlags.USE_SDK_PURCHASE_TRACK]: false
}
Loading

0 comments on commit 6b77385

Please sign in to comment.