polkamarkets-js 3.3.1 → 3.4.0

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": "polkamarkets-js",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -18,6 +18,7 @@ const WETH9Contract = require("./models/index").WETH9Contract;
18
18
  const ArbitrationContract = require("./models/index").ArbitrationContract;
19
19
  const ArbitrationProxyContract = require("./models/index").ArbitrationProxyContract;
20
20
 
21
+ const DualProvider = require("./utils/DualProvider");
21
22
  const Account = require('./utils/Account');
22
23
 
23
24
 
@@ -38,7 +39,8 @@ class Application {
38
39
  isSocialLogin = false,
39
40
  socialLoginParams,
40
41
  startBlock,
41
- defaultDecimals
42
+ defaultDecimals,
43
+ useDualProvider = false
42
44
  }) {
43
45
  this.web3Provider = web3Provider;
44
46
  // evm logs http source (optional)
@@ -48,6 +50,8 @@ class Application {
48
50
  this.isSocialLogin = isSocialLogin;
49
51
  this.startBlock = startBlock;
50
52
  this.defaultDecimals = defaultDecimals;
53
+ // use DualProvider to separate read (HttpProvider) and write (window.ethereum) operations
54
+ this.useDualProvider = useDualProvider;
51
55
 
52
56
  if (this.isSocialLogin) {
53
57
  this.socialLoginParams = socialLoginParams;
@@ -70,7 +74,14 @@ class Application {
70
74
  * @description Start the Application
71
75
  */
72
76
  start() {
73
- this.web3 = new Web3(new Web3.providers.HttpProvider(this.web3Provider));
77
+ if (this.useDualProvider) {
78
+ // Store read provider for later use in DualProvider
79
+ this.readProvider = new Web3.providers.HttpProvider(this.web3Provider);
80
+ this.web3 = new Web3(this.readProvider);
81
+ } else {
82
+ // Backwards compatible: standard HttpProvider
83
+ this.web3 = new Web3(new Web3.providers.HttpProvider(this.web3Provider));
84
+ }
74
85
  this.web3.eth.handleRevert = true;
75
86
  if (typeof window !== "undefined") {
76
87
  window.web3 = this.web3;
@@ -97,8 +108,18 @@ class Application {
97
108
  try {
98
109
  if (typeof window === "undefined") { return false; }
99
110
  if (window.ethereum) {
100
- window.web3 = new Web3(window.ethereum);
101
- this.web3 = window.web3;
111
+ if (this.useDualProvider) {
112
+ // New approach: DualProvider for reads (HttpProvider) and writes (window.ethereum)
113
+ const dualProvider = new DualProvider(this.readProvider, window.ethereum);
114
+ this.web3 = new Web3(dualProvider);
115
+ this.web3.eth.handleRevert = true;
116
+ window.web3 = this.web3;
117
+ } else {
118
+ // Backwards compatible: replace web3 with window.ethereum
119
+ window.web3 = new Web3(window.ethereum);
120
+ this.web3 = window.web3;
121
+ }
122
+
102
123
  await window.ethereum.enable();
103
124
  return true;
104
125
  }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * DualProvider - Routes requests to appropriate provider based on method type
3
+ * Read operations use HttpProvider, write operations use window.ethereum
4
+ */
5
+ class DualProvider {
6
+ constructor(readProvider, writeProvider) {
7
+ this.readProvider = readProvider;
8
+ this.writeProvider = writeProvider;
9
+
10
+ // Methods that require user interaction/signing (write operations)
11
+ this.writeMethods = new Set([
12
+ "eth_sendTransaction",
13
+ "eth_sendRawTransaction",
14
+ "eth_sign",
15
+ "eth_signTransaction",
16
+ "personal_sign",
17
+ "eth_signTypedData",
18
+ "eth_signTypedData_v3",
19
+ "eth_signTypedData_v4",
20
+ "wallet_switchEthereumChain",
21
+ "wallet_addEthereumChain",
22
+ "eth_accounts",
23
+ "eth_requestAccounts",
24
+ ]);
25
+ }
26
+
27
+ send(payload, callback) {
28
+ const provider = this.writeMethods.has(payload.method)
29
+ ? this.writeProvider
30
+ : this.readProvider;
31
+
32
+ return provider.send(payload, callback);
33
+ }
34
+
35
+ sendAsync(payload, callback) {
36
+ const provider = this.writeMethods.has(payload.method)
37
+ ? this.writeProvider
38
+ : this.readProvider;
39
+
40
+ return provider.sendAsync(payload, callback);
41
+ }
42
+
43
+ // Support for modern request/send pattern
44
+ request(args) {
45
+ const provider = this.writeMethods.has(args.method)
46
+ ? this.writeProvider
47
+ : this.readProvider;
48
+
49
+ if (provider.request) {
50
+ return provider.request(args);
51
+ }
52
+
53
+ // Fallback for providers that don't support request()
54
+ return new Promise((resolve, reject) => {
55
+ const callback = (error, response) => {
56
+ if (error) {
57
+ reject(error);
58
+ } else {
59
+ resolve(response.result);
60
+ }
61
+ };
62
+
63
+ if (provider.sendAsync) {
64
+ provider.sendAsync(
65
+ { jsonrpc: "2.0", id: Date.now(), ...args },
66
+ callback
67
+ );
68
+ } else if (provider.send) {
69
+ provider.send({ jsonrpc: "2.0", id: Date.now(), ...args }, callback);
70
+ } else {
71
+ reject(
72
+ new Error("Provider does not support request, send, or sendAsync")
73
+ );
74
+ }
75
+ });
76
+ }
77
+ }
78
+
79
+ module.exports = DualProvider;