@campnetwork/origin 1.0.0-alpha.6 → 1.0.0-alpha.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.
@@ -128,20 +128,20 @@ const mainnet = {
128
128
  // @ts-ignore
129
129
  let client = null;
130
130
  let publicClient = null;
131
- const getClient = (provider, name = "window.ethereum",
132
- // chain: "mainnet" | "testnet" = "testnet",
133
- chain, address) => {
134
- var _a;
131
+ let currentChain = null;
132
+ const getClient = (provider, name = "window.ethereum", chain, address) => {
133
+ var _a, _b;
135
134
  if (!provider && !client) {
136
135
  console.warn("Provider is required to create a client.");
137
136
  return null;
138
137
  }
138
+ const selectedChain = chain || testnet;
139
139
  if (!client ||
140
140
  (client.transport.name !== name && provider) ||
141
- (address !== ((_a = client.account) === null || _a === void 0 ? void 0 : _a.address) && provider)) {
141
+ (address !== ((_a = client.account) === null || _a === void 0 ? void 0 : _a.address) && provider) ||
142
+ (currentChain === null || currentChain === void 0 ? void 0 : currentChain.id) !== selectedChain.id) {
142
143
  const obj = {
143
- // chain: chain === "mainnet" ? mainnet : testnet,
144
- chain: chain || testnet,
144
+ chain: selectedChain,
145
145
  transport: custom(provider, {
146
146
  name: name,
147
147
  }),
@@ -150,18 +150,28 @@ chain, address) => {
150
150
  obj.account = toAccount(address);
151
151
  }
152
152
  client = createWalletClient(obj);
153
+ currentChain = selectedChain;
154
+ if (publicClient && ((_b = publicClient.chain) === null || _b === void 0 ? void 0 : _b.id) !== selectedChain.id) {
155
+ publicClient = null;
156
+ }
153
157
  }
154
158
  return client;
155
159
  };
156
- const getPublicClient = () => {
157
- if (!publicClient) {
160
+ const getPublicClient = (chain) => {
161
+ var _a;
162
+ const selectedChain = currentChain || testnet;
163
+ if (!publicClient || ((_a = publicClient.chain) === null || _a === void 0 ? void 0 : _a.id) !== selectedChain.id) {
158
164
  publicClient = createPublicClient({
159
- chain: testnet,
165
+ chain: selectedChain,
160
166
  transport: http(),
161
167
  });
162
168
  }
163
169
  return publicClient;
164
170
  };
171
+ const setChain = (chain) => {
172
+ currentChain = chain;
173
+ publicClient = null; // reset public client to be recreated with new chain
174
+ };
165
175
 
166
176
  var ipnftMainnetAbi = [
167
177
  {
@@ -2964,10 +2974,11 @@ class Origin {
2964
2974
  !uri) {
2965
2975
  throw new Error("Failed to register IpNFT: Missing required fields in registration response.");
2966
2976
  }
2967
- const [account] = yield this.viemClient.request({
2977
+ const accounts = (yield this.viemClient.request({
2968
2978
  method: "eth_requestAccounts",
2969
2979
  params: [],
2970
- });
2980
+ }));
2981
+ const account = accounts[0];
2971
2982
  const mintResult = yield this.mintWithSignature(account, tokenId, parents || [], creatorContentHash, uri, license, deadline, signature);
2972
2983
  if (mintResult.status !== "0x1") {
2973
2984
  console.error("Minting failed:", mintResult);
@@ -2991,10 +3002,11 @@ class Origin {
2991
3002
  !uri) {
2992
3003
  throw new Error("Failed to register Social IpNFT: Missing required fields in registration response.");
2993
3004
  }
2994
- const [account] = yield this.viemClient.request({
3005
+ const accounts = (yield this.viemClient.request({
2995
3006
  method: "eth_requestAccounts",
2996
3007
  params: [],
2997
- });
3008
+ }));
3009
+ const account = accounts[0];
2998
3010
  const mintResult = yield this.mintWithSignature(account, tokenId, [], creatorContentHash, uri, license, deadline, signature);
2999
3011
  if (mintResult.status !== "0x1") {
3000
3012
  throw new Error(`Minting Social IpNFT failed with status: ${mintResult.status}`);
@@ -3085,9 +3097,6 @@ class Origin {
3085
3097
  "stateMutability" in abiItem &&
3086
3098
  (abiItem.stateMutability === "view" ||
3087
3099
  abiItem.stateMutability === "pure");
3088
- if (!isView && !this.viemClient) {
3089
- throw new Error("WalletClient not connected.");
3090
- }
3091
3100
  if (isView) {
3092
3101
  const publicClient = getPublicClient();
3093
3102
  const result = (yield publicClient.readContract({
@@ -3098,40 +3107,41 @@ class Origin {
3098
3107
  })) || null;
3099
3108
  return result;
3100
3109
  }
3101
- else {
3102
- const [account] = yield this.viemClient.request({
3103
- method: "eth_requestAccounts",
3104
- params: [],
3105
- });
3106
- yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_ensureChainId).call(this, this.environment.CHAIN);
3107
- const publicClient = getPublicClient();
3108
- // simulate
3109
- const { result: simulatedResult, request } = yield publicClient.simulateContract({
3110
- account,
3111
- address: contractAddress,
3112
- abi,
3113
- functionName: methodName,
3114
- args: params,
3115
- value: options.value,
3116
- });
3117
- if (options.simulate) {
3118
- return simulatedResult;
3119
- }
3120
- try {
3121
- const txHash = yield this.viemClient.sendTransaction(request);
3122
- if (typeof txHash !== "string") {
3123
- throw new Error("Transaction failed to send.");
3124
- }
3125
- if (!options.waitForReceipt) {
3126
- return { txHash, simulatedResult };
3127
- }
3128
- const receipt = yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_waitForTxReceipt).call(this, txHash);
3129
- return { txHash, receipt, simulatedResult };
3110
+ if (!this.viemClient) {
3111
+ throw new Error("WalletClient not connected.");
3112
+ }
3113
+ const [account] = (yield this.viemClient.request({
3114
+ method: "eth_requestAccounts",
3115
+ params: [],
3116
+ }));
3117
+ yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_ensureChainId).call(this, this.environment.CHAIN);
3118
+ const publicClient = getPublicClient();
3119
+ // simulate
3120
+ const { result: simulatedResult, request } = yield publicClient.simulateContract({
3121
+ account,
3122
+ address: contractAddress,
3123
+ abi,
3124
+ functionName: methodName,
3125
+ args: params,
3126
+ value: options.value,
3127
+ });
3128
+ if (options.simulate) {
3129
+ return simulatedResult;
3130
+ }
3131
+ try {
3132
+ const txHash = yield this.viemClient.writeContract(request);
3133
+ if (typeof txHash !== "string") {
3134
+ throw new Error("Transaction failed to send.");
3130
3135
  }
3131
- catch (error) {
3132
- console.error("Transaction failed:", error);
3133
- throw new Error("Transaction failed: " + error);
3136
+ if (!options.waitForReceipt) {
3137
+ return { txHash, simulatedResult };
3134
3138
  }
3139
+ const receipt = yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_waitForTxReceipt).call(this, txHash);
3140
+ return { txHash, receipt, simulatedResult };
3141
+ }
3142
+ catch (error) {
3143
+ console.error("Transaction failed:", error);
3144
+ throw new Error("Transaction failed: " + error);
3135
3145
  }
3136
3146
  });
3137
3147
  }
@@ -3154,10 +3164,11 @@ class Origin {
3154
3164
  duration === undefined) {
3155
3165
  throw new Error("Terms missing price, paymentToken, or duration");
3156
3166
  }
3157
- const [account] = yield this.viemClient.request({
3167
+ const accounts = (yield this.viemClient.request({
3158
3168
  method: "eth_requestAccounts",
3159
3169
  params: [],
3160
- });
3170
+ }));
3171
+ const account = accounts[0];
3161
3172
  const totalCost = price;
3162
3173
  const isNative = paymentToken === zeroAddress;
3163
3174
  if (isNative) {
@@ -3322,19 +3333,41 @@ _Origin_instances = new WeakSet(), _Origin_generateURL = function _Origin_genera
3322
3333
  throw error;
3323
3334
  }
3324
3335
  });
3325
- }, _Origin_waitForTxReceipt = function _Origin_waitForTxReceipt(txHash) {
3326
- return __awaiter(this, void 0, void 0, function* () {
3327
- if (!this.viemClient)
3328
- throw new Error("WalletClient not connected.");
3329
- while (true) {
3330
- const receipt = yield this.viemClient.request({
3331
- method: "eth_getTransactionReceipt",
3332
- params: [txHash],
3336
+ }, _Origin_waitForTxReceipt = function _Origin_waitForTxReceipt(txHash_1) {
3337
+ return __awaiter(this, arguments, void 0, function* (txHash, opts = {}) {
3338
+ var _a, _b, _c;
3339
+ const publicClient = getPublicClient();
3340
+ let currentHash = txHash;
3341
+ const confirmations = (_a = opts.confirmations) !== null && _a !== void 0 ? _a : 1;
3342
+ const timeout = (_b = opts.timeoutMs) !== null && _b !== void 0 ? _b : 180000;
3343
+ const pollingInterval = (_c = opts.pollingIntervalMs) !== null && _c !== void 0 ? _c : 1500;
3344
+ try {
3345
+ const receipt = yield publicClient.waitForTransactionReceipt({
3346
+ hash: currentHash,
3347
+ confirmations,
3348
+ timeout,
3349
+ pollingInterval,
3350
+ onReplaced: (replacement) => {
3351
+ currentHash = replacement.transaction.hash;
3352
+ },
3333
3353
  });
3334
- if (receipt && receipt.blockNumber) {
3335
- return receipt;
3354
+ return receipt;
3355
+ }
3356
+ catch (err) {
3357
+ // fallback
3358
+ const start = Date.now();
3359
+ while (Date.now() - start < timeout) {
3360
+ try {
3361
+ const receipt = yield publicClient.getTransactionReceipt({
3362
+ hash: currentHash,
3363
+ });
3364
+ if (receipt && receipt.blockNumber)
3365
+ return receipt;
3366
+ }
3367
+ catch (_d) { }
3368
+ yield new Promise((r) => setTimeout(r, pollingInterval));
3336
3369
  }
3337
- yield new Promise((res) => setTimeout(res, 1000));
3370
+ throw err;
3338
3371
  }
3339
3372
  });
3340
3373
  }, _Origin_ensureChainId = function _Origin_ensureChainId(chain) {
@@ -3342,14 +3375,15 @@ _Origin_instances = new WeakSet(), _Origin_generateURL = function _Origin_genera
3342
3375
  // return;
3343
3376
  if (!this.viemClient)
3344
3377
  throw new Error("WalletClient not connected.");
3345
- let currentChainId = yield this.viemClient.request({
3378
+ let currentChainId = (yield this.viemClient.request({
3346
3379
  method: "eth_chainId",
3347
3380
  params: [],
3348
- });
3381
+ }));
3349
3382
  if (typeof currentChainId === "string") {
3350
3383
  currentChainId = parseInt(currentChainId, 16);
3351
3384
  }
3352
3385
  if (currentChainId !== chain.id) {
3386
+ setChain(chain);
3353
3387
  try {
3354
3388
  yield this.viemClient.request({
3355
3389
  method: "wallet_switchEthereumChain",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campnetwork/origin",
3
- "version": "1.0.0-alpha.6",
3
+ "version": "1.0.0-alpha.8",
4
4
  "main": "dist/core.cjs",
5
5
  "exports": {
6
6
  ".": {