Skip to content

Commit

Permalink
Add networkFees to EdgeTransaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
samholmes committed Dec 2, 2024
1 parent 838c3d6 commit 80bd7cc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- added: Added `networkFees` to `EdgeTransaction`.

## 2.20.3 (2024-11-25)

- fixed: Accept `''` to select the default API key.
Expand Down
8 changes: 7 additions & 1 deletion src/core/currency/wallet/currency-wallet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
InternalWalletStream,
streamTransactions
} from '../../../client-side'
import { upgradeCurrencyCode } from '../../../types/type-helpers'
import {
upgradeCurrencyCode,
upgradeTxNetworkFees
} from '../../../types/type-helpers'
import {
EdgeBalanceMap,
EdgeBalances,
Expand Down Expand Up @@ -324,6 +327,7 @@ export function makeCurrencyWalletApi(

// Filter transactions based on search criteria:
const edgeTx = combineTxWithFile(input, tx, file, tokenId)
upgradeTxNetworkFees(edgeTx)
if (!searchStringFilter(ai, edgeTx, searchString)) continue
if (!dateFilter(edgeTx, afterDate, beforeDate)) continue
const isKnown =
Expand Down Expand Up @@ -513,6 +517,7 @@ export function makeCurrencyWalletApi(
},
{ privateKeys }
)
upgradeTxNetworkFees(tx)
tx.networkFeeOption = networkFeeOption
tx.requestedCustomFee = customNetworkFee
tx.spendTargets = savedTargets
Expand Down Expand Up @@ -691,6 +696,7 @@ export function combineTxWithFile(
metadata: {},
nativeAmount: tx.nativeAmount.get(tokenId) ?? '0',
networkFee: tx.networkFee.get(tokenId) ?? '0',
networkFees: [],
otherParams: { ...tx.otherParams },
ourReceiveAddresses: tx.ourReceiveAddresses,
parentNetworkFee:
Expand Down
7 changes: 6 additions & 1 deletion src/core/currency/wallet/currency-wallet-callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { asMaybe } from 'cleaners'
import { isPixieShutdownError } from 'redux-pixies'
import { emit } from 'yaob'

import { upgradeCurrencyCode } from '../../../types/type-helpers'
import {
upgradeCurrencyCode,
upgradeTxNetworkFees
} from '../../../types/type-helpers'
import {
EdgeConfirmationState,
EdgeCurrencyEngineCallbacks,
Expand Down Expand Up @@ -280,6 +283,8 @@ export function makeCurrencyWalletCallbacks(
// Sanity-check incoming transactions:
if (txs == null) return
for (const tx of txs) {
// Backwards/Forwards compatibility:
upgradeTxNetworkFees(tx)
if (
typeof tx.txid !== 'string' ||
typeof tx.date !== 'number' ||
Expand Down
17 changes: 16 additions & 1 deletion src/types/type-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type {
EdgeCurrencyInfo,
EdgeSwapQuote,
EdgeTokenId,
EdgeTokenMap
EdgeTokenMap,
EdgeTransaction
} from './types'

/**
Expand Down Expand Up @@ -41,3 +42,17 @@ export function upgradeSwapQuote(quote: EdgeSwapQuote): EdgeSwapQuote {
}
return quote
}

export const upgradeTxNetworkFees = (tx: EdgeTransaction): void => {
if (tx.networkFees == null || tx.networkFees.length === 0) {
tx.networkFees = [
{
tokenId: tx.tokenId,
nativeAmount: tx.networkFee
}
]
if (tx.parentNetworkFee != null) {
tx.networkFees.push({ tokenId: null, nativeAmount: tx.parentNetworkFee })
}
}
}
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ export interface EdgeTransaction {
// Amounts:
nativeAmount: string
networkFee: string
networkFees: EdgeTxAmount[]
parentNetworkFee?: string

// Confirmation status:
Expand Down
35 changes: 35 additions & 0 deletions test/core/currency/wallet/currency-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,38 @@ describe('currency wallets', function () {
expect(await wallet.nativeToDenomination('10', 'TOKEN')).equals('0.01')
})

it('can make spend', async function () {
const { wallet, config } = await makeFakeCurrencyWallet()
await config.changeUserSettings({ balance: 100 }) // Spending balance
const tx = await wallet.makeSpend({
tokenId: null,
spendTargets: [
{
uniqueIdentifier: 'hello',
nativeAmount: '50',
publicAddress: 'somewhere'
}
],
networkFeeOption: 'high'
})
expect(tx.nativeAmount).equals('50')
expect(tx.metadata).deep.equals(undefined)
expect(tx.networkFeeOption).equals('high')
expect(tx.networkFee).equals('23')
expect(tx.networkFees).deep.equals([{ tokenId: null, nativeAmount: '23' }])
expect(tx.feeRateUsed).deep.equals({ fakePrice: 0 })
expect(tx.spendTargets).deep.equals([
{
currencyCode: 'FAKE',
memo: 'hello',
nativeAmount: '50',
publicAddress: 'somewhere',
uniqueIdentifier: 'hello'
}
])
expect(tx.deviceDescription).equals('iphone12')
})

it('can save metadata at spend time', async function () {
const log = makeAssertLog()
const { wallet, config } = await makeFakeCurrencyWallet()
Expand Down Expand Up @@ -515,6 +547,9 @@ describe('currency wallets', function () {
expect(txs[0].networkFeeOption).equals('high')
expect(txs[0].networkFee).equals('23')
expect(txs[0].parentNetworkFee).equals(undefined)
expect(txs[0].networkFees).deep.equals([
{ tokenId: null, nativeAmount: '23' }
])
expect(txs[0].feeRateUsed).deep.equals({ fakePrice: 0 })
expect(txs[0].spendTargets).deep.equals([
{
Expand Down
2 changes: 2 additions & 0 deletions test/fake/fake-currency-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class FakeCurrencyEngine implements EdgeCurrencyEngine {
memos: [],
nativeAmount: '0',
networkFee: '0',
networkFees: [],
ourReceiveAddresses: [],
signedTx: '',
tokenId,
Expand Down Expand Up @@ -291,6 +292,7 @@ class FakeCurrencyEngine implements EdgeCurrencyEngine {
memos,
nativeAmount: total,
networkFee: '23',
networkFees: [],
otherParams: {},
ourReceiveAddresses: [],
signedTx: '',
Expand Down
1 change: 1 addition & 0 deletions test/fake/fake-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ export const moreTxs: { [txid: string]: EdgeTransaction } = {
txid: '83137fa6212eff80a819c58bd3d500e240603a2bec25dbb96dd29fe5f4b7f69b',
nativeAmount: '10',
networkFee: '0',
networkFees: [],
currencyCode: 'FAKE',
tokenId: null,
chainAssetAction: {
Expand Down

0 comments on commit 80bd7cc

Please sign in to comment.