gn-provider 1.1.1 → 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);
14
- get apiPrefix(): string;
15
- isConnected(): boolean;
16
- connect(): Promise<this>;
17
13
  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<{
14
+ get apiPrefix(): string;
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,37 +57,17 @@ 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
- constructor(network, apiKey = '') {
73
- super();
74
- this._isConnected = false;
75
- // Inicializa propiedades primero
76
- this._network = network;
77
- this._apiKey = apiKey;
78
- this._isConnected = false;
79
- // Configura EventEmitter
80
- Object.setPrototypeOf(this, events_1.EventEmitter.prototype);
81
- // Conexión directa
82
- this.connect().catch(console.error);
83
- }
84
60
  get apiPrefix() {
85
61
  const networkStr = this._network.name === scryptlib.bsv.Networks.mainnet.name ? 'main' : 'test';
86
62
  return `https://api.whatsonchain.com/v1/bsv/${networkStr}`;
87
63
  }
88
- isConnected() {
89
- return this._isConnected;
90
- }
91
- connect() {
92
- return __awaiter(this, void 0, void 0, function* () {
64
+ constructor(network, apiKey = '') {
65
+ super();
66
+ this._isConnected = false;
67
+ this._getHeaders = () => {
68
+ return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
69
+ };
70
+ this.connect = () => __awaiter(this, void 0, void 0, function* () {
93
71
  var _a;
94
72
  try {
95
73
  const headers = this._getHeaders();
@@ -109,19 +87,61 @@ class GNProvider extends abstract_provider_1.Provider {
109
87
  throw new Error(`connect failed: ${error.message || "unknown error"}`);
110
88
  }
111
89
  });
112
- }
113
- _getHeaders() {
114
- return Object.assign({ 'Content-Type': 'application/json' }, (this._apiKey ? { 'woc-api-key': this._apiKey } : {}));
115
- }
116
- updateNetwork(network) {
117
- this._network = network;
118
- this.emit(ProviderEvent.NetworkChange, network);
119
- }
120
- getNetwork() {
121
- return this._network;
122
- }
123
- _ready() {
124
- return __awaiter(this, void 0, void 0, function* () {
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 {
114
+ return this._isConnected;
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* () {
125
145
  if (!this.isConnected()) {
126
146
  try {
127
147
  yield this.connect();
@@ -131,9 +151,8 @@ class GNProvider extends abstract_provider_1.Provider {
131
151
  }
132
152
  }
133
153
  });
134
- }
135
- sendRawTransaction(rawTxHex) {
136
- 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* () {
137
156
  var _a;
138
157
  yield this._ready();
139
158
  const headers = this._getHeaders();
@@ -159,9 +178,8 @@ class GNProvider extends abstract_provider_1.Provider {
159
178
  throw new Error(`GNProvider ERROR: ${error.message}`);
160
179
  }
161
180
  });
162
- }
163
- listUnspent(address, options) {
164
- 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* () {
165
183
  yield this._ready();
166
184
  const headers = this._getHeaders();
167
185
  const res = yield superagent.get(`${this.apiPrefix}/address/${address}/unspent`)
@@ -174,9 +192,8 @@ class GNProvider extends abstract_provider_1.Provider {
174
192
  }));
175
193
  return options ? (0, utils_1.filterUTXO)(utxos, options) : utxos;
176
194
  });
177
- }
178
- getBalance(address) {
179
- 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* () {
180
197
  try {
181
198
  const headers = this._getHeaders();
182
199
  const res = yield superagent.get(`${this.apiPrefix}/address/${address}/balance`)
@@ -195,9 +212,8 @@ class GNProvider extends abstract_provider_1.Provider {
195
212
  };
196
213
  }
197
214
  });
198
- }
199
- getTransaction(txHash) {
200
- 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* () {
201
217
  yield this._ready();
202
218
  const headers = this._getHeaders();
203
219
  try {
@@ -212,30 +228,14 @@ class GNProvider extends abstract_provider_1.Provider {
212
228
  throw new Error(`Error fetching transaction: ${error.message}`);
213
229
  }
214
230
  });
215
- }
216
- getFeePerKb() {
217
- return __awaiter(this, void 0, void 0, function* () {
218
- yield this._ready();
219
- const headers = this._getHeaders();
220
- try {
221
- const now = Math.floor(Date.now() / 1000);
222
- const from = now - 1800; // 30 minutos atrás
223
- const res = yield superagent.get(`${this.apiPrefix}/miner/fees?from=${from}&to=${now}`)
224
- .set(headers);
225
- if (res.body && Array.isArray(res.body) && res.body.length > 0) {
226
- const totalFeeRate = res.body.reduce((sum, minerData) => {
227
- return sum + minerData.min_fee_rate;
228
- }, 0);
229
- const averageFeeRate = totalFeeRate / res.body.length;
230
- const feeRateWithMargin = averageFeeRate * 1.3;
231
- return Math.round(feeRateWithMargin * 100) / 100;
232
- }
233
- throw new Error("No fee data available");
234
- }
235
- catch (error) {
236
- return 1.05; // Valor de fallback
237
- }
238
- });
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);
239
239
  }
240
240
  needIgnoreError(inMsg) {
241
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.1",
3
+ "version": "1.1.3",
4
4
  "files": [
5
5
  "dist",
6
6
  "scripts",