Skip to content

Commit

Permalink
Make tokenId's mandatory in most API's
Browse files Browse the repository at this point in the history
  • Loading branch information
swansontec committed Jan 3, 2024
1 parent 1c26084 commit 635384f
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 115 deletions.
2 changes: 1 addition & 1 deletion src/client-side.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ shareData({ fixUsername })
*/
export function streamTransactions(
this: InternalWalletMethods,
opts: EdgeStreamTransactionOptions = {}
opts: EdgeStreamTransactionOptions
): AsyncIterableIterator<EdgeTransaction[]> {
let stream: InternalWalletStream | undefined
let streamClosed = false
Expand Down
58 changes: 22 additions & 36 deletions src/core/currency/wallet/currency-wallet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { upgradeCurrencyCode } from '../../../types/type-helpers'
import {
EdgeBalances,
EdgeCurrencyCodeOptions,
EdgeCurrencyConfig,
EdgeCurrencyEngine,
EdgeCurrencyInfo,
Expand All @@ -30,6 +29,7 @@ import {
EdgeStakingStatus,
EdgeStreamTransactionOptions,
EdgeTokenId,
EdgeTokenIdOptions,
EdgeTransaction,
EdgeWalletInfo
} from '../../../types/types'
Expand Down Expand Up @@ -217,17 +217,14 @@ export function makeCurrencyWalletApi(
},

// Transactions history:
async getNumTransactions(
opts: EdgeCurrencyCodeOptions = {}
): Promise<number> {
const { currencyCode, tokenId } = upgradeCurrencyCode({
async getNumTransactions(opts: EdgeTokenIdOptions): Promise<number> {
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(
Expand All @@ -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',
Expand Down Expand Up @@ -321,26 +319,20 @@ export function makeCurrencyWalletApi(
},

async getTransactions(
opts: EdgeGetTransactionsOptions = {}
opts: EdgeGetTransactionsOptions
): Promise<EdgeTransaction[]> {
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:
Expand All @@ -356,19 +348,17 @@ export function makeCurrencyWalletApi(

// Addresses:
async getReceiveAddress(
opts: EdgeGetReceiveAddressOptions = {}
opts: EdgeGetReceiveAddressOptions
): Promise<EdgeReceiveAddress> {
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,
Expand Down Expand Up @@ -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:
Expand All @@ -437,8 +426,7 @@ export function makeCurrencyWalletApi(
return engine
.makeSpend(
{
currencyCode,
tokenId,
...upgradedCurrency,
spendTargets,
networkFeeOption,
customNetworkFee
Expand Down Expand Up @@ -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
})

Expand All @@ -507,7 +494,7 @@ export function makeCurrencyWalletApi(
uniqueIdentifier: memo
})
savedTargets.push({
currencyCode,
currencyCode: upgradedCurrency.currencyCode,
memo,
nativeAmount,
publicAddress,
Expand All @@ -524,7 +511,7 @@ export function makeCurrencyWalletApi(

const tx: EdgeTransaction = await engine.makeSpend(
{
currencyCode,
...upgradedCurrency,
customNetworkFee,
memos,
metadata,
Expand All @@ -534,8 +521,7 @@ export function makeCurrencyWalletApi(
pendingTxs,
rbfTxid,
skipChecks,
spendTargets: cleanTargets,
tokenId
spendTargets: cleanTargets
},
{ privateKeys }
)
Expand Down
4 changes: 3 additions & 1 deletion src/core/currency/wallet/currency-wallet-cleaners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ export function asIntegerString(raw: unknown): string {
// file cleaners
// ---------------------------------------------------------------------

export const asEdgeTokenId = asEither(asString, asNull)

export const asEdgeAssetAmount = asObject<EdgeAssetAmount>({
pluginId: asString,
tokenId: asOptional(asString, null),
tokenId: asEdgeTokenId,
nativeAmount: asOptional(asIntegerString)
})
export const asEdgeFiatAmount = asObject<EdgeFiatAmount>({
Expand Down
3 changes: 2 additions & 1 deletion src/core/currency/wallet/currency-wallet-pixie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,9 @@ export const walletPixie: TamePixie<CurrencyWalletProps> = 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({
Expand Down
8 changes: 5 additions & 3 deletions src/core/login/splitting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ export async function splitWalletInfo(
}

async function protectBchWallet(wallet: EdgeCurrencyWallet): Promise<void> {
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',
Expand All @@ -198,9 +200,9 @@ async function protectBchWallet(wallet: EdgeCurrencyWallet): Promise<void> {
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:
Expand Down
22 changes: 0 additions & 22 deletions src/core/swap/swap-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { gt, lt } from 'biggystring'
import { bridgifyObject, close } from 'yaob'

import { upgradeCurrencyCode } from '../../types/type-helpers'
import {
asMaybeInsufficientFundsError,
asMaybePendingFundsError,
Expand Down Expand Up @@ -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: ',
{
Expand Down
4 changes: 2 additions & 2 deletions src/types/type-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 }
}
Loading

0 comments on commit 635384f

Please sign in to comment.