-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PROTO-1813] Add geo to purchases (#8424)
- Loading branch information
1 parent
33157c6
commit f9c7705
Showing
5 changed files
with
208 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...provider/plugins/pedalboard/apps/solana-relay/src/routes/relay/attachLocationData.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { PaymentRouterProgram } from '@audius/spl' | ||
import { | ||
Keypair, | ||
PublicKey, | ||
TransactionInstruction, | ||
TransactionMessage, | ||
} from '@solana/web3.js' | ||
import { config } from '../../config' | ||
import { describe, it } from 'vitest' | ||
import { attachLocationData, isPaymentTransaction } from './attachLocationData' | ||
import assert from 'assert' | ||
|
||
const PAYMENT_ROUTER_PROGRAM_ID = new PublicKey(config.paymentRouterProgramId) | ||
const MEMO_V2_PROGRAM_ID = new PublicKey( | ||
'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr' | ||
) | ||
|
||
const getRandomPublicKey = () => Keypair.generate().publicKey | ||
|
||
describe('isPaymentTransaction', () => { | ||
it('returns true when instructions are a payment', async () => { | ||
const [pda, bump] = PublicKey.findProgramAddressSync( | ||
[new TextEncoder().encode('payment_router')], | ||
new PublicKey(PAYMENT_ROUTER_PROGRAM_ID) | ||
) | ||
const instructions = [ | ||
await PaymentRouterProgram.createRouteInstruction({ | ||
sender: getRandomPublicKey(), | ||
senderOwner: pda, | ||
paymentRouterPdaBump: bump, | ||
recipients: [getRandomPublicKey()], | ||
amounts: [BigInt(100)], | ||
totalAmount: BigInt(100), | ||
programId: PAYMENT_ROUTER_PROGRAM_ID | ||
}) | ||
] | ||
const isPayment = await isPaymentTransaction(instructions) | ||
assert.strictEqual(isPayment, true) | ||
}) | ||
|
||
it('returns false whe instructions are a recovery', async () => { | ||
const [pda, bump] = PublicKey.findProgramAddressSync( | ||
[new TextEncoder().encode('payment_router')], | ||
new PublicKey(PAYMENT_ROUTER_PROGRAM_ID) | ||
) | ||
const instructions = [ | ||
await PaymentRouterProgram.createRouteInstruction({ | ||
sender: getRandomPublicKey(), | ||
senderOwner: pda, | ||
paymentRouterPdaBump: bump, | ||
recipients: [getRandomPublicKey()], | ||
amounts: [BigInt(100)], | ||
totalAmount: BigInt(100), | ||
programId: PAYMENT_ROUTER_PROGRAM_ID | ||
}), | ||
new TransactionInstruction({ | ||
keys: [{ pubkey: getRandomPublicKey(), isSigner: true, isWritable: true }], | ||
data: Buffer.from('Recover Withdrawal'), | ||
programId: MEMO_V2_PROGRAM_ID | ||
}) | ||
] | ||
const isPayment = await isPaymentTransaction(instructions) | ||
assert.strictEqual(isPayment, false) | ||
}) | ||
}) | ||
|
||
describe('attachLocationData', () => { | ||
it('attaches a properly formatted geo instruction', async () => { | ||
const payerKey = getRandomPublicKey() | ||
const recentBlockhash = '5D1qXoiJqhYYJTyVCWijs9zX9n2AoZbcbLviHHZ1U14L' | ||
const [pda, bump] = PublicKey.findProgramAddressSync( | ||
[new TextEncoder().encode('payment_router')], | ||
new PublicKey(PAYMENT_ROUTER_PROGRAM_ID) | ||
) | ||
const instructions = [ | ||
await PaymentRouterProgram.createRouteInstruction({ | ||
sender: getRandomPublicKey(), | ||
senderOwner: pda, | ||
paymentRouterPdaBump: bump, | ||
recipients: [getRandomPublicKey()], | ||
amounts: [BigInt(100)], | ||
totalAmount: BigInt(100), | ||
programId: PAYMENT_ROUTER_PROGRAM_ID | ||
}), | ||
new TransactionInstruction({ | ||
keys: [{ pubkey: getRandomPublicKey(), isSigner: true, isWritable: true }], | ||
data: Buffer.from('Some random memo'), | ||
programId: MEMO_V2_PROGRAM_ID | ||
}) | ||
] | ||
const message = new TransactionMessage({ | ||
payerKey, | ||
recentBlockhash, | ||
instructions | ||
}) | ||
const updatedMessage = attachLocationData({ | ||
transactionMessage: message, | ||
location: {city: 'Nashville', region: 'TN', country: 'United States'} | ||
}) | ||
assert.strictEqual(updatedMessage.instructions.length, 3) | ||
|
||
assert.strictEqual(updatedMessage.instructions[0], instructions[0]) | ||
assert.strictEqual(updatedMessage.instructions[1], instructions[1]) | ||
|
||
assert.strictEqual( | ||
updatedMessage.instructions[2].data.toString(), | ||
'geo:{"city":"Nashville","region":"TN","country":"United States"}' | ||
) | ||
}) | ||
}) |
69 changes: 69 additions & 0 deletions
69
...very-provider/plugins/pedalboard/apps/solana-relay/src/routes/relay/attachLocationData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
PublicKey, | ||
TransactionInstruction, | ||
TransactionMessage | ||
} from '@solana/web3.js' | ||
import { config } from '../../config' | ||
import { LocationData } from '../../utils/ipData' | ||
|
||
const MEMO_PROGRAM_ID = 'Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo' | ||
const MEMO_V2_PROGRAM_ID = 'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr' | ||
const PAYMENT_ROUTER_PROGRAM_ID = config.paymentRouterProgramId | ||
|
||
const RECOVERY_MEMO_STRING = "Recover Withdrawal" | ||
const GEO_MEMO_STRING = "geo" | ||
|
||
/** | ||
* Determines whether or not the provided instructions are from a payment transaction | ||
* @param instructions the instructions to check | ||
* @returns boolean | ||
*/ | ||
export const isPaymentTransaction = ( | ||
instructions: TransactionInstruction[] | ||
) => { | ||
let usesPaymentRouter = false | ||
let isRecovery = false | ||
for (let i = 0; i < instructions.length; i++) { | ||
const instruction = instructions[i] | ||
switch (instruction.programId.toBase58()) { | ||
case PAYMENT_ROUTER_PROGRAM_ID: | ||
usesPaymentRouter = true | ||
break | ||
case MEMO_PROGRAM_ID: | ||
case MEMO_V2_PROGRAM_ID: { | ||
const memo = instruction.data.toString() | ||
if (memo === RECOVERY_MEMO_STRING) { | ||
isRecovery = true | ||
} | ||
break | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
return usesPaymentRouter && !isRecovery | ||
} | ||
|
||
/** | ||
* Attaches location data to a TransactionMessage | ||
*/ | ||
export const attachLocationData = ( | ||
{ transactionMessage, location }: | ||
{ | ||
transactionMessage: TransactionMessage, | ||
location: LocationData | ||
} | ||
): TransactionMessage => { | ||
const memoInstruction = new TransactionInstruction({ | ||
keys: [{ pubkey: transactionMessage.payerKey, isSigner: true, isWritable: true }], | ||
data: Buffer.from(`${GEO_MEMO_STRING}:${JSON.stringify(location)}`, 'utf-8'), | ||
programId: new PublicKey(MEMO_V2_PROGRAM_ID) | ||
}) | ||
const updatedInstructions = [...transactionMessage.instructions, memoInstruction] | ||
const msg = new TransactionMessage({ | ||
payerKey: transactionMessage.payerKey, | ||
recentBlockhash: transactionMessage.recentBlockhash, | ||
instructions: updatedInstructions | ||
}) | ||
return msg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters