Skip to content

Commit

Permalink
Add EdgeTxFiat data
Browse files Browse the repository at this point in the history
This allows fiat plugins to tag transactions with fiat exchange specific data similar to EdgeTxSwap. This is however placed in the `currencies` object so we can tag different outgoing currencies with different information. EdgeTxSwap should really have been architected the same way but we'll need a migration plan for that.
  • Loading branch information
paullinator committed Nov 26, 2023
1 parent 1515f34 commit 691c40d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/core/currency/wallet/currency-wallet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { makeStorageWalletApi } from '../../storage/storage-api'
import { getCurrencyMultiplier } from '../currency-selectors'
import { makeCurrencyWalletCallbacks } from './currency-wallet-callbacks'
import {
asEdgeTxFiat,
asEdgeTxSwap,
packMetadata,
TransactionFile,
Expand Down Expand Up @@ -468,6 +469,7 @@ export function makeCurrencyWalletApi(
memos,
skipChecks,
spendTargets = [],
fiatData,
swapData
} = spendInfo

Expand Down Expand Up @@ -536,6 +538,7 @@ export function makeCurrencyWalletApi(
tx.spendTargets = savedTargets
if (metadata != null) tx.metadata = metadata
if (swapData != null) tx.swapData = asEdgeTxSwap(swapData)
if (fiatData != null) tx.fiatData = asEdgeTxFiat(fiatData)
if (input.props.state.login.deviceDescription != null)
tx.deviceDescription = input.props.state.login.deviceDescription

Expand Down Expand Up @@ -665,7 +668,7 @@ export function combineTxWithFile(
if (file != null) {
if (file.creationDate < out.date) out.date = file.creationDate

const merged = mergeDeeply(
const merged: TransactionFile['currencies']['currencyCode'] = mergeDeeply(
file.currencies[walletCurrency],
file.currencies[currencyCode]
)
Expand All @@ -676,6 +679,10 @@ export function combineTxWithFile(
}
}

if (merged.fiatData != null) {
out.fiatData = merged.fiatData
}

if (file.feeRateRequested != null) {
if (typeof file.feeRateRequested === 'string') {
out.networkFeeOption = file.feeRateRequested
Expand Down
30 changes: 29 additions & 1 deletion src/core/currency/wallet/currency-wallet-cleaners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
asArray,
asBoolean,
asEither,
asMaybe,
asNull,
asNumber,
asObject,
Expand All @@ -11,7 +12,12 @@ import {
Cleaner
} from 'cleaners'

import { EdgeMetadata, EdgeTxSwap, JsonObject } from '../../../types/types'
import {
EdgeMetadata,
EdgeTxFiat,
EdgeTxSwap,
JsonObject
} from '../../../types/types'
import { asJsonObject } from '../../../util/file-helpers'

/**
Expand All @@ -35,6 +41,7 @@ export interface TransactionFile {
creationDate: number
currencies: {
[currencyCode: string]: {
fiatData?: EdgeTxFiat
metadata: DiskMetadata
nativeAmount?: string
providerFeeSent?: string
Expand Down Expand Up @@ -153,6 +160,26 @@ const asFeeRate: Cleaner<'high' | 'standard' | 'low'> = asValue(
'low'
)

export const asEdgeTxFiat = asObject<EdgeTxFiat>({
orderId: asString,
orderUri: asOptional(asString),
isEstimate: asBoolean,

fiatPlugin: asObject({
providerId: asString,
providerDisplayName: asString,
supportEmail: asOptional(asString)
}),
txType: asValue('buy', 'sell', 'sellNetworkFee'),
payinAddress: asMaybe(asString),
payoutAddress: asMaybe(asString),
fiatCurrencyCode: asString,
cryptoPluginId: asString,
cryptoTokenId: asMaybe(asString),
cryptoNativeAmount: asString,
walletId: asString
})

export const asEdgeTxSwap = asObject<EdgeTxSwap>({
orderId: asOptional(asString),
orderUri: asOptional(asString),
Expand Down Expand Up @@ -207,6 +234,7 @@ export const asTransactionFile = asObject<TransactionFile>({
creationDate: asNumber,
currencies: asObject(
asObject({
fiatData: asOptional(asEdgeTxFiat),
metadata: asDiskMetadata,
nativeAmount: asOptional(asString),
providerFeeSent: asOptional(asString)
Expand Down
9 changes: 9 additions & 0 deletions src/core/currency/wallet/currency-wallet-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export function searchStringFilter(
if (checkNullTypeAndIndex(displayName) || checkNullTypeAndIndex(pluginId))
return true
}
if (tx.fiatData != null) {
const { providerId = '', providerDisplayName = '' } = tx.fiatData.fiatPlugin
if (
checkNullTypeAndIndex(providerId) ||
checkNullTypeAndIndex(providerDisplayName)
)
return true
}

if (tx.spendTargets != null) {
for (const target of tx.spendTargets) {
const { publicAddress = '', memo = '' } = target
Expand Down
4 changes: 2 additions & 2 deletions src/core/currency/wallet/currency-wallet-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ export async function setupNewTxMetadata(
): Promise<void> {
const { dispatch, walletState, state, walletId } = input.props
const { fiat = 'iso:USD' } = walletState
const { currencyCode, spendTargets, swapData, txid } = tx
const { currencyCode, fiatData, spendTargets, swapData, txid } = tx
const disklet = getStorageWalletDisklet(state, walletId)

const creationDate = Date.now() / 1000
Expand All @@ -472,7 +472,7 @@ export async function setupNewTxMetadata(
currencies: {},
swap: swapData
}
json.currencies[currencyCode] = { metadata, nativeAmount }
json.currencies[currencyCode] = { metadata, nativeAmount, fiatData }

// Set up the fee metadata:
if (tx.networkFeeOption != null) {
Expand Down
14 changes: 13 additions & 1 deletion src/core/currency/wallet/currency-wallet-reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
EdgeStakingStatus,
EdgeTransaction,
EdgeTxAction,
EdgeTxFiat,
EdgeTxSwap,
EdgeWalletInfo,
EdgeWalletInfoFull,
JsonObject
Expand Down Expand Up @@ -52,6 +54,8 @@ export interface MergedTransaction {
ourReceiveAddresses: string[]
signedTx: string
txid: string
swapData?: EdgeTxSwap
fiatData?: EdgeTxFiat

nativeAmount: { [currencyCode: string]: string }
networkFee: { [currencyCode: string]: string }
Expand Down Expand Up @@ -424,7 +428,7 @@ export function mergeTx(
memos
} = tx

const out = {
const out: MergedTransaction = {
action,
blockHeight: tx.blockHeight,
confirmations: tx.confirmations ?? 'unconfirmed',
Expand All @@ -449,5 +453,13 @@ export function mergeTx(
out.networkFee[defaultCurrency] = String(tx.parentNetworkFee)
}

if (tx.fiatData != null) {
out.fiatData = tx.fiatData
}

if (tx.swapData != null) {
out.swapData = tx.swapData
}

return out
}
23 changes: 23 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ export interface EdgeTxSwap {
refundAddress?: string
}

export interface EdgeTxFiat {
orderId: string
orderUri?: string
isEstimate: boolean

fiatPlugin: {
providerId: string
providerDisplayName: string
supportEmail?: string
}

txType: 'buy' | 'sell' | 'sellNetworkFee'
payinAddress?: string
payoutAddress?: string
fiatCurrencyCode: string
cryptoPluginId: string
cryptoTokenId?: string
cryptoNativeAmount: string
walletId: string
}

export type EdgeConfirmationState =
// More than `EdgeCurrencyInfo.requiredConfirmations`:
| 'confirmed'
Expand Down Expand Up @@ -520,6 +541,7 @@ export interface EdgeTransaction {
readonly uniqueIdentifier: string | undefined
}>
swapData?: EdgeTxSwap
fiatData?: EdgeTxFiat
txSecret?: string // Monero decryption key

/**
Expand Down Expand Up @@ -574,6 +596,7 @@ export interface EdgeSpendInfo {
// Core:
metadata?: EdgeMetadata
swapData?: EdgeTxSwap
fiatData?: EdgeTxFiat
otherParams?: JsonObject

/** @deprecated Use tokenId instead */
Expand Down

0 comments on commit 691c40d

Please sign in to comment.