Skip to content

Commit

Permalink
Migrate useCoinflowWithdrawalAdapter to SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyrombo committed Dec 18, 2024
1 parent 8ea4716 commit 1515b7d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 56 deletions.
51 changes: 18 additions & 33 deletions packages/common/src/hooks/useCoinflowAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { useSelector, useDispatch } from 'react-redux'
import { useAudiusQueryContext } from '~/audius-query'
import { useAppContext } from '~/context'
import { Name } from '~/models/Analytics'
import {
decorateCoinflowWithdrawalTransaction,
relayTransaction
} from '~/services/audius-backend'
import { decorateCoinflowWithdrawalTransaction } from '~/services/audius-backend'
import {
BuyUSDCError,
PurchaseContentError,
Expand Down Expand Up @@ -68,9 +65,6 @@ export const useCoinflowWithdrawalAdapter = () => {
sendTransaction: async (
transaction: Transaction | VersionedTransaction
) => {
if (!feePayerOverride) {
throw new Error('Missing fee payer override')
}
if (transaction instanceof VersionedTransaction) {
throw new Error(
'VersionedTransaction not supported in withdrawal adapter'
Expand All @@ -79,49 +73,40 @@ export const useCoinflowWithdrawalAdapter = () => {
if (!currentUser) {
throw new Error('Missing current user')
}
const feePayer = new PublicKey(feePayerOverride)
const finalTransaction =
await decorateCoinflowWithdrawalTransaction(sdk, audiusBackend, {
ethAddress: currentUser,
transaction,
feePayer,
wallet
})
finalTransaction.partialSign(wallet)
const { res, error, errorCode } = await relayTransaction(
audiusBackend,
{
transaction: finalTransaction,
skipPreflight: true
}
)
if (!res) {
finalTransaction.sign([wallet])
try {
const res =
await sdk.services.claimableTokensClient.sendTransaction(
finalTransaction,
{ skipPreflight: true }
)
track(
make({
eventName: Name.WITHDRAW_USDC_COINFLOW_SEND_TRANSACTION,
signature: res
})
)
return res
} catch (error) {
console.error('Relaying Coinflow transaction failed.', {
error,
errorCode,
finalTransaction
})
track(
make({
eventName:
Name.WITHDRAW_USDC_COINFLOW_SEND_TRANSACTION_FAILED,
error: error ?? undefined,
errorCode: errorCode ?? undefined
error: (error as Error).message ?? undefined
})
)
throw new Error(
`Relaying Coinflow transaction failed: ${
error ?? 'Unknown error'
}`
)
throw error
}
track(
make({
eventName: Name.WITHDRAW_USDC_COINFLOW_SEND_TRANSACTION,
signature: res
})
)
return res
}
}
})
Expand Down
39 changes: 16 additions & 23 deletions packages/common/src/services/audius-backend/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,14 @@ export const decorateCoinflowWithdrawalTransaction = async (
audiusBackendInstance: AudiusBackend,
{
transaction,
feePayer,
ethAddress,
wallet
}: {
transaction: Transaction
feePayer: PublicKey
ethAddress: string
wallet: Keypair
}
) => {
const libs = await audiusBackendInstance.getAudiusLibsTyped()
const solanaWeb3Manager = libs.solanaWeb3Manager!

const userBank = await deriveUserBankPubkey(sdk, {
ethAddress,
mint: 'USDC'
Expand Down Expand Up @@ -369,15 +364,20 @@ export const decorateCoinflowWithdrawalTransaction = async (
data.decimals
)

const transferFromUserBankInstructions =
await solanaWeb3Manager.createTransferInstructionsFromCurrentUser({
amount: new BN(data.amount.toString()),
mint: 'usdc',
senderSolanaAddress: userBank,
recipientSolanaAddress: keys.destination.pubkey.toBase58(),
instructionIndex: transferInstructionIndex + 1,
feePayerKey: feePayer
const transferFromUserBankInstructions = [
await sdk.services.claimableTokensClient.createTransferSecpInstruction({
mint: 'USDC',
ethWallet: ethAddress,
destination: keys.destination.pubkey,
amount: data.amount,
instructionIndex: transferInstructionIndex + 1
}),
await sdk.services.claimableTokensClient.createTransferInstruction({
mint: 'USDC',
ethWallet: ethAddress,
destination: keys.destination.pubkey
})
]

const withdrawalMemoInstruction = new TransactionInstruction({
keys: [
Expand All @@ -400,17 +400,10 @@ export const decorateCoinflowWithdrawalTransaction = async (
withdrawalMemoInstruction
)

const { blockhash, lastValidBlockHeight } = await solanaWeb3Manager
.getConnection()
.getLatestBlockhash()
const modifiedTransaction = new Transaction({
blockhash,
feePayer,
lastValidBlockHeight
const tx = await sdk.services.solanaClient.buildTransaction({
instructions
})
modifiedTransaction.add(...instructions)

return modifiedTransaction
return tx
}

export const createTransferToUserBankTransaction = async (
Expand Down

0 comments on commit 1515b7d

Please sign in to comment.