Skip to content

Commit

Permalink
Remove buy-crypto flows (#10768)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyrombo authored Dec 18, 2024
1 parent 0d06502 commit 9592cf9
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 1,400 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BuyCryptoErrorCode } from '~/store/buy-crypto/types'
import { BuyUSDCErrorCode } from '~/store/index'
import {
PurchaseContentErrorCode,
Expand Down Expand Up @@ -35,25 +34,10 @@ export const usePurchaseContentErrorMessage = (
return messages.minimumPurchase(minUSDCPurchaseAmountCents)
case BuyUSDCErrorCode.MaxAmountExceeded:
return messages.maximumPurchase(maxUSDCPurchaseAmountCents)
case BuyCryptoErrorCode.COUNTRY_NOT_SUPPORTED:
case BuyUSDCErrorCode.CountryNotSupported:
return messages.countryNotSupported
case BuyCryptoErrorCode.BAD_AMOUNT:
return messages.badAmount(
minUSDCPurchaseAmountCents,
maxUSDCPurchaseAmountCents
)
case PurchaseErrorCode.InsufficientExternalTokenBalance:
return messages.insufficientExternalTokenBalance
case PurchaseErrorCode.NoQuote:
return messages.noQuote
case BuyCryptoErrorCode.BAD_FEE_PAYER:
case BuyCryptoErrorCode.BAD_PROVIDER:
case BuyCryptoErrorCode.BAD_TOKEN:
case BuyCryptoErrorCode.ON_RAMP_ERROR:
case BuyCryptoErrorCode.SWAP_ERROR:
case BuyCryptoErrorCode.UNKNOWN:
case BuyCryptoErrorCode.INSUFFICIENT_FUNDS_ERROR:
case BuyUSDCErrorCode.OnrampError:
case PurchaseErrorCode.Canceled:
case PurchaseErrorCode.InsufficientBalance:
Expand Down
5 changes: 1 addition & 4 deletions packages/common/src/hooks/useCoinflowAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
purchaseContentActions
} from '~/store'
import { getWalletAddresses } from '~/store/account/selectors'
import { BuyCryptoError } from '~/store/buy-crypto/types'
import { getFeePayer } from '~/store/solana/selectors'

type CoinflowAdapter = {
Expand Down Expand Up @@ -203,9 +202,7 @@ export const useCoinflowAdapter = ({
} catch (e) {
console.error('Caught error in sendTransaction', e)
const error =
e instanceof PurchaseContentError ||
e instanceof BuyUSDCError ||
e instanceof BuyCryptoError
e instanceof PurchaseContentError || e instanceof BuyUSDCError
? e
: new PurchaseContentError(PurchaseErrorCode.Unknown, `${e}`)
dispatch(
Expand Down
49 changes: 1 addition & 48 deletions packages/common/src/models/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
StringAudio,
WalletAddress
} from '~/models/Wallet'
import { MintName } from '~/services/index'
import { Nullable, Prettify } from '~/utils/typeUtils'
import { Nullable } from '~/utils/typeUtils'

import { Chain } from './Chain'
import { PlaylistLibraryKind } from './PlaylistLibrary'
Expand Down Expand Up @@ -415,21 +414,6 @@ export enum Name {
BUY_USDC_RECOVERY_FAILURE = 'Buy USDC: Recovery Failure',
BUY_USDC_ADD_FUNDS_MANUALLY = 'Buy USDC: Add Funds Manually',

// Buy Crypto
BUY_CRYPTO_STARTED = 'Buy Crypto: Started',
BUY_CRYPTO_ON_RAMP_OPENED = 'Buy Crypto: On Ramp Opened',
BUY_CRYPTO_ON_RAMP_SUCCESS = 'Buy Crypto: On Ramp Success',
BUY_CRYPTO_ON_RAMP_FAILURE = 'Buy Crypto: On Ramp Failure',
BUY_CRYPTO_ON_RAMP_CANCELED = 'Buy Crypto: On Ramp Canceled',
BUY_CRYPTO_ON_RAMP_CONFIRMED = 'Buy Crypto: On Ramp Confirmed',
BUY_CRYPTO_SUCCESS = 'Buy Crypto: Success',
BUY_CRYPTO_FAILURE = 'Buy Crypto: Failure',

// Buy Crypto Recovery
BUY_CRYPTO_RECOVERY_STARTED = 'Buy Crypto: Recovery Started',
BUY_CRYPTO_RECOVERY_SUCCESS = 'Buy Crypto: Recovery Success',
BUY_CRYPTO_RECOVERY_FAILURE = 'Buy Crypto: Recovery Failure',

// Withdraw USDC
WITHDRAW_USDC_MODAL_OPENED = 'Withdraw USDC: Modal Opened',
WITHDRAW_USDC_ADDRESS_PASTED = 'Withdraw USDC: Address Pasted',
Expand Down Expand Up @@ -2083,35 +2067,6 @@ type BuyUSDCAddFundsManually = {
eventName: Name.BUY_USDC_ADD_FUNDS_MANUALLY
}

// Buy Crypto

type BuyCryptoEvent = {
eventName:
| Name.BUY_CRYPTO_STARTED
| Name.BUY_CRYPTO_ON_RAMP_OPENED
| Name.BUY_CRYPTO_ON_RAMP_SUCCESS
| Name.BUY_CRYPTO_ON_RAMP_FAILURE
| Name.BUY_CRYPTO_ON_RAMP_CANCELED
| Name.BUY_CRYPTO_ON_RAMP_CONFIRMED
| Name.BUY_CRYPTO_SUCCESS
| Name.BUY_CRYPTO_FAILURE

provider: string
requestedAmount: number
mint: MintName
error?: string
}

type BuyCryptoRecoveryEvent = Prettify<
{
eventName:
| Name.BUY_CRYPTO_RECOVERY_STARTED
| Name.BUY_CRYPTO_RECOVERY_FAILURE
| Name.BUY_CRYPTO_RECOVERY_SUCCESS
intendedSOL: number
} & Omit<BuyCryptoEvent, 'eventName'>
>

// Withdraw USDC

export type WithdrawUSDCEventFields = {
Expand Down Expand Up @@ -2972,8 +2927,6 @@ export type AllTrackingEvents =
| BuyUSDCRecoverySuccess
| BuyUSDCRecoveryFailure
| BuyUSDCAddFundsManually
| BuyCryptoEvent
| BuyCryptoRecoveryEvent
| WithdrawUSDCModalOpened
| WithdrawUSDCAddressPasted
| WithdrawUSDCFormError
Expand Down
154 changes: 1 addition & 153 deletions packages/common/src/services/audius-backend/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ import {
getAssociatedTokenAddressSync
} from '@solana/spl-token'
import {
AddressLookupTableAccount,
Commitment,
ComputeBudgetProgram,
Connection,
Keypair,
PublicKey,
Transaction,
TransactionInstruction,
TransactionMessage,
VersionedTransaction
TransactionInstruction
} from '@solana/web3.js'
import BN from 'bn.js'

Expand All @@ -37,7 +34,6 @@ import { AudiusBackend } from './AudiusBackend'

const DEFAULT_RETRY_DELAY = 1000
const DEFAULT_MAX_RETRY_COUNT = 120
const PLACEHOLDER_SIGNATURE = new Array(64).fill(0)
export const RECOVERY_MEMO_STRING = 'Recover Withdrawal'
export const WITHDRAWAL_MEMO_STRING = 'Withdrawal'
export const PREPARE_WITHDRAWAL_MEMO_STRING = 'Prepare Withdrawal'
Expand Down Expand Up @@ -293,58 +289,6 @@ export const pollForTokenBalanceChange = async (
throw new Error(`${debugTokenName} balance polling exceeded maximum retries`)
}

/**
* Polls the given wallet until its SOL balance is different from initial balance or a timeoout.
* @throws an error if the balance doesn't change within the timeout.
*/
export const pollForBalanceChange = async (
audiusBackendInstance: AudiusBackend,
{
wallet,
initialBalance,
retryDelayMs = DEFAULT_RETRY_DELAY,
maxRetryCount = DEFAULT_MAX_RETRY_COUNT,
sdk
}: {
wallet: PublicKey
initialBalance?: bigint
retryDelayMs?: number
maxRetryCount?: number
sdk: AudiusSdk
}
) => {
console.info(`Polling SOL balance for ${wallet.toBase58()} ...`)
let balanceBN = await audiusBackendInstance.getAddressSolBalance({
address: wallet.toBase58(),
sdk
})
let balance = BigInt(balanceBN.toString())
if (initialBalance === undefined) {
initialBalance = balance
}
let retries = 0
while (balance === initialBalance && retries++ < maxRetryCount) {
console.debug(
`Polling SOL balance (${initialBalance} === ${balance}) [${retries}/${maxRetryCount}]`
)
await delay(retryDelayMs)
balanceBN = await audiusBackendInstance.getAddressSolBalance({
address: wallet.toBase58(),
sdk
})
balance = BigInt(balanceBN.toString())
}
if (balance !== initialBalance) {
console.debug(
`SOL balance changed by ${
balance - initialBalance
} (${initialBalance} => ${balance})`
)
return balance
}
throw new Error('SOL balance polling exceeded maximum retries')
}

export const findAssociatedTokenAddress = async (
audiusBackendInstance: AudiusBackend,
{ solanaAddress, mint }: { solanaAddress: string; mint: MintName }
Expand Down Expand Up @@ -595,102 +539,6 @@ export const relayTransaction = async (
})
}

/**
* Relays the given versioned transaction using the libs transaction handler
*/
export const relayVersionedTransaction = async (
audiusBackendInstance: AudiusBackend,
{
transaction,
addressLookupTableAccounts,
skipPreflight
}: {
transaction: VersionedTransaction
addressLookupTableAccounts: AddressLookupTableAccount[]
skipPreflight?: boolean
}
) => {
const placeholderSignature = Buffer.from(PLACEHOLDER_SIGNATURE)
const libs = await audiusBackendInstance.getAudiusLibsTyped()
const decompiledMessage = TransactionMessage.decompile(transaction.message, {
addressLookupTableAccounts
})
const signatures = transaction.message.staticAccountKeys
.slice(0, transaction.message.header.numRequiredSignatures)
.map((publicKey, index) => ({
publicKey: publicKey.toBase58(),
signature: Buffer.from(transaction.signatures[index])
}))
.filter((meta) => !meta.signature.equals(placeholderSignature))
return await libs.solanaWeb3Manager!.transactionHandler.handleTransaction({
instructions: decompiledMessage.instructions,
recentBlockhash: decompiledMessage.recentBlockhash,
signatures,
feePayerOverride: decompiledMessage.payerKey,
lookupTableAddresses: addressLookupTableAccounts.map((lut) =>
lut.key.toBase58()
),
skipPreflight
})
}

/**
* Helper that gets the lookup table accounts (that is, the account holding the lookup table,
* not the accounts _in_ the lookup table) from their addresses.
*/
export const getLookupTableAccounts = async (
audiusBackendInstance: AudiusBackend,
{ lookupTableAddresses }: { lookupTableAddresses: string[] }
) => {
const libs = await audiusBackendInstance.getAudiusLibsTyped()
const connection = libs.solanaWeb3Manager!.getConnection()
return await Promise.all(
lookupTableAddresses.map(async (address) => {
const account = await connection.getAddressLookupTable(
new PublicKey(address)
)
if (account.value == null) {
throw new Error(`Couldn't find lookup table ${address}`)
}
return account.value
})
)
}

/**
* Helper to create a versioned transaction with lookup tables
*/
export const createVersionedTransaction = async (
audiusBackendInstance: AudiusBackend,
{
instructions,
lookupTableAddresses,
feePayer,
sdk
}: {
instructions: TransactionInstruction[]
lookupTableAddresses: string[]
feePayer: PublicKey
sdk: AudiusSdk
}
) => {
const addressLookupTableAccounts = await getLookupTableAccounts(
audiusBackendInstance,
{ lookupTableAddresses }
)
const recentBlockhash = await getRecentBlockhash({ sdk })

const message = new TransactionMessage({
payerKey: feePayer,
recentBlockhash,
instructions
}).compileToV0Message(addressLookupTableAccounts)
return {
transaction: new VersionedTransaction(message),
addressLookupTableAccounts
}
}

// NOTE: The above all need to be updated to use SDK. The below is fresh.

/**
Expand Down
5 changes: 0 additions & 5 deletions packages/common/src/store/buy-crypto/index.ts

This file was deleted.

Loading

0 comments on commit 9592cf9

Please sign in to comment.