-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): subtractTokenMaps now properly subtract when there is an a…
…sset missing in one map
- Loading branch information
1 parent
dc6e2ea
commit 03e84bb
Showing
3 changed files
with
122 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,80 @@ | ||
import { TokenMap } from '../../Cardano'; | ||
import { isNotNil } from '@cardano-sdk/util'; | ||
/* eslint-disable complexity,sonarjs/cognitive-complexity */ | ||
import * as Cardano from '../../Cardano'; | ||
import uniq from 'lodash/uniq'; | ||
|
||
/** Subtract asset quantities in order */ | ||
export const subtractTokenMaps = (assets: (TokenMap | undefined)[]): TokenMap | undefined => { | ||
if (assets.length <= 0 || !isNotNil(assets[0])) return undefined; | ||
const result: TokenMap = new Map(assets[0]); | ||
const rest: TokenMap[] = assets.slice(1).filter(isNotNil); | ||
for (const assetTotals of rest) { | ||
for (const [assetId, assetQuantity] of assetTotals.entries()) { | ||
const total = result.get(assetId) ?? 0n; | ||
const diff = total - assetQuantity; | ||
diff === 0n ? result.delete(assetId) : result.set(assetId, diff); | ||
/** | ||
* Given two Cardano.TokenMaps, compute a Cardano.TokenMap with the difference between the left-hand side and the right-hand side. | ||
* | ||
* @param lhs the left-hand side of the subtraction operation. | ||
* @param rhs the right-hand side of the subtraction operation. | ||
* @returns The difference between both Cardano.TokenMaps. | ||
*/ | ||
export const subtractMaps = ( | ||
lhs: Cardano.TokenMap | undefined, | ||
rhs: Cardano.TokenMap | undefined | ||
): Cardano.TokenMap | undefined => { | ||
if (!rhs) { | ||
if (!lhs) return undefined; | ||
|
||
const nonEmptyValues = new Map<Cardano.AssetId, bigint>(); | ||
|
||
for (const [key, value] of lhs.entries()) { | ||
if (value !== 0n) nonEmptyValues.set(key, value); | ||
} | ||
|
||
return nonEmptyValues; | ||
} | ||
|
||
if (!lhs) { | ||
const negativeValues = new Map<Cardano.AssetId, bigint>(); | ||
|
||
for (const [key, value] of rhs.entries()) { | ||
if (value !== 0n) negativeValues.set(key, -value); | ||
} | ||
|
||
return negativeValues; | ||
} | ||
|
||
const result = new Map<Cardano.AssetId, bigint>(); | ||
const intersection = new Array<Cardano.AssetId>(); | ||
|
||
// any element that is present in the lhs and not in the rhs will be added as a positive value | ||
for (const [key, value] of lhs.entries()) { | ||
if (rhs.has(key)) { | ||
intersection.push(key); | ||
continue; | ||
} | ||
|
||
if (value !== 0n) result.set(key, value); | ||
} | ||
if (result.size === 0) { | ||
return undefined; | ||
|
||
// any element that is present in the rhs and not in the lhs will be added as a negative value | ||
for (const [key, value] of rhs.entries()) { | ||
if (lhs.has(key)) { | ||
intersection.push(key); | ||
continue; | ||
} | ||
|
||
if (value !== 0n) result.set(key, -value); | ||
} | ||
|
||
// Elements present in both maps will be subtracted (lhs - rhs) | ||
const uniqIntersection = uniq(intersection); | ||
|
||
for (const id of uniqIntersection) { | ||
const lshVal = lhs.get(id); | ||
const rshVal = rhs.get(id); | ||
const remainingCoins = lshVal! - rshVal!; | ||
|
||
if (remainingCoins !== 0n) result.set(id, remainingCoins); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
/** Subtract asset quantities in order */ | ||
export const subtractTokenMaps = (assets: (Cardano.TokenMap | undefined)[]): Cardano.TokenMap | undefined => { | ||
if (!assets || assets.length === 0) return undefined; | ||
|
||
return assets.reduce(subtractMaps); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters