Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate useCoinflowWithdrawalAdapter to SDK #10782

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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