@glowlabs-org/utils 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/cjs/browser.d.ts +2 -2
- package/dist/cjs/browser.js +12 -359
- package/dist/cjs/browser.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +33 -31
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/lib/{hooks/use-control-api.d.ts → control-api/control-router.d.ts} +1 -1
- package/dist/{esm/lib/hooks/use-regions.d.ts → cjs/lib/control-api/region-router.d.ts} +1 -1
- package/dist/cjs/{use-forwarder-CVlJS4UY.js → region-router-6Tfi1vcZ.js} +350 -1
- package/dist/cjs/region-router-6Tfi1vcZ.js.map +1 -0
- package/dist/esm/browser.d.ts +2 -2
- package/dist/esm/browser.js +2 -349
- package/dist/esm/browser.js.map +1 -1
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -2
- package/dist/esm/lib/{hooks/use-control-api.d.ts → control-api/control-router.d.ts} +1 -1
- package/dist/{cjs/lib/hooks/use-regions.d.ts → esm/lib/control-api/region-router.d.ts} +1 -1
- package/dist/esm/{use-forwarder-DD_5ZFZg.js → region-router-ClCO6Pac.js} +349 -2
- package/dist/esm/region-router-ClCO6Pac.js.map +1 -0
- package/package.json +1 -1
- package/src/browser.ts +2 -2
- package/src/index.ts +2 -0
- package/src/lib/{hooks/use-control-api.ts → control-api/control-router.ts} +1 -1
- package/src/lib/{hooks/use-regions.ts → control-api/region-router.ts} +1 -1
- package/dist/cjs/use-forwarder-CVlJS4UY.js.map +0 -1
- package/dist/esm/use-forwarder-DD_5ZFZg.js.map +0 -1
package/dist/cjs/index.js
CHANGED
@@ -15,7 +15,7 @@ var viem = require('viem');
|
|
15
15
|
var merkletreejs = require('merkletreejs');
|
16
16
|
var ethers = require('ethers');
|
17
17
|
var Decimal = require('decimal.js');
|
18
|
-
var
|
18
|
+
var regionRouter = require('./region-router-6Tfi1vcZ.js');
|
19
19
|
|
20
20
|
const GENESIS_TIMESTAMP = 1700352000;
|
21
21
|
|
@@ -18709,14 +18709,14 @@ async function createWeeklyReportLegacy(args) {
|
|
18709
18709
|
const existingValue = map.get(key);
|
18710
18710
|
existingValue.glowWeight += value.glowWeight;
|
18711
18711
|
existingValue.usdgWeight += value.usdgWeight;
|
18712
|
-
if (existingValue.glowWeight >
|
18713
|
-
existingValue.usdgWeight >
|
18712
|
+
if (existingValue.glowWeight > regionRouter.MAX_WEIGHT ||
|
18713
|
+
existingValue.usdgWeight > regionRouter.MAX_WEIGHT) {
|
18714
18714
|
throw new Error(`Accumulated weight overflow on wallet ${key}`);
|
18715
18715
|
}
|
18716
18716
|
map.set(key, existingValue);
|
18717
18717
|
}
|
18718
18718
|
else {
|
18719
|
-
if (value.glowWeight >
|
18719
|
+
if (value.glowWeight > regionRouter.MAX_WEIGHT || value.usdgWeight > regionRouter.MAX_WEIGHT) {
|
18720
18720
|
throw new Error(`Initial weight overflow on wallet ${key}`);
|
18721
18721
|
}
|
18722
18722
|
map.set(key, value);
|
@@ -18745,10 +18745,10 @@ async function createWeeklyReportLegacy(args) {
|
|
18745
18745
|
if (Math.abs(sumOfRewardSplitsUSDGWeight - 1) > percentTolerance)
|
18746
18746
|
throw new Error(`USDG weight splits do not add up to 1 for farm ${farm.shortId}`);
|
18747
18747
|
for (const rewardSplit of farm.rewardSplits) {
|
18748
|
-
const glowBigInt = BigInt(viem.parseUnits(customToFixed(farm.weeklyPayment,
|
18749
|
-
const usdgBigInt = BigInt(viem.parseUnits(customToFixed(farm.carbonCreditsProduced,
|
18750
|
-
const glowWeight = multiplyBigIntByDecimalPercentage(glowBigInt,
|
18751
|
-
const usdgWeight = multiplyBigIntByDecimalPercentage(usdgBigInt,
|
18748
|
+
const glowBigInt = BigInt(viem.parseUnits(customToFixed(farm.weeklyPayment, regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION), regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION));
|
18749
|
+
const usdgBigInt = BigInt(viem.parseUnits(customToFixed(farm.carbonCreditsProduced, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION), regionRouter.USDG_WEIGHT_DECIMAL_PRECISION));
|
18750
|
+
const glowWeight = multiplyBigIntByDecimalPercentage(glowBigInt, regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION, rewardSplit.glowSplitPercent);
|
18751
|
+
const usdgWeight = multiplyBigIntByDecimalPercentage(usdgBigInt, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION, rewardSplit.usdgSplitPercent);
|
18752
18752
|
const value = {
|
18753
18753
|
wallet: rewardSplit.walletAddress,
|
18754
18754
|
glowWeight,
|
@@ -18793,10 +18793,10 @@ async function createWeeklyReportLegacy(args) {
|
|
18793
18793
|
const root = tree.getHexRoot();
|
18794
18794
|
const totalGlowWeightFinalizedLeavesSum = finalLeaves.reduce((acc, { glowWeight }) => acc.add(glowWeight), ethers.ethers.BigNumber.from(0));
|
18795
18795
|
const totalGCCWeightFinalizedLeavesSum = finalLeaves.reduce((acc, { usdgWeight }) => acc.add(usdgWeight), ethers.ethers.BigNumber.from(0));
|
18796
|
-
if (totalGlowWeightFinalizedLeavesSum.toBigInt() >
|
18796
|
+
if (totalGlowWeightFinalizedLeavesSum.toBigInt() > regionRouter.MAX_WEIGHT) {
|
18797
18797
|
throw new Error("Total glow weight is greater than max weight");
|
18798
18798
|
}
|
18799
|
-
if (totalGCCWeightFinalizedLeavesSum.toBigInt() >
|
18799
|
+
if (totalGCCWeightFinalizedLeavesSum.toBigInt() > regionRouter.MAX_WEIGHT) {
|
18800
18800
|
throw new Error("Total gcc weight is greater than max weight");
|
18801
18801
|
}
|
18802
18802
|
const headlineStats = {
|
@@ -18804,12 +18804,12 @@ async function createWeeklyReportLegacy(args) {
|
|
18804
18804
|
totalCreditsProduced: viem.formatUnits(totalCreditsProduced, 18),
|
18805
18805
|
totalCreditsProducedBN: totalCreditsProduced.toString(),
|
18806
18806
|
totalGlowWeightInFinalized: totalGlowWeightFinalizedLeavesSum.toString(),
|
18807
|
-
totalGlowWeightHuman: viem.formatUnits(BigInt(totalGlowWeightFinalizedLeavesSum.toString()),
|
18807
|
+
totalGlowWeightHuman: viem.formatUnits(BigInt(totalGlowWeightFinalizedLeavesSum.toString()), regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION).toString(),
|
18808
18808
|
totalUSDGWeightInFinalized: totalGCCWeightFinalizedLeavesSum.toString(),
|
18809
|
-
totalUSDGWeightHuman: viem.formatUnits(BigInt(totalGCCWeightFinalizedLeavesSum.toString()),
|
18809
|
+
totalUSDGWeightHuman: viem.formatUnits(BigInt(totalGCCWeightFinalizedLeavesSum.toString()), regionRouter.USDG_WEIGHT_DECIMAL_PRECISION).toString(),
|
18810
18810
|
root: root,
|
18811
|
-
glowWeightDecimals:
|
18812
|
-
usdgWeightDecimals:
|
18811
|
+
glowWeightDecimals: regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION,
|
18812
|
+
usdgWeightDecimals: regionRouter.USDG_WEIGHT_DECIMAL_PRECISION,
|
18813
18813
|
};
|
18814
18814
|
const farmsWithMerkleProofs = finalLeaves.map((leaf) => {
|
18815
18815
|
const hashedLeaf = hashLeaf({
|
@@ -18884,7 +18884,7 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
18884
18884
|
// Fetch active farms for the week (source of truth) and audits for the week.
|
18885
18885
|
const [apiResponse, auditsRes] = await Promise.all([
|
18886
18886
|
fetchFarmsForWeek(week, gcaUrls, apiUrl),
|
18887
|
-
axios.get(`${
|
18887
|
+
axios.get(`${regionRouter.HUB_URL}/api/audits?omitDocuments=true&weekNumber=${week}`),
|
18888
18888
|
]);
|
18889
18889
|
const farms = apiResponse.filteredFarms; // List of farms active this week.
|
18890
18890
|
const audits = auditsRes.data; // Audits potentially containing adjusted credits.
|
@@ -18933,10 +18933,10 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
18933
18933
|
if (splitValue.isZero())
|
18934
18934
|
throw new Error(`Zero adjustedWeeklyCarbonCredit split for active farms in audit ${audit.id}`);
|
18935
18935
|
// Convert the decimal split value to a bigint using the defined precision.
|
18936
|
-
const splitBigInt = toBigInt(splitValue,
|
18936
|
+
const splitBigInt = toBigInt(splitValue, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION);
|
18937
18937
|
// Dust Check: Ensure that the conversion to bigint didn't truncate the value to zero.
|
18938
18938
|
if (splitBigInt === BigInt(0)) {
|
18939
|
-
throw new Error(`Adjusted credit split for audit ${audit.id} resulted in zero BigInt after conversion for precision ${
|
18939
|
+
throw new Error(`Adjusted credit split for audit ${audit.id} resulted in zero BigInt after conversion for precision ${regionRouter.USDG_WEIGHT_DECIMAL_PRECISION}. Original split value: ${splitValue.toString()}. This might indicate dust loss.`);
|
18940
18940
|
}
|
18941
18941
|
// Distribute the calculated split value to each active farm associated with this audit.
|
18942
18942
|
for (const sidString of auditActiveFarmSids) {
|
@@ -18980,7 +18980,7 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
18980
18980
|
if (Math.abs(sumUSDG - 1) > percentTolerance)
|
18981
18981
|
throw new Error(`USDG splits ≠1 for farm ${farm.shortId}`);
|
18982
18982
|
// Get the base GLOW payment for the farm, converted to bigint with appropriate precision.
|
18983
|
-
const glowBase = viem.parseUnits(customToFixed(farm.weeklyPayment,
|
18983
|
+
const glowBase = viem.parseUnits(customToFixed(farm.weeklyPayment, regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION), regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION);
|
18984
18984
|
// Retrieve the pre-calculated adjusted credit for this farm. Should always exist due to prior checks.
|
18985
18985
|
const adjustedCreditBigInt = shortIdToAdjustedCredit.get(String(farm.shortId)) ?? BigInt(0);
|
18986
18986
|
// Sanity check (likely redundant due to earlier checks, but safe).
|
@@ -18989,9 +18989,9 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
18989
18989
|
// Process each reward split recipient for this farm.
|
18990
18990
|
for (const split of farm.rewardSplits) {
|
18991
18991
|
// Calculate the GLOW weight for this recipient based on the farm's weekly payment and split percentage.
|
18992
|
-
const glowWeight = multiplyBigIntByDecimalPercentage(glowBase,
|
18992
|
+
const glowWeight = multiplyBigIntByDecimalPercentage(glowBase, regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION, split.glowSplitPercent);
|
18993
18993
|
// Calculate the USDG weight for this recipient based on the farm's *adjusted* credit and split percentage.
|
18994
|
-
const usdgWeight = multiplyBigIntByDecimalPercentage(adjustedCreditBigInt,
|
18994
|
+
const usdgWeight = multiplyBigIntByDecimalPercentage(adjustedCreditBigInt, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION, split.usdgSplitPercent);
|
18995
18995
|
// Dust Loss Checks: Ensure non-zero percentages didn't result in zero weight due to precision limits.
|
18996
18996
|
if (split.usdgSplitPercent > 0 &&
|
18997
18997
|
adjustedCreditBigInt > BigInt(0) &&
|
@@ -19002,7 +19002,7 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19002
19002
|
glowWeight === BigInt(0))
|
19003
19003
|
throw new Error(`Glow dust lost for farm ${farm.shortId} wallet ${split.walletAddress}`);
|
19004
19004
|
// Overflow Check: Ensure individual leaf weights don't exceed the maximum allowed.
|
19005
|
-
if (glowWeight >
|
19005
|
+
if (glowWeight > regionRouter.MAX_WEIGHT || usdgWeight > regionRouter.MAX_WEIGHT)
|
19006
19006
|
throw new Error(`Leaf weight overflow on wallet ${split.walletAddress}`);
|
19007
19007
|
// Accumulate the calculated weights into the map for the recipient wallet.
|
19008
19008
|
accumulateLeafWeights(split.walletAddress, { wallet: split.walletAddress, glowWeight, usdgWeight }, map);
|
@@ -19030,9 +19030,9 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19030
19030
|
const totalGlowWeight = finalizedLeaves.reduce((acc, { glowWeight }) => acc.add(glowWeight), ethers.ethers.BigNumber.from(0));
|
19031
19031
|
const totalUSDGWeight = finalizedLeaves.reduce((acc, { usdgWeight }) => acc.add(usdgWeight), ethers.ethers.BigNumber.from(0));
|
19032
19032
|
// Total Weight Overflow Checks: Ensure sums don't exceed maximum.
|
19033
|
-
if (totalGlowWeight.toBigInt() >
|
19033
|
+
if (totalGlowWeight.toBigInt() > regionRouter.MAX_WEIGHT)
|
19034
19034
|
throw new Error("Total glow weight overflow");
|
19035
|
-
if (totalUSDGWeight.toBigInt() >
|
19035
|
+
if (totalUSDGWeight.toBigInt() > regionRouter.MAX_WEIGHT)
|
19036
19036
|
throw new Error("Total USDG weight overflow");
|
19037
19037
|
// Generate Merkle proofs for each leaf and verify them.
|
19038
19038
|
const leavesWithProofs = finalizedLeaves.map((leaf) => {
|
@@ -19066,10 +19066,10 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19066
19066
|
// Calculate the total expected credits based on the adjusted values used.
|
19067
19067
|
const totalExpectedCredits = farms.reduce((acc, f) => {
|
19068
19068
|
const adj = shortIdToAdjustedCredit.get(String(f.shortId)) ?? BigInt(0);
|
19069
|
-
return acc.plus(fromBigInt(adj,
|
19069
|
+
return acc.plus(fromBigInt(adj, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION));
|
19070
19070
|
}, new Decimal(0));
|
19071
19071
|
// Convert total USDG weight back to human-readable decimal for deviation check.
|
19072
|
-
const totalUSDGWeightHuman = new Decimal(viem.formatUnits(BigInt(totalUSDGWeight.toString()),
|
19072
|
+
const totalUSDGWeightHuman = new Decimal(viem.formatUnits(BigInt(totalUSDGWeight.toString()), regionRouter.USDG_WEIGHT_DECIMAL_PRECISION));
|
19073
19073
|
// Check deviation between total adjusted credits used and the final sum of USDG weights.
|
19074
19074
|
if (greaterThanMaxDeviation(totalExpectedCredits.toNumber(), totalUSDGWeightHuman.toNumber(), 0.001 // 0.1% tolerance
|
19075
19075
|
)) {
|
@@ -19080,7 +19080,7 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19080
19080
|
throw new Error("totalExpectedCredits vs USDG weight deviation >0.1% ");
|
19081
19081
|
}
|
19082
19082
|
// Convert total Glow weight back to human-readable decimal.
|
19083
|
-
const totalGlowWeightHuman = new Decimal(viem.formatUnits(BigInt(totalGlowWeight.toString()),
|
19083
|
+
const totalGlowWeightHuman = new Decimal(viem.formatUnits(BigInt(totalGlowWeight.toString()), regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION));
|
19084
19084
|
// Sum the original weekly protocol fee payments from farm data.
|
19085
19085
|
const totalProtocolFeePayments = farms.reduce((acc, f) => acc + f.weeklyPayment, 0);
|
19086
19086
|
// Check deviation between total glow weight and total protocol fees paid.
|
@@ -19103,18 +19103,18 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19103
19103
|
totalUSDGWeightInFinalized: totalUSDGWeight.toString(),
|
19104
19104
|
totalUSDGWeightHuman: totalUSDGWeightHuman.toString(),
|
19105
19105
|
root: merkleRoot,
|
19106
|
-
glowWeightDecimals:
|
19107
|
-
usdgWeightDecimals:
|
19106
|
+
glowWeightDecimals: regionRouter.GLOW_WEIGHT_DECIMAL_PRECISION,
|
19107
|
+
usdgWeightDecimals: regionRouter.USDG_WEIGHT_DECIMAL_PRECISION,
|
19108
19108
|
};
|
19109
19109
|
// List of shortIds and their final adjusted credit value (human-readable).
|
19110
19110
|
const shortIdAdjustedList = Array.from(shortIdToAdjustedCredit.entries()).map(([shortId, creditBigInt]) => ({
|
19111
19111
|
shortId,
|
19112
|
-
adjustedCredit: fromBigInt(creditBigInt,
|
19112
|
+
adjustedCredit: fromBigInt(creditBigInt, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION).toString(),
|
19113
19113
|
}));
|
19114
19114
|
// Detailed comparison between originally reported credits and adjusted credits for each farm.
|
19115
19115
|
const creditsDeviationList = farms.map((farm) => {
|
19116
19116
|
const adj = shortIdToAdjustedCredit.get(String(farm.shortId)) ?? BigInt(0);
|
19117
|
-
const adjusted = fromBigInt(adj,
|
19117
|
+
const adjusted = fromBigInt(adj, regionRouter.USDG_WEIGHT_DECIMAL_PRECISION);
|
19118
19118
|
const produced = new Decimal(farm.carbonCreditsProduced);
|
19119
19119
|
let deviation;
|
19120
19120
|
let absDeviation;
|
@@ -19153,7 +19153,9 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19153
19153
|
};
|
19154
19154
|
}
|
19155
19155
|
|
19156
|
-
exports.
|
19156
|
+
exports.ControlRouter = regionRouter.ControlRouter;
|
19157
|
+
exports.RegionRouter = regionRouter.RegionRouter;
|
19158
|
+
exports.useForwarder = regionRouter.useForwarder;
|
19157
19159
|
exports.GENESIS_TIMESTAMP = GENESIS_TIMESTAMP;
|
19158
19160
|
exports.createWeeklyReport = createWeeklyReport;
|
19159
19161
|
exports.createWeeklyReportLegacy = createWeeklyReportLegacy;
|