genlayer-js 0.28.0 → 0.28.1

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 CHANGED
@@ -213,6 +213,35 @@ const receipt = await readClient.waitForTransactionReceipt({
213
213
  });
214
214
  ```
215
215
 
216
+ ### Switching the wallet to the correct network
217
+
218
+ When using MetaMask or another browser wallet, the wallet may be connected to a different chain than what your client is configured for. Use `client.connect()` to switch the wallet to the correct GenLayer network before sending transactions:
219
+
220
+ ```typescript
221
+ import { createClient } from "genlayer-js";
222
+ import { studionet } from "genlayer-js/chains";
223
+
224
+ const client = createClient({
225
+ chain: studionet,
226
+ account: address as `0x${string}`,
227
+ });
228
+
229
+ // Switch MetaMask to the correct chain (adds the network if not present)
230
+ await client.connect("studionet");
231
+
232
+ // Now transactions will go to the right network
233
+ const txHash = await client.writeContract({
234
+ address: contractAddress,
235
+ functionName: "create_profile",
236
+ args: ["alice", "Hello world"],
237
+ value: BigInt(0),
238
+ });
239
+ ```
240
+
241
+ Available networks: `"localnet"`, `"studionet"`, `"testnetAsimov"`, `"testnetBradbury"`.
242
+
243
+ > **Note:** If the wallet is on the wrong chain when you call `writeContract`, the SDK will throw a clear error telling you which chain the wallet is on vs. which chain the client expects. Call `client.connect()` to resolve this.
244
+
216
245
  ### Staking Operations
217
246
 
218
247
  The SDK provides staking functionality for validators and delegators on testnet-bradbury (and testnet-asimov).
package/dist/index.cjs CHANGED
@@ -2263,6 +2263,20 @@ var PROVIDER_METHODS = /* @__PURE__ */ new Set([
2263
2263
  "personal_sign",
2264
2264
  "eth_signTypedData_v4"
2265
2265
  ]);
2266
+ var assertChainMatch = async (provider, chainConfig) => {
2267
+ const expectedChainIdHex = `0x${chainConfig.id.toString(16)}`;
2268
+ try {
2269
+ const currentChainId = await provider.request({ method: "eth_chainId" });
2270
+ if (currentChainId !== expectedChainIdHex) {
2271
+ const currentId = parseInt(currentChainId, 16);
2272
+ throw new Error(
2273
+ `Wallet is on chain ${currentId} but client is configured for chain ${chainConfig.id} (${chainConfig.name}). Call client.connect("${chainConfig.name}") or switch your wallet to the correct network before sending transactions.`
2274
+ );
2275
+ }
2276
+ } catch (err) {
2277
+ if (err instanceof Error && err.message.startsWith("Wallet is on chain")) throw err;
2278
+ }
2279
+ };
2266
2280
  var getCustomTransportConfig = (config, chainConfig) => {
2267
2281
  const isAddress = typeof config.account !== "object";
2268
2282
  return {
@@ -2271,6 +2285,9 @@ var getCustomTransportConfig = (config, chainConfig) => {
2271
2285
  const provider = config.provider || (typeof window !== "undefined" ? window.ethereum : void 0);
2272
2286
  if (provider) {
2273
2287
  try {
2288
+ if (method === "eth_sendTransaction" || method === "eth_signTransaction") {
2289
+ await assertChainMatch(provider, chainConfig);
2290
+ }
2274
2291
  return await provider.request({ method, params });
2275
2292
  } catch (err) {
2276
2293
  console.warn(`Error using provider for method ${method}:`, err);
package/dist/index.js CHANGED
@@ -2263,6 +2263,20 @@ var PROVIDER_METHODS = /* @__PURE__ */ new Set([
2263
2263
  "personal_sign",
2264
2264
  "eth_signTypedData_v4"
2265
2265
  ]);
2266
+ var assertChainMatch = async (provider, chainConfig) => {
2267
+ const expectedChainIdHex = `0x${chainConfig.id.toString(16)}`;
2268
+ try {
2269
+ const currentChainId = await provider.request({ method: "eth_chainId" });
2270
+ if (currentChainId !== expectedChainIdHex) {
2271
+ const currentId = parseInt(currentChainId, 16);
2272
+ throw new Error(
2273
+ `Wallet is on chain ${currentId} but client is configured for chain ${chainConfig.id} (${chainConfig.name}). Call client.connect("${chainConfig.name}") or switch your wallet to the correct network before sending transactions.`
2274
+ );
2275
+ }
2276
+ } catch (err) {
2277
+ if (err instanceof Error && err.message.startsWith("Wallet is on chain")) throw err;
2278
+ }
2279
+ };
2266
2280
  var getCustomTransportConfig = (config, chainConfig) => {
2267
2281
  const isAddress = typeof config.account !== "object";
2268
2282
  return {
@@ -2271,6 +2285,9 @@ var getCustomTransportConfig = (config, chainConfig) => {
2271
2285
  const provider = config.provider || (typeof window !== "undefined" ? window.ethereum : void 0);
2272
2286
  if (provider) {
2273
2287
  try {
2288
+ if (method === "eth_sendTransaction" || method === "eth_signTransaction") {
2289
+ await assertChainMatch(provider, chainConfig);
2290
+ }
2274
2291
  return await provider.request({ method, params });
2275
2292
  } catch (err) {
2276
2293
  console.warn(`Error using provider for method ${method}:`, err);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "genlayer-js",
3
3
  "type": "module",
4
- "version": "0.28.0",
4
+ "version": "0.28.1",
5
5
  "description": "GenLayer JavaScript SDK",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",