Skip to content

Commit

Permalink
refactor: consolidate core/Ogmios/util into core Cardano and Asset mo…
Browse files Browse the repository at this point in the history
…dules
  • Loading branch information
mkazlauskas committed Oct 26, 2021
1 parent b231031 commit ee7108e
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 89 deletions.
4 changes: 2 additions & 2 deletions packages/cip2/src/RoundRobinRandomImprove/change.ts
@@ -1,4 +1,4 @@
import { Ogmios, CSL, Cardano, coreToCsl } from '@cardano-sdk/core';
import { CSL, Cardano, coreToCsl } from '@cardano-sdk/core';
import { orderBy } from 'lodash-es';
import { ComputeMinimumCoinQuantity, TokenBundleSizeExceedsLimit } from '../types';
import { InputSelectionError, InputSelectionFailure } from '../InputSelectionError';
Expand Down Expand Up @@ -195,7 +195,7 @@ const coalesceChangeBundlesForMinCoinRequirement = (

while (sortedBundles.length > 1 && !satisfiesMinCoinRequirement(sortedBundles[sortedBundles.length - 1])) {
const smallestBundle = sortedBundles.pop()!;
sortedBundles[sortedBundles.length - 1] = Ogmios.util.coalesceValueQuantities([
sortedBundles[sortedBundles.length - 1] = Cardano.util.coalesceValueQuantities([
sortedBundles[sortedBundles.length - 1],
smallestBundle
]);
Expand Down
14 changes: 7 additions & 7 deletions packages/cip2/test/util/properties.ts
@@ -1,6 +1,6 @@
import { AssetId, SelectionConstraints } from '@cardano-sdk/util-dev';
import { ImplicitCoin, SelectionResult } from '../../src/types';
import { cslUtil, Cardano, Ogmios, CSL, cslToCore } from '@cardano-sdk/core';
import { cslUtil, Cardano, CSL, cslToCore } from '@cardano-sdk/core';
import { InputSelectionError, InputSelectionFailure } from '../../src/InputSelectionError';
import fc, { Arbitrary } from 'fast-check';

Expand All @@ -24,10 +24,10 @@ const assertExtraChangeProperties = (
};

const totalOutputsValue = (outputs: Set<CSL.TransactionOutput>) =>
Ogmios.util.coalesceValueQuantities([...outputs].map((output) => cslToCore.value(output.amount())));
Cardano.util.coalesceValueQuantities([...outputs].map((output) => cslToCore.value(output.amount())));

const totalUtxosValue = (results: SelectionResult) =>
Ogmios.util.coalesceValueQuantities(
Cardano.util.coalesceValueQuantities(
[...results.selection.inputs].map((selectedUtxo) => cslToCore.value(selectedUtxo.output().amount()))
);

Expand All @@ -51,7 +51,7 @@ const inputSelectionTotals = ({
...vRequestedOutputs,
coins: vRequestedOutputs.coins + BigInt(implicitCoin?.deposit || 0) + vFee
};
const vChange = Ogmios.util.coalesceValueQuantities(
const vChange = Cardano.util.coalesceValueQuantities(
[...results.selection.change].map((value) => cslToCore.value(value))
);
return { vSelected, vRequested, vChange };
Expand Down Expand Up @@ -115,11 +115,11 @@ export const assertFailureProperties = ({
constraints: SelectionConstraints.MockSelectionConstraints;
implicitCoin?: ImplicitCoin;
}) => {
const availableQuantities = Ogmios.util.coalesceValueQuantities([
const availableQuantities = Cardano.util.coalesceValueQuantities([
...utxoAmounts,
{ coins: BigInt(implicitCoin?.input || 0) }
]);
const requestedQuantities = Ogmios.util.coalesceValueQuantities([
const requestedQuantities = Cardano.util.coalesceValueQuantities([
...outputsAmounts,
{ coins: BigInt(implicitCoin?.deposit || 0) + constraints.minimumCost }
]);
Expand Down Expand Up @@ -183,7 +183,7 @@ export const generateSelectionParams = (() => {
)
.filter((values) => {
// sum of coin or any asset can't exceed MAX_U64
const { coins, assets } = Ogmios.util.coalesceValueQuantities(values);
const { coins, assets } = Cardano.util.coalesceValueQuantities(values);
return (
coins + implicitCoin <= cslUtil.MAX_U64 &&
(!assets || Object.values(assets).every((quantity) => quantity <= cslUtil.MAX_U64))
Expand Down
17 changes: 17 additions & 0 deletions packages/core/src/Asset/util.ts
@@ -1,3 +1,4 @@
import { TokenMap } from '../Cardano';
import { CSL } from '../CSL';

export type AssetId = string;
Expand All @@ -19,3 +20,19 @@ export const parseAssetId = (assetId: AssetId) => {
assetName: CSL.AssetName.new(Buffer.from(assetName, 'hex'))
};
};

/**
* Sum asset quantities
*/
export const coalesceTokenMaps = (totals: (TokenMap | undefined)[]): TokenMap | undefined => {
const result: TokenMap = {};
for (const assetTotals of totals.filter((quantities) => !!quantities)) {
for (const assetKey in assetTotals) {
result[assetKey] = (result[assetKey] || 0n) + assetTotals[assetKey];
}
}
if (Object.keys(result).length === 0) {
return undefined;
}
return result;
};
11 changes: 11 additions & 0 deletions packages/core/src/Cardano/util.ts
@@ -1,4 +1,15 @@
import { Asset, BigIntMath } from '..';
import { Value } from './types';

/**
* Blockchain restriction for minimum coin quantity in a UTxO
*/
export const computeMinUtxoValue = (coinsPerUtxoWord: bigint): bigint => coinsPerUtxoWord * 29n;

/**
* Sum all quantities
*/
export const coalesceValueQuantities = (quantities: Value[]): Value => ({
coins: BigIntMath.sum(quantities.map(({ coins }) => coins)),
assets: Asset.util.coalesceTokenMaps(quantities.map(({ assets }) => assets))
});
1 change: 0 additions & 1 deletion packages/core/src/Ogmios/index.ts

This file was deleted.

26 changes: 0 additions & 26 deletions packages/core/src/Ogmios/util.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/index.ts
@@ -1,6 +1,5 @@
export * as Asset from './Asset';
export * as Cardano from './Cardano';
export * as Ogmios from './Ogmios';
export * from './Genesis';
export * from './WalletProvider';
export * as Transaction from './Transaction';
Expand Down
37 changes: 35 additions & 2 deletions packages/core/test/Cardano/util.test.ts
@@ -1,7 +1,40 @@
import { computeMinUtxoValue } from '../../src/Cardano/util';
import * as Cardano from '../../src/Cardano';

describe('Cardano', () => {
describe('util', () => {
it('computeMinUtxoValue', () => expect(typeof computeMinUtxoValue(100n)).toBe('bigint'));
it('computeMinUtxoValue', () => expect(typeof Cardano.util.computeMinUtxoValue(100n)).toBe('bigint'));

describe('coalesceValueQuantities', () => {
it('coin only', () => {
const q1: Cardano.Value = { coins: 50n };
const q2: Cardano.Value = { coins: 100n };
expect(Cardano.util.coalesceValueQuantities([q1, q2])).toEqual({ coins: 150n });
});
it('coin and assets', () => {
const TSLA_Asset = 'b32_1vk0jj9lmv0cjkvmxw337u467atqcgkauwd4eczaugzagyghp25lTSLA';
const PXL_Asset = 'b32_1rmy9mnhz0ukepmqlng0yee62ve7un05trpzxxg3lnjtqzp4dmmrPXL';
const q1: Cardano.Value = {
coins: 50n,
assets: {
[TSLA_Asset]: 50n,
[PXL_Asset]: 100n
}
};
const q2: Cardano.Value = { coins: 100n };
const q3: Cardano.Value = {
coins: 20n,
assets: {
[TSLA_Asset]: 20n
}
};
expect(Cardano.util.coalesceValueQuantities([q1, q2, q3])).toEqual({
coins: 170n,
assets: {
[TSLA_Asset]: 70n,
[PXL_Asset]: 100n
}
});
});
});
});
});
39 changes: 0 additions & 39 deletions packages/core/test/Ogmios/util.test.ts

This file was deleted.

12 changes: 2 additions & 10 deletions packages/wallet/src/BalanceTracker.ts
@@ -1,4 +1,4 @@
import { Cardano, Ogmios } from '@cardano-sdk/core';
import { Cardano } from '@cardano-sdk/core';
import Emittery from 'emittery';
import { dummyLogger } from 'ts-log';
import { UtxoRepository, UtxoRepositoryEvent, UtxoRepositoryFields } from './types';
Expand Down Expand Up @@ -48,14 +48,6 @@ export class BalanceTracker extends Emittery<BalanceTrackerEvents> implements Ba
}

#getBalance(utxo: Cardano.Utxo[]): Cardano.Value {
return Ogmios.util.coalesceValueQuantities(
utxo.map(([_, txOut]) => {
const { coins, assets } = txOut.value;
return {
coins: BigInt(coins),
assets
};
})
);
return Cardano.util.coalesceValueQuantities(utxo.map(([_, txOut]) => txOut.value));
}
}
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit ee7108e

Please sign in to comment.