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;SAC7C,IAAI,CAAC,oBAAoB,CAAC;SAC1B,IAAI,CAAC,oBAAoB,CAAC;SAC1B,IAAI,CAAC,UAAU,CAAC;SAChB,QAAQ,CAAC,EAAE,CAAC,CAAC;IAEhB,MAAM,QAAQ,GAAG,IAAI,wBAAS,CAAC,cAAc,CAAC;SAC3C,IAAI,CAAC,mBAAmB,CAAC;SACzB,IAAI,CAAC,mBAAmB,CAAC;SACzB,IAAI,CAAC,SAAS,CAAC;SACf,QAAQ,CAAC,EAAE,CAAC,CAAC;IAEhB,MAAM,iBAAiB,GAAG,MAAM,CAC9B,WAAW,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,iBAAiB,CAAC,CACxD,CAAC;IAEF,OAAO;QACL,iBAAiB;QACjB,IAAI,EAAE;YACJ,QAAQ,EAAE;gBACR,IAAI,EAAE,eAAe;gBACrB,GAAG,EAAE,cAAc;aACpB;YACD,aAAa,EAAE;gBACb,IAAI,EAAE,oBAAoB;gBAC1B,GAAG,EAAE,mBAAmB;aACzB;YACD,aAAa,EAAE;gBACb,IAAI,EAAE,oBAAoB;gBAC1B,GAAG,EAAE,mBAAmB;aACzB;SACF;QACD,KAAK,EAAE;YACL,IAAI,EAAE,SAAS;YACf,GAAG,EAAE,QAAQ;SACd;KACF,CAAC;AACJ,CAAC;AA3ED,0CA2EC;AAED;;;;;;GAMG;AACH,SAAS,WAAW,CAClB,IAAS,EACT,WAAyC;IAEzC,OAAO,IAAI;SACR,GAAG,CAAC,WAAW,CAAC;SAChB,MAAM,CAAY,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,IAAI,wBAAS,CAAC,CAAC,CAAC,CAAC;SACxE,QAAQ,CAAC,EAAE,CAAC,CAAC;AAClB,CAAC","sourcesContent":["import { BigNumber } from 'bignumber.js';\n\nimport type {\n  TransactionPayControllerMessenger,\n  TransactionPayQuote,\n  TransactionPayRequiredToken,\n  TransactionPayTotals,\n} from '../types';\n\n/**\n * Calculate totals for a list of quotes and tokens.\n *\n * @param quotes - List of bridge quotes.\n * @param tokens - List of required transaction tokens.\n * @param _messenger - Controller messenger.\n * @returns The calculated totals in USD and fiat currency.\n */\nexport function calculateTotals(\n  quotes: TransactionPayQuote<unknown>[],\n  tokens: TransactionPayRequiredToken[],\n  _messenger: TransactionPayControllerMessenger,\n): TransactionPayTotals {\n  const providerFeeFiat = sumProperty(\n    quotes,\n    (quote) => quote.fees.provider.fiat,\n  );\n\n  const providerFeeUsd = sumProperty(\n    quotes,\n    (quote) => quote.fees.provider.usd,\n  );\n\n  const sourceNetworkFeeFiat = sumProperty(\n    quotes,\n    (quote) => quote.fees.sourceNetwork.fiat,\n  );\n\n  const sourceNetworkFeeUsd = sumProperty(\n    quotes,\n    (quote) => quote.fees.sourceNetwork.usd,\n  );\n\n  const targetNetworkFeeFiat = sumProperty(\n    quotes,\n    (quote) => quote.fees.targetNetwork.fiat,\n  );\n\n  const targetNetworkFeeUsd = sumProperty(\n    quotes,\n    (quote) => quote.fees.targetNetwork.usd,\n  );\n\n  const amountFiat = sumProperty(tokens, (token) => token.amountFiat);\n  const amountUsd = sumProperty(tokens, (token) => token.amountUsd);\n\n  const totalFiat = new BigNumber(providerFeeFiat)\n    .plus(sourceNetworkFeeFiat)\n    .plus(targetNetworkFeeFiat)\n    .plus(amountFiat)\n    .toString(10);\n\n  const totalUsd = new BigNumber(providerFeeUsd)\n    .plus(sourceNetworkFeeUsd)\n    .plus(targetNetworkFeeUsd)\n    .plus(amountUsd)\n    .toString(10);\n\n  const estimatedDuration = Number(\n    sumProperty(quotes, (quote) => quote.estimatedDuration),\n  );\n\n  return {\n    estimatedDuration,\n    fees: {\n      provider: {\n        fiat: providerFeeFiat,\n        usd: providerFeeUsd,\n      },\n      sourceNetwork: {\n        fiat: sourceNetworkFeeFiat,\n        usd: sourceNetworkFeeUsd,\n      },\n      targetNetwork: {\n        fiat: targetNetworkFeeFiat,\n        usd: targetNetworkFeeUsd,\n      },\n    },\n    total: {\n      fiat: totalFiat,\n      usd: totalUsd,\n    },\n  };\n}\n\n/**\n * Sum a specific property from a list of items.\n *\n * @param data - List of items.\n * @param getProperty - Function to extract the property to sum from each item.\n * @returns The summed value as a string.\n */\nfunction sumProperty<T>(\n  data: T[],\n  getProperty: (item: T) => BigNumber.Value,\n): string {\n  return data\n    .map(getProperty)\n    .reduce<BigNumber>((total, value) => total.plus(value), new BigNumber(0))\n    .toString(10);\n}\n"]}