gn-provider 1.1.2 → 1.1.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.
@@ -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);
13
+ private _getHeaders;
14
14
  get apiPrefix(): string;
15
- isConnected(): boolean;
16
15
  connect: () => Promise<this>;
17
- 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<{
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,38 +57,16 @@ 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
- }*/
60
+ get apiPrefix() {
61
+ const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
62
+ return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
63
+ }
72
64
  constructor(network, apiKey = '') {
73
65
  super();
74
66
  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
- }
93
- }*/
67
+ this._getHeaders = () => {
68
+ return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
69
+ };
94
70
  this.connect = () => __awaiter(this, void 0, void 0, function* () {
95
71
  var _a;
96
72
  try {
@@ -111,34 +87,61 @@ class GNProvider extends abstract_provider_1.Provider {
111
87
  throw new Error(`connect failed: ${error.message || "unknown error"}`);
112
88
  }
113
89
  });
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() {
90
+ //async getFeePerKb(): Promise<number> {
91
+ this.getFeePerKb = () => __awaiter(this, void 0, void 0, function* () {
92
+ yield this._ready();
93
+ const headers = this._getHeaders();
94
+ try {
95
+ const now = Math.floor(Date.now() / 1000);
96
+ const from = now - 1800; // 30 minutos atrás
97
+ const res = yield superagent.get(`${this.apiPrefix}/miner/fees?from=${from}&to=${now}`)
98
+ .set(headers);
99
+ if (res.body && Array.isArray(res.body) && res.body.length > 0) {
100
+ const totalFeeRate = res.body.reduce((sum, minerData) => {
101
+ return sum + minerData.min_fee_rate;
102
+ }, 0);
103
+ const averageFeeRate = totalFeeRate / res.body.length;
104
+ const feeRateWithMargin = averageFeeRate * 1.3;
105
+ return Math.round(feeRateWithMargin * 100) / 100;
106
+ }
107
+ throw new Error("No fee data available");
108
+ }
109
+ catch (error) {
110
+ return 1.05; // Valor de fallback
111
+ }
112
+ });
113
+ /*isConnected(): boolean {
128
114
  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* () {
115
+ }*/
116
+ this.isConnected = () => this._isConnected;
117
+ /*private _getHeaders() {
118
+ return {
119
+ 'Content-Type': 'application/json',
120
+ ...(this._apiKey ? { 'woc-api-key': this._apiKey } : {})
121
+ };
122
+ }*/
123
+ /*updateNetwork(network: scryptlib.bsv.Networks.Network): void {
124
+ this._network = network;
125
+ this.emit(ProviderEvent.NetworkChange, network);
126
+ }*/
127
+ this.updateNetwork = (network) => {
128
+ this._network = network;
129
+ this.emit(ProviderEvent.NetworkChange, network);
130
+ };
131
+ /*getNetwork(): scryptlib.bsv.Networks.Network {
132
+ return this._network;
133
+ }*/
134
+ this.getNetwork = () => this._network;
135
+ /*protected async _ready(): Promise<void> {
136
+ if (!this.isConnected()) {
137
+ try {
138
+ await this.connect();
139
+ } catch (error) {
140
+ throw error;
141
+ }
142
+ }
143
+ }*/
144
+ this._ready = () => __awaiter(this, void 0, void 0, function* () {
142
145
  if (!this.isConnected()) {
143
146
  try {
144
147
  yield this.connect();
@@ -148,9 +151,8 @@ class GNProvider extends abstract_provider_1.Provider {
148
151
  }
149
152
  }
150
153
  });
151
- }
152
- sendRawTransaction(rawTxHex) {
153
- return __awaiter(this, void 0, void 0, function* () {
154
+ //async sendRawTransaction(rawTxHex: string): Promise<TxHash> {
155
+ this.sendRawTransaction = (rawTxHex) => __awaiter(this, void 0, void 0, function* () {
154
156
  var _a;
155
157
  yield this._ready();
156
158
  const headers = this._getHeaders();
@@ -176,9 +178,8 @@ class GNProvider extends abstract_provider_1.Provider {
176
178
  throw new Error(`GNProvider ERROR: ${error.message}`);
177
179
  }
178
180
  });
179
- }
180
- listUnspent(address, options) {
181
- return __awaiter(this, void 0, void 0, function* () {
181
+ //async listUnspent(address: AddressOption, options?: UtxoQueryOptions): Promise<UTXO[]> {
182
+ this.listUnspent = (address, options) => __awaiter(this, void 0, void 0, function* () {
182
183
  yield this._ready();
183
184
  const headers = this._getHeaders();
184
185
  const res = yield superagent.get(`${this.apiPrefix}/address/${address}/unspent`)
@@ -191,9 +192,8 @@ class GNProvider extends abstract_provider_1.Provider {
191
192
  }));
192
193
  return options ? (0, utils_1.filterUTXO)(utxos, options) : utxos;
193
194
  });
194
- }
195
- getBalance(address) {
196
- return __awaiter(this, void 0, void 0, function* () {
195
+ //async getBalance(address: AddressOption): Promise<{ confirmed: number; unconfirmed: number }> {
196
+ this.getBalance = (address) => __awaiter(this, void 0, void 0, function* () {
197
197
  try {
198
198
  const headers = this._getHeaders();
199
199
  const res = yield superagent.get(`${this.apiPrefix}/address/${address}/balance`)
@@ -212,9 +212,8 @@ class GNProvider extends abstract_provider_1.Provider {
212
212
  };
213
213
  }
214
214
  });
215
- }
216
- getTransaction(txHash) {
217
- return __awaiter(this, void 0, void 0, function* () {
215
+ //async getTransaction(txHash: string): Promise<TransactionResponse> {
216
+ this.getTransaction = (txHash) => __awaiter(this, void 0, void 0, function* () {
218
217
  yield this._ready();
219
218
  const headers = this._getHeaders();
220
219
  try {
@@ -229,30 +228,14 @@ class GNProvider extends abstract_provider_1.Provider {
229
228
  throw new Error(`Error fetching transaction: ${error.message}`);
230
229
  }
231
230
  });
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
- });
231
+ // Inicializa propiedades primero
232
+ this._network = network;
233
+ this._apiKey = apiKey;
234
+ this._isConnected = false;
235
+ // Configura EventEmitter
236
+ Object.setPrototypeOf(this, events_1.EventEmitter.prototype);
237
+ // Conexión directa
238
+ this.connect().catch(console.error);
256
239
  }
257
240
  needIgnoreError(inMsg) {
258
241
  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.3",
4
4
  "files": [
5
5
  "dist",
6
6
  "scripts",