Skip to content

Commit

Permalink
Migrate createTransferToUserBankTransaction to SDK (#10783)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyrombo authored Dec 19, 2024
1 parent 45c0558 commit 6e5ab0d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 57 deletions.
36 changes: 12 additions & 24 deletions packages/common/src/services/audius-backend/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ export const MEMO_PROGRAM_ID = new PublicKey(
'Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo'
)

const MINT_DECIMALS: Record<MintName, number> = {
wAUDIO: 8,
USDC: 6
}

// TODO: Import from libs https://linear.app/audius/issue/PAY-1750/export-mintname-and-default-mint-from-libs
export type MintName = 'wAUDIO' | 'USDC'
export const DEFAULT_MINT: MintName = 'wAUDIO'
Expand Down Expand Up @@ -414,33 +409,26 @@ export const decorateCoinflowWithdrawalTransaction = async (
}

export const createTransferToUserBankTransaction = async (
audiusBackendInstance: AudiusBackend,
sdk: AudiusSdk,
{
userBank,
wallet,
amount,
memo,
mint = 'wAUDIO',
recentBlockhash,
feePayer
mintPublicKey,
mintDecimals
}: {
userBank: PublicKey
wallet: Keypair
amount: bigint
memo: string
mint?: MintName
feePayer?: PublicKey
recentBlockhash?: string
mintPublicKey: PublicKey
mintDecimals: number
}
) => {
const libs = await audiusBackendInstance.getAudiusLibsTyped()
const mintPublicKey = libs.solanaWeb3Manager!.mints[mint]
const associatedTokenAccount = await findAssociatedTokenAddress(
audiusBackendInstance,
{
solanaAddress: wallet.publicKey.toBase58(),
mint
}
const associatedTokenAccount = getAssociatedTokenAddressSync(
mintPublicKey,
wallet.publicKey
)
// See: https://github.com/solana-labs/solana-program-library/blob/d6297495ea4dcc1bd48f3efdd6e3bbdaef25a495/memo/js/src/index.ts#L27
const memoInstruction = new TransactionInstruction({
Expand All @@ -460,11 +448,11 @@ export const createTransferToUserBankTransaction = async (
userBank, // destination
wallet.publicKey, // owner
amount, // amount
MINT_DECIMALS[mint] // decimals
mintDecimals // decimals
)
const tx = new Transaction({ recentBlockhash, feePayer })
tx.add(memoInstruction)
tx.add(transferInstruction)
const tx = sdk.services.solanaClient.buildTransaction({
instructions: [memoInstruction, transferInstruction]
})
return tx
}

Expand Down
47 changes: 14 additions & 33 deletions packages/common/src/store/buy-usdc/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import {
createPaymentRouterRouteTransaction,
createTransferToUserBankTransaction,
findAssociatedTokenAddress,
getRecentBlockhash,
getTokenAccountInfo,
getUserbankAccountInfo,
pollForTokenBalanceChange,
recoverUsdcFromRootWallet,
relayTransaction
recoverUsdcFromRootWallet
} from '~/services/audius-backend/solana'
import {
getAccountUser,
Expand Down Expand Up @@ -170,49 +168,32 @@ function* transferStep({
usePaymentRouter?: boolean
memo: string
}) {
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
const feePayer = yield* select(getFeePayer)
if (!feePayer) {
throw new Error('Missing feePayer unexpectedly')
}
const feePayerOverride = new PublicKey(feePayer)
const sdk = yield* getSDK()
const recentBlockhash = yield* call(getRecentBlockhash, { sdk })
const { USDC_MINT_ADDRESS } = yield* getContext('env')
const mintPublicKey = new PublicKey(USDC_MINT_ADDRESS)
const mintDecimals = USDC(0).decimalPlaces

yield* call(
retry,
async () => {
console.debug(`Starting transfer transaction...`)
const transferTransaction = await createTransferToUserBankTransaction(
audiusBackendInstance,
sdk,
{
wallet,
userBank,
mint: 'USDC',
mintPublicKey,
mintDecimals,
amount,
memo,
feePayer: feePayerOverride,
recentBlockhash
memo
}
)
transferTransaction.partialSign(wallet)

console.debug(`Starting transfer transaction...`)
const { res, error } = await relayTransaction(audiusBackendInstance, {
transaction: transferTransaction
})

if (res) {
console.debug(`Transfer transaction succeeded: ${res}`)
return
}

console.debug(
`Transfer transaction stringified: ${JSON.stringify(
transferTransaction
)}`
transferTransaction.sign([wallet])
const res = await sdk.services.solanaClient.sendTransaction(
transferTransaction
)
// Throw to retry
throw new Error(error ?? 'Unknown USDC user bank transfer error')

console.debug(`Transfer transaction succeeded: ${res}`)
},
{
minTimeout: retryDelayMs,
Expand Down

0 comments on commit 6e5ab0d

Please sign in to comment.