@campnetwork/origin 1.0.0-alpha.13 → 1.0.0-alpha.3

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.
@@ -1,5 +1,5 @@
1
1
  import React, { JSX } from 'react';
2
- import { Address, Hex, WalletClient, Abi } from 'viem';
2
+ import { Address, Hex, Abi } from 'viem';
3
3
  import { UseQueryResult } from '@tanstack/react-query';
4
4
 
5
5
  interface Environment {
@@ -85,10 +85,9 @@ declare function finalizeDelete(this: Origin, tokenId: bigint): Promise<any>;
85
85
  /**
86
86
  * Calls the getOrCreateRoyaltyVault method on the IPNFT contract.
87
87
  * @param tokenOwner The address of the token owner for whom to get or create the royalty vault.
88
- * @param simulateOnly If true, simulates the transaction without executing it.
89
88
  * @returns The address of the royalty vault associated with the specified token owner.
90
89
  */
91
- declare function getOrCreateRoyaltyVault(this: Origin, tokenOwner: Address, simulateOnly?: boolean): Promise<Address>;
90
+ declare function getOrCreateRoyaltyVault(this: Origin, tokenOwner: Address): Promise<Address>;
92
91
 
93
92
  /**
94
93
  * Returns the license terms associated with a specific token ID.
@@ -181,7 +180,6 @@ type CallOptions = {
181
180
  value?: bigint;
182
181
  gas?: bigint;
183
182
  waitForReceipt?: boolean;
184
- simulate?: boolean;
185
183
  };
186
184
  /**
187
185
  * The Origin class
@@ -210,9 +208,9 @@ declare class Origin {
210
208
  private jwt;
211
209
  environment: Environment;
212
210
  private viemClient?;
213
- constructor(jwt: string, environment: Environment, viemClient?: WalletClient);
211
+ constructor(jwt: string, environment: Environment, viemClient?: any);
214
212
  getJwt(): string;
215
- setViemClient(client: WalletClient): void;
213
+ setViemClient(client: any): void;
216
214
  uploadFile(file: File, options?: {
217
215
  progressCallback?: (percent: number) => void;
218
216
  }): Promise<any>;
@@ -253,7 +251,6 @@ declare class Origin {
253
251
  getData(tokenId: bigint): Promise<any>;
254
252
  /**
255
253
  * Get royalty information for a wallet address, including the royalty vault address and its balance.
256
- * @param {Address} [token] - Optional token address to check royalties for. If not provided, checks for native token.
257
254
  * @param {Address} [owner] - Optional wallet address to check royalties for. If not provided, uses the connected wallet.
258
255
  * @returns {Promise<RoyaltyInfo>} A promise that resolves with the royalty vault address and balance information.
259
256
  * @throws {Error} Throws an error if no wallet is connected and no owner address is provided.
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import React, { createContext, useState, useContext, useEffect, useLayoutEffect, useRef, useSyncExternalStore } from 'react';
3
- import { custom, createWalletClient, createPublicClient, http, erc20Abi, getAbiItem, zeroAddress, formatEther, formatUnits, checksumAddress } from 'viem';
3
+ import { custom, createWalletClient, createPublicClient, http, erc20Abi, getAbiItem, encodeFunctionData, zeroAddress, formatEther, formatUnits, checksumAddress } from 'viem';
4
4
  import { toAccount } from 'viem/accounts';
5
5
  import { createSiweMessage } from 'viem/siwe';
6
6
  import axios from 'axios';
@@ -128,20 +128,20 @@ const mainnet = {
128
128
  // @ts-ignore
129
129
  let client = null;
130
130
  let publicClient = null;
131
- let currentChain = null;
132
- const getClient = (provider, name = "window.ethereum", chain, address) => {
133
- var _a, _b;
131
+ const getClient = (provider, name = "window.ethereum",
132
+ // chain: "mainnet" | "testnet" = "testnet",
133
+ chain, address) => {
134
+ var _a;
134
135
  if (!provider && !client) {
135
136
  console.warn("Provider is required to create a client.");
136
137
  return null;
137
138
  }
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) ||
142
- (currentChain === null || currentChain === void 0 ? void 0 : currentChain.id) !== selectedChain.id) {
141
+ (address !== ((_a = client.account) === null || _a === void 0 ? void 0 : _a.address) && provider)) {
143
142
  const obj = {
144
- chain: selectedChain,
143
+ // chain: chain === "mainnet" ? mainnet : testnet,
144
+ chain: chain || testnet,
145
145
  transport: custom(provider, {
146
146
  name: name,
147
147
  }),
@@ -150,28 +150,18 @@ const getClient = (provider, name = "window.ethereum", 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
- }
157
153
  }
158
154
  return client;
159
155
  };
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) {
156
+ const getPublicClient = () => {
157
+ if (!publicClient) {
164
158
  publicClient = createPublicClient({
165
- chain: selectedChain,
159
+ chain: testnet,
166
160
  transport: http(),
167
161
  });
168
162
  }
169
163
  return publicClient;
170
164
  };
171
- const setChain = (chain) => {
172
- currentChain = chain;
173
- publicClient = null; // reset public client to be recreated with new chain
174
- };
175
165
 
176
166
  var ipnftMainnetAbi = [
177
167
  {
@@ -2736,16 +2726,10 @@ function finalizeDelete(tokenId) {
2736
2726
  /**
2737
2727
  * Calls the getOrCreateRoyaltyVault method on the IPNFT contract.
2738
2728
  * @param tokenOwner The address of the token owner for whom to get or create the royalty vault.
2739
- * @param simulateOnly If true, simulates the transaction without executing it.
2740
2729
  * @returns The address of the royalty vault associated with the specified token owner.
2741
2730
  */
2742
- function getOrCreateRoyaltyVault(tokenOwner_1) {
2743
- return __awaiter(this, arguments, void 0, function* (tokenOwner, simulateOnly = false) {
2744
- const royaltyVaultTx = yield this.callContractMethod(this.environment.DATANFT_CONTRACT_ADDRESS, this.environment.IPNFT_ABI, "getOrCreateRoyaltyVault", [tokenOwner], { waitForReceipt: true, simulate: simulateOnly });
2745
- return simulateOnly
2746
- ? royaltyVaultTx
2747
- : royaltyVaultTx.simulatedResult;
2748
- });
2731
+ function getOrCreateRoyaltyVault(tokenOwner) {
2732
+ return this.callContractMethod(this.environment.DATANFT_CONTRACT_ADDRESS, this.environment.IPNFT_ABI, "getOrCreateRoyaltyVault", [tokenOwner]);
2749
2733
  }
2750
2734
 
2751
2735
  /**
@@ -2976,15 +2960,14 @@ class Origin {
2976
2960
  !uri) {
2977
2961
  throw new Error("Failed to register IpNFT: Missing required fields in registration response.");
2978
2962
  }
2979
- const accounts = (yield this.viemClient.request({
2963
+ const [account] = yield this.viemClient.request({
2980
2964
  method: "eth_requestAccounts",
2981
2965
  params: [],
2982
- }));
2983
- const account = accounts[0];
2966
+ });
2984
2967
  const mintResult = yield this.mintWithSignature(account, tokenId, parents || [], creatorContentHash, uri, license, deadline, signature);
2985
- if (["0x1", "success"].indexOf(mintResult.receipt.status) === -1) {
2968
+ if (mintResult.status !== "0x1") {
2986
2969
  console.error("Minting failed:", mintResult);
2987
- throw new Error(`Minting failed with status: ${mintResult.receipt.status}`);
2970
+ throw new Error(`Minting failed with status: ${mintResult.status}`);
2988
2971
  }
2989
2972
  return tokenId.toString();
2990
2973
  });
@@ -3004,13 +2987,12 @@ class Origin {
3004
2987
  !uri) {
3005
2988
  throw new Error("Failed to register Social IpNFT: Missing required fields in registration response.");
3006
2989
  }
3007
- const accounts = (yield this.viemClient.request({
2990
+ const [account] = yield this.viemClient.request({
3008
2991
  method: "eth_requestAccounts",
3009
2992
  params: [],
3010
- }));
3011
- const account = accounts[0];
2993
+ });
3012
2994
  const mintResult = yield this.mintWithSignature(account, tokenId, [], creatorContentHash, uri, license, deadline, signature);
3013
- if (["0x1", "success"].indexOf(mintResult.status) === -1) {
2995
+ if (mintResult.status !== "0x1") {
3014
2996
  throw new Error(`Minting Social IpNFT failed with status: ${mintResult.status}`);
3015
2997
  }
3016
2998
  return tokenId.toString();
@@ -3099,6 +3081,9 @@ class Origin {
3099
3081
  "stateMutability" in abiItem &&
3100
3082
  (abiItem.stateMutability === "view" ||
3101
3083
  abiItem.stateMutability === "pure");
3084
+ if (!isView && !this.viemClient) {
3085
+ throw new Error("WalletClient not connected.");
3086
+ }
3102
3087
  if (isView) {
3103
3088
  const publicClient = getPublicClient();
3104
3089
  const result = (yield publicClient.readContract({
@@ -3109,41 +3094,38 @@ class Origin {
3109
3094
  })) || null;
3110
3095
  return result;
3111
3096
  }
3112
- if (!this.viemClient) {
3113
- throw new Error("WalletClient not connected.");
3114
- }
3115
- const [account] = (yield this.viemClient.request({
3116
- method: "eth_requestAccounts",
3117
- params: [],
3118
- }));
3119
- yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_ensureChainId).call(this, this.environment.CHAIN);
3120
- const publicClient = getPublicClient();
3121
- // simulate
3122
- const { result: simulatedResult, request } = yield publicClient.simulateContract({
3123
- account,
3124
- address: contractAddress,
3125
- abi,
3126
- functionName: methodName,
3127
- args: params,
3128
- value: options.value,
3129
- });
3130
- if (options.simulate) {
3131
- return simulatedResult;
3132
- }
3133
- try {
3134
- const txHash = yield this.viemClient.writeContract(request);
3135
- if (typeof txHash !== "string") {
3136
- throw new Error("Transaction failed to send.");
3097
+ else {
3098
+ const [account] = yield this.viemClient.request({
3099
+ method: "eth_requestAccounts",
3100
+ params: [],
3101
+ });
3102
+ const data = encodeFunctionData({
3103
+ abi,
3104
+ functionName: methodName,
3105
+ args: params,
3106
+ });
3107
+ yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_ensureChainId).call(this, this.environment.CHAIN);
3108
+ try {
3109
+ const txHash = yield this.viemClient.sendTransaction({
3110
+ to: contractAddress,
3111
+ data,
3112
+ account,
3113
+ value: options.value,
3114
+ gas: options.gas,
3115
+ });
3116
+ if (typeof txHash !== "string") {
3117
+ throw new Error("Transaction failed to send.");
3118
+ }
3119
+ if (!options.waitForReceipt) {
3120
+ return txHash;
3121
+ }
3122
+ const receipt = yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_waitForTxReceipt).call(this, txHash);
3123
+ return receipt;
3137
3124
  }
3138
- if (!options.waitForReceipt) {
3139
- return { txHash, simulatedResult };
3125
+ catch (error) {
3126
+ console.error("Transaction failed:", error);
3127
+ throw new Error("Transaction failed: " + error);
3140
3128
  }
3141
- const receipt = yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_waitForTxReceipt).call(this, txHash);
3142
- return { txHash, receipt, simulatedResult };
3143
- }
3144
- catch (error) {
3145
- console.error("Transaction failed:", error);
3146
- throw new Error("Transaction failed: " + error);
3147
3129
  }
3148
3130
  });
3149
3131
  }
@@ -3166,11 +3148,10 @@ class Origin {
3166
3148
  duration === undefined) {
3167
3149
  throw new Error("Terms missing price, paymentToken, or duration");
3168
3150
  }
3169
- const accounts = (yield this.viemClient.request({
3151
+ const [account] = yield this.viemClient.request({
3170
3152
  method: "eth_requestAccounts",
3171
3153
  params: [],
3172
- }));
3173
- const account = accounts[0];
3154
+ });
3174
3155
  const totalCost = price;
3175
3156
  const isNative = paymentToken === zeroAddress;
3176
3157
  if (isNative) {
@@ -3205,7 +3186,6 @@ class Origin {
3205
3186
  }
3206
3187
  /**
3207
3188
  * Get royalty information for a wallet address, including the royalty vault address and its balance.
3208
- * @param {Address} [token] - Optional token address to check royalties for. If not provided, checks for native token.
3209
3189
  * @param {Address} [owner] - Optional wallet address to check royalties for. If not provided, uses the connected wallet.
3210
3190
  * @returns {Promise<RoyaltyInfo>} A promise that resolves with the royalty vault address and balance information.
3211
3191
  * @throws {Error} Throws an error if no wallet is connected and no owner address is provided.
@@ -3222,7 +3202,7 @@ class Origin {
3222
3202
  return __awaiter(this, void 0, void 0, function* () {
3223
3203
  const walletAddress = yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_resolveWalletAddress).call(this, owner);
3224
3204
  try {
3225
- const royaltyVaultAddress = yield this.getOrCreateRoyaltyVault(walletAddress, true);
3205
+ const royaltyVaultAddress = yield this.getOrCreateRoyaltyVault(walletAddress);
3226
3206
  const publicClient = getPublicClient();
3227
3207
  let balance;
3228
3208
  let balanceFormatted;
@@ -3277,8 +3257,8 @@ class Origin {
3277
3257
  claimRoyalties(token, owner) {
3278
3258
  return __awaiter(this, void 0, void 0, function* () {
3279
3259
  const walletAddress = yield __classPrivateFieldGet(this, _Origin_instances, "m", _Origin_resolveWalletAddress).call(this, owner);
3280
- const royaltyVaultAddress = yield this.getOrCreateRoyaltyVault(walletAddress, true);
3281
- return this.callContractMethod(royaltyVaultAddress, this.environment.ROYALTY_VAULT_ABI, "claimRoyalty", [token !== null && token !== void 0 ? token : zeroAddress], { waitForReceipt: true });
3260
+ const royaltyVaultAddress = yield this.getOrCreateRoyaltyVault(walletAddress);
3261
+ return this.callContractMethod(royaltyVaultAddress, this.environment.ROYALTY_VAULT_ABI, "claimRoyalties", [token !== null && token !== void 0 ? token : zeroAddress], { waitForReceipt: true });
3282
3262
  });
3283
3263
  }
3284
3264
  }
@@ -3335,41 +3315,19 @@ _Origin_instances = new WeakSet(), _Origin_generateURL = function _Origin_genera
3335
3315
  throw error;
3336
3316
  }
3337
3317
  });
3338
- }, _Origin_waitForTxReceipt = function _Origin_waitForTxReceipt(txHash_1) {
3339
- return __awaiter(this, arguments, void 0, function* (txHash, opts = {}) {
3340
- var _a, _b, _c;
3341
- const publicClient = getPublicClient();
3342
- let currentHash = txHash;
3343
- const confirmations = (_a = opts.confirmations) !== null && _a !== void 0 ? _a : 1;
3344
- const timeout = (_b = opts.timeoutMs) !== null && _b !== void 0 ? _b : 180000;
3345
- const pollingInterval = (_c = opts.pollingIntervalMs) !== null && _c !== void 0 ? _c : 1500;
3346
- try {
3347
- const receipt = yield publicClient.waitForTransactionReceipt({
3348
- hash: currentHash,
3349
- confirmations,
3350
- timeout,
3351
- pollingInterval,
3352
- onReplaced: (replacement) => {
3353
- currentHash = replacement.transaction.hash;
3354
- },
3318
+ }, _Origin_waitForTxReceipt = function _Origin_waitForTxReceipt(txHash) {
3319
+ return __awaiter(this, void 0, void 0, function* () {
3320
+ if (!this.viemClient)
3321
+ throw new Error("WalletClient not connected.");
3322
+ while (true) {
3323
+ const receipt = yield this.viemClient.request({
3324
+ method: "eth_getTransactionReceipt",
3325
+ params: [txHash],
3355
3326
  });
3356
- return receipt;
3357
- }
3358
- catch (err) {
3359
- // fallback
3360
- const start = Date.now();
3361
- while (Date.now() - start < timeout) {
3362
- try {
3363
- const receipt = yield publicClient.getTransactionReceipt({
3364
- hash: currentHash,
3365
- });
3366
- if (receipt && receipt.blockNumber)
3367
- return receipt;
3368
- }
3369
- catch (_d) { }
3370
- yield new Promise((r) => setTimeout(r, pollingInterval));
3327
+ if (receipt && receipt.blockNumber) {
3328
+ return receipt;
3371
3329
  }
3372
- throw err;
3330
+ yield new Promise((res) => setTimeout(res, 1000));
3373
3331
  }
3374
3332
  });
3375
3333
  }, _Origin_ensureChainId = function _Origin_ensureChainId(chain) {
@@ -3377,15 +3335,14 @@ _Origin_instances = new WeakSet(), _Origin_generateURL = function _Origin_genera
3377
3335
  // return;
3378
3336
  if (!this.viemClient)
3379
3337
  throw new Error("WalletClient not connected.");
3380
- let currentChainId = (yield this.viemClient.request({
3338
+ let currentChainId = yield this.viemClient.request({
3381
3339
  method: "eth_chainId",
3382
3340
  params: [],
3383
- }));
3341
+ });
3384
3342
  if (typeof currentChainId === "string") {
3385
3343
  currentChainId = parseInt(currentChainId, 16);
3386
3344
  }
3387
3345
  if (currentChainId !== chain.id) {
3388
- setChain(chain);
3389
3346
  try {
3390
3347
  yield this.viemClient.request({
3391
3348
  method: "wallet_switchEthereumChain",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@campnetwork/origin",
3
- "version": "1.0.0-alpha.13",
3
+ "version": "1.0.0-alpha.3",
4
4
  "main": "dist/core.cjs",
5
5
  "exports": {
6
6
  ".": {