@hinkal/common 0.0.207 → 0.0.209
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hinkal/common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.209",
|
|
4
4
|
"homepage": "hinkal.pro",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Hinkal Protocol"
|
|
@@ -23,6 +23,14 @@
|
|
|
23
23
|
"./providers/Wagmiv1ProviderAdapter": {
|
|
24
24
|
"import": "./providers/Wagmiv1ProviderAdapter.mjs",
|
|
25
25
|
"require": "./providers/Wagmiv1ProviderAdapter.cjs"
|
|
26
|
+
},
|
|
27
|
+
"./providers/prepareEthersHinkal": {
|
|
28
|
+
"import": "./providers/prepareEthersHinkal.mjs",
|
|
29
|
+
"require": "./providers/prepareEthersHinkal.cjs"
|
|
30
|
+
},
|
|
31
|
+
"./providers/prepareWagmiv1EthersHinkal": {
|
|
32
|
+
"import": "./providers/prepareWagmiv1EthersHinkal.mjs",
|
|
33
|
+
"require": "./providers/prepareWagmiv1EthersHinkal.cjs"
|
|
26
34
|
}
|
|
27
35
|
},
|
|
28
36
|
"scripts": {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("../constants/chains.constants.cjs"),d=require("../constants/contracts.constants.cjs"),a=require("../error-handling/error-codes.constants.cjs"),c=require("ethers"),o=require("@wagmi/core");class h{connector;chainId;originalProvider;fetchProvider;signer;chainEventListener;unsubscribeFns=[];initConnector(t){this.connector=t}requireConnector(){if(!this.connector)throw Error("No Connector In Provider Adapter")}async init(t){this.requireConnector(),t&&(this.chainId=t),this.originalProvider=await this.connector.getProvider(),this.fetchProvider=this.createFetchProvider()??this.originalProvider,this.fetchProvider===this.originalProvider&&console.warn("fetchProvider not available");const r=await this.connector.getWalletClient();this.signer=await this.walletClientToSigner(r,this.chainId)}async disconnectFromConnector(){this.release(),await o.disconnect()}async connectToConnector(t){const{isConnected:r}=o.getAccount();r&&await this.disconnectFromConnector();try{return(await o.connect({connector:t})).chain.id}catch{throw new Error(a.transactionErrorCodes.CONNECTION_FAILED)}}async waitForTransaction(t,r){if((await this.fetchProvider?.waitForTransaction(t,r))?.status)return!0;throw Error(a.transactionErrorCodes.TRANSACTION_NOT_CONFIRMED)}async signMessage(t){const r=await o.signMessage({message:t});if(!r)throw new Error(a.transactionErrorCodes.SIGNING_FAILED);if(r.includes("error"))throw new Error(a.transactionErrorCodes.SIGNATURE_UNSUPPORTED_PERSONAL_SIGN);return r}async signTypedData(t,r,e){return this.signer._signTypedData(t,r,e)}getSelectedNetwork=()=>{if(!this.chainId)throw new Error("Illegal state: no chaindId");return i.networkRegistry[this.chainId]};async switchNetwork(t){return o.switchNetwork({chainId:t.chainId})}createFetchProvider(){try{const r=i.networkRegistry[this.chainId]?.fetchRpcUrl;return r?r.includes("wss")?new c.providers.WebSocketProvider(r):new c.providers.StaticJsonRpcProvider(r):void 0}catch(t){console.log("create Fetch Provider error",t);return}}async getAddress(){const{address:t}=o.getAccount();if(!t)throw new Error("IllegalState");return c.utils.getAddress(t)}setChainEventListener(t){this.chainEventListener=t,this.chainEventListener&&(this.unsubscribeFns.push(o.watchAccount(r=>{if(!this.chainEventListener){console.warn("chainEventListener is not set");return}this.chainEventListener.onAccountChanged()})),this.unsubscribeFns.push(o.watchNetwork(r=>{if(!this.chainEventListener){console.warn("chainEventListener is not set");return}this.chainEventListener.onChainChanged(r.chain?.id)})))}onAccountChanged(){return this.init()}onChainChanged(t){return this.init(t)}release(){this.removeListeners()}removeListeners(){this.unsubscribeFns.forEach(t=>t()),this.unsubscribeFns=[]}getContractMetadata(t,r){const e=r??this.chainId;if(!e)throw new Error("No chainId provided in context");const n=i.networkRegistry[e];if(!n)throw new Error(a.transactionErrorCodes.UNSUPPORTED_NETWORK);const s=d.contractMetadataMapping[t];if(!s)throw new Error(`Unsupported contractType: ${t}`);return s(n.contractData)}getContract(t,r=void 0,e){const n=this.getContractMetadata(t,e);if(!n.abi)throw new Error(`No ABI configured for contractType: ${t}`);if(n.address&&r)throw new Error(`Overriding address is not supported for contractType: ${t}`);const s=n.address??r;if(!s)throw new Error(`No contractAddress configured for contractType: ${t}`);return new c.ethers.Contract(s,n.abi)}getContractWithSigner(t,r=void 0){if(!this.signer)throw new Error("IllegalState: no signer");return this.getContract(t,r).connect(this.signer)}getContractWithFetcher(t,r=void 0){if(!this.fetchProvider)throw new Error("fetchProvider not initialized");return this.getContract(t,r).connect(this.fetchProvider)}getContractWithFetcherForEthereum(t,r=void 0){const e=this.chainId===i.chainIds.localhost&&i.localhostNetwork===i.chainIds.ethMainnet?i.chainIds.localhost:i.chainIds.ethMainnet;return this.getContract(t,r).connect(new c.ethers.providers.StaticJsonRpcProvider(i.networkRegistry[e].fetchRpcUrl))}async sendTransaction(t){if(!this.signer)throw new Error("IllegalState: no signer");return await this.signer.sendTransaction(t)}async patchExternalProvider(t){const r=await t.getProvider();let e;if(r instanceof c.ethers.providers.Web3Provider?e=r.provider:e=r,"isWalletConnect"in e){const n=await t.getChainId();e.http=e.setHttpProvider?.(n)}}async connectAndPatchProvider(t){const r=await this.connectToConnector(t);return await this.patchExternalProvider(t),r}isPermitterAvailable(){return!!this.getSelectedNetwork()?.contractData?.permitterAddress}async getGasPrice(){const t=await this.fetchProvider?.getGasPrice();if(!t)throw Error("Could not fetch gas price in getGasPrice");return t.toBigInt()}async walletClientToSigner(t,r){const{account:e,transport:n}=t,s={chainId:r,name:""};return new c.providers.Web3Provider(n,s).getSigner(e.address)}}const w=()=>new h;exports.Wagmiv1ProviderAdapter=h;exports.default=w;
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const i=require("../constants/chains.constants.cjs"),d=require("../constants/contracts.constants.cjs"),a=require("../error-handling/error-codes.constants.cjs"),c=require("ethers"),o=require("@wagmi/core");class h{connector;chainId;originalProvider;fetchProvider;signer;chainEventListener;unsubscribeFns=[];initConnector(t){this.connector=t}requireConnector(){if(!this.connector)throw Error("No Connector In Provider Adapter")}async init(t){this.requireConnector(),t&&(this.chainId=t),this.originalProvider=await this.connector.getProvider(),this.fetchProvider=this.createFetchProvider()??this.originalProvider,this.fetchProvider===this.originalProvider&&console.warn("fetchProvider not available");const r=await this.connector.getWalletClient();this.signer=await this.walletClientToSigner(r,this.chainId)}async disconnectFromConnector(){this.release(),await o.disconnect()}async connectToConnector(t){const{isConnected:r}=o.getAccount();r&&await this.disconnectFromConnector();try{return(await o.connect({connector:t})).chain.id}catch{throw new Error(a.transactionErrorCodes.CONNECTION_FAILED)}}async waitForTransaction(t,r){if((await this.fetchProvider?.waitForTransaction(t,r))?.status)return!0;throw Error(a.transactionErrorCodes.TRANSACTION_NOT_CONFIRMED)}async signMessage(t){const r=await o.signMessage({message:t});if(!r)throw new Error(a.transactionErrorCodes.SIGNING_FAILED);if(r.includes("error"))throw new Error(a.transactionErrorCodes.SIGNATURE_UNSUPPORTED_PERSONAL_SIGN);return r}async signTypedData(t,r,e){return this.signer._signTypedData(t,r,e)}getSelectedNetwork=()=>{if(!this.chainId)throw new Error("Illegal state: no chaindId");return i.networkRegistry[this.chainId]};async switchNetwork(t){return o.switchNetwork({chainId:t.chainId})}createFetchProvider(){try{const r=i.networkRegistry[this.chainId]?.fetchRpcUrl;return r?r.includes("wss")?new c.providers.WebSocketProvider(r):new c.providers.StaticJsonRpcProvider(r):void 0}catch(t){console.log("create Fetch Provider error",t);return}}async getAddress(){const{address:t}=o.getAccount();if(!t)throw new Error("IllegalState");return c.utils.getAddress(t)}setChainEventListener(t){this.chainEventListener=t,this.chainEventListener&&(this.unsubscribeFns.push(o.watchAccount(r=>{if(!this.chainEventListener){console.warn("chainEventListener is not set");return}this.chainEventListener.onAccountChanged()})),this.unsubscribeFns.push(o.watchNetwork(r=>{if(!this.chainEventListener){console.warn("chainEventListener is not set");return}this.chainEventListener.onChainChanged(r.chain?.id)})))}onAccountChanged(){return this.init()}onChainChanged(t){return this.init(t)}release(){this.removeListeners()}removeListeners(){this.unsubscribeFns.forEach(t=>t()),this.unsubscribeFns=[]}getContractMetadata(t,r){const e=r??this.chainId;if(!e)throw new Error("No chainId provided in context");const n=i.networkRegistry[e];if(!n)throw new Error(a.transactionErrorCodes.UNSUPPORTED_NETWORK);const s=d.contractMetadataMapping[t];if(!s)throw new Error(`Unsupported contractType: ${t}`);return s(n.contractData)}getContract(t,r=void 0,e){const n=this.getContractMetadata(t,e);if(!n.abi)throw new Error(`No ABI configured for contractType: ${t}`);if(n.address&&r)throw new Error(`Overriding address is not supported for contractType: ${t}`);const s=n.address??r;if(!s)throw new Error(`No contractAddress configured for contractType: ${t}`);return new c.ethers.Contract(s,n.abi)}getContractWithSigner(t,r=void 0){if(!this.signer)throw new Error("IllegalState: no signer");return this.getContract(t,r).connect(this.signer)}getContractWithFetcher(t,r=void 0){if(!this.fetchProvider)throw new Error("fetchProvider not initialized");return this.getContract(t,r).connect(this.fetchProvider)}getContractWithFetcherForEthereum(t,r=void 0){const e=this.chainId===i.chainIds.localhost&&i.localhostNetwork===i.chainIds.ethMainnet?i.chainIds.localhost:i.chainIds.ethMainnet;return this.getContract(t,r).connect(new c.ethers.providers.StaticJsonRpcProvider(i.networkRegistry[e].fetchRpcUrl))}async sendTransaction(t){if(!this.signer)throw new Error("IllegalState: no signer");return await this.signer.sendTransaction(t)}async patchExternalProvider(t){const r=await t.getProvider();let e;if(r instanceof c.ethers.providers.Web3Provider?e=r.provider:e=r,"isWalletConnect"in e){const n=await t.getChainId();e.http=e.setHttpProvider?.(n)}}async connectAndPatchProvider(t){const r=await this.connectToConnector(t);return await this.patchExternalProvider(t),r}isPermitterAvailable(){return!!this.getSelectedNetwork()?.contractData?.permitterAddress}async getGasPrice(){const t=await this.fetchProvider?.getGasPrice();if(!t)throw Error("Could not fetch gas price in getGasPrice");return t.toBigInt()}async walletClientToSigner(t,r){const{account:e,transport:n}=t,s={chainId:r,name:""};return new c.providers.Web3Provider(n,s).getSigner(e.address)}}const w=new h,g=()=>new h;exports.Wagmiv1ProviderAdapter=h;exports.default=g;exports.wagmiv1ProvierAdapter=w;
|
|
@@ -39,5 +39,6 @@ export declare class Wagmiv1ProviderAdapter implements IProviderAdapter<Connecto
|
|
|
39
39
|
getGasPrice(): Promise<bigint>;
|
|
40
40
|
walletClientToSigner(walletClient: WalletClient, chainId: number): Promise<ethers.providers.JsonRpcSigner>;
|
|
41
41
|
}
|
|
42
|
+
export declare const wagmiv1ProvierAdapter: Wagmiv1ProviderAdapter;
|
|
42
43
|
declare const _default: <T = unknown>() => IProviderAdapter<T>;
|
|
43
44
|
export default _default;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { networkRegistry as s, chainIds as c, localhostNetwork as
|
|
2
|
-
import { contractMetadataMapping as
|
|
1
|
+
import { networkRegistry as s, chainIds as c, localhostNetwork as l } from "../constants/chains.constants.mjs";
|
|
2
|
+
import { contractMetadataMapping as g } from "../constants/contracts.constants.mjs";
|
|
3
3
|
import { transactionErrorCodes as o } from "../error-handling/error-codes.constants.mjs";
|
|
4
|
-
import { providers as a, utils as
|
|
5
|
-
import { disconnect as
|
|
6
|
-
class
|
|
4
|
+
import { providers as a, utils as v, ethers as h } from "ethers";
|
|
5
|
+
import { disconnect as u, getAccount as d, connect as f, signMessage as p, switchNetwork as P, watchAccount as C, watchNetwork as E } from "@wagmi/core";
|
|
6
|
+
class w {
|
|
7
7
|
connector;
|
|
8
8
|
chainId;
|
|
9
9
|
originalProvider;
|
|
@@ -24,13 +24,13 @@ class P {
|
|
|
24
24
|
this.signer = await this.walletClientToSigner(r, this.chainId);
|
|
25
25
|
}
|
|
26
26
|
async disconnectFromConnector() {
|
|
27
|
-
this.release(), await
|
|
27
|
+
this.release(), await u();
|
|
28
28
|
}
|
|
29
29
|
async connectToConnector(t) {
|
|
30
30
|
const { isConnected: r } = d();
|
|
31
31
|
r && await this.disconnectFromConnector();
|
|
32
32
|
try {
|
|
33
|
-
return (await
|
|
33
|
+
return (await f({ connector: t })).chain.id;
|
|
34
34
|
} catch {
|
|
35
35
|
throw new Error(o.CONNECTION_FAILED);
|
|
36
36
|
}
|
|
@@ -41,7 +41,7 @@ class P {
|
|
|
41
41
|
throw Error(o.TRANSACTION_NOT_CONFIRMED);
|
|
42
42
|
}
|
|
43
43
|
async signMessage(t) {
|
|
44
|
-
const r = await
|
|
44
|
+
const r = await p({ message: t });
|
|
45
45
|
if (!r)
|
|
46
46
|
throw new Error(o.SIGNING_FAILED);
|
|
47
47
|
if (r.includes("error"))
|
|
@@ -57,7 +57,7 @@ class P {
|
|
|
57
57
|
return s[this.chainId];
|
|
58
58
|
};
|
|
59
59
|
async switchNetwork(t) {
|
|
60
|
-
return
|
|
60
|
+
return P({ chainId: t.chainId });
|
|
61
61
|
}
|
|
62
62
|
createFetchProvider() {
|
|
63
63
|
try {
|
|
@@ -72,7 +72,7 @@ class P {
|
|
|
72
72
|
const { address: t } = d();
|
|
73
73
|
if (!t)
|
|
74
74
|
throw new Error("IllegalState");
|
|
75
|
-
return
|
|
75
|
+
return v.getAddress(t);
|
|
76
76
|
}
|
|
77
77
|
setChainEventListener(t) {
|
|
78
78
|
this.chainEventListener = t, this.chainEventListener && (this.unsubscribeFns.push(
|
|
@@ -112,7 +112,7 @@ class P {
|
|
|
112
112
|
const n = s[e];
|
|
113
113
|
if (!n)
|
|
114
114
|
throw new Error(o.UNSUPPORTED_NETWORK);
|
|
115
|
-
const i =
|
|
115
|
+
const i = g[t];
|
|
116
116
|
if (!i)
|
|
117
117
|
throw new Error(`Unsupported contractType: ${t}`);
|
|
118
118
|
return i(n.contractData);
|
|
@@ -139,7 +139,7 @@ class P {
|
|
|
139
139
|
return this.getContract(t, r).connect(this.fetchProvider);
|
|
140
140
|
}
|
|
141
141
|
getContractWithFetcherForEthereum(t, r = void 0) {
|
|
142
|
-
const e = this.chainId === c.localhost &&
|
|
142
|
+
const e = this.chainId === c.localhost && l === c.ethMainnet ? c.localhost : c.ethMainnet;
|
|
143
143
|
return this.getContract(t, r).connect(
|
|
144
144
|
new h.providers.StaticJsonRpcProvider(s[e].fetchRpcUrl)
|
|
145
145
|
);
|
|
@@ -178,8 +178,9 @@ class P {
|
|
|
178
178
|
return new a.Web3Provider(n, i).getSigner(e.address);
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
|
-
const T = () => new
|
|
181
|
+
const T = new w(), b = () => new w();
|
|
182
182
|
export {
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
w as Wagmiv1ProviderAdapter,
|
|
184
|
+
b as default,
|
|
185
|
+
T as wagmiv1ProvierAdapter
|
|
185
186
|
};
|