From 8623b63f6bf92d23dd0038ebf84ea27cf8d72c58 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 21 Nov 2025 10:57:27 -0800 Subject: [PATCH 1/2] Add tokenId to EdgeTxSwap --- CHANGELOG.md | 2 ++ src/core/currency/wallet/currency-wallet-api.ts | 6 +++++- src/core/currency/wallet/currency-wallet-cleaners.ts | 5 +++-- src/types/type-helpers.ts | 9 +++++++++ src/types/types.ts | 1 + test/core/currency/wallet/currency-wallet.test.ts | 1 + test/fake/fake-transactions.ts | 3 +++ 7 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b16e3f8ac..5ae7116db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- added: Add `payoutTokenId` to `EdgeTxSwap` + ## 2.36.0 (2025-11-04) - added: Added `EdgeSubscribedAddress` type for `onSubscribeAddresses` and `startEngine`. diff --git a/src/core/currency/wallet/currency-wallet-api.ts b/src/core/currency/wallet/currency-wallet-api.ts index 55a9f02b6..a0807307f 100644 --- a/src/core/currency/wallet/currency-wallet-api.ts +++ b/src/core/currency/wallet/currency-wallet-api.ts @@ -10,6 +10,7 @@ import { } from '../../../client-side' import { upgradeCurrencyCode, + upgradeSwapData, upgradeTxNetworkFees } from '../../../types/type-helpers' import { @@ -597,9 +598,12 @@ export function makeCurrencyWalletApi( tx.currencyCode = upgradedCurrency.currencyCode tx.tokenId = upgradedCurrency.tokenId if (metadata != null) tx.metadata = metadata - if (swapData != null) tx.swapData = asEdgeTxSwap(swapData) if (savedAction != null) tx.savedAction = asEdgeTxAction(savedAction) if (assetAction != null) tx.assetAction = asEdgeAssetAction(assetAction) + if (swapData != null) { + tx.swapData = asEdgeTxSwap(swapData) + upgradeSwapData(tx) + } if (input.props.state.login.deviceDescription != null) tx.deviceDescription = input.props.state.login.deviceDescription diff --git a/src/core/currency/wallet/currency-wallet-cleaners.ts b/src/core/currency/wallet/currency-wallet-cleaners.ts index 703dbfdf4..9c19cba75 100644 --- a/src/core/currency/wallet/currency-wallet-cleaners.ts +++ b/src/core/currency/wallet/currency-wallet-cleaners.ts @@ -120,6 +120,8 @@ interface LegacyMapFile { // building-block cleaners // --------------------------------------------------------------------- +export const asEdgeTokenId = asEither(asString, asNull) + const asFeeRate: Cleaner<'high' | 'standard' | 'low'> = asValue( 'high', 'standard', @@ -141,6 +143,7 @@ export const asEdgeTxSwap = asObject({ // Address information: payoutAddress: asString, payoutCurrencyCode: asString, + payoutTokenId: asEdgeTokenId, payoutNativeAmount: asString, payoutWalletId: asString, refundAddress: asOptional(asString) @@ -158,8 +161,6 @@ export function asIntegerString(raw: unknown): string { // file cleaners // --------------------------------------------------------------------- -export const asEdgeTokenId = asEither(asString, asNull) - export const asEdgeAssetAmount = asObject({ pluginId: asString, tokenId: asEdgeTokenId, diff --git a/src/types/type-helpers.ts b/src/types/type-helpers.ts index 0443419b4..799c2419c 100644 --- a/src/types/type-helpers.ts +++ b/src/types/type-helpers.ts @@ -56,3 +56,12 @@ export const upgradeTxNetworkFees = (tx: EdgeTransaction): void => { } } } + +// Swap data could be present without the payoutTokenId. We can add it if there's a swap savedAction present. +export const upgradeSwapData = (tx: EdgeTransaction): void => { + if (tx.swapData != null && tx.swapData?.payoutTokenId === undefined) { + if (tx.savedAction != null && tx.savedAction.actionType === 'swap') { + tx.swapData.payoutTokenId = tx.savedAction.toAsset.tokenId + } + } +} diff --git a/src/types/types.ts b/src/types/types.ts index 824462a46..83c077ce9 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -569,6 +569,7 @@ export interface EdgeTxSwap { payoutAddress: string payoutCurrencyCode: string payoutNativeAmount: string + payoutTokenId: EdgeTokenId payoutWalletId: string refundAddress?: string } diff --git a/test/core/currency/wallet/currency-wallet.test.ts b/test/core/currency/wallet/currency-wallet.test.ts index e0e514195..dfd14db20 100644 --- a/test/core/currency/wallet/currency-wallet.test.ts +++ b/test/core/currency/wallet/currency-wallet.test.ts @@ -558,6 +558,7 @@ describe('currency wallets', function () { }, payoutAddress: 'get it here', payoutCurrencyCode: 'TOKEN', + payoutTokenId: 'TOKEN', payoutNativeAmount: '1', payoutWalletId: wallet.id } diff --git a/test/fake/fake-transactions.ts b/test/fake/fake-transactions.ts index 57370f3e4..0efa2ab42 100644 --- a/test/fake/fake-transactions.ts +++ b/test/fake/fake-transactions.ts @@ -278,6 +278,7 @@ export const walletTxs: { [txid: string]: Partial } = { }, payoutAddress: '0x1d21f26739b20caf898aa1d2c37007577309e466', payoutCurrencyCode: 'eth', + payoutTokenId: null, payoutNativeAmount: '85486000000000000', payoutWalletId: '2D6BLbfk/Ex8jIt8wvy/uuD2rRjGklj00gW0gUfxrnc=', refundAddress: '33zRiHEfmpt9Hirga6GQb8Xp71veiq3NhJ' @@ -328,6 +329,7 @@ export const walletTxs: { [txid: string]: Partial } = { }, payoutAddress: '0x1d21f26739b20caf898aa1d2c37007577309e466', payoutCurrencyCode: 'eth', + payoutTokenId: null, payoutNativeAmount: '45188000000000000', payoutWalletId: '2D6BLbfk/Ex8jIt8wvy/uuD2rRjGklj00gW0gUfxrnc=', refundAddress: '33eFPT8vFvgCHMijNR7LpqNZBMR2utcq1P' @@ -378,6 +380,7 @@ export const walletTxs: { [txid: string]: Partial } = { }, payoutAddress: 'MKC9DxwFjXCSprj6HNXBvvqDGviMrTmV9q', payoutCurrencyCode: 'ltc', + payoutTokenId: null, payoutNativeAmount: '12518100', payoutWalletId: 'aumm1+iQsXDXxiSYzuX8qToQpwQXX03CTVKomKPTLaQ=', refundAddress: '33eFPT8vFvgCHMijNR7LpqNZBMR2utcq1P' From ac53f48860a06c32620c98331414c5b1237df5d8 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 21 Nov 2025 13:18:00 -0800 Subject: [PATCH 2/2] Add tokenId-ready replacements for denomination conversion tools --- CHANGELOG.md | 1 + src/core/currency/currency-selectors.ts | 28 +++++++++++++++++++ .../currency/wallet/currency-wallet-api.ts | 27 +++++++++++++++++- src/types/types.ts | 10 +++++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ae7116db..b5be44f60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- added: Add tokenId-based denomination conversion utils to wallets - added: Add `payoutTokenId` to `EdgeTxSwap` ## 2.36.0 (2025-11-04) diff --git a/src/core/currency/currency-selectors.ts b/src/core/currency/currency-selectors.ts index 1fca70fd8..f9eb5f7aa 100644 --- a/src/core/currency/currency-selectors.ts +++ b/src/core/currency/currency-selectors.ts @@ -1,10 +1,13 @@ import { EdgeCurrencyInfo, EdgeCurrencyWallet, + EdgeToken, + EdgeTokenId, EdgeTokenMap } from '../../types/types' import { ApiInput, RootProps } from '../root-pixie' +/** @deprecated Use `getTokenMultiplier` instead. */ export function getCurrencyMultiplier( currencyInfo: EdgeCurrencyInfo, allTokens: EdgeTokenMap, @@ -28,6 +31,31 @@ export function getCurrencyMultiplier( return '1' } +export function getTokenMultiplier( + currencyInfo: EdgeCurrencyInfo, + allTokens: EdgeTokenMap, + tokenId: EdgeTokenId +): string { + if (tokenId == null) { + for (const denomination of currencyInfo.denominations) { + if (denomination.name === currencyInfo.currencyCode) { + return denomination.multiplier + } + } + } else { + const token: EdgeToken | undefined = allTokens[tokenId] + if (token != null) { + for (const denomination of token.denominations) { + if (denomination.name === token.currencyCode) { + return denomination.multiplier + } + } + } + } + + return '1' +} + export function waitForCurrencyWallet( ai: ApiInput, walletId: string diff --git a/src/core/currency/wallet/currency-wallet-api.ts b/src/core/currency/wallet/currency-wallet-api.ts index a0807307f..8ca01467a 100644 --- a/src/core/currency/wallet/currency-wallet-api.ts +++ b/src/core/currency/wallet/currency-wallet-api.ts @@ -43,7 +43,10 @@ import { import { makeMetaTokens } from '../../account/custom-tokens' import { toApiInput } from '../../root-pixie' import { makeStorageWalletApi } from '../../storage/storage-api' -import { getCurrencyMultiplier } from '../currency-selectors' +import { + getCurrencyMultiplier, + getTokenMultiplier +} from '../currency-selectors' import { makeCurrencyWalletCallbacks } from './currency-wallet-callbacks' import { asEdgeAssetAction, @@ -163,6 +166,28 @@ export function makeCurrencyWalletApi( get currencyInfo(): EdgeCurrencyInfo { return plugin.currencyInfo }, + async convertDenominatedToNative( + denominatedAmount: string, + tokenId: EdgeTokenId + ): Promise { + const multiplier = getTokenMultiplier( + plugin.currencyInfo, + input.props.state.accounts[accountId].allTokens[pluginId], + tokenId + ) + return mul(denominatedAmount, multiplier) + }, + async convertNativeToDenominated( + nativeAmount: string, + tokenId: EdgeTokenId + ): Promise { + const multiplier = getTokenMultiplier( + plugin.currencyInfo, + input.props.state.accounts[accountId].allTokens[pluginId], + tokenId + ) + return div(nativeAmount, multiplier, multiplier.length) + }, async denominationToNative( denominatedAmount: string, currencyCode: string diff --git a/src/types/types.ts b/src/types/types.ts index 83c077ce9..cce69c295 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1369,6 +1369,16 @@ export interface EdgeCurrencyWallet { // Generic: readonly otherMethods: EdgeOtherMethods + readonly convertDenominatedToNative: ( + denominatedAmount: string, + tokenId: EdgeTokenId + ) => Promise + + readonly convertNativeToDenominated: ( + nativeAmount: string, + tokenId: EdgeTokenId + ) => Promise + /** @deprecated Use the information in EdgeCurrencyInfo / EdgeToken. */ readonly denominationToNative: ( denominatedAmount: string,