Skip to content

Commit

Permalink
refactor: rename some props with some fixes in the AssetSummarizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ShookLyngs committed Aug 11, 2024
1 parent 75840f8 commit 1440fb8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
41 changes: 25 additions & 16 deletions packages/rgbpp/src/rgbpp/summary/asset-summarizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import { leToU128, encodeCellId } from '@rgbpp-sdk/ckb';

export interface AssetSummary {
amount: bigint;
utxos: number;
cells: number;
utxoCount: number;
cellCount: number;
}

export interface AssetGroupSummary {
utxoId: string;
cellIds: string[];
assets: Record<string, AssetSummary>;
// The key of the assets record is the `xudtTypeArgs` (the unique identifier for the asset type)
assets: Record<string, AssetSummary>; // Record<xudtTypeAgs, AssetSummary>
}

export interface TransactionGroupSummary {
utxos: number;
cells: number;
utxoCount: number;
cellCount: number;
utxoIds: string[];
cellIds: string[];
assets: Record<string, AssetSummary>;
Expand All @@ -29,7 +30,7 @@ export class AssetSummarizer {

addGroup(utxo: Utxo, cells: Cell[]): AssetGroupSummary {
const utxoId = encodeUtxoId(utxo.txid, utxo.vout);
const assets: Record<string, Omit<AssetSummary, 'xudtTypeArgs'>> = {};
const assets: Record<string, AssetSummary> = {};
const cellIds: string[] = [];

for (const cell of cells) {
Expand All @@ -38,13 +39,13 @@ export class AssetSummarizer {
const amount = leToU128(cell.data.substring(0, 34));
if (assets[xudtTypeArgs] === undefined) {
assets[xudtTypeArgs] = {
utxos: 1,
cells: 0,
utxoCount: 1,
cellCount: 0,
amount: 0n,
};
}

assets[xudtTypeArgs]!.cells += 1;
assets[xudtTypeArgs]!.cellCount += 1;
assets[xudtTypeArgs]!.amount += amount;
}

Expand All @@ -63,6 +64,14 @@ export class AssetSummarizer {
return this.summarizeGroups(groupResults);
}

/**
* Summarizes asset information across multiple AssetGroupSummary objects.
* If no groups are provided, it summarizes the internally stored groups.
*
* @param groups - Optional array of AssetGroupSummary objects to summarize.
* @returns TransactionGroupSummary - A summary of all assets across the specified groups,
* including total UTXO and cell counts, UTXO and cell IDs, and aggregated asset information.
*/
summarizeGroups(groups?: AssetGroupSummary[]): TransactionGroupSummary {
const targetGroups = groups ?? this.groups;
const utxoIds = targetGroups.map((summary) => summary.utxoId);
Expand All @@ -72,24 +81,24 @@ export class AssetSummarizer {
for (const xudtTypeArgs in summary.assets) {
if (result[xudtTypeArgs] === undefined) {
result[xudtTypeArgs] = {
utxos: 0,
cells: 0,
utxoCount: 0,
cellCount: 0,
amount: 0n,
};
}

result[xudtTypeArgs]!.utxos += summary.assets[xudtTypeArgs]!.utxos;
result[xudtTypeArgs]!.cells += summary.assets[xudtTypeArgs]!.cells;
result[xudtTypeArgs]!.utxoCount += summary.assets[xudtTypeArgs]!.utxoCount;
result[xudtTypeArgs]!.cellCount += summary.assets[xudtTypeArgs]!.cellCount;
result[xudtTypeArgs]!.amount += summary.assets[xudtTypeArgs]!.amount;
}
return result;
},
{} as Record<string, Omit<AssetSummary, 'xudtTypeArgs'>>,
{} as Record<string, AssetSummary>,
);

return {
utxos: utxoIds.length,
cells: cellIds.length,
utxoCount: utxoIds.length,
cellCount: cellIds.length,
utxoIds,
cellIds,
assets,
Expand Down
8 changes: 4 additions & 4 deletions packages/rgbpp/tests/RgbppXudt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ describe('RgbppXudt', () => {
console.log('result.summary.excluded.assets', result.summary.excluded.assets);

expect(result.summary.included.assets).toHaveProperty(xudtTypeArgs);
expect(result.summary.included.assets[xudtTypeArgs].cells).toEqual(50);
expect(result.summary.included.assets[xudtTypeArgs].utxos).toEqual(50);
expect(result.summary.included.assets[xudtTypeArgs].cellCount).toEqual(50);
expect(result.summary.included.assets[xudtTypeArgs].utxoCount).toEqual(50);

expect(result.summary.excluded.assets).toHaveProperty(xudtTypeArgs);
expect(result.summary.excluded.assets[xudtTypeArgs].cells).toEqual(41);
expect(result.summary.excluded.assets[xudtTypeArgs].utxos).toEqual(1);
expect(result.summary.excluded.assets[xudtTypeArgs].cellCount).toEqual(41);
expect(result.summary.excluded.assets[xudtTypeArgs].utxoCount).toEqual(1);

expect(result.transactions).toHaveLength(2);
expect(result).toMatchSnapshot();
Expand Down
32 changes: 16 additions & 16 deletions packages/rgbpp/tests/__snapshots__/RgbppXudt.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"assets": {
"0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e": {
"amount": 4100000000n,
"cells": 41,
"utxos": 1,
"cellCount": 41,
"utxoCount": 1,
},
},
"cellCount": 41,
"cellIds": [
"0x00f43b843640fab7c9951a7931632e6ade6ef3337f89489b1c2b400793ece665:0x0",
"0xa398426747fb052d0832a3296ad89b72253428b6caa19e8be14d4fcb0befeb2d:0x0",
Expand Down Expand Up @@ -54,20 +55,20 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"0x3e7a84ff71358d30b6e4213cd5f5cf56bae03053a665bed68277a6fb5c852818:0x0",
"0xb0ffef5e6998ddc86019de4d90a035c3aceb17fbfcaabb6a225dd27533e8673b:0x0",
],
"cells": 41,
"utxoCount": 1,
"utxoIds": [
"69eea91d69b850abd92338fa4f0c9a11d0ed68f74bf5201cb7424dc49506af38:0",
],
"utxos": 1,
},
"included": {
"assets": {
"0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e": {
"amount": 50000000000n,
"cells": 50,
"utxos": 50,
"cellCount": 50,
"utxoCount": 50,
},
},
"cellCount": 50,
"cellIds": [
"0x6adafca165f29c713d15da4e9415d610c02c1b8ce585cc4f5430aa5de3e0e239:0x0",
"0x122d92afd897e5cce4f17896468453bef911e8ee5a2141e8da4e91fa37863b7d:0x0",
Expand Down Expand Up @@ -120,7 +121,7 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"0xa75bca0eaa472a83b9a155d4c75dbc3c0e846b7a841fd143ac051c2c07e4aa38:0x0",
"0x5860c0a6a834e0bb2dfbc8d5f4c1885cbbe3367285f5dfb7d464fa042031d17c:0x0",
],
"cells": 50,
"utxoCount": 50,
"utxoIds": [
"fd16aafb86ef05a373e59d2b812fcb117c65b647aa899085136913961127efca:0",
"1007a173f8ace6567758d309fb7a281c1f19c5961ff69fa32789c9a83ddfac10:0",
Expand Down Expand Up @@ -173,7 +174,6 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"f1ee2c32debdbca6e0bdd654f3356501951e9fda7f288df62144583b38c5d6b6:0",
"f6ce37f9d89597172566ae0b3e18edcb1c92011add8190e0b53b5caf9566aea1:0",
],
"utxos": 50,
},
},
"transactions": [
Expand Down Expand Up @@ -563,10 +563,11 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"assets": {
"0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e": {
"amount": 40000000000n,
"cells": 40,
"utxos": 40,
"cellCount": 40,
"utxoCount": 40,
},
},
"cellCount": 40,
"cellIds": [
"0x6adafca165f29c713d15da4e9415d610c02c1b8ce585cc4f5430aa5de3e0e239:0x0",
"0x122d92afd897e5cce4f17896468453bef911e8ee5a2141e8da4e91fa37863b7d:0x0",
Expand Down Expand Up @@ -609,7 +610,7 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"0x6fb324d629f69d1d011015325559bd1a3f58cd2bd0e2dce23b006ea85eade376:0x0",
"0xb5c70400545feef1c591c072b386e663805d984ecc90c9ebd282f39990a7cf17:0x0",
],
"cells": 40,
"utxoCount": 40,
"utxoIds": [
"fd16aafb86ef05a373e59d2b812fcb117c65b647aa899085136913961127efca:0",
"1007a173f8ace6567758d309fb7a281c1f19c5961ff69fa32789c9a83ddfac10:0",
Expand Down Expand Up @@ -652,7 +653,6 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"8a81a8dc324d22499562e416a884e7bf9a55f5de7b28ee454ad3e76fa6597283:0",
"ce808ec46872008364568e931b4fe484e0e8ea3c154770adb10f5c6b2b3ec286:0",
],
"utxos": 40,
},
},
{
Expand Down Expand Up @@ -801,10 +801,11 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"assets": {
"0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e": {
"amount": 10000000000n,
"cells": 10,
"utxos": 10,
"cellCount": 10,
"utxoCount": 10,
},
},
"cellCount": 10,
"cellIds": [
"0xdc0c3056e70b4fe64eda00cdc1fd6b2ae435871608b9fbb75efd13a3e78b496e:0x0",
"0xd30143cc94d6fdbb656db10a7635e936929febebc9e1424d163ab36a9041e9ba:0x0",
Expand All @@ -817,7 +818,7 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"0xa75bca0eaa472a83b9a155d4c75dbc3c0e846b7a841fd143ac051c2c07e4aa38:0x0",
"0x5860c0a6a834e0bb2dfbc8d5f4c1885cbbe3367285f5dfb7d464fa042031d17c:0x0",
],
"cells": 10,
"utxoCount": 10,
"utxoIds": [
"2c5d749dc3451230f2497384c02dc0e07efc5743bce02faf47db4a191095c5d8:0",
"4418969debbb39c1ff3e82ba90375106ba471ffc66f87dd004ce216e3bd3d8a4:0",
Expand All @@ -830,7 +831,6 @@ exports[`RgbppXudt > buildRgbppTransferAllTxs() > 50 included cells, 41 excluded
"f1ee2c32debdbca6e0bdd654f3356501951e9fda7f288df62144583b38c5d6b6:0",
"f6ce37f9d89597172566ae0b3e18edcb1c92011add8190e0b53b5caf9566aea1:0",
],
"utxos": 10,
},
},
],
Expand Down
32 changes: 16 additions & 16 deletions packages/rgbpp/tests/mocked/50-included-41-excluded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9152,8 +9152,8 @@ const buildResult: RgbppTransferAllTxsResult = {
assets: {
'0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e': {
amount: BigInt('4100000000'),
cells: 41,
utxos: 1,
cellCount: 41,
utxoCount: 1,
},
},
cellIds: [
Expand Down Expand Up @@ -9199,16 +9199,16 @@ const buildResult: RgbppTransferAllTxsResult = {
'0x3e7a84ff71358d30b6e4213cd5f5cf56bae03053a665bed68277a6fb5c852818:0x0',
'0xb0ffef5e6998ddc86019de4d90a035c3aceb17fbfcaabb6a225dd27533e8673b:0x0',
],
cells: 41,
cellCount: 41,
utxoIds: ['69eea91d69b850abd92338fa4f0c9a11d0ed68f74bf5201cb7424dc49506af38:0'],
utxos: 1,
utxoCount: 1,
},
included: {
assets: {
'0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e': {
amount: BigInt('50000000000'),
cells: 50,
utxos: 50,
cellCount: 50,
utxoCount: 50,
},
},
cellIds: [
Expand Down Expand Up @@ -9263,7 +9263,7 @@ const buildResult: RgbppTransferAllTxsResult = {
'0xa75bca0eaa472a83b9a155d4c75dbc3c0e846b7a841fd143ac051c2c07e4aa38:0x0',
'0x5860c0a6a834e0bb2dfbc8d5f4c1885cbbe3367285f5dfb7d464fa042031d17c:0x0',
],
cells: 50,
cellCount: 50,
utxoIds: [
'fd16aafb86ef05a373e59d2b812fcb117c65b647aa899085136913961127efca:0',
'1007a173f8ace6567758d309fb7a281c1f19c5961ff69fa32789c9a83ddfac10:0',
Expand Down Expand Up @@ -9316,7 +9316,7 @@ const buildResult: RgbppTransferAllTxsResult = {
'f1ee2c32debdbca6e0bdd654f3356501951e9fda7f288df62144583b38c5d6b6:0',
'f6ce37f9d89597172566ae0b3e18edcb1c92011add8190e0b53b5caf9566aea1:0',
],
utxos: 50,
utxoCount: 50,
},
},
transactions: [
Expand Down Expand Up @@ -9705,8 +9705,8 @@ const buildResult: RgbppTransferAllTxsResult = {
assets: {
'0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e': {
amount: BigInt('40000000000'),
cells: 40,
utxos: 40,
cellCount: 40,
utxoCount: 40,
},
},
cellIds: [
Expand Down Expand Up @@ -9751,7 +9751,7 @@ const buildResult: RgbppTransferAllTxsResult = {
'0x6fb324d629f69d1d011015325559bd1a3f58cd2bd0e2dce23b006ea85eade376:0x0',
'0xb5c70400545feef1c591c072b386e663805d984ecc90c9ebd282f39990a7cf17:0x0',
],
cells: 40,
cellCount: 40,
utxoIds: [
'fd16aafb86ef05a373e59d2b812fcb117c65b647aa899085136913961127efca:0',
'1007a173f8ace6567758d309fb7a281c1f19c5961ff69fa32789c9a83ddfac10:0',
Expand Down Expand Up @@ -9794,7 +9794,7 @@ const buildResult: RgbppTransferAllTxsResult = {
'8a81a8dc324d22499562e416a884e7bf9a55f5de7b28ee454ad3e76fa6597283:0',
'ce808ec46872008364568e931b4fe484e0e8ea3c154770adb10f5c6b2b3ec286:0',
],
utxos: 40,
utxoCount: 40,
},
},
{
Expand Down Expand Up @@ -9931,8 +9931,8 @@ const buildResult: RgbppTransferAllTxsResult = {
assets: {
'0xc3fcf12ef840771b1eac4709d61d93be7b13424c16fefd0b2071c204bdeb9f4e': {
amount: BigInt('10000000000'),
cells: 10,
utxos: 10,
cellCount: 10,
utxoCount: 10,
},
},
cellIds: [
Expand All @@ -9947,7 +9947,7 @@ const buildResult: RgbppTransferAllTxsResult = {
'0xa75bca0eaa472a83b9a155d4c75dbc3c0e846b7a841fd143ac051c2c07e4aa38:0x0',
'0x5860c0a6a834e0bb2dfbc8d5f4c1885cbbe3367285f5dfb7d464fa042031d17c:0x0',
],
cells: 10,
cellCount: 10,
utxoIds: [
'2c5d749dc3451230f2497384c02dc0e07efc5743bce02faf47db4a191095c5d8:0',
'4418969debbb39c1ff3e82ba90375106ba471ffc66f87dd004ce216e3bd3d8a4:0',
Expand All @@ -9960,7 +9960,7 @@ const buildResult: RgbppTransferAllTxsResult = {
'f1ee2c32debdbca6e0bdd654f3356501951e9fda7f288df62144583b38c5d6b6:0',
'f6ce37f9d89597172566ae0b3e18edcb1c92011add8190e0b53b5caf9566aea1:0',
],
utxos: 10,
utxoCount: 10,
},
},
],
Expand Down

0 comments on commit 1440fb8

Please sign in to comment.