{"version":3,"file":"coin-value.mjs","names":["balanceFlows","price"],"sources":["../../../src/transaction-analyzer/rules/coin-value.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { createAnalyzer } from '../analyzer.js';\nimport { balanceFlows } from './balance-flows.js';\nimport type { CoinFlow } from './balance-flows.js';\n\nexport interface CoinValueAnalyzerOptions {\n\tgetCoinPrices: (coinTypes: string[]) => Promise<\n\t\t{\n\t\t\tcoinType: string;\n\t\t\tdecimals: number;\n\t\t\tprice: number | null;\n\t\t}[]\n\t>;\n}\n\nexport interface CoinValueAnalysis {\n\tcoinTypesWithoutPrice: string[];\n\ttotal: number;\n\tcoinTypes: {\n\t\tcoinType: string;\n\t\tdecimals: number;\n\t\tprice: number;\n\t\tamount: bigint;\n\t\tconvertedAmount: number;\n\t}[];\n}\n\nexport const coinValues = createAnalyzer({\n\tdependencies: { balanceFlows },\n\tanalyze:\n\t\t({ getCoinPrices }: CoinValueAnalyzerOptions) =>\n\t\tasync ({ balanceFlows }) => {\n\t\t\treturn { result: await getCoinValues(balanceFlows.sender, getCoinPrices) };\n\t\t},\n});\n\nexport async function getCoinValues(\n\tbalanceFlows: CoinFlow[],\n\tgetCoinPrices: CoinValueAnalyzerOptions['getCoinPrices'],\n): Promise<CoinValueAnalysis> {\n\tconst outflows = balanceFlows\n\t\t.filter((flow) => flow.amount < 0n)\n\t\t.map((flow) => ({ coinType: flow.coinType, amount: -flow.amount }));\n\tconst prices = await getCoinPrices(outflows.map((flow) => flow.coinType));\n\n\tlet total = 0;\n\tconst coinTypesWithoutPrice: string[] = [];\n\tconst coinTypes: CoinValueAnalysis['coinTypes'] = [];\n\n\tfor (const flow of outflows) {\n\t\tconst price = prices.find((price) => price.coinType === flow.coinType);\n\t\tif (price?.price != null) {\n\t\t\tconst convertedAmount = (Number(flow.amount) / 10 ** price.decimals) * price.price;\n\t\t\ttotal += convertedAmount;\n\t\t\tcoinTypes.push({\n\t\t\t\tcoinType: flow.coinType,\n\t\t\t\tdecimals: price.decimals,\n\t\t\t\tprice: price.price,\n\t\t\t\tamount: flow.amount,\n\t\t\t\tconvertedAmount,\n\t\t\t});\n\t\t} else {\n\t\t\tcoinTypesWithoutPrice.push(flow.coinType);\n\t\t}\n\t}\n\n\treturn { total, coinTypesWithoutPrice, coinTypes };\n}\n"],"mappings":";;;;AA6BA,MAAa,aAAa,eAAe;CACxC,cAAc,EAAE,cAAc;CAC9B,UACE,EAAE,oBACH,OAAO,EAAE,mCAAmB;AAC3B,SAAO,EAAE,QAAQ,MAAM,cAAcA,eAAa,QAAQ,cAAc,EAAE;;CAE5E,CAAC;AAEF,eAAsB,cACrB,gBACA,eAC6B;CAC7B,MAAM,WAAWA,eACf,QAAQ,SAAS,KAAK,SAAS,GAAG,CAClC,KAAK,UAAU;EAAE,UAAU,KAAK;EAAU,QAAQ,CAAC,KAAK;EAAQ,EAAE;CACpE,MAAM,SAAS,MAAM,cAAc,SAAS,KAAK,SAAS,KAAK,SAAS,CAAC;CAEzE,IAAI,QAAQ;CACZ,MAAM,wBAAkC,EAAE;CAC1C,MAAM,YAA4C,EAAE;AAEpD,MAAK,MAAM,QAAQ,UAAU;EAC5B,MAAM,QAAQ,OAAO,MAAM,YAAUC,QAAM,aAAa,KAAK,SAAS;AACtE,MAAI,OAAO,SAAS,MAAM;GACzB,MAAM,kBAAmB,OAAO,KAAK,OAAO,GAAG,MAAM,MAAM,WAAY,MAAM;AAC7E,YAAS;AACT,aAAU,KAAK;IACd,UAAU,KAAK;IACf,UAAU,MAAM;IAChB,OAAO,MAAM;IACb,QAAQ,KAAK;IACb;IACA,CAAC;QAEF,uBAAsB,KAAK,KAAK,SAAS;;AAI3C,QAAO;EAAE;EAAO;EAAuB;EAAW"}