Skip to content

Commit

Permalink
feat(common): add utxoSumResultDiff() function
Browse files Browse the repository at this point in the history
  • Loading branch information
capt-nemo429 committed Feb 20, 2023
1 parent 198a0b7 commit 79caa7b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/common/src/utils/bigIntUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function _removeLeadingZeros(value: string): string {
}

export function sumBy<T>(
collection: T[],
collection: readonly T[],
iteratee: (value: T) => bigint,
condition?: (value: T) => boolean
): bigint {
Expand Down
39 changes: 38 additions & 1 deletion packages/common/src/utils/boxUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box } from "../types";
import { sumBy } from "./bigIntUtils";
import { areRegistersDenselyPacked, utxoSum } from "./boxUtils";
import { areRegistersDenselyPacked, BoxAmounts, utxoSum, utxoSumResultDiff } from "./boxUtils";

export const regularBoxesMock: Box<bigint>[] = [
{
Expand Down Expand Up @@ -244,6 +244,43 @@ describe("UTxO sum", () => {
});
});

describe("utxoSumResultDiff()", () => {
it("Should calculate the difference between two summaries", () => {
const sumA: BoxAmounts = {
nanoErgs: 100n,
tokens: [
{ tokenId: "token_id_1", amount: 5n },
{ tokenId: "token_id_2", amount: 875n },
{ tokenId: "token_id_3", amount: 100n },
{ tokenId: "token_id_4", amount: 200n }
]
};

const sumB: BoxAmounts = {
nanoErgs: 10n,
tokens: [
{ tokenId: "token_id_1", amount: 2n },
{ tokenId: "token_id_2", amount: 880n },
{ tokenId: "token_id_3", amount: 100n }
]
};

expect(utxoSumResultDiff(sumA, sumB)).toEqual({
nanoErgs: 90n,
tokens: [
{ tokenId: "token_id_1", amount: 3n },
{ tokenId: "token_id_2", amount: -5n },
{ tokenId: "token_id_4", amount: 200n }
]
});

expect(utxoSumResultDiff(sumA, sumA)).toEqual({
nanoErgs: 0n,
tokens: []
});
});
});

describe("Densely pack check - areRegistersDenselyPacked()", () => {
it("Should pass for VALID registers packing", () => {
expect(
Expand Down
26 changes: 21 additions & 5 deletions packages/common/src/utils/boxUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { isDefined, isUndefined } from "./objectUtils";

const NANOERGS_TOKEN_ID = "nanoErgs";

export function utxoSum(boxes: MinimalBoxAmountFields[]): BoxAmounts;
export function utxoSum(boxes: MinimalBoxAmountFields[], tokenId: TokenId): bigint;
export function utxoSum(boxes: MinimalBoxAmountFields[], tokenId?: TokenId): BoxAmounts | bigint {
export function utxoSum(boxes: MinimalBoxAmounts): BoxAmounts;
export function utxoSum(boxes: MinimalBoxAmounts, tokenId: TokenId): bigint;
export function utxoSum(boxes: MinimalBoxAmounts, tokenId?: TokenId): BoxAmounts | bigint {
const balances: { [tokenId: string]: bigint } = {};

for (const box of boxes) {
Expand Down Expand Up @@ -38,6 +38,22 @@ export function utxoSum(boxes: MinimalBoxAmountFields[], tokenId?: TokenId): Box
};
}

export function utxoSumResultDiff(amountsA: BoxAmounts, amountsB: BoxAmounts): BoxAmounts {
const tokens: TokenAmount<bigint>[] = [];
const nanoErgs = amountsA.nanoErgs - amountsB.nanoErgs;

for (const token of amountsA.tokens) {
const balance =
token.amount - (amountsB.tokens.find((t) => t.tokenId === token.tokenId)?.amount || _0n);

if (balance !== _0n) {
tokens.push({ tokenId: token.tokenId, amount: balance });
}
}

return { nanoErgs, tokens };
}

const MIN_REGISTER_NUMBER = 4;
const MAX_REGISTER_NUMBER = 9;

Expand Down Expand Up @@ -71,7 +87,7 @@ export type BoxAmounts = {
tokens: TokenAmount<bigint>[];
};

type MinimalBoxAmountFields = {
type MinimalBoxAmounts = readonly {
value: Amount;
assets: TokenAmount<Amount>[];
};
}[];

0 comments on commit 79caa7b

Please sign in to comment.