edge-core-js
Version:
Edge account & wallet management library
59 lines (42 loc) • 1.35 kB
JavaScript
function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
/**
* Translates a currency code to a tokenId,
* and then back again for bi-directional backwards compatibility.
*/
export function upgradeCurrencyCode(opts
) {
const { currencyInfo, allTokens } = opts
// Find the tokenId:
let tokenId = opts.tokenId
if (
tokenId === undefined &&
opts.currencyCode != null &&
opts.currencyCode !== currencyInfo.currencyCode
) {
tokenId = Object.keys(allTokens).find(
tokenId => allTokens[tokenId].currencyCode === opts.currencyCode
)
}
// Get the currency code:
const { currencyCode } = tokenId == null ? currencyInfo : allTokens[tokenId]
return { currencyCode, tokenId: _nullishCoalesce(tokenId, () => ( null)) }
}
export function upgradeSwapQuote(quote) {
if (quote.networkFee != null && quote.networkFee.tokenId == null) {
quote.networkFee.tokenId = quote.request.fromTokenId
}
return quote
}
export const upgradeTxNetworkFees = (tx) => {
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 })
}
}
}