Skip to content

Commit

Permalink
Implement saveTxAction
Browse files Browse the repository at this point in the history
  • Loading branch information
paullinator committed Dec 6, 2023
1 parent 24fdc13 commit 20449ef
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
28 changes: 26 additions & 2 deletions src/core/currency/wallet/currency-wallet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../../../client-side'
import { upgradeCurrencyCode } from '../../../types/type-helpers'
import {
EdgeAssetAction,
EdgeBalances,
EdgeCurrencyCodeOptions,
EdgeCurrencyConfig,
Expand All @@ -31,6 +32,7 @@ import {
EdgeStakingStatus,
EdgeStreamTransactionOptions,
EdgeTransaction,
EdgeTxAction,
EdgeWalletInfo
} from '../../../types/types'
import { mergeDeeply } from '../../../util/util'
Expand Down Expand Up @@ -561,6 +563,28 @@ export function makeCurrencyWalletApi(
await engine.saveTx(tx)
fakeCallbacks.onTransactionsChanged([tx])
},
async saveTxAction(
txid: string,
tokenId: string | null,
assetAction: EdgeAssetAction,
savedAction: EdgeTxAction
): Promise<void> {
const { accountApi } = input.props.output.accounts[accountId]
const { allTokens, currencyInfo } = accountApi.currencyConfig[pluginId]
const { currencyCode } =
tokenId == null ? currencyInfo : allTokens[tokenId]

await setCurrencyWalletTxMetadata(
input,
txid,
currencyCode,
tokenId,
fakeCallbacks,
undefined,
assetAction,
savedAction
)
},
async saveTxMetadata(
txid: string,
currencyCode: string,
Expand All @@ -580,8 +604,8 @@ export function makeCurrencyWalletApi(
txid,
currencyCode,
tokenId,
packMetadata(metadata, input.props.walletState.fiat),
fakeCallbacks
fakeCallbacks,
packMetadata(metadata, input.props.walletState.fiat)
)
},
async signMessage(
Expand Down
24 changes: 19 additions & 5 deletions src/core/currency/wallet/currency-wallet-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { number as currencyFromNumber } from 'currency-codes'
import { Disklet, justFiles, navigateDisklet } from 'disklet'

import {
EdgeAssetAction,
EdgeCurrencyEngineCallbacks,
EdgeTransaction
EdgeTransaction,
EdgeTxAction
} from '../../../types/types'
import { makeJsonFile } from '../../../util/file-helpers'
import { mergeDeeply } from '../../../util/util'
Expand Down Expand Up @@ -419,8 +421,10 @@ export async function setCurrencyWalletTxMetadata(
txid: string,
currencyCode: string,
tokenId: string | null,
metadata: DiskMetadata,
fakeCallbacks: EdgeCurrencyEngineCallbacks
fakeCallbacks: EdgeCurrencyEngineCallbacks,
metadata?: DiskMetadata,
assetAction?: EdgeAssetAction,
savedAction?: EdgeTxAction
): Promise<void> {
const { dispatch, state, walletId } = input.props
const disklet = getStorageWalletDisklet(state, walletId)
Expand Down Expand Up @@ -459,12 +463,22 @@ export async function setCurrencyWalletTxMetadata(
txid,
internal: false,
creationDate,
savedAction,
currencies: {},
tokens: {}
}
newFile.currencies[currencyCode] = {
metadata
if (metadata != null) {
newFile.currencies[currencyCode] = {
metadata
}
}

if (assetAction != null) {
newFile.tokens[tokenId ?? PARENT_TOKEN_ID] = {
assetAction
}
}

const json = mergeDeeply(oldFile, newFile)

// Save the new file:
Expand Down
7 changes: 7 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,13 @@ export interface EdgeCurrencyWallet {
) => Promise<EdgePaymentProtocolInfo>
readonly makeSpend: (spendInfo: EdgeSpendInfo) => Promise<EdgeTransaction>
readonly saveTx: (tx: EdgeTransaction) => Promise<void>
readonly saveTxAction: (
txid: string,
tokenId: string | null,
assetAction: EdgeAssetAction,
savedAction: EdgeTxAction
) => Promise<void>

readonly saveTxMetadata: (
txid: string,
currencyCode: string,
Expand Down
21 changes: 21 additions & 0 deletions test/core/currency/wallet/currency-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,28 @@ describe('currency wallets', function () {
name: 'me',
amountFiat: 0.75
}
const assetAction: EdgeAssetAction = {
assetActionType: 'swap'
}
const savedAction: EdgeTxAction = {
actionType: 'swap',
orderId: 'myorderid',
canBePartial: false,
sourceAsset: {
pluginId: 'bitcoin',
tokenId: undefined,
nativeAmount: '1234'
},
destAsset: {
pluginId: 'ethereum',
tokenId: 'mytokenid',
nativeAmount: '2345'
}
}

await config.changeUserSettings({ txs: { a: { nativeAmount: '25' } } })
await wallet.saveTxMetadata('a', 'FAKE', metadata)
await wallet.saveTxAction('a', null, assetAction, savedAction)

const txs = await wallet.getTransactions({})
expect(txs.length).equals(1)
Expand All @@ -542,6 +562,7 @@ describe('currency wallets', function () {
exchangeAmount: { 'iso:USD': 0.75 },
...metadata
})
expect(txs[0].savedAction).deep.equals(savedAction)
})

it('can be paused and un-paused', async function () {
Expand Down

0 comments on commit 20449ef

Please sign in to comment.