diff --git a/src/client-side.ts b/src/client-side.ts index 671add842..06ffaaaa2 100644 --- a/src/client-side.ts +++ b/src/client-side.ts @@ -107,7 +107,7 @@ shareData({ fixUsername }) */ export function streamTransactions( this: InternalWalletMethods, - opts: EdgeStreamTransactionOptions = {} + opts: EdgeStreamTransactionOptions ): AsyncIterableIterator { let stream: InternalWalletStream | undefined let streamClosed = false diff --git a/src/core/currency/wallet/currency-wallet-api.ts b/src/core/currency/wallet/currency-wallet-api.ts index 9326501a8..fd0039e19 100644 --- a/src/core/currency/wallet/currency-wallet-api.ts +++ b/src/core/currency/wallet/currency-wallet-api.ts @@ -10,7 +10,6 @@ import { import { upgradeCurrencyCode } from '../../../types/type-helpers' import { EdgeBalances, - EdgeCurrencyCodeOptions, EdgeCurrencyConfig, EdgeCurrencyEngine, EdgeCurrencyInfo, @@ -30,6 +29,7 @@ import { EdgeStakingStatus, EdgeStreamTransactionOptions, EdgeTokenId, + EdgeTokenIdOptions, EdgeTransaction, EdgeWalletInfo } from '../../../types/types' @@ -217,17 +217,14 @@ export function makeCurrencyWalletApi( }, // Transactions history: - async getNumTransactions( - opts: EdgeCurrencyCodeOptions = {} - ): Promise { - const { currencyCode, tokenId } = upgradeCurrencyCode({ + async getNumTransactions(opts: EdgeTokenIdOptions): Promise { + const upgradedCurrency = upgradeCurrencyCode({ allTokens: input.props.state.accounts[accountId].allTokens[pluginId], currencyInfo: plugin.currencyInfo, - currencyCode: opts.currencyCode, tokenId: opts.tokenId }) - return engine.getNumTransactions({ currencyCode, tokenId }) + return engine.getNumTransactions(upgradedCurrency) }, async $internalStreamTransactions( @@ -245,11 +242,12 @@ export function makeCurrencyWalletApi( tokenId == null ? this.currencyInfo : this.currencyConfig.allTokens[tokenId] + const upgradedCurrency = { currencyCode, tokenId } // Load transactions from the engine if necessary: let state = input.props.walletState if (!state.gotTxs.has(tokenId)) { - const txs = await engine.getTransactions({ currencyCode }) + const txs = await engine.getTransactions(upgradedCurrency) fakeCallbacks.onTransactionsChanged(txs) input.props.dispatch({ type: 'CURRENCY_ENGINE_GOT_TXS', @@ -321,26 +319,20 @@ export function makeCurrencyWalletApi( }, async getTransactions( - opts: EdgeGetTransactionsOptions = {} + opts: EdgeGetTransactionsOptions ): Promise { - const { - currencyCode = plugin.currencyInfo.currencyCode, - endDate: beforeDate, - startDate: afterDate, - searchString - } = opts - const { tokenId } = upgradeCurrencyCode({ + const { endDate: beforeDate, startDate: afterDate, searchString } = opts + const upgradedCurrency = upgradeCurrencyCode({ allTokens: input.props.state.accounts[accountId].allTokens[pluginId], currencyInfo: plugin.currencyInfo, - currencyCode, tokenId: opts.tokenId }) const stream = await out.$internalStreamTransactions({ + ...upgradedCurrency, afterDate, beforeDate, - searchString, - tokenId + searchString }) // We have no length, so iterate to get everything: @@ -356,19 +348,17 @@ export function makeCurrencyWalletApi( // Addresses: async getReceiveAddress( - opts: EdgeGetReceiveAddressOptions = {} + opts: EdgeGetReceiveAddressOptions ): Promise { - const { currencyCode, tokenId } = upgradeCurrencyCode({ + const upgradedCurrency = upgradeCurrencyCode({ allTokens: input.props.state.accounts[accountId].allTokens[pluginId], currencyInfo: plugin.currencyInfo, - currencyCode: opts.currencyCode, tokenId: opts.tokenId }) const freshAddress = await engine.getFreshAddress({ - forceIndex: opts.forceIndex, - currencyCode, - tokenId + ...upgradedCurrency, + forceIndex: opts.forceIndex }) const receiveAddress: EdgeReceiveAddress = { ...freshAddress, @@ -406,13 +396,12 @@ export function makeCurrencyWalletApi( // Figure out which asset this is: const { networkFeeOption, customNetworkFee } = spendInfo - const { currencyCode, tokenId } = upgradeCurrencyCode({ + const upgradedCurrency = upgradeCurrencyCode({ allTokens: input.props.state.accounts[accountId].allTokens[pluginId], currencyInfo: plugin.currencyInfo, - currencyCode: spendInfo.currencyCode, tokenId: spendInfo.tokenId }) - const balance = engine.getBalance({ currencyCode, tokenId }) + const balance = engine.getBalance(upgradedCurrency) // Copy all the spend targets, setting the amounts to 0 // but keeping all other information so we can get accurate fees: @@ -437,8 +426,7 @@ export function makeCurrencyWalletApi( return engine .makeSpend( { - currencyCode, - tokenId, + ...upgradedCurrency, spendTargets, networkFeeOption, customNetworkFee @@ -480,10 +468,9 @@ export function makeCurrencyWalletApi( } = spendInfo // Figure out which asset this is: - const { currencyCode, tokenId } = upgradeCurrencyCode({ + const upgradedCurrency = upgradeCurrencyCode({ allTokens: input.props.state.accounts[accountId].allTokens[pluginId], currencyInfo: plugin.currencyInfo, - currencyCode: spendInfo.currencyCode, tokenId: spendInfo.tokenId }) @@ -507,7 +494,7 @@ export function makeCurrencyWalletApi( uniqueIdentifier: memo }) savedTargets.push({ - currencyCode, + currencyCode: upgradedCurrency.currencyCode, memo, nativeAmount, publicAddress, @@ -524,7 +511,7 @@ export function makeCurrencyWalletApi( const tx: EdgeTransaction = await engine.makeSpend( { - currencyCode, + ...upgradedCurrency, customNetworkFee, memos, metadata, @@ -534,8 +521,7 @@ export function makeCurrencyWalletApi( pendingTxs, rbfTxid, skipChecks, - spendTargets: cleanTargets, - tokenId + spendTargets: cleanTargets }, { privateKeys } ) diff --git a/src/core/currency/wallet/currency-wallet-cleaners.ts b/src/core/currency/wallet/currency-wallet-cleaners.ts index 8d6b60c51..6cbc72b86 100644 --- a/src/core/currency/wallet/currency-wallet-cleaners.ts +++ b/src/core/currency/wallet/currency-wallet-cleaners.ts @@ -157,9 +157,11 @@ export function asIntegerString(raw: unknown): string { // file cleaners // --------------------------------------------------------------------- +export const asEdgeTokenId = asEither(asString, asNull) + export const asEdgeAssetAmount = asObject({ pluginId: asString, - tokenId: asOptional(asString, null), + tokenId: asEdgeTokenId, nativeAmount: asOptional(asIntegerString) }) export const asEdgeFiatAmount = asObject({ diff --git a/src/core/currency/wallet/currency-wallet-pixie.ts b/src/core/currency/wallet/currency-wallet-pixie.ts index ac4dc900b..56df77a58 100644 --- a/src/core/currency/wallet/currency-wallet-pixie.ts +++ b/src/core/currency/wallet/currency-wallet-pixie.ts @@ -131,8 +131,9 @@ export const walletPixie: TamePixie = combinePixies({ input.onOutput(engine) // Grab initial state: + const parentCurrency = { currencyCode, tokenId: null } const balance = asMaybe(asIntegerString)( - engine.getBalance({ currencyCode, tokenId: undefined }) + engine.getBalance(parentCurrency) ) if (balance != null) { input.props.dispatch({ diff --git a/src/core/login/splitting.ts b/src/core/login/splitting.ts index d6559ba0c..78b1e858a 100644 --- a/src/core/login/splitting.ts +++ b/src/core/login/splitting.ts @@ -179,9 +179,11 @@ export async function splitWalletInfo( } async function protectBchWallet(wallet: EdgeCurrencyWallet): Promise { + const bchCurrency = { currencyCode: 'BCH', tokenId: null } + // Create a UTXO which can be spend only on the ABC network const spendInfoSplit: EdgeSpendInfo = { - currencyCode: 'BCH', + ...bchCurrency, spendTargets: [ { nativeAmount: '10000', @@ -198,9 +200,9 @@ async function protectBchWallet(wallet: EdgeCurrencyWallet): Promise { await wallet.saveTx(broadcastedSplitTx) // Taint the rest of the wallet using the UTXO from before - const { publicAddress } = await wallet.getReceiveAddress() + const { publicAddress } = await wallet.getReceiveAddress(bchCurrency) const spendInfoTaint: EdgeSpendInfo = { - currencyCode: 'BCH', + ...bchCurrency, metadata: { name: 'Replay Protection Tx', notes: diff --git a/src/core/swap/swap-api.ts b/src/core/swap/swap-api.ts index 45fd7c8eb..42f6d0674 100644 --- a/src/core/swap/swap-api.ts +++ b/src/core/swap/swap-api.ts @@ -1,7 +1,6 @@ import { gt, lt } from 'biggystring' import { bridgifyObject, close } from 'yaob' -import { upgradeCurrencyCode } from '../../types/type-helpers' import { asMaybeInsufficientFundsError, asMaybePendingFundsError, @@ -34,27 +33,6 @@ export async function fetchSwapQuotes( const { swapSettings, userSettings } = account const swapPlugins = ai.props.state.plugins.swap - // Upgrade legacy currency codes: - const from = upgradeCurrencyCode({ - allTokens: request.fromWallet.currencyConfig.allTokens, - currencyInfo: request.fromWallet.currencyInfo, - currencyCode: request.fromCurrencyCode, - tokenId: request.fromTokenId - }) - const to = upgradeCurrencyCode({ - allTokens: request.toWallet.currencyConfig.allTokens, - currencyInfo: request.toWallet.currencyInfo, - currencyCode: request.toCurrencyCode, - tokenId: request.toTokenId - }) - request = { - ...request, - fromTokenId: from.tokenId, - toTokenId: to.tokenId, - fromCurrencyCode: from.currencyCode, - toCurrencyCode: to.currencyCode - } - log.warn( 'Requesting swap quotes for: ', { diff --git a/src/types/type-helpers.ts b/src/types/type-helpers.ts index 5bed8a865..9b2408fd3 100644 --- a/src/types/type-helpers.ts +++ b/src/types/type-helpers.ts @@ -9,7 +9,7 @@ export function upgradeCurrencyCode(opts: { currencyInfo: EdgeCurrencyInfo currencyCode?: string tokenId?: EdgeTokenId -}): { currencyCode: string; tokenId?: EdgeTokenId } { +}): { currencyCode: string; tokenId: EdgeTokenId } { const { currencyInfo, allTokens } = opts // Find the tokenId: @@ -27,5 +27,5 @@ export function upgradeCurrencyCode(opts: { // Get the currency code: const { currencyCode } = tokenId == null ? currencyInfo : allTokens[tokenId] - return { currencyCode, tokenId } + return { currencyCode, tokenId: tokenId ?? null } } diff --git a/src/types/types.ts b/src/types/types.ts index 6989e287f..fe4193a72 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -256,7 +256,7 @@ export interface EdgeMemo { export interface EdgeAssetAmount { pluginId: string - tokenId?: EdgeTokenId + tokenId: EdgeTokenId nativeAmount?: string } @@ -515,7 +515,7 @@ export interface EdgeMetadataChange { export interface EdgeNetworkFee2 { readonly nativeAmount: string readonly currencyPluginId: string - readonly tokenId?: EdgeTokenId + readonly tokenId: EdgeTokenId } export interface EdgeTxSwap { @@ -593,7 +593,7 @@ export interface EdgeTransaction { requestedCustomFee?: JsonObject feeRateUsed?: JsonObject spendTargets?: Array<{ - readonly currencyCode: string + readonly currencyCode: string // Saved for future reference readonly nativeAmount: string readonly publicAddress: string @@ -645,7 +645,7 @@ export interface EdgePaymentProtocolInfo { export interface EdgeSpendInfo { // Basic information: - tokenId?: EdgeTokenId + tokenId: EdgeTokenId privateKeys?: string[] spendTargets: EdgeSpendTarget[] memos?: EdgeMemo[] @@ -664,9 +664,6 @@ export interface EdgeSpendInfo { metadata?: EdgeMetadata swapData?: EdgeTxSwap otherParams?: JsonObject - - /** @deprecated Use tokenId instead */ - currencyCode?: string } // query data ---------------------------------------------------------- @@ -769,11 +766,8 @@ export interface EdgeEncodeUri { // options ------------------------------------------------------------- -export interface EdgeCurrencyCodeOptions { - tokenId?: EdgeTokenId - - /** @deprecated Use `tokenId` instead. */ - currencyCode?: string +export interface EdgeTokenIdOptions { + tokenId: EdgeTokenId } export interface EdgeGetTransactionsOptions { @@ -781,10 +775,7 @@ export interface EdgeGetTransactionsOptions { startDate?: Date endDate?: Date searchString?: string - tokenId?: EdgeTokenId - - /** @deprecated Use tokenId instead */ - currencyCode?: string + tokenId: EdgeTokenId } export interface EdgeStreamTransactionOptions { @@ -810,16 +801,16 @@ export interface EdgeStreamTransactionOptions { searchString?: string /** The token to query, or undefined for the main currency */ - tokenId?: EdgeTokenId + tokenId: EdgeTokenId } -export type EdgeGetReceiveAddressOptions = EdgeCurrencyCodeOptions & { +export type EdgeGetReceiveAddressOptions = EdgeTokenIdOptions & { forceIndex?: number } export interface EdgeEngineActivationOptions { // If null, activate parent wallet: - activateTokenIds?: string[] + activateTokenIds: EdgeTokenId[] // Wallet if the user is paying with a different currency: paymentInfo?: { @@ -834,24 +825,24 @@ export interface EdgeEngineGetActivationAssetsOptions { currencyWallets: { [walletId: string]: EdgeCurrencyWallet } // If null, activate parent wallet: - activateTokenIds?: string[] + activateTokenIds: EdgeTokenId[] } export interface EdgeEnginePrivateKeyOptions { privateKeys?: JsonObject } -export interface EdgeSaveTxMetadataOptions { +export interface EdgeSaveTxActionOptions { txid: string tokenId: EdgeTokenId - metadata: EdgeMetadataChange + assetAction: EdgeAssetAction + savedAction: EdgeTxAction } -export interface EdgeSaveTxActionOptions { +export interface EdgeSaveTxMetadataOptions { txid: string tokenId: EdgeTokenId - assetAction: EdgeAssetAction - savedAction: EdgeTxAction + metadata: EdgeMetadataChange } export interface EdgeSignMessageOptions { @@ -910,10 +901,10 @@ export interface EdgeCurrencyEngine { // Chain state: readonly getBlockHeight: () => number - readonly getBalance: (opts: EdgeCurrencyCodeOptions) => string - readonly getNumTransactions: (opts: EdgeCurrencyCodeOptions) => number + readonly getBalance: (opts: EdgeTokenIdOptions) => string + readonly getNumTransactions: (opts: EdgeTokenIdOptions) => number readonly getTransactions: ( - opts: EdgeCurrencyCodeOptions + opts: EdgeTokenIdOptions ) => Promise readonly getTxids?: () => EdgeTxidMap @@ -1071,21 +1062,21 @@ export interface EdgeCurrencyWalletEvents { export interface EdgeGetActivationAssetsOptions { activateWalletId: string // If null, activate parent wallet: - activateTokenIds?: string[] + activateTokenIds: EdgeTokenId[] } export interface EdgeGetActivationAssetsResults { assetOptions: Array<{ paymentWalletId?: string // If walletId is present, use MUST activate with this wallet currencyPluginId: string - tokenId?: EdgeTokenId + tokenId: EdgeTokenId }> } export interface EdgeActivationOptions { activateWalletId: string // If null, activate parent wallet: - activateTokenIds?: string[] + activateTokenIds: EdgeTokenId[] // Wallet if the user is paying with a different currency: paymentInfo?: { @@ -1104,7 +1095,7 @@ export interface EdgeActivationResult { export interface EdgeActivationQuote { readonly paymentWalletId: string - readonly paymentTokenId?: string + readonly paymentTokenId: EdgeTokenId readonly fromNativeAmount: string readonly networkFee: EdgeNetworkFee2 @@ -1168,19 +1159,17 @@ export interface EdgeCurrencyWallet { readonly detectedTokenIds: string[] // Transaction history: - readonly getNumTransactions: ( - opts?: EdgeCurrencyCodeOptions - ) => Promise + readonly getNumTransactions: (opts: EdgeTokenIdOptions) => Promise readonly getTransactions: ( - opts?: EdgeGetTransactionsOptions + opts: EdgeGetTransactionsOptions ) => Promise readonly streamTransactions: ( - opts?: EdgeStreamTransactionOptions + opts: EdgeStreamTransactionOptions ) => AsyncIterableIterator // Addresses: readonly getReceiveAddress: ( - opts?: EdgeGetReceiveAddressOptions + opts: EdgeGetReceiveAddressOptions ) => Promise readonly lockReceiveAddress: ( receiveAddress: EdgeReceiveAddress @@ -1256,18 +1245,12 @@ export interface EdgeSwapRequest { toWallet: EdgeCurrencyWallet // What? - fromTokenId?: EdgeTokenId - toTokenId?: EdgeTokenId + fromTokenId: EdgeTokenId + toTokenId: EdgeTokenId // How much? nativeAmount: string quoteFor: 'from' | 'max' | 'to' - - /** @deprecated Use fromTokenId instead */ - fromCurrencyCode?: string - - /** @deprecated Use toTokenId instead */ - toCurrencyCode?: string } /** diff --git a/test/fake/fake-currency-plugin.ts b/test/fake/fake-currency-plugin.ts index 8d05b97ec..4b3397769 100644 --- a/test/fake/fake-currency-plugin.ts +++ b/test/fake/fake-currency-plugin.ts @@ -2,7 +2,6 @@ import { add, lt } from 'biggystring' import { asNumber, asObject, asOptional, asString } from 'cleaners' import { - EdgeCurrencyCodeOptions, EdgeCurrencyEngine, EdgeCurrencyEngineCallbacks, EdgeCurrencyEngineOptions, @@ -17,6 +16,7 @@ import { EdgeSpendInfo, EdgeStakingStatus, EdgeToken, + EdgeTokenIdOptions, EdgeTokenMap, EdgeTransaction, EdgeWalletInfo, @@ -212,7 +212,7 @@ class FakeCurrencyEngine implements EdgeCurrencyEngine { return this.state.blockHeight } - getBalance(opts: EdgeCurrencyCodeOptions): string { + getBalance(opts: EdgeTokenIdOptions): string { const { tokenId = null } = opts if (tokenId == null) return this.state.balance.toString() if (tokenId === 'badf00d5') this.state.tokenBalance.toString() @@ -220,7 +220,7 @@ class FakeCurrencyEngine implements EdgeCurrencyEngine { throw new Error('Unknown currency') } - getNumTransactions(opts: EdgeCurrencyCodeOptions): number { + getNumTransactions(opts: EdgeTokenIdOptions): number { return Object.keys(this.state.txs).length }