Skip to content

Commit

Permalink
chore: onramp tests (#1944)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir authored Feb 29, 2024
1 parent b77ac55 commit 0e10f06
Show file tree
Hide file tree
Showing 7 changed files with 573 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/common/src/utils/TypeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface TransactionMetadata {
sentTo: string
status: TransactionStatus | CoinbaseTransactionStatus
nonce: number
chain?: string
}

export interface TransactionTransfer {
Expand Down
52 changes: 38 additions & 14 deletions packages/core/src/controllers/OnRampController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface OnRampControllerState {

type StateKey = keyof OnRampControllerState

const USDC_CURRENCY_DEFAULT = {
export const USDC_CURRENCY_DEFAULT = {
id: '2b92315d-eab7-5bef-84fa-089a131333f5',
name: 'USD Coin',
symbol: 'USDC',
Expand All @@ -50,7 +50,7 @@ const USDC_CURRENCY_DEFAULT = {
]
}

const USD_CURRENCY_DEFAULT = {
export const USD_CURRENCY_DEFAULT = {
id: 'USD',
payment_method_limits: [
{
Expand All @@ -66,8 +66,7 @@ const USD_CURRENCY_DEFAULT = {
]
}

// -- State --------------------------------------------- //
const state = proxy<OnRampControllerState>({
const defaultState = {
providers: ONRAMP_PROVIDERS as OnRampProvider[],
selectedProvider: null,
error: null,
Expand All @@ -76,7 +75,10 @@ const state = proxy<OnRampControllerState>({
purchaseCurrencies: [USDC_CURRENCY_DEFAULT],
paymentCurrencies: [],
quotesLoading: false
})
}

// -- State --------------------------------------------- //
const state = proxy<OnRampControllerState>(defaultState)

// -- Controller ---------------------------------------- //
export const OnRampController = {
Expand Down Expand Up @@ -124,15 +126,37 @@ export const OnRampController = {

async getQuote() {
state.quotesLoading = true
const quote = await BlockchainApiController.getOnrampQuote({
purchaseCurrency: state.purchaseCurrency,
paymentCurrency: state.paymentCurrency,
amount: state.paymentAmount?.toString() || '0',
network: state.purchaseCurrency?.name
})
state.quotesLoading = false
state.purchaseAmount = Number(quote.purchaseAmount.amount)
try {
const quote = await BlockchainApiController.getOnrampQuote({
purchaseCurrency: state.purchaseCurrency,
paymentCurrency: state.paymentCurrency,
amount: state.paymentAmount?.toString() || '0',
network: state.purchaseCurrency?.symbol
})
state.quotesLoading = false
state.purchaseAmount = Number(quote.purchaseAmount.amount)

return quote
} catch (error) {
state.error = (error as Error).message
state.quotesLoading = false

return null
} finally {
state.quotesLoading = false
}
},

return quote
resetState() {
state.providers = ONRAMP_PROVIDERS as OnRampProvider[]
state.selectedProvider = null
state.error = null
state.purchaseCurrency = USDC_CURRENCY_DEFAULT
state.paymentCurrency = USD_CURRENCY_DEFAULT
state.purchaseCurrencies = [USDC_CURRENCY_DEFAULT]
state.paymentCurrencies = []
state.paymentAmount = undefined
state.purchaseAmount = undefined
state.quotesLoading = false
}
}
1 change: 1 addition & 0 deletions packages/core/src/controllers/TransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const TransactionsController = {
transactions.forEach(transaction => {
const year = new Date(transaction.metadata.minedAt).getFullYear()
const month = new Date(transaction.metadata.minedAt).getMonth()

const yearTransactions = grouped[year] ?? {}
const monthTransactions = yearTransactions[month] ?? []

Expand Down
151 changes: 151 additions & 0 deletions packages/core/tests/constants/OnrampTransactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
export const ONRAMP_TRANSACTIONS_RESPONSES_JAN = {
SUCCESS: {
id: '1eeccf2f-ef04-6d48-a2dd-0e1dca1d3cfb',
metadata: {
operationType: 'buy',
hash: '0xbf5f116e0e77b304404ff873b527578d8c0a247732a50b0da174a533b669ab5b',
minedAt: '2024-01-15T16:59:37.345Z',
sentFrom: 'Coinbase',
sentTo: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA64',
status: 'ONRAMP_TRANSACTION_STATUS_SUCCESS',
nonce: 1,
chain: 'eip155:137'
},
transfers: [
{
fungible_info: {
name: 'USDC',
symbol: 'USDC'
},
direction: 'in',
quantity: {
numeric: '3.003898'
}
}
]
},
FAILED: {
id: '1eeccf2f-ef04-6d48-a2dd-0e1dca1d3cfb',
metadata: {
operationType: 'buy',
hash: '',
minedAt: '2024-01-15T16:59:37.345Z',
sentFrom: 'Coinbase',
sentTo: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA64',
status: 'ONRAMP_TRANSACTION_STATUS_FAILED',
nonce: 1
},
transfers: [
{
fungible_info: {
name: 'USDC',
symbol: 'USDC'
},
direction: 'in',
quantity: {
numeric: '4.995375'
}
}
]
},
IN_PROGRESS: {
id: '1eeccf2f-ef04-6d48-a2dd-0e1dca1d3cfb',
metadata: {
operationType: 'buy',
hash: '',
minedAt: '2024-01-15T16:59:37.345Z',
sentFrom: 'Coinbase',
sentTo: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA64',
status: 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS',
nonce: 1
},
transfers: [
{
fungible_info: {
name: 'USDC',
symbol: 'USDC'
},
direction: 'in',
quantity: {
numeric: '4.995375'
}
}
]
}
}

export const ONRAMP_TRANSACTIONS_RESPONSES_FEB = {
SUCCESS: {
id: '1eecc239-9ed5-696e-afeb-129d128962f1',
metadata: {
operationType: 'buy',
hash: '0xbf5f116e0e77b304404ff873b527578d8c0a247732a50b0da174a533b669ab5b',
minedAt: '2024-02-15T16:59:37.345Z',
sentFrom: 'Coinbase',
sentTo: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA64',
status: 'ONRAMP_TRANSACTION_STATUS_SUCCESS',
nonce: 1,
chain: 'eip155:137'
},
transfers: [
{
fungible_info: {
name: 'USDC',
symbol: 'USDC'
},
direction: 'in',
quantity: {
numeric: '3.003898'
}
}
]
},
FAILED: {
id: '1eecc239-9ed5-696e-afeb-129d128962f1',
metadata: {
operationType: 'buy',
hash: '',
minedAt: '2024-02-15T16:59:37.345Z',
sentFrom: 'Coinbase',
sentTo: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA64',
status: 'ONRAMP_TRANSACTION_STATUS_FAILED',
nonce: 1
},
transfers: [
{
fungible_info: {
name: 'USDC',
symbol: 'USDC'
},
direction: 'in',
quantity: {
numeric: '4.995375'
}
}
]
},
IN_PROGRESS: {
id: '1eecc239-9ed5-696e-afeb-129d128962f1',
metadata: {
operationType: 'buy',
hash: '',
minedAt: '2024-02-15T16:59:37.345Z',
sentFrom: 'Coinbase',
sentTo: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA64',
status: 'ONRAMP_TRANSACTION_STATUS_IN_PROGRESS',
nonce: 1
},
transfers: [
{
fungible_info: {
name: 'USDC',
symbol: 'USDC'
},
direction: 'in',
quantity: {
numeric: '4.995375'
}
}
]
}
}
Loading

0 comments on commit 0e10f06

Please sign in to comment.