gn-provider 1.1.2 → 1.1.4

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.
@@ -10,22 +10,22 @@ export declare class GNProvider extends Provider {
10
10
  private _network;
11
11
  private _isConnected;
12
12
  private _apiKey;
13
- constructor(network: scryptlib.bsv.Networks.Network, apiKey?: string);
14
- get apiPrefix(): string;
15
- isConnected(): boolean;
16
- connect: () => Promise<this>;
13
+ private apiPrefix;
17
14
  private _getHeaders;
18
- updateNetwork(network: scryptlib.bsv.Networks.Network): void;
19
- getNetwork(): scryptlib.bsv.Networks.Network;
20
- protected _ready(): Promise<void>;
21
- sendRawTransaction(rawTxHex: string): Promise<TxHash>;
22
- listUnspent(address: AddressOption, options?: UtxoQueryOptions): Promise<UTXO[]>;
23
- getBalance(address: AddressOption): Promise<{
15
+ connect: () => Promise<this>;
16
+ getFeePerKb: () => Promise<number>;
17
+ constructor(network: scryptlib.bsv.Networks.Network, apiKey?: string);
18
+ isConnected: () => boolean;
19
+ updateNetwork: (network: scryptlib.bsv.Networks.Network) => void;
20
+ getNetwork: () => scryptlib.bsv.Networks.Network;
21
+ protected _ready: () => Promise<void>;
22
+ sendRawTransaction: (rawTxHex: string) => Promise<TxHash>;
23
+ listUnspent: (address: AddressOption, options?: UtxoQueryOptions) => Promise<UTXO[]>;
24
+ getBalance: (address: AddressOption) => Promise<{
24
25
  confirmed: number;
25
26
  unconfirmed: number;
26
27
  }>;
27
- getTransaction(txHash: string): Promise<TransactionResponse>;
28
- getFeePerKb(): Promise<number>;
28
+ getTransaction: (txHash: string) => Promise<TransactionResponse>;
29
29
  private needIgnoreError;
30
30
  private friendlyBIP22RejectionMsg;
31
31
  }
@@ -48,8 +48,6 @@ exports.GNProvider = void 0;
48
48
  const events_1 = require("events");
49
49
  const scryptlib = __importStar(require("scryptlib"));
50
50
  const abstract_provider_1 = require("scrypt-ts/dist/bsv/abstract-provider");
51
- //import * as bsv from 'bsv';
52
- //import superagent from 'superagent';
53
51
  const superagent = __importStar(require("superagent"));
54
52
  const utils_1 = require("scrypt-ts/dist/bsv/utils");
55
53
  //import { EventEmitter } from 'events';
@@ -59,43 +57,25 @@ var ProviderEvent;
59
57
  ProviderEvent["NetworkChange"] = "networkChange";
60
58
  })(ProviderEvent || (ProviderEvent = {}));
61
59
  class GNProvider extends abstract_provider_1.Provider {
62
- /*protected _initializeConnection = () => {
63
- this.connect().catch(console.error);
64
- }*/
65
- /*constructor(network: scryptlib.bsv.Networks.Network, apiKey = '') {
66
- super();
67
- Object.setPrototypeOf(this, EventEmitter.prototype);
68
- this._network = network;
69
- this._apiKey = apiKey;
70
- this._initializeConnection();
71
- }*/
72
60
  constructor(network, apiKey = '') {
73
61
  super();
74
62
  this._isConnected = false;
75
- /*async connect(): Promise<this> {
76
- try {
77
- const headers = this._getHeaders();
78
- const res = await superagent.get(`${this.apiPrefix}/woc`)
79
- .timeout(3000)
80
- .set(headers);
81
-
82
- if (res.ok && res.text === "Whats On Chain") {
83
- this._isConnected = true;
84
- this.emit(ProviderEvent.Connected, true);
85
- return this;
86
- }
87
- throw new Error(res.body?.msg || res.text);
88
- } catch (error: any) {
89
- this._isConnected = false;
90
- this.emit(ProviderEvent.Connected, false);
91
- throw new Error(`connect failed: ${error.message || "unknown error"}`);
92
- }
63
+ this._getHeaders = () => {
64
+ return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
65
+ };
66
+ /*get apiPrefix(): string {
67
+ const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
68
+ return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
93
69
  }*/
70
+ /*apiPrefix = (): string => {
71
+ const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
72
+ return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
73
+ }*/
94
74
  this.connect = () => __awaiter(this, void 0, void 0, function* () {
95
75
  var _a;
96
76
  try {
97
77
  const headers = this._getHeaders();
98
- const res = yield superagent.get(`${this.apiPrefix}/woc`)
78
+ const res = yield superagent.get(`${this.apiPrefix()}/woc`) //`${this.apiPrefix}/woc`)
99
79
  .timeout(3000)
100
80
  .set(headers);
101
81
  if (res.ok && res.text === "Whats On Chain") {
@@ -111,34 +91,61 @@ class GNProvider extends abstract_provider_1.Provider {
111
91
  throw new Error(`connect failed: ${error.message || "unknown error"}`);
112
92
  }
113
93
  });
114
- // Inicializa propiedades primero
115
- this._network = network;
116
- this._apiKey = apiKey;
117
- this._isConnected = false;
118
- // Configura EventEmitter
119
- Object.setPrototypeOf(this, events_1.EventEmitter.prototype);
120
- // Conexión directa
121
- this.connect().catch(console.error);
122
- }
123
- get apiPrefix() {
124
- const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
125
- return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
126
- }
127
- isConnected() {
94
+ //async getFeePerKb(): Promise<number> {
95
+ this.getFeePerKb = () => __awaiter(this, void 0, void 0, function* () {
96
+ yield this._ready();
97
+ const headers = this._getHeaders();
98
+ try {
99
+ const now = Math.floor(Date.now() / 1000);
100
+ const from = now - 1800; // 30 minutos atrás
101
+ const res = yield superagent.get(`${this.apiPrefix()}/miner/fees?from=${from}&to=${now}`)
102
+ .set(headers);
103
+ if (res.body && Array.isArray(res.body) && res.body.length > 0) {
104
+ const totalFeeRate = res.body.reduce((sum, minerData) => {
105
+ return sum + minerData.min_fee_rate;
106
+ }, 0);
107
+ const averageFeeRate = totalFeeRate / res.body.length;
108
+ const feeRateWithMargin = averageFeeRate * 1.3;
109
+ return Math.round(feeRateWithMargin * 100) / 100;
110
+ }
111
+ throw new Error("No fee data available");
112
+ }
113
+ catch (error) {
114
+ return 1.05; // Valor de fallback
115
+ }
116
+ });
117
+ /*isConnected(): boolean {
128
118
  return this._isConnected;
129
- }
130
- _getHeaders() {
131
- return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
132
- }
133
- updateNetwork(network) {
134
- this._network = network;
135
- this.emit(ProviderEvent.NetworkChange, network);
136
- }
137
- getNetwork() {
138
- return this._network;
139
- }
140
- _ready() {
141
- return __awaiter(this, void 0, void 0, function* () {
119
+ }*/
120
+ this.isConnected = () => this._isConnected;
121
+ /*private _getHeaders() {
122
+ return {
123
+ 'Content-Type': 'application/json',
124
+ ...(this._apiKey ? { 'woc-api-key': this._apiKey } : {})
125
+ };
126
+ }*/
127
+ /*updateNetwork(network: scryptlib.bsv.Networks.Network): void {
128
+ this._network = network;
129
+ this.emit(ProviderEvent.NetworkChange, network);
130
+ }*/
131
+ this.updateNetwork = (network) => {
132
+ this._network = network;
133
+ this.emit(ProviderEvent.NetworkChange, network);
134
+ };
135
+ /*getNetwork(): scryptlib.bsv.Networks.Network {
136
+ return this._network;
137
+ }*/
138
+ this.getNetwork = () => this._network;
139
+ /*protected async _ready(): Promise<void> {
140
+ if (!this.isConnected()) {
141
+ try {
142
+ await this.connect();
143
+ } catch (error) {
144
+ throw error;
145
+ }
146
+ }
147
+ }*/
148
+ this._ready = () => __awaiter(this, void 0, void 0, function* () {
142
149
  if (!this.isConnected()) {
143
150
  try {
144
151
  yield this.connect();
@@ -148,16 +155,15 @@ class GNProvider extends abstract_provider_1.Provider {
148
155
  }
149
156
  }
150
157
  });
151
- }
152
- sendRawTransaction(rawTxHex) {
153
- return __awaiter(this, void 0, void 0, function* () {
158
+ //async sendRawTransaction(rawTxHex: string): Promise<TxHash> {
159
+ this.sendRawTransaction = (rawTxHex) => __awaiter(this, void 0, void 0, function* () {
154
160
  var _a;
155
161
  yield this._ready();
156
162
  const headers = this._getHeaders();
157
163
  const size = Math.max(1, rawTxHex.length / 2 / 1024); // Tamaño en KB
158
164
  const timeout = Math.max(10000, 1000 * size); // Timeout dinámico
159
165
  try {
160
- const res = yield superagent.post(`${this.apiPrefix}/tx/raw`)
166
+ const res = yield superagent.post(`${this.apiPrefix()}/tx/raw`)
161
167
  .timeout({
162
168
  response: timeout,
163
169
  deadline: 60000
@@ -176,12 +182,11 @@ class GNProvider extends abstract_provider_1.Provider {
176
182
  throw new Error(`GNProvider ERROR: ${error.message}`);
177
183
  }
178
184
  });
179
- }
180
- listUnspent(address, options) {
181
- return __awaiter(this, void 0, void 0, function* () {
185
+ //async listUnspent(address: AddressOption, options?: UtxoQueryOptions): Promise<UTXO[]> {
186
+ this.listUnspent = (address, options) => __awaiter(this, void 0, void 0, function* () {
182
187
  yield this._ready();
183
188
  const headers = this._getHeaders();
184
- const res = yield superagent.get(`${this.apiPrefix}/address/${address}/unspent`)
189
+ const res = yield superagent.get(`${this.apiPrefix()}/address/${address}/unspent`)
185
190
  .set(headers);
186
191
  const utxos = res.body.map((item) => ({
187
192
  txId: item.tx_hash,
@@ -191,12 +196,11 @@ class GNProvider extends abstract_provider_1.Provider {
191
196
  }));
192
197
  return options ? (0, utils_1.filterUTXO)(utxos, options) : utxos;
193
198
  });
194
- }
195
- getBalance(address) {
196
- return __awaiter(this, void 0, void 0, function* () {
199
+ //async getBalance(address: AddressOption): Promise<{ confirmed: number; unconfirmed: number }> {
200
+ this.getBalance = (address) => __awaiter(this, void 0, void 0, function* () {
197
201
  try {
198
202
  const headers = this._getHeaders();
199
- const res = yield superagent.get(`${this.apiPrefix}/address/${address}/balance`)
203
+ const res = yield superagent.get(`${this.apiPrefix()}/address/${address}/balance`)
200
204
  .set(headers);
201
205
  return {
202
206
  confirmed: res.body.confirmed || 0,
@@ -212,13 +216,12 @@ class GNProvider extends abstract_provider_1.Provider {
212
216
  };
213
217
  }
214
218
  });
215
- }
216
- getTransaction(txHash) {
217
- return __awaiter(this, void 0, void 0, function* () {
219
+ //async getTransaction(txHash: string): Promise<TransactionResponse> {
220
+ this.getTransaction = (txHash) => __awaiter(this, void 0, void 0, function* () {
218
221
  yield this._ready();
219
222
  const headers = this._getHeaders();
220
223
  try {
221
- const res = yield superagent.get(`${this.apiPrefix}/tx/${txHash}/hex`)
224
+ const res = yield superagent.get(`${this.apiPrefix()}/tx/${txHash}/hex`)
222
225
  .set(headers);
223
226
  if (res.ok) {
224
227
  return new scryptlib.bsv.Transaction(res.text);
@@ -229,30 +232,18 @@ class GNProvider extends abstract_provider_1.Provider {
229
232
  throw new Error(`Error fetching transaction: ${error.message}`);
230
233
  }
231
234
  });
232
- }
233
- getFeePerKb() {
234
- return __awaiter(this, void 0, void 0, function* () {
235
- yield this._ready();
236
- const headers = this._getHeaders();
237
- try {
238
- const now = Math.floor(Date.now() / 1000);
239
- const from = now - 1800; // 30 minutos atrás
240
- const res = yield superagent.get(`${this.apiPrefix}/miner/fees?from=${from}&to=${now}`)
241
- .set(headers);
242
- if (res.body && Array.isArray(res.body) && res.body.length > 0) {
243
- const totalFeeRate = res.body.reduce((sum, minerData) => {
244
- return sum + minerData.min_fee_rate;
245
- }, 0);
246
- const averageFeeRate = totalFeeRate / res.body.length;
247
- const feeRateWithMargin = averageFeeRate * 1.3;
248
- return Math.round(feeRateWithMargin * 100) / 100;
249
- }
250
- throw new Error("No fee data available");
251
- }
252
- catch (error) {
253
- return 1.05; // Valor de fallback
254
- }
255
- });
235
+ // Inicializa propiedades primero
236
+ this._network = network;
237
+ this._apiKey = apiKey;
238
+ this._isConnected = false;
239
+ this.apiPrefix = () => {
240
+ const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
241
+ return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
242
+ };
243
+ // Configura EventEmitter
244
+ Object.setPrototypeOf(this, events_1.EventEmitter.prototype);
245
+ // Conexión directa
246
+ this.connect().catch(console.error);
256
247
  }
257
248
  needIgnoreError(inMsg) {
258
249
  if (inMsg.includes('Transaction already in the mempool'))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gn-provider",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "files": [
5
5
  "dist",
6
6
  "scripts",