@glowlabs-org/utils 0.2.0 → 0.2.2
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/dist/cjs/browser.d.ts +4 -0
- package/dist/cjs/browser.js +20 -0
- package/dist/cjs/browser.js.map +1 -0
- package/dist/cjs/constants/addresses.d.ts +1 -0
- package/dist/cjs/index.js +31 -597
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/use-forwarder-CVlJS4UY.js +586 -0
- package/dist/cjs/use-forwarder-CVlJS4UY.js.map +1 -0
- package/dist/esm/browser.d.ts +4 -0
- package/dist/esm/browser.js +3 -0
- package/dist/esm/browser.js.map +1 -0
- package/dist/esm/constants/addresses.d.ts +1 -0
- package/dist/esm/index.js +3 -568
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/use-forwarder-DD_5ZFZg.js +577 -0
- package/dist/esm/use-forwarder-DD_5ZFZg.js.map +1 -0
- package/package.json +15 -1
- package/rollup.config.js +10 -3
- package/src/browser.ts +4 -0
- package/src/constants/addresses.ts +6 -0
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"browser.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
package/dist/esm/index.js
CHANGED
@@ -13,6 +13,8 @@ import { parseUnits, formatUnits } from 'viem';
|
|
13
13
|
import { MerkleTree } from 'merkletreejs';
|
14
14
|
import { ethers, BigNumber } from 'ethers';
|
15
15
|
import Decimal from 'decimal.js';
|
16
|
+
import { H as HUB_URL, U as USDG_WEIGHT_DECIMAL_PRECISION, G as GLOW_WEIGHT_DECIMAL_PRECISION, M as MAX_WEIGHT } from './use-forwarder-DD_5ZFZg.js';
|
17
|
+
export { u as useForwarder } from './use-forwarder-DD_5ZFZg.js';
|
16
18
|
|
17
19
|
const GENESIS_TIMESTAMP = 1700352000;
|
18
20
|
|
@@ -18626,8 +18628,6 @@ function customToFixed(num, precision) {
|
|
18626
18628
|
return fixed;
|
18627
18629
|
}
|
18628
18630
|
|
18629
|
-
const HUB_URL = "https://glow.org";
|
18630
|
-
|
18631
18631
|
function multiplyBigIntByDecimalPercentage(bigInt, numberOfDecimals, percentage) {
|
18632
18632
|
const DENOMINATOR = BigInt(10 ** numberOfDecimals);
|
18633
18633
|
const percentageAsBigNum = BigInt(parseUnits(customToFixed(percentage, numberOfDecimals), numberOfDecimals));
|
@@ -18691,19 +18691,6 @@ function accumulateLeafWeights(key, value, map) {
|
|
18691
18691
|
// Numeric string regex: matches valid numbers (digits, optional single dot, digits after dot)
|
18692
18692
|
const NUMERIC_REGEX = /^(?:\d+\.?\d*|\.\d+)$/;
|
18693
18693
|
|
18694
|
-
/**
|
18695
|
-
* @dev This is actually not as intuitive as it seems.
|
18696
|
-
* Glow actually has 18 decimals, but glow weight is based on the amount of protocol fees (USDC) that the farm paid
|
18697
|
-
* Therefore, the weight is based on the amount of USDC that was paid, which has 8 decimals
|
18698
|
-
*/
|
18699
|
-
const GLOW_WEIGHT_DECIMAL_PRECISION = 8;
|
18700
|
-
/**
|
18701
|
-
* @dev This is actually not as intuitive as it seems.
|
18702
|
-
* USDG weight is based on the amount of carbon credits produced, but the max value of a weight is ((2*64)-1) / 5 so we need to choose sensible precision to make sure that number never overflows
|
18703
|
-
*/
|
18704
|
-
const USDG_WEIGHT_DECIMAL_PRECISION = 8;
|
18705
|
-
const MAX_WEIGHT = (BigInt(2) ** BigInt(64) - BigInt(1)) / BigInt(5);
|
18706
|
-
|
18707
18694
|
const leafTypes = ["address", "uint256", "uint256"];
|
18708
18695
|
function hashLeaf({ address, glowWeight, usdcWeight, }) {
|
18709
18696
|
const hash = ethers.utils.solidityKeccak256(leafTypes, [
|
@@ -19165,557 +19152,5 @@ async function createWeeklyReport({ week, gcaUrls, apiUrl, }) {
|
|
19165
19152
|
};
|
19166
19153
|
}
|
19167
19154
|
|
19168
|
-
|
19169
|
-
{
|
19170
|
-
inputs: [{ internalType: "address", name: "_forwarder", type: "address" }],
|
19171
|
-
stateMutability: "nonpayable",
|
19172
|
-
type: "constructor",
|
19173
|
-
},
|
19174
|
-
{
|
19175
|
-
inputs: [{ internalType: "address", name: "target", type: "address" }],
|
19176
|
-
name: "AddressEmptyCode",
|
19177
|
-
type: "error",
|
19178
|
-
},
|
19179
|
-
{
|
19180
|
-
inputs: [{ internalType: "address", name: "account", type: "address" }],
|
19181
|
-
name: "AddressInsufficientBalance",
|
19182
|
-
type: "error",
|
19183
|
-
},
|
19184
|
-
{ inputs: [], name: "FailedInnerCall", type: "error" },
|
19185
|
-
{
|
19186
|
-
inputs: [{ internalType: "address", name: "token", type: "address" }],
|
19187
|
-
name: "SafeERC20FailedOperation",
|
19188
|
-
type: "error",
|
19189
|
-
},
|
19190
|
-
{
|
19191
|
-
anonymous: false,
|
19192
|
-
inputs: [
|
19193
|
-
{ indexed: true, internalType: "address", name: "from", type: "address" },
|
19194
|
-
{ indexed: true, internalType: "address", name: "to", type: "address" },
|
19195
|
-
{
|
19196
|
-
indexed: true,
|
19197
|
-
internalType: "address",
|
19198
|
-
name: "token",
|
19199
|
-
type: "address",
|
19200
|
-
},
|
19201
|
-
{
|
19202
|
-
indexed: false,
|
19203
|
-
internalType: "uint256",
|
19204
|
-
name: "amount",
|
19205
|
-
type: "uint256",
|
19206
|
-
},
|
19207
|
-
{
|
19208
|
-
indexed: false,
|
19209
|
-
internalType: "string",
|
19210
|
-
name: "message",
|
19211
|
-
type: "string",
|
19212
|
-
},
|
19213
|
-
],
|
19214
|
-
name: "Forward",
|
19215
|
-
type: "event",
|
19216
|
-
},
|
19217
|
-
{
|
19218
|
-
inputs: [],
|
19219
|
-
name: "FORWARD_ADDRESS",
|
19220
|
-
outputs: [{ internalType: "address", name: "", type: "address" }],
|
19221
|
-
stateMutability: "view",
|
19222
|
-
type: "function",
|
19223
|
-
},
|
19224
|
-
{
|
19225
|
-
inputs: [
|
19226
|
-
{ internalType: "address", name: "token", type: "address" },
|
19227
|
-
{ internalType: "address", name: "to", type: "address" },
|
19228
|
-
{ internalType: "uint256", name: "amount", type: "uint256" },
|
19229
|
-
{ internalType: "string", name: "message", type: "string" },
|
19230
|
-
],
|
19231
|
-
name: "forward",
|
19232
|
-
outputs: [],
|
19233
|
-
stateMutability: "nonpayable",
|
19234
|
-
type: "function",
|
19235
|
-
},
|
19236
|
-
];
|
19237
|
-
|
19238
|
-
const ERC20_ABI = [
|
19239
|
-
{
|
19240
|
-
inputs: [
|
19241
|
-
{ name: "spender", type: "address" },
|
19242
|
-
{ name: "amount", type: "uint256" },
|
19243
|
-
],
|
19244
|
-
name: "approve",
|
19245
|
-
outputs: [{ name: "", type: "bool" }],
|
19246
|
-
stateMutability: "nonpayable",
|
19247
|
-
type: "function",
|
19248
|
-
},
|
19249
|
-
{
|
19250
|
-
inputs: [
|
19251
|
-
{ name: "owner", type: "address" },
|
19252
|
-
{ name: "spender", type: "address" },
|
19253
|
-
],
|
19254
|
-
name: "allowance",
|
19255
|
-
outputs: [{ name: "", type: "uint256" }],
|
19256
|
-
stateMutability: "view",
|
19257
|
-
type: "function",
|
19258
|
-
},
|
19259
|
-
{
|
19260
|
-
inputs: [
|
19261
|
-
{ name: "to", type: "address" },
|
19262
|
-
{ name: "amount", type: "uint256" },
|
19263
|
-
],
|
19264
|
-
name: "transfer",
|
19265
|
-
outputs: [{ name: "", type: "bool" }],
|
19266
|
-
stateMutability: "nonpayable",
|
19267
|
-
type: "function",
|
19268
|
-
},
|
19269
|
-
{
|
19270
|
-
inputs: [{ name: "account", type: "address" }],
|
19271
|
-
name: "balanceOf",
|
19272
|
-
outputs: [{ name: "", type: "uint256" }],
|
19273
|
-
stateMutability: "view",
|
19274
|
-
type: "function",
|
19275
|
-
},
|
19276
|
-
{
|
19277
|
-
inputs: [
|
19278
|
-
{ name: "account", type: "address" },
|
19279
|
-
{ name: "amount", type: "uint256" },
|
19280
|
-
],
|
19281
|
-
name: "mint",
|
19282
|
-
outputs: [{ name: "", type: "bool" }],
|
19283
|
-
stateMutability: "nonpayable",
|
19284
|
-
type: "function",
|
19285
|
-
},
|
19286
|
-
];
|
19287
|
-
|
19288
|
-
// Contract-specific addresses
|
19289
|
-
const mainnetAddresses = {
|
19290
|
-
USDG: "0xe010ec500720bE9EF3F82129E7eD2Ee1FB7955F2",
|
19291
|
-
GLW: "0xf4fbC617A5733EAAF9af08E1Ab816B103388d8B6",
|
19292
|
-
USDC: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
19293
|
-
FORWARDER: "0x0000000000000000000000000000000000000000", // TODO: Update with actual mainnet address
|
19294
|
-
FOUNDATION_WALLET: "0x0000000000000000000000000000000000000000", // TODO: Update with actual mainnet foundation wallet
|
19295
|
-
};
|
19296
|
-
const sepoliaAddresses = {
|
19297
|
-
USDG: "0xda78313A3fF949890112c1B746AB1c75d1b1c17B",
|
19298
|
-
GLW: "0x2039161fcE4C8e5CF5FE64e17Fd290E8dFF3c9BD",
|
19299
|
-
USDC: "0x93c898be98cd2618ba84a6dccf5003d3bbe40356",
|
19300
|
-
FORWARDER: "0x9c1d61303D46BFAb1eC5F25c12A1Bf4cB3d06416",
|
19301
|
-
FOUNDATION_WALLET: "0x5e230FED487c86B90f6508104149F087d9B1B0A7",
|
19302
|
-
};
|
19303
|
-
const getAddresses = (CHAIN_ID) => {
|
19304
|
-
switch (CHAIN_ID) {
|
19305
|
-
case 1:
|
19306
|
-
return mainnetAddresses;
|
19307
|
-
case 11155111:
|
19308
|
-
return sepoliaAddresses;
|
19309
|
-
default:
|
19310
|
-
console.warn(`Unsupported chain ID: ${CHAIN_ID}, falling back to mainnet addresses`);
|
19311
|
-
return mainnetAddresses;
|
19312
|
-
}
|
19313
|
-
};
|
19314
|
-
|
19315
|
-
var ForwarderError;
|
19316
|
-
(function (ForwarderError) {
|
19317
|
-
ForwarderError["CONTRACT_NOT_AVAILABLE"] = "Contract not available";
|
19318
|
-
ForwarderError["SIGNER_NOT_AVAILABLE"] = "Signer not available";
|
19319
|
-
ForwarderError["UNKNOWN_ERROR"] = "Unknown error";
|
19320
|
-
ForwarderError["INVALID_FORWARD_TYPE"] = "Invalid forward type";
|
19321
|
-
ForwarderError["MISSING_REQUIRED_PARAMS"] = "Missing required parameters";
|
19322
|
-
})(ForwarderError || (ForwarderError = {}));
|
19323
|
-
// Utility to extract the most useful revert reason from an ethers error object
|
19324
|
-
function parseEthersError(error) {
|
19325
|
-
if (!error)
|
19326
|
-
return "Unknown error";
|
19327
|
-
const possibleError = error;
|
19328
|
-
// If the error originates from a callStatic it will often be found at `error?.error?.body`
|
19329
|
-
if (possibleError?.error?.body) {
|
19330
|
-
try {
|
19331
|
-
const body = JSON.parse(possibleError.error.body);
|
19332
|
-
// Hardhat style errors
|
19333
|
-
if (body?.error?.message)
|
19334
|
-
return body.error.message;
|
19335
|
-
}
|
19336
|
-
catch { }
|
19337
|
-
}
|
19338
|
-
// Found on MetaMask/Alchemy shape errors
|
19339
|
-
if (possibleError?.data?.message)
|
19340
|
-
return possibleError.data.message;
|
19341
|
-
if (possibleError?.error?.message)
|
19342
|
-
return possibleError.error.message;
|
19343
|
-
// Standard ethers v5 message
|
19344
|
-
if (possibleError?.reason)
|
19345
|
-
return possibleError.reason;
|
19346
|
-
if (possibleError?.message)
|
19347
|
-
return possibleError.message;
|
19348
|
-
return ForwarderError.UNKNOWN_ERROR;
|
19349
|
-
}
|
19350
|
-
// Type-guard style helper to ensure a signer exists throughout the rest of the function.
|
19351
|
-
function assertSigner(maybeSigner) {
|
19352
|
-
if (!maybeSigner) {
|
19353
|
-
throw new Error(ForwarderError.SIGNER_NOT_AVAILABLE);
|
19354
|
-
}
|
19355
|
-
}
|
19356
|
-
function useForwarder(signer, CHAIN_ID) {
|
19357
|
-
// Use dynamic addresses based on chain configuration
|
19358
|
-
const ADDRESSES = getAddresses(CHAIN_ID);
|
19359
|
-
// Framework-agnostic processing flag
|
19360
|
-
let isProcessing = false;
|
19361
|
-
const setIsProcessing = (value) => {
|
19362
|
-
isProcessing = value;
|
19363
|
-
};
|
19364
|
-
// Returns a contract instance for Forwarder
|
19365
|
-
function getForwarderContract() {
|
19366
|
-
assertSigner(signer);
|
19367
|
-
return new ethers.Contract(ADDRESSES.FORWARDER, FORWARDER_ABI, signer);
|
19368
|
-
}
|
19369
|
-
/**
|
19370
|
-
* Construct the message for the forward call based on type and parameters
|
19371
|
-
*/
|
19372
|
-
function constructForwardMessage(params) {
|
19373
|
-
const { type, applicationId, farmId, regionId, userAddress } = params;
|
19374
|
-
switch (type) {
|
19375
|
-
case "PayProtocolFeeAndMintGCTLAndStake":
|
19376
|
-
if (!applicationId) {
|
19377
|
-
throw new Error(ForwarderError.MISSING_REQUIRED_PARAMS);
|
19378
|
-
}
|
19379
|
-
return `PayProtocolFeeAndMintGCTLAndStake::${applicationId}`;
|
19380
|
-
case "PayProtocolFee":
|
19381
|
-
if (!applicationId) {
|
19382
|
-
throw new Error(ForwarderError.MISSING_REQUIRED_PARAMS);
|
19383
|
-
}
|
19384
|
-
return `PayProtocolFee::${applicationId}`;
|
19385
|
-
case "MintGCTLAndStake":
|
19386
|
-
if (!regionId) {
|
19387
|
-
throw new Error(ForwarderError.MISSING_REQUIRED_PARAMS);
|
19388
|
-
}
|
19389
|
-
return `MintGCTLAndStake::${regionId}`;
|
19390
|
-
case "MintGCTL":
|
19391
|
-
if (!userAddress) {
|
19392
|
-
throw new Error(ForwarderError.MISSING_REQUIRED_PARAMS);
|
19393
|
-
}
|
19394
|
-
return `MintGCTL::${userAddress}`;
|
19395
|
-
case "BuySolarFarm":
|
19396
|
-
if (!farmId) {
|
19397
|
-
throw new Error(ForwarderError.MISSING_REQUIRED_PARAMS);
|
19398
|
-
}
|
19399
|
-
return `BuySolarFarm::${farmId}`;
|
19400
|
-
default:
|
19401
|
-
throw new Error(ForwarderError.INVALID_FORWARD_TYPE);
|
19402
|
-
}
|
19403
|
-
}
|
19404
|
-
/**
|
19405
|
-
* Get the appropriate token contract based on currency
|
19406
|
-
*/
|
19407
|
-
function getTokenContract(currency = "USDC") {
|
19408
|
-
assertSigner(signer);
|
19409
|
-
let tokenAddress;
|
19410
|
-
switch (currency) {
|
19411
|
-
case "USDC":
|
19412
|
-
tokenAddress = ADDRESSES.USDC;
|
19413
|
-
break;
|
19414
|
-
case "GLW":
|
19415
|
-
tokenAddress = ADDRESSES.GLW;
|
19416
|
-
break;
|
19417
|
-
case "USDG":
|
19418
|
-
tokenAddress = ADDRESSES.USDG;
|
19419
|
-
break;
|
19420
|
-
default:
|
19421
|
-
throw new Error(`Currency ${currency} not yet supported. Only USDC, GLW, and USDG are currently supported.`);
|
19422
|
-
}
|
19423
|
-
return new ethers.Contract(tokenAddress, ERC20_ABI, signer);
|
19424
|
-
}
|
19425
|
-
/**
|
19426
|
-
* Check current token allowance for the forwarder contract
|
19427
|
-
* @param owner The wallet address to check allowance for
|
19428
|
-
* @param currency The currency to check allowance for
|
19429
|
-
*/
|
19430
|
-
async function checkTokenAllowance(owner, currency = "USDC") {
|
19431
|
-
assertSigner(signer);
|
19432
|
-
try {
|
19433
|
-
const tokenContract = getTokenContract(currency);
|
19434
|
-
if (!tokenContract)
|
19435
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19436
|
-
const allowance = await tokenContract.allowance(owner, ADDRESSES.FORWARDER);
|
19437
|
-
return allowance;
|
19438
|
-
}
|
19439
|
-
catch (error) {
|
19440
|
-
throw new Error(parseEthersError(error));
|
19441
|
-
}
|
19442
|
-
}
|
19443
|
-
/**
|
19444
|
-
* Check user's token balance
|
19445
|
-
* @param owner The wallet address to check balance for
|
19446
|
-
* @param currency The currency to check balance for
|
19447
|
-
*/
|
19448
|
-
async function checkTokenBalance(owner, currency = "USDC") {
|
19449
|
-
assertSigner(signer);
|
19450
|
-
try {
|
19451
|
-
const tokenContract = getTokenContract(currency);
|
19452
|
-
if (!tokenContract)
|
19453
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19454
|
-
const balance = await tokenContract.balanceOf(owner);
|
19455
|
-
return balance;
|
19456
|
-
}
|
19457
|
-
catch (error) {
|
19458
|
-
throw new Error(parseEthersError(error));
|
19459
|
-
}
|
19460
|
-
}
|
19461
|
-
/**
|
19462
|
-
* Approve tokens for the forwarder contract
|
19463
|
-
* @param amount Amount to approve (BigNumber)
|
19464
|
-
* @param currency The currency to approve
|
19465
|
-
*/
|
19466
|
-
async function approveToken(amount, currency = "USDC") {
|
19467
|
-
assertSigner(signer);
|
19468
|
-
try {
|
19469
|
-
const tokenContract = getTokenContract(currency);
|
19470
|
-
if (!tokenContract)
|
19471
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19472
|
-
setIsProcessing(true);
|
19473
|
-
// Approve only the specific amount needed
|
19474
|
-
const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, amount);
|
19475
|
-
await approveTx.wait();
|
19476
|
-
return true;
|
19477
|
-
}
|
19478
|
-
catch (error) {
|
19479
|
-
throw new Error(parseEthersError(error));
|
19480
|
-
}
|
19481
|
-
finally {
|
19482
|
-
setIsProcessing(false);
|
19483
|
-
}
|
19484
|
-
}
|
19485
|
-
/**
|
19486
|
-
* Forward tokens through the forwarder contract with type-specific handling
|
19487
|
-
* @param params Forward parameters including type, amount, and required fields
|
19488
|
-
*/
|
19489
|
-
async function forwardTokens(params) {
|
19490
|
-
assertSigner(signer);
|
19491
|
-
try {
|
19492
|
-
const forwarderContract = getForwarderContract();
|
19493
|
-
if (!forwarderContract)
|
19494
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19495
|
-
setIsProcessing(true);
|
19496
|
-
const { amount, currency = "USDC" } = params;
|
19497
|
-
const tokenContract = getTokenContract(currency);
|
19498
|
-
if (!tokenContract)
|
19499
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19500
|
-
const owner = await signer.getAddress();
|
19501
|
-
// Construct the appropriate message for this forward type
|
19502
|
-
const message = constructForwardMessage(params);
|
19503
|
-
// Check allowance and approve if necessary
|
19504
|
-
const allowance = await tokenContract.allowance(owner, ADDRESSES.FORWARDER);
|
19505
|
-
if (allowance.lt(amount)) {
|
19506
|
-
try {
|
19507
|
-
const approveTx = await tokenContract.approve(ADDRESSES.FORWARDER, ethers.constants.MaxUint256);
|
19508
|
-
await approveTx.wait();
|
19509
|
-
}
|
19510
|
-
catch (approveError) {
|
19511
|
-
throw new Error(parseEthersError(approveError) || "Token approval failed");
|
19512
|
-
}
|
19513
|
-
}
|
19514
|
-
// Get the token address based on currency
|
19515
|
-
let tokenAddress;
|
19516
|
-
switch (currency) {
|
19517
|
-
case "USDC":
|
19518
|
-
tokenAddress = ADDRESSES.USDC;
|
19519
|
-
break;
|
19520
|
-
case "USDG":
|
19521
|
-
tokenAddress = ADDRESSES.USDG;
|
19522
|
-
break;
|
19523
|
-
case "GLW":
|
19524
|
-
tokenAddress = ADDRESSES.GLW;
|
19525
|
-
break;
|
19526
|
-
default:
|
19527
|
-
throw new Error(`Unsupported currency for forwarding: ${currency}`);
|
19528
|
-
}
|
19529
|
-
// Run a static call first to surface any revert reason
|
19530
|
-
try {
|
19531
|
-
await forwarderContract.callStatic.forward(tokenAddress, ADDRESSES.FOUNDATION_WALLET, amount, message, { from: owner });
|
19532
|
-
}
|
19533
|
-
catch (staticError) {
|
19534
|
-
throw new Error(parseEthersError(staticError));
|
19535
|
-
}
|
19536
|
-
// Execute the forward transaction
|
19537
|
-
const tx = await forwarderContract.forward(tokenAddress, ADDRESSES.FOUNDATION_WALLET, amount, message);
|
19538
|
-
await tx.wait();
|
19539
|
-
return tx.hash;
|
19540
|
-
}
|
19541
|
-
catch (txError) {
|
19542
|
-
throw new Error(parseEthersError(txError));
|
19543
|
-
}
|
19544
|
-
finally {
|
19545
|
-
setIsProcessing(false);
|
19546
|
-
}
|
19547
|
-
}
|
19548
|
-
/**
|
19549
|
-
* Forward tokens for protocol fee payment and GCTL minting with staking
|
19550
|
-
*/
|
19551
|
-
async function payProtocolFeeAndMintGCTLAndStake(amount, userAddress, applicationId, regionId, currency = "USDC") {
|
19552
|
-
assertSigner(signer);
|
19553
|
-
// GCTL minting only supports USDC and USDG
|
19554
|
-
if (currency === "GLW") {
|
19555
|
-
throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
|
19556
|
-
}
|
19557
|
-
return forwardTokens({
|
19558
|
-
amount,
|
19559
|
-
userAddress,
|
19560
|
-
type: "PayProtocolFeeAndMintGCTLAndStake",
|
19561
|
-
currency,
|
19562
|
-
applicationId,
|
19563
|
-
regionId,
|
19564
|
-
});
|
19565
|
-
}
|
19566
|
-
/**
|
19567
|
-
* Forward tokens for protocol fee payment only
|
19568
|
-
*/
|
19569
|
-
async function payProtocolFee(amount, userAddress, applicationId, currency = "USDC") {
|
19570
|
-
assertSigner(signer);
|
19571
|
-
return forwardTokens({
|
19572
|
-
amount,
|
19573
|
-
userAddress,
|
19574
|
-
type: "PayProtocolFee",
|
19575
|
-
currency,
|
19576
|
-
applicationId,
|
19577
|
-
});
|
19578
|
-
}
|
19579
|
-
/**
|
19580
|
-
* Forward USDC to mint GCTL and stake to a region
|
19581
|
-
*/
|
19582
|
-
async function mintGCTLAndStake(amount, userAddress, regionId) {
|
19583
|
-
assertSigner(signer);
|
19584
|
-
return forwardTokens({
|
19585
|
-
amount,
|
19586
|
-
userAddress,
|
19587
|
-
type: "MintGCTLAndStake",
|
19588
|
-
currency: "USDC",
|
19589
|
-
regionId,
|
19590
|
-
});
|
19591
|
-
}
|
19592
|
-
/**
|
19593
|
-
* Forward USDC to mint GCTL (existing functionality, keeping for compatibility)
|
19594
|
-
*/
|
19595
|
-
async function mintGCTL(amount, userAddress) {
|
19596
|
-
assertSigner(signer);
|
19597
|
-
return forwardTokens({
|
19598
|
-
amount,
|
19599
|
-
userAddress,
|
19600
|
-
type: "MintGCTL",
|
19601
|
-
currency: "USDC",
|
19602
|
-
});
|
19603
|
-
}
|
19604
|
-
/**
|
19605
|
-
* Forward tokens to buy a solar farm
|
19606
|
-
*/
|
19607
|
-
async function buySolarFarm(amount, userAddress, farmId, currency = "USDC") {
|
19608
|
-
assertSigner(signer);
|
19609
|
-
return forwardTokens({
|
19610
|
-
amount,
|
19611
|
-
userAddress,
|
19612
|
-
type: "BuySolarFarm",
|
19613
|
-
currency,
|
19614
|
-
farmId,
|
19615
|
-
});
|
19616
|
-
}
|
19617
|
-
/**
|
19618
|
-
* Estimate gas for forwarding with type-specific handling
|
19619
|
-
* @param params Forward parameters
|
19620
|
-
* @param ethPriceInUSD Current ETH price in USD (for cost estimation)
|
19621
|
-
*/
|
19622
|
-
async function estimateGasForForward(params, ethPriceInUSD) {
|
19623
|
-
assertSigner(signer);
|
19624
|
-
try {
|
19625
|
-
const forwarderContract = getForwarderContract();
|
19626
|
-
if (!forwarderContract)
|
19627
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19628
|
-
const { amount, currency = "USDC" } = params;
|
19629
|
-
// Construct the appropriate message for this forward type
|
19630
|
-
const message = constructForwardMessage(params);
|
19631
|
-
// Get token address
|
19632
|
-
let tokenAddress;
|
19633
|
-
switch (currency) {
|
19634
|
-
case "USDC":
|
19635
|
-
tokenAddress = ADDRESSES.USDC;
|
19636
|
-
break;
|
19637
|
-
case "USDG":
|
19638
|
-
tokenAddress = ADDRESSES.USDG;
|
19639
|
-
break;
|
19640
|
-
case "GLW":
|
19641
|
-
tokenAddress = ADDRESSES.GLW;
|
19642
|
-
break;
|
19643
|
-
default:
|
19644
|
-
throw new Error(`Unsupported currency for gas estimation: ${currency}`);
|
19645
|
-
}
|
19646
|
-
const gasPrice = await signer.getGasPrice();
|
19647
|
-
const estimatedGas = await forwarderContract.estimateGas.forward(tokenAddress, ADDRESSES.FOUNDATION_WALLET, amount, message);
|
19648
|
-
const estimatedCost = estimatedGas.mul(gasPrice);
|
19649
|
-
if (ethPriceInUSD) {
|
19650
|
-
const estimatedCostInEth = ethers.utils.formatEther(estimatedCost);
|
19651
|
-
const estimatedCostInUSD = (parseFloat(estimatedCostInEth) * ethPriceInUSD).toFixed(2);
|
19652
|
-
return estimatedCostInUSD;
|
19653
|
-
}
|
19654
|
-
else {
|
19655
|
-
throw new Error("Could not fetch the ETH price to calculate cost in USD.");
|
19656
|
-
}
|
19657
|
-
}
|
19658
|
-
catch (error) {
|
19659
|
-
throw new Error(parseEthersError(error));
|
19660
|
-
}
|
19661
|
-
}
|
19662
|
-
/**
|
19663
|
-
* Mint test USDC (only works on testnets with mintable USDC contracts)
|
19664
|
-
* @param amount Amount of USDC to mint (BigNumber, 6 decimals)
|
19665
|
-
* @param recipient Address to mint USDC to
|
19666
|
-
*/
|
19667
|
-
async function mintTestUSDC(amount, recipient) {
|
19668
|
-
assertSigner(signer);
|
19669
|
-
if (CHAIN_ID !== 11155111) {
|
19670
|
-
throw new Error("Minting test USDC is only supported on Sepolia");
|
19671
|
-
}
|
19672
|
-
try {
|
19673
|
-
const usdcContract = getTokenContract("USDC"); // Use getTokenContract for consistency
|
19674
|
-
if (!usdcContract)
|
19675
|
-
throw new Error(ForwarderError.CONTRACT_NOT_AVAILABLE);
|
19676
|
-
setIsProcessing(true);
|
19677
|
-
// Try to call mint function (common for test tokens)
|
19678
|
-
const tx = await usdcContract.mint(recipient, amount);
|
19679
|
-
await tx.wait();
|
19680
|
-
return tx.hash;
|
19681
|
-
}
|
19682
|
-
catch (error) {
|
19683
|
-
// If mint function doesn't exist or fails, provide helpful error
|
19684
|
-
const errorMessage = parseEthersError(error);
|
19685
|
-
if (errorMessage.includes("mint")) {
|
19686
|
-
throw new Error("This USDC contract doesn't support minting");
|
19687
|
-
}
|
19688
|
-
throw new Error(errorMessage);
|
19689
|
-
}
|
19690
|
-
finally {
|
19691
|
-
setIsProcessing(false);
|
19692
|
-
}
|
19693
|
-
}
|
19694
|
-
return {
|
19695
|
-
// New methods for different forward types
|
19696
|
-
forwardTokens,
|
19697
|
-
payProtocolFeeAndMintGCTLAndStake,
|
19698
|
-
payProtocolFee,
|
19699
|
-
mintGCTLAndStake,
|
19700
|
-
mintGCTL,
|
19701
|
-
buySolarFarm,
|
19702
|
-
// Token operations
|
19703
|
-
approveToken,
|
19704
|
-
checkTokenAllowance,
|
19705
|
-
checkTokenBalance,
|
19706
|
-
// Utility methods
|
19707
|
-
estimateGasForForward,
|
19708
|
-
mintTestUSDC,
|
19709
|
-
constructForwardMessage,
|
19710
|
-
// State
|
19711
|
-
get isProcessing() {
|
19712
|
-
return isProcessing;
|
19713
|
-
},
|
19714
|
-
addresses: ADDRESSES,
|
19715
|
-
// Signer availability
|
19716
|
-
isSignerAvailable: !!signer,
|
19717
|
-
};
|
19718
|
-
}
|
19719
|
-
|
19720
|
-
export { GENESIS_TIMESTAMP, createWeeklyReport, createWeeklyReportLegacy, useForwarder };
|
19155
|
+
export { GENESIS_TIMESTAMP, createWeeklyReport, createWeeklyReportLegacy };
|
19721
19156
|
//# sourceMappingURL=index.js.map
|