Skip to content

Commit

Permalink
[PAY-2221] Purchase content through payment router (#6880)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharit-tan authored Dec 8, 2023
1 parent 5c56fcb commit 976b787
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 8 deletions.
1 change: 1 addition & 0 deletions dev-tools/compose/docker-compose.identity.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ services:
solanaRewardsManagerProgramId: '${SOLANA_REWARD_MANAGER_PUBLIC_KEY}'
solanaRewardsManagerProgramPDA: '${SOLANA_REWARD_MANAGER_PDA_PUBLIC_KEY}'
solanaRewardsManagerTokenPDA: '${SOLANA_REWARD_MANAGER_TOKEN_PDA_PUBLIC_KEY}'
solanaPaymentRouterProgramId: '${SOLANA_PAYMENT_ROUTER_PUBLIC_KEY}'
solanaAudiusAnchorDataProgramId: '${SOLANA_AUDIUS_DATA_PUBLIC_KEY}'
depends_on:
db:
Expand Down
6 changes: 5 additions & 1 deletion packages/common/src/services/audius-backend/AudiusBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ type AudiusBackendSolanaConfig = Partial<{
rewardsManagerProgramId: string
rewardsManagerProgramPda: string
rewardsManagerTokenPda: string
paymentRouterProgramId: string
solanaClusterEndpoint: string
solanaFeePayerAddress: string
solanaTokenAddress: string
Expand Down Expand Up @@ -311,6 +312,7 @@ export const audiusBackend = ({
rewardsManagerProgramId,
rewardsManagerProgramPda,
rewardsManagerTokenPda,
paymentRouterProgramId,
solanaClusterEndpoint,
solanaFeePayerAddress,
solanaTokenAddress,
Expand Down Expand Up @@ -776,7 +778,8 @@ export const audiusBackend = ({
!claimableTokenProgramAddress ||
!rewardsManagerProgramId ||
!rewardsManagerProgramPda ||
!rewardsManagerTokenPda
!rewardsManagerTokenPda ||
!paymentRouterProgramId
) {
return {
error: true
Expand All @@ -795,6 +798,7 @@ export const audiusBackend = ({
rewardsManagerProgramId,
rewardsManagerProgramPDA: rewardsManagerProgramPda,
rewardsManagerTokenPDA: rewardsManagerTokenPda,
paymentRouterProgramId,
useRelay: true
})
}
Expand Down
6 changes: 6 additions & 0 deletions packages/identity-service/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,12 @@ const config = convict({
default: '',
env: 'solanaRewardsManagerTokenPDA'
},
solanaPaymentRouterProgramId: {
doc: 'The address of our Payment Router program',
format: String,
default: '',
env: 'solanaPaymentRouterProgramId'
},
solanaConfirmationTimeout: {
doc: 'The timeout used to send solana transactions through solanaWeb3 connection in ms',
format: Number,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
decodeInstruction,
isCloseAccountInstruction,
isTransferCheckedInstruction,
isSyncNativeInstruction
isSyncNativeInstruction,
getAssociatedTokenAddressSync
} from '@solana/spl-token'
import {
PublicKey,
Expand Down Expand Up @@ -36,6 +37,9 @@ const CLAIMABLE_TOKEN_PROGRAM_ID: string = config.get(
const REWARDS_MANAGER_PROGRAM_ID: string = config.get(
'solanaRewardsManagerProgramId'
)
const PAYMENT_ROUTER_PROGRAM_ID: string = config.get(
'solanaPaymentRouterProgramId'
)
const JUPITER_AGGREGATOR_V6_PROGRAM_ID =
'JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4'

Expand All @@ -55,6 +59,17 @@ const claimableTokenAuthorities = [audioMintAddress, usdcMintAddress].reduce(
{} as Record<string, PublicKey>
)

const [paymentRouterPda, _] = PublicKey.findProgramAddressSync(
[Buffer.from('payment_router')],
new PublicKey(PAYMENT_ROUTER_PROGRAM_ID)
)

const paymentRouterUSDCTokenAccount = getAssociatedTokenAddressSync(
new PublicKey(usdcMintAddress),
paymentRouterPda,
true
)

/**
* Only allow the createTokenAccount instruction of the Associated Token
* Account program, provided it has matching close instructions.
Expand Down Expand Up @@ -144,7 +159,12 @@ const assertAllowedTokenProgramInstruction = async (
const userbank = await (
await audiusLibsWrapper.getAudiusLibsAsync()
).solanaWeb3Manager!.deriveUserBank({ ethAddress: wallet, mint: 'usdc' })
if (!destination.equals(userbank)) {

// Check that destination is either a userbank or a payment router token account
if (
!destination.equals(userbank) &&
!destination.equals(paymentRouterUSDCTokenAccount)
) {
throw new InvalidRelayInstructionError(
instructionIndex,
`Invalid destination account: ${destination.toBase58()}`
Expand Down Expand Up @@ -418,6 +438,7 @@ export const assertRelayAllowedInstructions = async (
)
break
case Secp256k1Program.programId.toBase58():
case PAYMENT_ROUTER_PROGRAM_ID:
case MEMO_PROGRAM_ID:
// All instructions of these programs are allowed
break
Expand Down
2 changes: 2 additions & 0 deletions packages/libs/src/AudiusLibs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export class AudiusLibs {
rewardsManagerProgramId,
rewardsManagerProgramPDA,
rewardsManagerTokenPDA,
paymentRouterProgramId,
useRelay,
feePayerSecretKeys,
confirmationTimeout
Expand All @@ -275,6 +276,7 @@ export class AudiusLibs {
rewardsManagerProgramId,
rewardsManagerProgramPDA,
rewardsManagerTokenPDA,
paymentRouterProgramId,
useRelay,
feePayerKeypairs: feePayerSecretKeys?.map((key) =>
Keypair.fromSecretKey(key)
Expand Down
2 changes: 2 additions & 0 deletions packages/libs/src/NativeAudiusLibs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export class AudiusLibs {
rewardsManagerProgramId,
rewardsManagerProgramPDA,
rewardsManagerTokenPDA,
paymentRouterProgramId,
useRelay,
feePayerSecretKeys,
confirmationTimeout
Expand All @@ -255,6 +256,7 @@ export class AudiusLibs {
rewardsManagerProgramId,
rewardsManagerProgramPDA,
rewardsManagerTokenPDA,
paymentRouterProgramId,
useRelay,
feePayerKeypairs: feePayerSecretKeys?.map((key) =>
Keypair.fromSecretKey(key)
Expand Down
2 changes: 2 additions & 0 deletions packages/libs/src/WebAudiusLibs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export class AudiusLibs {
rewardsManagerProgramId,
rewardsManagerProgramPDA,
rewardsManagerTokenPDA,
paymentRouterProgramId,
useRelay,
feePayerSecretKeys,
confirmationTimeout
Expand All @@ -269,6 +270,7 @@ export class AudiusLibs {
rewardsManagerProgramId,
rewardsManagerProgramPDA,
rewardsManagerTokenPDA,
paymentRouterProgramId,
useRelay,
feePayerKeypairs: feePayerSecretKeys?.map((key) =>
Keypair.fromSecretKey(key)
Expand Down
1 change: 1 addition & 0 deletions packages/libs/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const AUDIO_DECIMALS = 18
export const WAUDIO_DECIMALS = 8
export const USDC_DECIMALS = 6
export const CURRENT_USER_EXISTS_LOCAL_STORAGE_KEY = '@audius/libs:found-user'
export enum AuthHeaders {
MESSAGE = 'Encoded-Data-Message',
Expand Down
Loading

0 comments on commit 976b787

Please sign in to comment.