-
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.
refactor(core)!: changes value sent and received inspectors
- Loading branch information
1 parent
47efa0e
commit bdecf31
Showing
17 changed files
with
538 additions
and
50 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
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,3 +1,5 @@ | ||
export * from './assetId'; | ||
export * from './metadatumToCip25'; | ||
export * from './coalesceTokenMaps'; | ||
export * from './removeNegativesFromTokenMap'; | ||
export * from './subtractTokenMaps'; |
18 changes: 18 additions & 0 deletions
18
packages/core/src/Asset/util/removeNegativesFromTokenMap.ts
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TokenMap } from '../../Cardano'; | ||
|
||
/** | ||
* Remove all negative quantities from a TokenMap. | ||
* Does not modify the original TokenMap | ||
* | ||
* @param {TokenMap} assets TokenMap to remove negative quantities | ||
* @returns {TokenMap} a copy of `assets` with negative quantities removed, could be empty | ||
*/ | ||
export const removeNegativesFromTokenMap = (assets: TokenMap): TokenMap => { | ||
const result: TokenMap = new Map(assets); | ||
for (const [assetId, assetQuantity] of result) { | ||
if (assetQuantity < 0) { | ||
result.delete(assetId); | ||
} | ||
} | ||
return result; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { TokenMap } from '../../Cardano'; | ||
import { util } from '../../util'; | ||
|
||
/** | ||
* Subtract asset quantities in order | ||
*/ | ||
export const subtractTokenMaps = (assets: (TokenMap | undefined)[]): TokenMap | undefined => { | ||
if (assets.length <= 0 || !util.isNotNil(assets[0])) return undefined; | ||
const result: TokenMap = assets[0]; | ||
const rest: TokenMap[] = assets.slice(1).filter(util.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); | ||
} | ||
} | ||
if (result.size === 0) { | ||
return undefined; | ||
} | ||
return result; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { TxAlonzo, TxIn, Value } from '../types'; | ||
|
||
/** | ||
* Resolves the value of an input by looking for the matching output in a list of transactions | ||
* | ||
* @param {TxIn} input input to resolve value for | ||
* @param {TxAlonzo[]} transactions list of transactions to find the matching output | ||
* @returns {Value | undefined} input value or undefined if not found | ||
*/ | ||
export const resolveInputValue = (input: TxIn, transactions: TxAlonzo[]): Value | undefined => { | ||
const tx = transactions.find((transaction) => transaction.id === input.txId); | ||
return tx?.body.outputs[input.index]?.value; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Asset, BigIntMath } from '../..'; | ||
import { Value } from '../types'; | ||
|
||
/** | ||
* Subtract all quantities | ||
*/ | ||
export const subtractValueQuantities = (quantities: Value[]) => ({ | ||
assets: Asset.util.subtractTokenMaps(quantities.map(({ assets }) => assets)), | ||
coins: BigIntMath.subtract(quantities.map(({ coins }) => coins)) | ||
}); |
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
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
22 changes: 22 additions & 0 deletions
22
packages/core/test/Asset/util/removeNegativesFromTokenMap.test.ts
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Asset } from '@cardano-sdk/core'; | ||
import { AssetId } from '@cardano-sdk/util-dev'; | ||
|
||
describe('Asset', () => { | ||
describe('util', () => { | ||
describe('removeNegativesFromTokenMap', () => { | ||
it('should delete tokens with negative quantities from a token map', () => { | ||
const asset = new Map([ | ||
[AssetId.PXL, -100n], | ||
[AssetId.Unit, 50n], | ||
[AssetId.TSLA, 0n] | ||
]); | ||
expect(Asset.util.removeNegativesFromTokenMap(asset)).toEqual( | ||
new Map([ | ||
[AssetId.Unit, 50n], | ||
[AssetId.TSLA, 0n] | ||
]) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Asset } from '@cardano-sdk/core'; | ||
import { AssetId } from '@cardano-sdk/util-dev'; | ||
|
||
describe('Asset', () => { | ||
describe('util', () => { | ||
describe('subtractTokenMaps', () => { | ||
it('should subtract quantities correctly when all assets have the same tokens', () => { | ||
const initialAsset = new Map([ | ||
[AssetId.PXL, 100n], | ||
[AssetId.Unit, 50n] | ||
]); | ||
const asset2 = new Map([ | ||
[AssetId.PXL, 23n], | ||
[AssetId.Unit, 20n] | ||
]); | ||
expect(Asset.util.subtractTokenMaps([initialAsset, asset2])).toEqual( | ||
new Map([ | ||
[AssetId.PXL, 77n], | ||
[AssetId.Unit, 30n] | ||
]) | ||
); | ||
}); | ||
it('should delete tokens from result when quantity is 0', () => { | ||
const initialAsset = new Map([ | ||
[AssetId.PXL, 100n], | ||
[AssetId.Unit, 50n] | ||
]); | ||
const asset2 = new Map([ | ||
[AssetId.PXL, 23n], | ||
[AssetId.Unit, 50n] | ||
]); | ||
expect(Asset.util.subtractTokenMaps([initialAsset, asset2])).toEqual(new Map([[AssetId.PXL, 77n]])); | ||
}); | ||
it('should be able to return negative quantities', () => { | ||
const initialAsset = new Map([ | ||
[AssetId.PXL, 100n], | ||
[AssetId.Unit, 50n] | ||
]); | ||
const asset2 = new Map([ | ||
[AssetId.PXL, 173n], | ||
[AssetId.Unit, 50n] | ||
]); | ||
const asset3 = new Map([[AssetId.TSLA, 44n]]); | ||
expect(Asset.util.subtractTokenMaps([initialAsset, asset2, asset3])).toEqual( | ||
new Map([ | ||
[AssetId.PXL, -73n], | ||
[AssetId.TSLA, -44n] | ||
]) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.