@glowlabs-org/utils 0.2.113 → 0.2.115

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.
@@ -223,7 +223,7 @@ const sepoliaAddresses = {
223
223
  USDG_UNISWAP: "0x2a085A3aEA8982396533327c854753Ce521B666d",
224
224
  GLW_UNISWAP: "0x8e27016D0B866a56CE74A1a280c749dD679bb0Fa",
225
225
  FORWARDER: "0xDaC24F18171224eeaf6a9B38f5C5081aDbDff969",
226
- OFFCHAIN_FRACTIONS: "0x9976d13930bdEB80C03706B7a6333B00fdee3BB3",
226
+ OFFCHAIN_FRACTIONS: "0x7D97528eeF8B8D17AA1Ba3307Ed39b07b3A1b69B",
227
227
  COUNTERFACTUAL_HOLDER_FACTORY: "0x2c3AB887746F6f4a8a4b9Db6aC800eb71945509A",
228
228
  FOUNDATION_WALLET: "0x5e230FED487c86B90f6508104149F087d9B1B0A7",
229
229
  UNISWAP_V2_ROUTER: "0xeE567Fe1712Faf6149d80dA1E6934E354124CfE3",
@@ -288,7 +288,7 @@ var ForwarderError;
288
288
  ForwarderError["MISSING_REQUIRED_PARAMS"] = "Missing required parameters";
289
289
  })(ForwarderError || (ForwarderError = {}));
290
290
  // Utility to extract the most useful revert reason from an ethers error object
291
- function parseEthersError$1(error) {
291
+ function parseEthersError(error) {
292
292
  if (!error)
293
293
  return "Unknown error";
294
294
  const possibleError = error;
@@ -315,7 +315,7 @@ function parseEthersError$1(error) {
315
315
  return ForwarderError.UNKNOWN_ERROR;
316
316
  }
317
317
  // Type-guard style helper to ensure a signer exists throughout the rest of the function.
318
- function assertSigner$1(maybeSigner) {
318
+ function assertSigner(maybeSigner) {
319
319
  if (!maybeSigner) {
320
320
  throw new Error(ForwarderError.SIGNER_NOT_AVAILABLE);
321
321
  }
@@ -330,7 +330,7 @@ function useForwarder(signer, CHAIN_ID) {
330
330
  };
331
331
  // Returns a contract instance for Forwarder
332
332
  function getForwarderContract() {
333
- assertSigner$1(signer);
333
+ assertSigner(signer);
334
334
  return new Contract(ADDRESSES.FORWARDER, FORWARDER_ABI, signer);
335
335
  }
336
336
  /**
@@ -392,7 +392,7 @@ function useForwarder(signer, CHAIN_ID) {
392
392
  * Get the appropriate token contract based on currency
393
393
  */
394
394
  function getTokenContract(currency = "USDC") {
395
- assertSigner$1(signer);
395
+ assertSigner(signer);
396
396
  let tokenAddress;
397
397
  switch (currency) {
398
398
  case "USDC":
@@ -415,7 +415,7 @@ function useForwarder(signer, CHAIN_ID) {
415
415
  * @param currency The currency to check allowance for
416
416
  */
417
417
  async function checkTokenAllowance(owner, currency = "USDC") {
418
- assertSigner$1(signer);
418
+ assertSigner(signer);
419
419
  try {
420
420
  const tokenContract = getTokenContract(currency);
421
421
  if (!tokenContract)
@@ -424,7 +424,7 @@ function useForwarder(signer, CHAIN_ID) {
424
424
  return allowance;
425
425
  }
426
426
  catch (error) {
427
- throw new Error(parseEthersError$1(error));
427
+ throw new Error(parseEthersError(error));
428
428
  }
429
429
  }
430
430
  /**
@@ -433,7 +433,7 @@ function useForwarder(signer, CHAIN_ID) {
433
433
  * @param currency The currency to check balance for
434
434
  */
435
435
  async function checkTokenBalance(owner, currency = "USDC") {
436
- assertSigner$1(signer);
436
+ assertSigner(signer);
437
437
  try {
438
438
  const tokenContract = getTokenContract(currency);
439
439
  if (!tokenContract)
@@ -442,7 +442,7 @@ function useForwarder(signer, CHAIN_ID) {
442
442
  return balance;
443
443
  }
444
444
  catch (error) {
445
- throw new Error(parseEthersError$1(error));
445
+ throw new Error(parseEthersError(error));
446
446
  }
447
447
  }
448
448
  /**
@@ -451,7 +451,7 @@ function useForwarder(signer, CHAIN_ID) {
451
451
  * @param currency The currency to approve
452
452
  */
453
453
  async function approveToken(amount, currency = "USDC") {
454
- assertSigner$1(signer);
454
+ assertSigner(signer);
455
455
  try {
456
456
  const tokenContract = getTokenContract(currency);
457
457
  if (!tokenContract)
@@ -463,7 +463,7 @@ function useForwarder(signer, CHAIN_ID) {
463
463
  return true;
464
464
  }
465
465
  catch (error) {
466
- throw new Error(parseEthersError$1(error));
466
+ throw new Error(parseEthersError(error));
467
467
  }
468
468
  finally {
469
469
  setIsProcessing(false);
@@ -474,7 +474,7 @@ function useForwarder(signer, CHAIN_ID) {
474
474
  * @param params Forward parameters including type, amount, and required fields
475
475
  */
476
476
  async function forwardTokens(params) {
477
- assertSigner$1(signer);
477
+ assertSigner(signer);
478
478
  try {
479
479
  const forwarderContract = getForwarderContract();
480
480
  if (!forwarderContract)
@@ -506,7 +506,7 @@ function useForwarder(signer, CHAIN_ID) {
506
506
  await approveTx.wait();
507
507
  }
508
508
  catch (approveError) {
509
- throw new Error(parseEthersError$1(approveError) || "Token approval failed");
509
+ throw new Error(parseEthersError(approveError) || "Token approval failed");
510
510
  }
511
511
  }
512
512
  // Get the token address based on currency
@@ -544,7 +544,7 @@ function useForwarder(signer, CHAIN_ID) {
544
544
  }
545
545
  }
546
546
  catch (staticError) {
547
- throw new Error(parseEthersError$1(staticError));
547
+ throw new Error(parseEthersError(staticError));
548
548
  }
549
549
  // Execute the forward transaction
550
550
  let tx;
@@ -560,7 +560,7 @@ function useForwarder(signer, CHAIN_ID) {
560
560
  return tx.hash;
561
561
  }
562
562
  catch (txError) {
563
- throw new Error(parseEthersError$1(txError));
563
+ throw new Error(parseEthersError(txError));
564
564
  }
565
565
  finally {
566
566
  setIsProcessing(false);
@@ -570,7 +570,7 @@ function useForwarder(signer, CHAIN_ID) {
570
570
  * Forward tokens for protocol fee payment and GCTL minting with staking
571
571
  */
572
572
  async function payProtocolFeeAndMintGCTLAndStake(amount, userAddress, applicationId, regionId, currency = "USDC") {
573
- assertSigner$1(signer);
573
+ assertSigner(signer);
574
574
  // GCTL minting only supports USDC and USDG
575
575
  if (currency === "GLW") {
576
576
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -588,7 +588,7 @@ function useForwarder(signer, CHAIN_ID) {
588
588
  * Forward tokens for protocol fee payment and GCTL minting with staking
589
589
  */
590
590
  async function sponsorProtocolFeeAndMintGCTLAndStake(amount, userAddress, applicationId, currency = "USDC") {
591
- assertSigner$1(signer);
591
+ assertSigner(signer);
592
592
  if (currency === "GLW") {
593
593
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
594
594
  }
@@ -604,7 +604,7 @@ function useForwarder(signer, CHAIN_ID) {
604
604
  * Forward tokens for protocol fee payment only
605
605
  */
606
606
  async function payProtocolFee(amount, userAddress, applicationId, currency = "USDC") {
607
- assertSigner$1(signer);
607
+ assertSigner(signer);
608
608
  return forwardTokens({
609
609
  amount,
610
610
  userAddress,
@@ -617,7 +617,7 @@ function useForwarder(signer, CHAIN_ID) {
617
617
  * Forward tokens for protocol fee payment only
618
618
  */
619
619
  async function sponsorProtocolFee(amount, userAddress, applicationId, currency = "USDC") {
620
- assertSigner$1(signer);
620
+ assertSigner(signer);
621
621
  return forwardTokens({
622
622
  amount,
623
623
  userAddress,
@@ -630,7 +630,7 @@ function useForwarder(signer, CHAIN_ID) {
630
630
  * Forward USDC to mint GCTL and stake to a region
631
631
  */
632
632
  async function mintGCTLAndStake(amount, userAddress, regionId, currency = "USDC") {
633
- assertSigner$1(signer);
633
+ assertSigner(signer);
634
634
  // GCTL minting only supports USDC and USDG
635
635
  if (currency === "GLW") {
636
636
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -647,7 +647,7 @@ function useForwarder(signer, CHAIN_ID) {
647
647
  * Forward USDC to mint GCTL (existing functionality, keeping for compatibility)
648
648
  */
649
649
  async function mintGCTL(amount, userAddress, currency = "USDC") {
650
- assertSigner$1(signer);
650
+ assertSigner(signer);
651
651
  // GCTL minting only supports USDC and USDG
652
652
  if (currency === "GLW") {
653
653
  throw new Error("GCTL minting is not supported with GLW payment. Use USDC or USDG.");
@@ -663,7 +663,7 @@ function useForwarder(signer, CHAIN_ID) {
663
663
  * Forward tokens to pay audit fees (USDC only, calls forward())
664
664
  */
665
665
  async function payAuditFees(amount, userAddress, applicationId) {
666
- assertSigner$1(signer);
666
+ assertSigner(signer);
667
667
  return forwardTokens({
668
668
  amount,
669
669
  userAddress,
@@ -676,7 +676,7 @@ function useForwarder(signer, CHAIN_ID) {
676
676
  * Forward tokens to buy a solar farm
677
677
  */
678
678
  async function buySolarFarm(amount, userAddress, farmId, currency = "USDC") {
679
- assertSigner$1(signer);
679
+ assertSigner(signer);
680
680
  return forwardTokens({
681
681
  amount,
682
682
  userAddress,
@@ -689,7 +689,7 @@ function useForwarder(signer, CHAIN_ID) {
689
689
  * Forward tokens to commit to a Kickstarter (USDC or USDG only)
690
690
  */
691
691
  async function commitKickstarter(amount, userAddress, kickstarterId, currency = "USDC") {
692
- assertSigner$1(signer);
692
+ assertSigner(signer);
693
693
  if (currency === "GLW") {
694
694
  throw new Error("CommitKickstarter supports only USDC or USDG");
695
695
  }
@@ -707,7 +707,7 @@ function useForwarder(signer, CHAIN_ID) {
707
707
  * @param ethPriceInUSD Current ETH price in USD (for cost estimation)
708
708
  */
709
709
  async function estimateGasForForward(params, ethPriceInUSD) {
710
- assertSigner$1(signer);
710
+ assertSigner(signer);
711
711
  try {
712
712
  const forwarderContract = getForwarderContract();
713
713
  if (!forwarderContract)
@@ -761,7 +761,7 @@ function useForwarder(signer, CHAIN_ID) {
761
761
  }
762
762
  }
763
763
  catch (error) {
764
- throw new Error(parseEthersError$1(error));
764
+ throw new Error(parseEthersError(error));
765
765
  }
766
766
  }
767
767
  /**
@@ -770,7 +770,7 @@ function useForwarder(signer, CHAIN_ID) {
770
770
  * @param recipient Address to mint USDC to
771
771
  */
772
772
  async function mintTestUSDC(amount, recipient) {
773
- assertSigner$1(signer);
773
+ assertSigner(signer);
774
774
  if (CHAIN_ID !== 11155111) {
775
775
  throw new Error("Minting test USDC is only supported on Sepolia");
776
776
  }
@@ -786,7 +786,7 @@ function useForwarder(signer, CHAIN_ID) {
786
786
  }
787
787
  catch (error) {
788
788
  // If mint function doesn't exist or fails, provide helpful error
789
- const errorMessage = parseEthersError$1(error);
789
+ const errorMessage = parseEthersError(error);
790
790
  if (errorMessage.includes("mint")) {
791
791
  throw new Error("This USDC contract doesn't support minting");
792
792
  }
@@ -1298,40 +1298,37 @@ var OffchainFractionsError;
1298
1298
  OffchainFractionsError["INSUFFICIENT_BALANCE"] = "Insufficient balance";
1299
1299
  OffchainFractionsError["INSUFFICIENT_ALLOWANCE"] = "Insufficient allowance";
1300
1300
  })(OffchainFractionsError || (OffchainFractionsError = {}));
1301
- // Utility to extract the most useful revert reason from an ethers error object
1302
- function parseEthersError(error) {
1301
+ // Utility to extract the most useful revert reason from a viem error object
1302
+ function parseViemError(error) {
1303
1303
  if (!error)
1304
1304
  return "Unknown error";
1305
- const possibleError = error;
1306
- // If the error originates from a callStatic it will often be found at `error?.error?.body`
1307
- if (possibleError?.error?.body) {
1308
- try {
1309
- const body = JSON.parse(possibleError.error.body);
1310
- // Hardhat style errors
1311
- if (body?.error?.message)
1312
- return body.error.message;
1305
+ // Check if it's a viem BaseError
1306
+ if (error instanceof Error) {
1307
+ // For contract revert errors
1308
+ if (error.cause?.reason) {
1309
+ return error.cause.reason;
1310
+ }
1311
+ // For viem's shortMessage
1312
+ if (error.shortMessage) {
1313
+ return error.shortMessage;
1314
+ }
1315
+ // Fallback to regular message
1316
+ if (error.message) {
1317
+ return error.message;
1313
1318
  }
1314
- catch { }
1315
1319
  }
1316
- // Found on MetaMask/Alchemy shape errors
1317
- if (possibleError?.data?.message)
1318
- return possibleError.data.message;
1319
- if (possibleError?.error?.message)
1320
- return possibleError.error.message;
1321
- // Standard ethers v5 message
1322
- if (possibleError?.reason)
1323
- return possibleError.reason;
1324
- if (possibleError?.message)
1325
- return possibleError.message;
1326
1320
  return OffchainFractionsError.UNKNOWN_ERROR;
1327
1321
  }
1328
- // Type-guard style helper to ensure a signer exists throughout the rest of the function.
1329
- function assertSigner(maybeSigner) {
1330
- if (!maybeSigner) {
1322
+ // Type-guard style helper to ensure a wallet client exists throughout the rest of the function.
1323
+ function assertWalletClient(maybeWalletClient) {
1324
+ if (!maybeWalletClient) {
1331
1325
  throw new Error(OffchainFractionsError.SIGNER_NOT_AVAILABLE);
1332
1326
  }
1327
+ if (!maybeWalletClient.account) {
1328
+ throw new Error("Wallet client must have an account");
1329
+ }
1333
1330
  }
1334
- function useOffchainFractions(signer, CHAIN_ID) {
1331
+ function useOffchainFractions(walletClient, publicClient, CHAIN_ID) {
1335
1332
  // Use dynamic addresses based on chain configuration
1336
1333
  const ADDRESSES = getAddresses(CHAIN_ID);
1337
1334
  // Framework-agnostic processing flag
@@ -1339,17 +1336,11 @@ function useOffchainFractions(signer, CHAIN_ID) {
1339
1336
  const setIsProcessing = (value) => {
1340
1337
  isProcessing = value;
1341
1338
  };
1342
- // Returns a contract instance for OffchainFractions
1343
- function getOffchainFractionsContract() {
1344
- assertSigner(signer);
1345
- return new Contract(ADDRESSES.OFFCHAIN_FRACTIONS, OFFCHAIN_FRACTIONS_ABI, signer);
1346
- }
1347
- /**
1348
- * Get the appropriate token contract
1349
- */
1350
- function getTokenContract(tokenAddress) {
1351
- assertSigner(signer);
1352
- return new Contract(tokenAddress, ERC20_ABI, signer);
1339
+ // Helper to assert public client is available
1340
+ function assertPublicClient(maybePublicClient) {
1341
+ if (!maybePublicClient) {
1342
+ throw new Error("Public client not available");
1343
+ }
1353
1344
  }
1354
1345
  /**
1355
1346
  * Check current token allowance for the offchain fractions contract
@@ -1357,16 +1348,18 @@ function useOffchainFractions(signer, CHAIN_ID) {
1357
1348
  * @param tokenAddress The token contract address
1358
1349
  */
1359
1350
  async function checkTokenAllowance(owner, tokenAddress) {
1360
- assertSigner(signer);
1351
+ assertPublicClient(publicClient);
1361
1352
  try {
1362
- const tokenContract = getTokenContract(tokenAddress);
1363
- if (!tokenContract)
1364
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1365
- const allowance = await tokenContract.allowance(owner, ADDRESSES.OFFCHAIN_FRACTIONS);
1353
+ const allowance = await publicClient.readContract({
1354
+ address: tokenAddress,
1355
+ abi: ERC20_ABI,
1356
+ functionName: "allowance",
1357
+ args: [owner, ADDRESSES.OFFCHAIN_FRACTIONS],
1358
+ });
1366
1359
  return allowance;
1367
1360
  }
1368
1361
  catch (error) {
1369
- throw new Error(parseEthersError(error));
1362
+ throw new Error(parseViemError(error));
1370
1363
  }
1371
1364
  }
1372
1365
  /**
@@ -1375,16 +1368,18 @@ function useOffchainFractions(signer, CHAIN_ID) {
1375
1368
  * @param tokenAddress The token contract address
1376
1369
  */
1377
1370
  async function checkTokenBalance(owner, tokenAddress) {
1378
- assertSigner(signer);
1371
+ assertPublicClient(publicClient);
1379
1372
  try {
1380
- const tokenContract = getTokenContract(tokenAddress);
1381
- if (!tokenContract)
1382
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1383
- const balance = await tokenContract.balanceOf(owner);
1373
+ const balance = await publicClient.readContract({
1374
+ address: tokenAddress,
1375
+ abi: ERC20_ABI,
1376
+ functionName: "balanceOf",
1377
+ args: [owner],
1378
+ });
1384
1379
  return balance;
1385
1380
  }
1386
1381
  catch (error) {
1387
- throw new Error(parseEthersError(error));
1382
+ throw new Error(parseViemError(error));
1388
1383
  }
1389
1384
  }
1390
1385
  /**
@@ -1393,18 +1388,25 @@ function useOffchainFractions(signer, CHAIN_ID) {
1393
1388
  * @param amount Amount to approve (BigNumber)
1394
1389
  */
1395
1390
  async function approveToken(tokenAddress, amount) {
1396
- assertSigner(signer);
1391
+ assertWalletClient(walletClient);
1392
+ assertPublicClient(publicClient);
1397
1393
  try {
1398
- const tokenContract = getTokenContract(tokenAddress);
1399
- if (!tokenContract)
1400
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1401
1394
  setIsProcessing(true);
1402
- const approveTx = await tokenContract.approve(ADDRESSES.OFFCHAIN_FRACTIONS, amount);
1403
- await approveTx.wait();
1395
+ const hash = await walletClient.writeContract({
1396
+ address: tokenAddress,
1397
+ abi: ERC20_ABI,
1398
+ functionName: "approve",
1399
+ args: [ADDRESSES.OFFCHAIN_FRACTIONS, amount],
1400
+ chain: walletClient.chain,
1401
+ account: walletClient.account,
1402
+ });
1403
+ await publicClient.waitForTransactionReceipt({
1404
+ hash,
1405
+ });
1404
1406
  return true;
1405
1407
  }
1406
1408
  catch (error) {
1407
- throw new Error(parseEthersError(error));
1409
+ throw new Error(parseViemError(error));
1408
1410
  }
1409
1411
  finally {
1410
1412
  setIsProcessing(false);
@@ -1415,11 +1417,9 @@ function useOffchainFractions(signer, CHAIN_ID) {
1415
1417
  * @param params Parameters for creating the fraction
1416
1418
  */
1417
1419
  async function createFraction(params) {
1418
- assertSigner(signer);
1420
+ assertWalletClient(walletClient);
1421
+ assertPublicClient(publicClient);
1419
1422
  try {
1420
- const contract = getOffchainFractionsContract();
1421
- if (!contract)
1422
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1423
1423
  setIsProcessing(true);
1424
1424
  const { id, token, step, totalSteps, expiration, to, useCounterfactualAddress, minSharesToRaise, closer, } = params;
1425
1425
  // Validate parameters
@@ -1432,22 +1432,55 @@ function useOffchainFractions(signer, CHAIN_ID) {
1432
1432
  if (minSharesToRaise > totalSteps) {
1433
1433
  throw new Error("minSharesToRaise cannot be greater than totalSteps");
1434
1434
  }
1435
- // Run a static call first to surface any revert reason
1435
+ // Run a simulation first to surface any revert reason
1436
1436
  try {
1437
- await contract
1438
- .getFunction("createFraction")
1439
- .staticCall(id, token, step, totalSteps, expiration, to, useCounterfactualAddress, minSharesToRaise, closer);
1437
+ await publicClient.simulateContract({
1438
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1439
+ abi: OFFCHAIN_FRACTIONS_ABI,
1440
+ functionName: "createFraction",
1441
+ args: [
1442
+ id,
1443
+ token,
1444
+ step,
1445
+ totalSteps,
1446
+ expiration,
1447
+ to,
1448
+ useCounterfactualAddress,
1449
+ minSharesToRaise,
1450
+ closer,
1451
+ ],
1452
+ account: walletClient.account,
1453
+ });
1440
1454
  }
1441
- catch (staticError) {
1442
- throw new Error(parseEthersError(staticError));
1455
+ catch (simulationError) {
1456
+ throw new Error(parseViemError(simulationError));
1443
1457
  }
1444
1458
  // Execute the transaction
1445
- const tx = await contract.getFunction("createFraction")(id, token, step, totalSteps, expiration, to, useCounterfactualAddress, minSharesToRaise, closer);
1446
- await tx.wait();
1447
- return tx.hash;
1459
+ const hash = await walletClient.writeContract({
1460
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1461
+ abi: OFFCHAIN_FRACTIONS_ABI,
1462
+ functionName: "createFraction",
1463
+ args: [
1464
+ id,
1465
+ token,
1466
+ step,
1467
+ totalSteps,
1468
+ expiration,
1469
+ to,
1470
+ useCounterfactualAddress,
1471
+ minSharesToRaise,
1472
+ closer,
1473
+ ],
1474
+ chain: walletClient.chain,
1475
+ account: walletClient.account,
1476
+ });
1477
+ await publicClient.waitForTransactionReceipt({
1478
+ hash,
1479
+ });
1480
+ return hash;
1448
1481
  }
1449
1482
  catch (error) {
1450
- throw new Error(parseEthersError(error));
1483
+ throw new Error(parseViemError(error));
1451
1484
  }
1452
1485
  finally {
1453
1486
  setIsProcessing(false);
@@ -1458,11 +1491,9 @@ function useOffchainFractions(signer, CHAIN_ID) {
1458
1491
  * @param params Parameters for buying fractions
1459
1492
  */
1460
1493
  async function buyFractions(params) {
1461
- assertSigner(signer);
1494
+ assertWalletClient(walletClient);
1495
+ assertPublicClient(publicClient);
1462
1496
  try {
1463
- const contract = getOffchainFractionsContract();
1464
- if (!contract)
1465
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1466
1497
  setIsProcessing(true);
1467
1498
  const { creator, id, stepsToBuy, minStepsToBuy, refundTo, creditTo, useCounterfactualAddressForRefund, } = params;
1468
1499
  // Validate parameters
@@ -1478,7 +1509,10 @@ function useOffchainFractions(signer, CHAIN_ID) {
1478
1509
  // Get fraction data to calculate required amount
1479
1510
  const fractionData = await getFraction(creator, id);
1480
1511
  const requiredAmount = stepsToBuy * fractionData.step;
1481
- const owner = await signer.getAddress();
1512
+ const owner = walletClient.account?.address;
1513
+ if (!owner) {
1514
+ throw new Error("No account found in wallet client");
1515
+ }
1482
1516
  // Check token balance
1483
1517
  const balance = await checkTokenBalance(owner, fractionData.token);
1484
1518
  if (balance < requiredAmount) {
@@ -1487,28 +1521,63 @@ function useOffchainFractions(signer, CHAIN_ID) {
1487
1521
  // Check and approve tokens if necessary
1488
1522
  const allowance = await checkTokenAllowance(owner, fractionData.token);
1489
1523
  if (allowance < requiredAmount) {
1490
- const tokenContract = getTokenContract(fractionData.token);
1491
- const approveTx = await tokenContract.approve(ADDRESSES.OFFCHAIN_FRACTIONS, requiredAmount);
1492
- await approveTx.wait();
1524
+ const approveHash = await walletClient.writeContract({
1525
+ address: fractionData.token,
1526
+ abi: ERC20_ABI,
1527
+ functionName: "approve",
1528
+ args: [ADDRESSES.OFFCHAIN_FRACTIONS, requiredAmount],
1529
+ chain: walletClient.chain,
1530
+ account: walletClient.account,
1531
+ });
1532
+ await publicClient.waitForTransactionReceipt({
1533
+ hash: approveHash,
1534
+ });
1493
1535
  }
1494
- // Run a static call first to surface any revert reason
1536
+ // Run a simulation first to surface any revert reason
1495
1537
  try {
1496
- await contract
1497
- .getFunction("buyFractions")
1498
- .staticCall(creator, id, stepsToBuy, minStepsToBuy, refundTo, creditTo, useCounterfactualAddressForRefund, {
1499
- from: owner,
1538
+ await publicClient.simulateContract({
1539
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1540
+ abi: OFFCHAIN_FRACTIONS_ABI,
1541
+ functionName: "buyFractions",
1542
+ args: [
1543
+ creator,
1544
+ id,
1545
+ stepsToBuy,
1546
+ minStepsToBuy,
1547
+ refundTo,
1548
+ creditTo,
1549
+ useCounterfactualAddressForRefund,
1550
+ ],
1551
+ account: walletClient.account,
1500
1552
  });
1501
1553
  }
1502
- catch (staticError) {
1503
- throw new Error(parseEthersError(staticError));
1554
+ catch (simulationError) {
1555
+ throw new Error(parseViemError(simulationError));
1504
1556
  }
1505
1557
  // Execute the transaction
1506
- const tx = await contract.getFunction("buyFractions")(creator, id, stepsToBuy, minStepsToBuy, refundTo, creditTo, useCounterfactualAddressForRefund);
1507
- await tx.wait();
1508
- return tx.hash;
1558
+ const hash = await walletClient.writeContract({
1559
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1560
+ abi: OFFCHAIN_FRACTIONS_ABI,
1561
+ functionName: "buyFractions",
1562
+ args: [
1563
+ creator,
1564
+ id,
1565
+ stepsToBuy,
1566
+ minStepsToBuy,
1567
+ refundTo,
1568
+ creditTo,
1569
+ useCounterfactualAddressForRefund,
1570
+ ],
1571
+ chain: walletClient.chain,
1572
+ account: walletClient.account,
1573
+ });
1574
+ await publicClient.waitForTransactionReceipt({
1575
+ hash,
1576
+ });
1577
+ return hash;
1509
1578
  }
1510
1579
  catch (error) {
1511
- throw new Error(parseEthersError(error));
1580
+ throw new Error(parseViemError(error));
1512
1581
  }
1513
1582
  finally {
1514
1583
  setIsProcessing(false);
@@ -1521,11 +1590,9 @@ function useOffchainFractions(signer, CHAIN_ID) {
1521
1590
  * @param id The unique identifier of the fraction sale
1522
1591
  */
1523
1592
  async function claimRefund(user, creator, id) {
1524
- assertSigner(signer);
1593
+ assertWalletClient(walletClient);
1594
+ assertPublicClient(publicClient);
1525
1595
  try {
1526
- const contract = getOffchainFractionsContract();
1527
- if (!contract)
1528
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1529
1596
  setIsProcessing(true);
1530
1597
  // Normalize addresses to lowercase for consistency
1531
1598
  const normalizedUser = user?.toLowerCase();
@@ -1545,37 +1612,52 @@ function useOffchainFractions(signer, CHAIN_ID) {
1545
1612
  if (!normalizedUser || !normalizedCreator || !id) {
1546
1613
  throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
1547
1614
  }
1548
- const owner = await signer.getAddress();
1615
+ const owner = walletClient.account?.address;
1616
+ if (!owner) {
1617
+ throw new Error("No account found in wallet client");
1618
+ }
1549
1619
  // Check if user has steps purchased
1550
1620
  const userSteps = await getStepsPurchased(user, creator, id);
1551
1621
  if (userSteps === 0n) {
1552
1622
  throw new Error("No steps purchased for this fraction");
1553
1623
  }
1554
- // Run a static call first to surface any revert reason
1624
+ // Run a simulation first to surface any revert reason
1555
1625
  try {
1556
- console.log("Calling claimRefund static call with:", {
1626
+ console.log("Calling claimRefund simulation with:", {
1557
1627
  user,
1558
1628
  creator,
1559
1629
  id,
1560
1630
  from: owner,
1561
1631
  contractMethod: "claimRefund",
1562
1632
  });
1563
- // Ethers v6 syntax
1564
- await contract.claimRefund.staticCall(user, creator, id, {
1565
- from: owner,
1633
+ await publicClient.simulateContract({
1634
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1635
+ abi: OFFCHAIN_FRACTIONS_ABI,
1636
+ functionName: "claimRefund",
1637
+ args: [user, creator, id],
1638
+ account: walletClient.account,
1566
1639
  });
1567
1640
  }
1568
- catch (staticError) {
1569
- console.error("Static call failed:", staticError);
1570
- throw new Error(parseEthersError(staticError));
1641
+ catch (simulationError) {
1642
+ console.error("Simulation failed:", simulationError);
1643
+ throw new Error(parseViemError(simulationError));
1571
1644
  }
1572
- // Execute the transaction (ethers v6 syntax)
1573
- const tx = await contract.claimRefund(user, creator, id);
1574
- await tx.wait();
1575
- return tx.hash;
1645
+ // Execute the transaction
1646
+ const hash = await walletClient.writeContract({
1647
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1648
+ abi: OFFCHAIN_FRACTIONS_ABI,
1649
+ functionName: "claimRefund",
1650
+ args: [user, creator, id],
1651
+ chain: walletClient.chain,
1652
+ account: walletClient.account,
1653
+ });
1654
+ await publicClient.waitForTransactionReceipt({
1655
+ hash,
1656
+ });
1657
+ return hash;
1576
1658
  }
1577
1659
  catch (error) {
1578
- throw new Error(parseEthersError(error));
1660
+ throw new Error(parseViemError(error));
1579
1661
  }
1580
1662
  finally {
1581
1663
  setIsProcessing(false);
@@ -1587,33 +1669,47 @@ function useOffchainFractions(signer, CHAIN_ID) {
1587
1669
  * @param id The unique identifier of the fraction sale to close
1588
1670
  */
1589
1671
  async function closeFraction(creator, id) {
1590
- assertSigner(signer);
1672
+ assertWalletClient(walletClient);
1673
+ assertPublicClient(publicClient);
1591
1674
  try {
1592
- const contract = getOffchainFractionsContract();
1593
- if (!contract)
1594
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1595
1675
  setIsProcessing(true);
1596
1676
  // Validate parameters
1597
1677
  if (!creator || !id) {
1598
1678
  throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
1599
1679
  }
1600
- const owner = await signer.getAddress();
1601
- // Run a static call first to surface any revert reason
1680
+ const owner = walletClient.account?.address;
1681
+ if (!owner) {
1682
+ throw new Error("No account found in wallet client");
1683
+ }
1684
+ // Run a simulation first to surface any revert reason
1602
1685
  try {
1603
- await contract.getFunction("closeFraction").staticCall(creator, id, {
1604
- from: owner,
1686
+ await publicClient.simulateContract({
1687
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1688
+ abi: OFFCHAIN_FRACTIONS_ABI,
1689
+ functionName: "closeFraction",
1690
+ args: [creator, id],
1691
+ account: walletClient.account,
1605
1692
  });
1606
1693
  }
1607
- catch (staticError) {
1608
- throw new Error(parseEthersError(staticError));
1694
+ catch (simulationError) {
1695
+ throw new Error(parseViemError(simulationError));
1609
1696
  }
1610
1697
  // Execute the transaction
1611
- const tx = await contract.getFunction("closeFraction")(creator, id);
1612
- await tx.wait();
1613
- return tx.hash;
1698
+ const hash = await walletClient.writeContract({
1699
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1700
+ abi: OFFCHAIN_FRACTIONS_ABI,
1701
+ functionName: "closeFraction",
1702
+ args: [creator, id],
1703
+ chain: walletClient.chain,
1704
+ account: walletClient.account,
1705
+ });
1706
+ await publicClient.waitForTransactionReceipt({
1707
+ hash,
1708
+ });
1709
+ return hash;
1614
1710
  }
1615
1711
  catch (error) {
1616
- throw new Error(parseEthersError(error));
1712
+ throw new Error(parseViemError(error));
1617
1713
  }
1618
1714
  finally {
1619
1715
  setIsProcessing(false);
@@ -1625,12 +1721,14 @@ function useOffchainFractions(signer, CHAIN_ID) {
1625
1721
  * @param id The unique identifier of the fraction sale
1626
1722
  */
1627
1723
  async function getFraction(creator, id) {
1628
- assertSigner(signer);
1629
- try {
1630
- const contract = getOffchainFractionsContract();
1631
- if (!contract)
1632
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1633
- const result = await contract.getFunction("getFraction")(creator, id);
1724
+ assertPublicClient(publicClient);
1725
+ try {
1726
+ const result = (await publicClient.readContract({
1727
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1728
+ abi: OFFCHAIN_FRACTIONS_ABI,
1729
+ functionName: "getFraction",
1730
+ args: [creator, id],
1731
+ }));
1634
1732
  return {
1635
1733
  token: result.token,
1636
1734
  expiration: Number(result.expiration),
@@ -1646,7 +1744,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1646
1744
  };
1647
1745
  }
1648
1746
  catch (error) {
1649
- throw new Error(parseEthersError(error));
1747
+ throw new Error(parseViemError(error));
1650
1748
  }
1651
1749
  }
1652
1750
  /**
@@ -1656,24 +1754,26 @@ function useOffchainFractions(signer, CHAIN_ID) {
1656
1754
  * @param id The unique identifier of the fraction sale
1657
1755
  */
1658
1756
  async function getStepsPurchased(user, creator, id) {
1659
- assertSigner(signer);
1757
+ assertPublicClient(publicClient);
1660
1758
  try {
1661
- const contract = getOffchainFractionsContract();
1662
- if (!contract)
1663
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1664
1759
  // Debug logging
1665
1760
  console.log("getStepsPurchased parameters:", {
1666
1761
  user,
1667
1762
  creator,
1668
1763
  id,
1669
- contractAddress: contract.address,
1764
+ contractAddress: ADDRESSES.OFFCHAIN_FRACTIONS,
1670
1765
  });
1671
- const result = await contract.getFunction("stepsPurchased")(user, creator, id);
1766
+ const result = (await publicClient.readContract({
1767
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1768
+ abi: OFFCHAIN_FRACTIONS_ABI,
1769
+ functionName: "stepsPurchased",
1770
+ args: [user, creator, id],
1771
+ }));
1672
1772
  console.log("getStepsPurchased result:", result.toString());
1673
1773
  return result;
1674
1774
  }
1675
1775
  catch (error) {
1676
- throw new Error(parseEthersError(error));
1776
+ throw new Error(parseViemError(error));
1677
1777
  }
1678
1778
  }
1679
1779
  /**
@@ -1681,16 +1781,18 @@ function useOffchainFractions(signer, CHAIN_ID) {
1681
1781
  * @returns The wildcard operator address that can perform refunds for any user
1682
1782
  */
1683
1783
  async function getRefundWildcardOperator() {
1684
- assertSigner(signer);
1685
- try {
1686
- const contract = getOffchainFractionsContract();
1687
- if (!contract)
1688
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1689
- const result = await contract.getFunction("REFUND_WILDCARD_OPERATOR")();
1784
+ assertPublicClient(publicClient);
1785
+ try {
1786
+ const result = (await publicClient.readContract({
1787
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1788
+ abi: OFFCHAIN_FRACTIONS_ABI,
1789
+ functionName: "REFUND_WILDCARD_OPERATOR",
1790
+ args: [],
1791
+ }));
1690
1792
  return result;
1691
1793
  }
1692
1794
  catch (error) {
1693
- throw new Error(parseEthersError(error));
1795
+ throw new Error(parseViemError(error));
1694
1796
  }
1695
1797
  }
1696
1798
  /**
@@ -1700,19 +1802,21 @@ function useOffchainFractions(signer, CHAIN_ID) {
1700
1802
  * @param id The unique identifier of the fraction sale
1701
1803
  */
1702
1804
  async function getRefundDetails(user, creator, id) {
1703
- assertSigner(signer);
1704
- try {
1705
- const contract = getOffchainFractionsContract();
1706
- if (!contract)
1707
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1708
- const result = await contract.getFunction("getRefundDetails")(user, creator, id);
1805
+ assertPublicClient(publicClient);
1806
+ try {
1807
+ const result = (await publicClient.readContract({
1808
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1809
+ abi: OFFCHAIN_FRACTIONS_ABI,
1810
+ functionName: "getRefundDetails",
1811
+ args: [user, creator, id],
1812
+ }));
1709
1813
  return {
1710
1814
  refundTo: result.refundTo,
1711
1815
  useCounterfactualAddress: result.useCounterfactualAddress,
1712
1816
  };
1713
1817
  }
1714
1818
  catch (error) {
1715
- throw new Error(parseEthersError(error));
1819
+ throw new Error(parseViemError(error));
1716
1820
  }
1717
1821
  }
1718
1822
  /**
@@ -1723,35 +1827,57 @@ function useOffchainFractions(signer, CHAIN_ID) {
1723
1827
  * @param useCounterfactualAddress Whether to use counterfactual address for refunds
1724
1828
  */
1725
1829
  async function setRefundDetails(creator, id, refundTo, useCounterfactualAddress) {
1726
- assertSigner(signer);
1830
+ assertWalletClient(walletClient);
1831
+ assertPublicClient(publicClient);
1727
1832
  try {
1728
- const contract = getOffchainFractionsContract();
1729
- if (!contract)
1730
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1731
1833
  setIsProcessing(true);
1732
1834
  // Validate parameters
1733
1835
  if (!creator || !id || !refundTo) {
1734
1836
  throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
1735
1837
  }
1736
- const owner = await signer.getAddress();
1737
- // Run a static call first to surface any revert reason
1838
+ const owner = walletClient.account?.address;
1839
+ if (!owner) {
1840
+ throw new Error("No account found in wallet client");
1841
+ }
1842
+ // Run a simulation first to surface any revert reason
1738
1843
  try {
1739
- await contract
1740
- .getFunction("setRefundDetails")
1741
- .staticCall(creator, id, refundTo, useCounterfactualAddress, {
1742
- from: owner,
1844
+ await publicClient.simulateContract({
1845
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1846
+ abi: OFFCHAIN_FRACTIONS_ABI,
1847
+ functionName: "setRefundDetails",
1848
+ args: [
1849
+ creator,
1850
+ id,
1851
+ refundTo,
1852
+ useCounterfactualAddress,
1853
+ ],
1854
+ account: walletClient.account,
1743
1855
  });
1744
1856
  }
1745
- catch (staticError) {
1746
- throw new Error(parseEthersError(staticError));
1857
+ catch (simulationError) {
1858
+ throw new Error(parseViemError(simulationError));
1747
1859
  }
1748
1860
  // Execute the transaction
1749
- const tx = await contract.getFunction("setRefundDetails")(creator, id, refundTo, useCounterfactualAddress);
1750
- await tx.wait();
1751
- return tx.hash;
1861
+ const hash = await walletClient.writeContract({
1862
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1863
+ abi: OFFCHAIN_FRACTIONS_ABI,
1864
+ functionName: "setRefundDetails",
1865
+ args: [
1866
+ creator,
1867
+ id,
1868
+ refundTo,
1869
+ useCounterfactualAddress,
1870
+ ],
1871
+ chain: walletClient.chain,
1872
+ account: walletClient.account,
1873
+ });
1874
+ await publicClient.waitForTransactionReceipt({
1875
+ hash,
1876
+ });
1877
+ return hash;
1752
1878
  }
1753
1879
  catch (error) {
1754
- throw new Error(parseEthersError(error));
1880
+ throw new Error(parseViemError(error));
1755
1881
  }
1756
1882
  finally {
1757
1883
  setIsProcessing(false);
@@ -1763,35 +1889,47 @@ function useOffchainFractions(signer, CHAIN_ID) {
1763
1889
  * @param isApproved Whether to approve or revoke the operator
1764
1890
  */
1765
1891
  async function setRefundOperatorStatus(refundOperator, isApproved) {
1766
- assertSigner(signer);
1892
+ assertWalletClient(walletClient);
1893
+ assertPublicClient(publicClient);
1767
1894
  try {
1768
- const contract = getOffchainFractionsContract();
1769
- if (!contract)
1770
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1771
1895
  setIsProcessing(true);
1772
1896
  // Validate parameters
1773
1897
  if (!refundOperator) {
1774
1898
  throw new Error(OffchainFractionsError.INVALID_PARAMETERS);
1775
1899
  }
1776
- const owner = await signer.getAddress();
1777
- // Run a static call first to surface any revert reason
1900
+ const owner = walletClient.account?.address;
1901
+ if (!owner) {
1902
+ throw new Error("No account found in wallet client");
1903
+ }
1904
+ // Run a simulation first to surface any revert reason
1778
1905
  try {
1779
- await contract
1780
- .getFunction("setRefundOperatorStatus")
1781
- .staticCall(refundOperator, isApproved, {
1782
- from: owner,
1906
+ await publicClient.simulateContract({
1907
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1908
+ abi: OFFCHAIN_FRACTIONS_ABI,
1909
+ functionName: "setRefundOperatorStatus",
1910
+ args: [refundOperator, isApproved],
1911
+ account: walletClient.account,
1783
1912
  });
1784
1913
  }
1785
- catch (staticError) {
1786
- throw new Error(parseEthersError(staticError));
1914
+ catch (simulationError) {
1915
+ throw new Error(parseViemError(simulationError));
1787
1916
  }
1788
1917
  // Execute the transaction
1789
- const tx = await contract.getFunction("setRefundOperatorStatus")(refundOperator, isApproved);
1790
- await tx.wait();
1791
- return tx.hash;
1918
+ const hash = await walletClient.writeContract({
1919
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1920
+ abi: OFFCHAIN_FRACTIONS_ABI,
1921
+ functionName: "setRefundOperatorStatus",
1922
+ args: [refundOperator, isApproved],
1923
+ chain: walletClient.chain,
1924
+ account: walletClient.account,
1925
+ });
1926
+ await publicClient.waitForTransactionReceipt({
1927
+ hash,
1928
+ });
1929
+ return hash;
1792
1930
  }
1793
1931
  catch (error) {
1794
- throw new Error(parseEthersError(error));
1932
+ throw new Error(parseViemError(error));
1795
1933
  }
1796
1934
  finally {
1797
1935
  setIsProcessing(false);
@@ -1803,16 +1941,18 @@ function useOffchainFractions(signer, CHAIN_ID) {
1803
1941
  * @param refundOperator The refund operator address
1804
1942
  */
1805
1943
  async function isRefundOperatorApproved(user, refundOperator) {
1806
- assertSigner(signer);
1807
- try {
1808
- const contract = getOffchainFractionsContract();
1809
- if (!contract)
1810
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1811
- const result = await contract.getFunction("isRefundOperatorApproved")(user, refundOperator);
1944
+ assertPublicClient(publicClient);
1945
+ try {
1946
+ const result = (await publicClient.readContract({
1947
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1948
+ abi: OFFCHAIN_FRACTIONS_ABI,
1949
+ functionName: "isRefundOperatorApproved",
1950
+ args: [user, refundOperator],
1951
+ }));
1812
1952
  return result;
1813
1953
  }
1814
1954
  catch (error) {
1815
- throw new Error(parseEthersError(error));
1955
+ throw new Error(parseViemError(error));
1816
1956
  }
1817
1957
  }
1818
1958
  /**
@@ -1821,16 +1961,18 @@ function useOffchainFractions(signer, CHAIN_ID) {
1821
1961
  * @param refundOperator The refund operator address
1822
1962
  */
1823
1963
  async function getRefundApprovalStatus(user, refundOperator) {
1824
- assertSigner(signer);
1825
- try {
1826
- const contract = getOffchainFractionsContract();
1827
- if (!contract)
1828
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1829
- const result = await contract.getFunction("refundApprovals")(user, refundOperator);
1964
+ assertPublicClient(publicClient);
1965
+ try {
1966
+ const result = (await publicClient.readContract({
1967
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
1968
+ abi: OFFCHAIN_FRACTIONS_ABI,
1969
+ functionName: "refundApprovals",
1970
+ args: [user, refundOperator],
1971
+ }));
1830
1972
  return result.isApproved;
1831
1973
  }
1832
1974
  catch (error) {
1833
- throw new Error(parseEthersError(error));
1975
+ throw new Error(parseViemError(error));
1834
1976
  }
1835
1977
  }
1836
1978
  /**
@@ -1844,7 +1986,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1844
1986
  return Date.now() / 1000 > fraction.expiration;
1845
1987
  }
1846
1988
  catch (error) {
1847
- throw new Error(parseEthersError(error));
1989
+ throw new Error(parseViemError(error));
1848
1990
  }
1849
1991
  }
1850
1992
  /**
@@ -1858,7 +2000,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1858
2000
  return fraction.soldSteps >= fraction.minSharesToRaise;
1859
2001
  }
1860
2002
  catch (error) {
1861
- throw new Error(parseEthersError(error));
2003
+ throw new Error(parseViemError(error));
1862
2004
  }
1863
2005
  }
1864
2006
  /**
@@ -1872,7 +2014,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1872
2014
  return fraction.soldSteps >= fraction.totalSteps;
1873
2015
  }
1874
2016
  catch (error) {
1875
- throw new Error(parseEthersError(error));
2017
+ throw new Error(parseViemError(error));
1876
2018
  }
1877
2019
  }
1878
2020
  /**
@@ -1886,7 +2028,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1886
2028
  return fraction.soldSteps * fraction.step;
1887
2029
  }
1888
2030
  catch (error) {
1889
- throw new Error(parseEthersError(error));
2031
+ throw new Error(parseViemError(error));
1890
2032
  }
1891
2033
  }
1892
2034
  /**
@@ -1900,7 +2042,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1900
2042
  return fraction.totalSteps - fraction.soldSteps;
1901
2043
  }
1902
2044
  catch (error) {
1903
- throw new Error(parseEthersError(error));
2045
+ throw new Error(parseViemError(error));
1904
2046
  }
1905
2047
  }
1906
2048
  /**
@@ -1909,20 +2051,31 @@ function useOffchainFractions(signer, CHAIN_ID) {
1909
2051
  * @param ethPriceInUSD Current ETH price in USD (for cost estimation)
1910
2052
  */
1911
2053
  async function estimateGasForCreateFraction(params, ethPriceInUSD) {
1912
- assertSigner(signer);
2054
+ assertWalletClient(walletClient);
2055
+ assertPublicClient(publicClient);
1913
2056
  try {
1914
- const contract = getOffchainFractionsContract();
1915
- if (!contract)
1916
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1917
2057
  const { id, token, step, totalSteps, expiration, to, useCounterfactualAddress, minSharesToRaise, closer, } = params;
1918
- const feeData = await signer.provider?.getFeeData();
1919
- const gasPrice = feeData?.gasPrice ?? feeData?.maxFeePerGas ?? 0n;
1920
- if (gasPrice === 0n) {
2058
+ const gasPrice = await publicClient.getGasPrice();
2059
+ if (!gasPrice) {
1921
2060
  throw new Error("Could not fetch gas price to estimate cost.");
1922
2061
  }
1923
- const estimatedGas = await contract
1924
- .getFunction("createFraction")
1925
- .estimateGas(id, token, step, totalSteps, expiration, to, useCounterfactualAddress, minSharesToRaise, closer);
2062
+ const estimatedGas = await publicClient.estimateContractGas({
2063
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
2064
+ abi: OFFCHAIN_FRACTIONS_ABI,
2065
+ functionName: "createFraction",
2066
+ args: [
2067
+ id,
2068
+ token,
2069
+ step,
2070
+ totalSteps,
2071
+ expiration,
2072
+ to,
2073
+ useCounterfactualAddress,
2074
+ minSharesToRaise,
2075
+ closer,
2076
+ ],
2077
+ account: walletClient.account,
2078
+ });
1926
2079
  const estimatedCost = estimatedGas * gasPrice;
1927
2080
  if (ethPriceInUSD) {
1928
2081
  const estimatedCostInEth = formatEther(estimatedCost);
@@ -1934,7 +2087,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1934
2087
  }
1935
2088
  }
1936
2089
  catch (error) {
1937
- throw new Error(parseEthersError(error));
2090
+ throw new Error(parseViemError(error));
1938
2091
  }
1939
2092
  }
1940
2093
  /**
@@ -1943,20 +2096,29 @@ function useOffchainFractions(signer, CHAIN_ID) {
1943
2096
  * @param ethPriceInUSD Current ETH price in USD (for cost estimation)
1944
2097
  */
1945
2098
  async function estimateGasForBuyFractions(params, ethPriceInUSD) {
1946
- assertSigner(signer);
2099
+ assertWalletClient(walletClient);
2100
+ assertPublicClient(publicClient);
1947
2101
  try {
1948
- const contract = getOffchainFractionsContract();
1949
- if (!contract)
1950
- throw new Error(OffchainFractionsError.CONTRACT_NOT_AVAILABLE);
1951
2102
  const { creator, id, stepsToBuy, minStepsToBuy, refundTo, creditTo, useCounterfactualAddressForRefund, } = params;
1952
- const feeData = await signer.provider?.getFeeData();
1953
- const gasPrice = feeData?.gasPrice ?? feeData?.maxFeePerGas ?? 0n;
1954
- if (gasPrice === 0n) {
2103
+ const gasPrice = await publicClient.getGasPrice();
2104
+ if (!gasPrice) {
1955
2105
  throw new Error("Could not fetch gas price to estimate cost.");
1956
2106
  }
1957
- const estimatedGas = await contract
1958
- .getFunction("buyFractions")
1959
- .estimateGas(creator, id, stepsToBuy, minStepsToBuy, refundTo, creditTo, useCounterfactualAddressForRefund);
2107
+ const estimatedGas = await publicClient.estimateContractGas({
2108
+ address: ADDRESSES.OFFCHAIN_FRACTIONS,
2109
+ abi: OFFCHAIN_FRACTIONS_ABI,
2110
+ functionName: "buyFractions",
2111
+ args: [
2112
+ creator,
2113
+ id,
2114
+ stepsToBuy,
2115
+ minStepsToBuy,
2116
+ refundTo,
2117
+ creditTo,
2118
+ useCounterfactualAddressForRefund,
2119
+ ],
2120
+ account: walletClient.account,
2121
+ });
1960
2122
  const estimatedCost = estimatedGas * gasPrice;
1961
2123
  if (ethPriceInUSD) {
1962
2124
  const estimatedCostInEth = formatEther(estimatedCost);
@@ -1968,7 +2130,7 @@ function useOffchainFractions(signer, CHAIN_ID) {
1968
2130
  }
1969
2131
  }
1970
2132
  catch (error) {
1971
- throw new Error(parseEthersError(error));
2133
+ throw new Error(parseViemError(error));
1972
2134
  }
1973
2135
  }
1974
2136
  return {
@@ -2005,8 +2167,8 @@ function useOffchainFractions(signer, CHAIN_ID) {
2005
2167
  return isProcessing;
2006
2168
  },
2007
2169
  addresses: ADDRESSES,
2008
- // Signer availability
2009
- isSignerAvailable: !!signer,
2170
+ // Wallet client availability
2171
+ isSignerAvailable: !!walletClient,
2010
2172
  };
2011
2173
  }
2012
2174
 
@@ -3409,4 +3571,4 @@ function FarmsRouter(baseUrl) {
3409
3571
  }
3410
3572
 
3411
3573
  export { ControlRouter as C, DECIMALS_BY_TOKEN as D, FarmsRouter as F, GLOW_WEIGHT_DECIMAL_PRECISION as G, HUB_URL as H, KickstarterRouter as K, MAX_WEIGHT as M, OffchainFractionsError as O, PAYMENT_CURRENCIES as P, RegionRouter as R, TRANSFER_TYPES as T, USDG_WEIGHT_DECIMAL_PRECISION as U, WalletsRouter as W, useOffchainFractions as a, ForwarderError as b, REGIONS as c, OFF_CHAIN_PAYMENT_CURRENCIES as d, allRegions as e, usStates as f, countries as g, FORWARDER_ABI as h, OFFCHAIN_FRACTIONS_ABI as i, getAddresses as j, GCA_URLS as k, regionMetadata as r, useForwarder as u };
3412
- //# sourceMappingURL=farms-router-CDZkFsAs.js.map
3574
+ //# sourceMappingURL=farms-router-BUi6rxeX.js.map