@locuschain/lib 0.0.7 → 0.0.9
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/dist/errors/index.amd.js +22 -0
- package/dist/errors/index.cjs.js +20 -0
- package/dist/errors/index.d.mts +1 -0
- package/dist/errors/index.esm.js +18 -0
- package/dist/errors/rpc-error.d.mts +13 -0
- package/dist/support/index.amd.js +1 -1
- package/dist/support/index.cjs.js +1 -1
- package/dist/support/index.esm.js +1 -1
- package/dist/web3/index.amd.js +41 -22
- package/dist/web3/index.cjs.js +41 -22
- package/dist/web3/index.d.cts +4 -4
- package/dist/web3/index.d.mts +4 -4
- package/dist/web3/index.esm.js +34 -15
- package/dist/web3/providers/http-rpc-provider.d.mts +2 -3
- package/dist/web3/providers/rpc-provider.d.mts +5 -5
- package/package.json +9 -1
- package/dist/rpc-error-8sxpHnEq.js +0 -18
- package/dist/rpc-error-CIW1Ier-.js +0 -22
- package/dist/rpc-error-DxFStPj8.js +0 -20
- package/dist/web3/rpc-error.d.mts +0 -13
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
define(['exports'], (function (exports) { 'use strict';
|
|
2
|
+
|
|
3
|
+
exports.RpcErrorCode = void 0;
|
|
4
|
+
(function (RpcErrorCode) {
|
|
5
|
+
RpcErrorCode["ERROR"] = "LOCUSLIB_ERROR";
|
|
6
|
+
RpcErrorCode["RUNTIME_ERROR"] = "LOCUSLIB_RUNTIME_ERROR";
|
|
7
|
+
RpcErrorCode["NET_ERROR"] = "LOCUSLIB_NET_ERROR";
|
|
8
|
+
RpcErrorCode["TIMEOUT"] = "LOCUSLIB_TIMEOUT";
|
|
9
|
+
RpcErrorCode["ABORTED"] = "LOCUSLIB_ABORTED";
|
|
10
|
+
RpcErrorCode["REQUIRE_ACCOUNT"] = "LOCUSLIB_REQUIRE_ACCOUNT";
|
|
11
|
+
})(exports.RpcErrorCode || (exports.RpcErrorCode = {}));
|
|
12
|
+
class RpcError extends Error {
|
|
13
|
+
constructor(code, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.message = message;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
exports.RpcError = RpcError;
|
|
21
|
+
|
|
22
|
+
}));
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
exports.RpcErrorCode = void 0;
|
|
4
|
+
(function (RpcErrorCode) {
|
|
5
|
+
RpcErrorCode["ERROR"] = "LOCUSLIB_ERROR";
|
|
6
|
+
RpcErrorCode["RUNTIME_ERROR"] = "LOCUSLIB_RUNTIME_ERROR";
|
|
7
|
+
RpcErrorCode["NET_ERROR"] = "LOCUSLIB_NET_ERROR";
|
|
8
|
+
RpcErrorCode["TIMEOUT"] = "LOCUSLIB_TIMEOUT";
|
|
9
|
+
RpcErrorCode["ABORTED"] = "LOCUSLIB_ABORTED";
|
|
10
|
+
RpcErrorCode["REQUIRE_ACCOUNT"] = "LOCUSLIB_REQUIRE_ACCOUNT";
|
|
11
|
+
})(exports.RpcErrorCode || (exports.RpcErrorCode = {}));
|
|
12
|
+
class RpcError extends Error {
|
|
13
|
+
constructor(code, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.code = code;
|
|
16
|
+
this.message = message;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
exports.RpcError = RpcError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './rpc-error.mts';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var RpcErrorCode;
|
|
2
|
+
(function (RpcErrorCode) {
|
|
3
|
+
RpcErrorCode["ERROR"] = "LOCUSLIB_ERROR";
|
|
4
|
+
RpcErrorCode["RUNTIME_ERROR"] = "LOCUSLIB_RUNTIME_ERROR";
|
|
5
|
+
RpcErrorCode["NET_ERROR"] = "LOCUSLIB_NET_ERROR";
|
|
6
|
+
RpcErrorCode["TIMEOUT"] = "LOCUSLIB_TIMEOUT";
|
|
7
|
+
RpcErrorCode["ABORTED"] = "LOCUSLIB_ABORTED";
|
|
8
|
+
RpcErrorCode["REQUIRE_ACCOUNT"] = "LOCUSLIB_REQUIRE_ACCOUNT";
|
|
9
|
+
})(RpcErrorCode || (RpcErrorCode = {}));
|
|
10
|
+
class RpcError extends Error {
|
|
11
|
+
constructor(code, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.message = message;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { RpcError, RpcErrorCode };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare enum RpcErrorCode {
|
|
2
|
+
ERROR = "LOCUSLIB_ERROR",
|
|
3
|
+
RUNTIME_ERROR = "LOCUSLIB_RUNTIME_ERROR",
|
|
4
|
+
NET_ERROR = "LOCUSLIB_NET_ERROR",
|
|
5
|
+
TIMEOUT = "LOCUSLIB_TIMEOUT",
|
|
6
|
+
ABORTED = "LOCUSLIB_ABORTED",
|
|
7
|
+
REQUIRE_ACCOUNT = "LOCUSLIB_REQUIRE_ACCOUNT"
|
|
8
|
+
}
|
|
9
|
+
export declare class RpcError extends Error {
|
|
10
|
+
code: string;
|
|
11
|
+
message: string;
|
|
12
|
+
constructor(code: string, message: string);
|
|
13
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
define(['exports', '../index-BkQ38g4T', '../
|
|
1
|
+
define(['exports', '../index-BkQ38g4T', '../errors/index.amd', 'lodash', '../address-class-Dp7aNQb2'], (function (exports, utils$2, errors, _, addressClass) { 'use strict';
|
|
2
2
|
|
|
3
3
|
function _interopNamespaceDefault(e) {
|
|
4
4
|
var n = Object.create(null);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { c as convertHexToData, a as convertDataToHex, b as convertHexToAddress, d as convertAddressToHex, _ as __awaiter } from '../index-CB-t6xed.js';
|
|
2
|
-
import '../
|
|
2
|
+
import '../errors/index.esm.js';
|
|
3
3
|
import * as _ from 'lodash';
|
|
4
4
|
import '../address-class-CHAZYhdZ.js';
|
|
5
5
|
|
package/dist/web3/index.amd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../
|
|
1
|
+
define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../errors/index.amd', '../address-class-Dp7aNQb2', 'lodash'], (function (exports, utils, txType, errors, addressClass, _) { 'use strict';
|
|
2
2
|
|
|
3
3
|
class Account {
|
|
4
4
|
constructor({ address, nsk }) {
|
|
@@ -84,7 +84,6 @@ define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../rpc-error-CIW
|
|
|
84
84
|
constructor() {
|
|
85
85
|
super(...arguments);
|
|
86
86
|
this.id = 0;
|
|
87
|
-
this.canceledLastCallIndex = -1;
|
|
88
87
|
}
|
|
89
88
|
setAccountProvider(accountProvider) {
|
|
90
89
|
this.accountProvider = accountProvider;
|
|
@@ -97,43 +96,64 @@ define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../rpc-error-CIW
|
|
|
97
96
|
this.rpcOptions = Object.assign(Object.assign({}, this.rpcOptions), { abortController: new AbortController() });
|
|
98
97
|
return true;
|
|
99
98
|
}
|
|
99
|
+
getSignal(rpcOption) {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
const controller = (_a = rpcOption === null || rpcOption === void 0 ? void 0 : rpcOption.abortController) !== null && _a !== void 0 ? _a : (_b = this.rpcOptions) === null || _b === void 0 ? void 0 : _b.abortController;
|
|
102
|
+
const signals = [AbortSignal.timeout(30000)];
|
|
103
|
+
if (controller) {
|
|
104
|
+
signals.push(controller.signal);
|
|
105
|
+
}
|
|
106
|
+
return AbortSignal.any(signals);
|
|
107
|
+
}
|
|
100
108
|
request(cmd, params, rpcOption) {
|
|
101
109
|
return utils.__awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
if (this.getSignal().aborted) {
|
|
111
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
112
|
+
}
|
|
113
|
+
if (this.id === Number.MAX_SAFE_INTEGER) {
|
|
114
|
+
this.id = 0;
|
|
106
115
|
}
|
|
107
|
-
const id = ++this.id;
|
|
108
116
|
const data = {
|
|
109
|
-
id,
|
|
117
|
+
id: ++this.id,
|
|
110
118
|
jsonrpc: '2.0',
|
|
111
119
|
method: cmd,
|
|
112
120
|
params
|
|
113
121
|
};
|
|
114
122
|
try {
|
|
115
|
-
const
|
|
116
|
-
|
|
123
|
+
const options = Object.assign(Object.assign({}, this.rpcOptions), rpcOption);
|
|
124
|
+
const callRpc1Signal = this.getSignal();
|
|
125
|
+
const rpc1Result = yield this.callRpc(data, { signal: callRpc1Signal, headersInit: options.headersInit });
|
|
126
|
+
if (callRpc1Signal.aborted) {
|
|
127
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
128
|
+
}
|
|
129
|
+
if (!utils.TX_CREATION_RPC_LIST.includes(cmd)) {
|
|
117
130
|
return rpc1Result;
|
|
131
|
+
}
|
|
118
132
|
if (!this.accountProvider)
|
|
119
|
-
throw new
|
|
133
|
+
throw new errors.RpcError(errors.RpcErrorCode.REQUIRE_ACCOUNT, 'No accounts found. Please add an account before tx creation.');
|
|
120
134
|
// rpc1Result가 T(제네릭)이므로 'in' 연산자 사용을 위해 any로 캐스팅하여 체크합니다.
|
|
121
135
|
const txResult = rpc1Result;
|
|
122
136
|
if (!txResult || typeof txResult !== 'object' || !('hash' in txResult) || !('tx' in txResult)) {
|
|
123
137
|
// console.warn('rpcResul1>>', txResult)
|
|
124
|
-
throw new
|
|
138
|
+
throw new errors.RpcError(errors.RpcErrorCode.REQUIRE_ACCOUNT, 'Invalid transaction creation result.');
|
|
125
139
|
}
|
|
126
140
|
// console.debug('rpcResul1>>', txResult)
|
|
127
|
-
const additionalParams = yield this.accountProvider.signTx(txResult, params,
|
|
128
|
-
const newData = Object.assign(Object.assign({}, data), { params: params.map((p, idx) => (idx === 0 ? Object.assign(Object.assign({}, p), additionalParams) : p)) });
|
|
129
|
-
const
|
|
141
|
+
const additionalParams = yield this.accountProvider.signTx(txResult, params, options.address);
|
|
142
|
+
const newData = Object.assign(Object.assign({}, data), { id: ++this.id, params: params.map((p, idx) => (idx === 0 ? Object.assign(Object.assign({}, p), additionalParams) : p)) });
|
|
143
|
+
const callRpc2Signal = this.getSignal();
|
|
144
|
+
const rpcResul2 = yield this.callRpc(newData, { signal: callRpc2Signal, headersInit: options.headersInit });
|
|
145
|
+
if (callRpc2Signal.aborted) {
|
|
146
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
147
|
+
}
|
|
148
|
+
return rpcResul2;
|
|
130
149
|
// console.debug('rpcResul2', rpcResult2)
|
|
131
|
-
return rpcResult2;
|
|
132
150
|
}
|
|
133
151
|
catch (err) {
|
|
152
|
+
if (err.code === errors.RpcErrorCode.ABORTED)
|
|
153
|
+
throw err;
|
|
154
|
+
throw new errors.RpcError(errors.RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
134
155
|
// console.trace(err)
|
|
135
156
|
// console.log(err.stack);
|
|
136
|
-
throw new rpcError.RpcError(rpcError.RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
137
157
|
}
|
|
138
158
|
});
|
|
139
159
|
}
|
|
@@ -143,7 +163,6 @@ define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../rpc-error-CIW
|
|
|
143
163
|
constructor(rpcUrl, rpcOptions) {
|
|
144
164
|
super();
|
|
145
165
|
this.id = 0;
|
|
146
|
-
this.canceledLastCallIndex = -1;
|
|
147
166
|
this.rpcUrl = rpcUrl;
|
|
148
167
|
this.rpcOptions = Object.assign({ abortController: new AbortController() }, rpcOptions);
|
|
149
168
|
}
|
|
@@ -152,7 +171,7 @@ define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../rpc-error-CIW
|
|
|
152
171
|
// console.debug('params', data)
|
|
153
172
|
var _a;
|
|
154
173
|
if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
155
|
-
throw new
|
|
174
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
156
175
|
}
|
|
157
176
|
try {
|
|
158
177
|
const res = yield fetch(this.rpcUrl, {
|
|
@@ -162,16 +181,16 @@ define(['exports', '../index-BkQ38g4T', '../tx-type-BeeK7Ci3', '../rpc-error-CIW
|
|
|
162
181
|
signal: options === null || options === void 0 ? void 0 : options.signal
|
|
163
182
|
});
|
|
164
183
|
if (!res.ok)
|
|
165
|
-
throw new
|
|
184
|
+
throw new errors.RpcError(errors.RpcErrorCode.NET_ERROR, `node response http status : ${res.status}`);
|
|
166
185
|
const response = yield res.json();
|
|
167
186
|
if (response.error) {
|
|
168
187
|
const { code, message } = response.error;
|
|
169
|
-
throw new
|
|
188
|
+
throw new errors.RpcError(message === 'timeout error' ? errors.RpcErrorCode.TIMEOUT : code, message);
|
|
170
189
|
}
|
|
171
190
|
return response.result;
|
|
172
191
|
}
|
|
173
192
|
catch (err) {
|
|
174
|
-
throw new
|
|
193
|
+
throw new errors.RpcError(err.name === 'TimeoutError' ? errors.RpcErrorCode.TIMEOUT : errors.RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
175
194
|
}
|
|
176
195
|
});
|
|
177
196
|
}
|
package/dist/web3/index.cjs.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var utils = require('../index-BJsuMvb_.js');
|
|
4
4
|
var txType = require('../tx-type-jR5Owltc.js');
|
|
5
|
-
var
|
|
5
|
+
var errors = require('../errors/index.cjs.js');
|
|
6
6
|
require('../address-class-Cjy48Uqk.js');
|
|
7
7
|
require('lodash');
|
|
8
8
|
|
|
@@ -90,7 +90,6 @@ class RpcProvider extends utils.Rpc {
|
|
|
90
90
|
constructor() {
|
|
91
91
|
super(...arguments);
|
|
92
92
|
this.id = 0;
|
|
93
|
-
this.canceledLastCallIndex = -1;
|
|
94
93
|
}
|
|
95
94
|
setAccountProvider(accountProvider) {
|
|
96
95
|
this.accountProvider = accountProvider;
|
|
@@ -103,43 +102,64 @@ class RpcProvider extends utils.Rpc {
|
|
|
103
102
|
this.rpcOptions = Object.assign(Object.assign({}, this.rpcOptions), { abortController: new AbortController() });
|
|
104
103
|
return true;
|
|
105
104
|
}
|
|
105
|
+
getSignal(rpcOption) {
|
|
106
|
+
var _a, _b;
|
|
107
|
+
const controller = (_a = rpcOption === null || rpcOption === void 0 ? void 0 : rpcOption.abortController) !== null && _a !== void 0 ? _a : (_b = this.rpcOptions) === null || _b === void 0 ? void 0 : _b.abortController;
|
|
108
|
+
const signals = [AbortSignal.timeout(30000)];
|
|
109
|
+
if (controller) {
|
|
110
|
+
signals.push(controller.signal);
|
|
111
|
+
}
|
|
112
|
+
return AbortSignal.any(signals);
|
|
113
|
+
}
|
|
106
114
|
request(cmd, params, rpcOption) {
|
|
107
115
|
return utils.__awaiter(this, void 0, void 0, function* () {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
116
|
+
if (this.getSignal().aborted) {
|
|
117
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
118
|
+
}
|
|
119
|
+
if (this.id === Number.MAX_SAFE_INTEGER) {
|
|
120
|
+
this.id = 0;
|
|
112
121
|
}
|
|
113
|
-
const id = ++this.id;
|
|
114
122
|
const data = {
|
|
115
|
-
id,
|
|
123
|
+
id: ++this.id,
|
|
116
124
|
jsonrpc: '2.0',
|
|
117
125
|
method: cmd,
|
|
118
126
|
params
|
|
119
127
|
};
|
|
120
128
|
try {
|
|
121
|
-
const
|
|
122
|
-
|
|
129
|
+
const options = Object.assign(Object.assign({}, this.rpcOptions), rpcOption);
|
|
130
|
+
const callRpc1Signal = this.getSignal();
|
|
131
|
+
const rpc1Result = yield this.callRpc(data, { signal: callRpc1Signal, headersInit: options.headersInit });
|
|
132
|
+
if (callRpc1Signal.aborted) {
|
|
133
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
134
|
+
}
|
|
135
|
+
if (!utils.TX_CREATION_RPC_LIST.includes(cmd)) {
|
|
123
136
|
return rpc1Result;
|
|
137
|
+
}
|
|
124
138
|
if (!this.accountProvider)
|
|
125
|
-
throw new
|
|
139
|
+
throw new errors.RpcError(errors.RpcErrorCode.REQUIRE_ACCOUNT, 'No accounts found. Please add an account before tx creation.');
|
|
126
140
|
// rpc1Result가 T(제네릭)이므로 'in' 연산자 사용을 위해 any로 캐스팅하여 체크합니다.
|
|
127
141
|
const txResult = rpc1Result;
|
|
128
142
|
if (!txResult || typeof txResult !== 'object' || !('hash' in txResult) || !('tx' in txResult)) {
|
|
129
143
|
// console.warn('rpcResul1>>', txResult)
|
|
130
|
-
throw new
|
|
144
|
+
throw new errors.RpcError(errors.RpcErrorCode.REQUIRE_ACCOUNT, 'Invalid transaction creation result.');
|
|
131
145
|
}
|
|
132
146
|
// console.debug('rpcResul1>>', txResult)
|
|
133
|
-
const additionalParams = yield this.accountProvider.signTx(txResult, params,
|
|
134
|
-
const newData = Object.assign(Object.assign({}, data), { params: params.map((p, idx) => (idx === 0 ? Object.assign(Object.assign({}, p), additionalParams) : p)) });
|
|
135
|
-
const
|
|
147
|
+
const additionalParams = yield this.accountProvider.signTx(txResult, params, options.address);
|
|
148
|
+
const newData = Object.assign(Object.assign({}, data), { id: ++this.id, params: params.map((p, idx) => (idx === 0 ? Object.assign(Object.assign({}, p), additionalParams) : p)) });
|
|
149
|
+
const callRpc2Signal = this.getSignal();
|
|
150
|
+
const rpcResul2 = yield this.callRpc(newData, { signal: callRpc2Signal, headersInit: options.headersInit });
|
|
151
|
+
if (callRpc2Signal.aborted) {
|
|
152
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
153
|
+
}
|
|
154
|
+
return rpcResul2;
|
|
136
155
|
// console.debug('rpcResul2', rpcResult2)
|
|
137
|
-
return rpcResult2;
|
|
138
156
|
}
|
|
139
157
|
catch (err) {
|
|
158
|
+
if (err.code === errors.RpcErrorCode.ABORTED)
|
|
159
|
+
throw err;
|
|
160
|
+
throw new errors.RpcError(errors.RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
140
161
|
// console.trace(err)
|
|
141
162
|
// console.log(err.stack);
|
|
142
|
-
throw new rpcError.RpcError(rpcError.RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
143
163
|
}
|
|
144
164
|
});
|
|
145
165
|
}
|
|
@@ -149,7 +169,6 @@ class HttpRpcProvider extends RpcProvider {
|
|
|
149
169
|
constructor(rpcUrl, rpcOptions) {
|
|
150
170
|
super();
|
|
151
171
|
this.id = 0;
|
|
152
|
-
this.canceledLastCallIndex = -1;
|
|
153
172
|
this.rpcUrl = rpcUrl;
|
|
154
173
|
this.rpcOptions = Object.assign({ abortController: new AbortController() }, rpcOptions);
|
|
155
174
|
}
|
|
@@ -158,7 +177,7 @@ class HttpRpcProvider extends RpcProvider {
|
|
|
158
177
|
// console.debug('params', data)
|
|
159
178
|
var _a;
|
|
160
179
|
if ((_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
161
|
-
throw new
|
|
180
|
+
throw new errors.RpcError(errors.RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
162
181
|
}
|
|
163
182
|
try {
|
|
164
183
|
const res = yield fetch(this.rpcUrl, {
|
|
@@ -168,16 +187,16 @@ class HttpRpcProvider extends RpcProvider {
|
|
|
168
187
|
signal: options === null || options === void 0 ? void 0 : options.signal
|
|
169
188
|
});
|
|
170
189
|
if (!res.ok)
|
|
171
|
-
throw new
|
|
190
|
+
throw new errors.RpcError(errors.RpcErrorCode.NET_ERROR, `node response http status : ${res.status}`);
|
|
172
191
|
const response = yield res.json();
|
|
173
192
|
if (response.error) {
|
|
174
193
|
const { code, message } = response.error;
|
|
175
|
-
throw new
|
|
194
|
+
throw new errors.RpcError(message === 'timeout error' ? errors.RpcErrorCode.TIMEOUT : code, message);
|
|
176
195
|
}
|
|
177
196
|
return response.result;
|
|
178
197
|
}
|
|
179
198
|
catch (err) {
|
|
180
|
-
throw new
|
|
199
|
+
throw new errors.RpcError(err.name === 'TimeoutError' ? errors.RpcErrorCode.TIMEOUT : errors.RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
181
200
|
}
|
|
182
201
|
});
|
|
183
202
|
}
|
package/dist/web3/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AccountProvider } from
|
|
2
|
-
import { HttpRpcProvider } from
|
|
3
|
-
import { RpcProvider } from
|
|
4
|
-
import { RpcOptions } from
|
|
1
|
+
import { AccountProvider } from './providers/account-provider.mts';
|
|
2
|
+
import { HttpRpcProvider } from './providers/http-rpc-provider.mts';
|
|
3
|
+
import { RpcProvider } from './providers/rpc-provider.mts';
|
|
4
|
+
import { RpcOptions } from './types/index.mts';
|
|
5
5
|
export declare class Web3 {
|
|
6
6
|
private accountProvider?;
|
|
7
7
|
private rpcProvider?;
|
package/dist/web3/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AccountProvider } from
|
|
2
|
-
import { HttpRpcProvider } from
|
|
3
|
-
import { RpcProvider } from
|
|
4
|
-
import { RpcOptions } from
|
|
1
|
+
import { AccountProvider } from './providers/account-provider.mts';
|
|
2
|
+
import { HttpRpcProvider } from './providers/http-rpc-provider.mts';
|
|
3
|
+
import { RpcProvider } from './providers/rpc-provider.mts';
|
|
4
|
+
import { RpcOptions } from './types/index.mts';
|
|
5
5
|
export declare class Web3 {
|
|
6
6
|
private accountProvider?;
|
|
7
7
|
private rpcProvider?;
|
package/dist/web3/index.esm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { s as sign, _ as __awaiter, v as verifyTxParams, R as Rpc, T as TX_CREATION_RPC_LIST } from '../index-CB-t6xed.js';
|
|
2
2
|
import { T as TxType } from '../tx-type-nyLU5xv5.js';
|
|
3
|
-
import {
|
|
3
|
+
import { RpcError, RpcErrorCode } from '../errors/index.esm.js';
|
|
4
4
|
import '../address-class-CHAZYhdZ.js';
|
|
5
5
|
import 'lodash';
|
|
6
6
|
|
|
@@ -88,7 +88,6 @@ class RpcProvider extends Rpc {
|
|
|
88
88
|
constructor() {
|
|
89
89
|
super(...arguments);
|
|
90
90
|
this.id = 0;
|
|
91
|
-
this.canceledLastCallIndex = -1;
|
|
92
91
|
}
|
|
93
92
|
setAccountProvider(accountProvider) {
|
|
94
93
|
this.accountProvider = accountProvider;
|
|
@@ -101,24 +100,39 @@ class RpcProvider extends Rpc {
|
|
|
101
100
|
this.rpcOptions = Object.assign(Object.assign({}, this.rpcOptions), { abortController: new AbortController() });
|
|
102
101
|
return true;
|
|
103
102
|
}
|
|
103
|
+
getSignal(rpcOption) {
|
|
104
|
+
var _a, _b;
|
|
105
|
+
const controller = (_a = rpcOption === null || rpcOption === void 0 ? void 0 : rpcOption.abortController) !== null && _a !== void 0 ? _a : (_b = this.rpcOptions) === null || _b === void 0 ? void 0 : _b.abortController;
|
|
106
|
+
const signals = [AbortSignal.timeout(30000)];
|
|
107
|
+
if (controller) {
|
|
108
|
+
signals.push(controller.signal);
|
|
109
|
+
}
|
|
110
|
+
return AbortSignal.any(signals);
|
|
111
|
+
}
|
|
104
112
|
request(cmd, params, rpcOption) {
|
|
105
113
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
-
|
|
107
|
-
const signal = AbortSignal.any(callRpcOptions.abortController ? [callRpcOptions.abortController.signal, AbortSignal.timeout(30000)] : [AbortSignal.timeout(30000)]);
|
|
108
|
-
if (signal.aborted) {
|
|
114
|
+
if (this.getSignal().aborted) {
|
|
109
115
|
throw new RpcError(RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
110
116
|
}
|
|
111
|
-
|
|
117
|
+
if (this.id === Number.MAX_SAFE_INTEGER) {
|
|
118
|
+
this.id = 0;
|
|
119
|
+
}
|
|
112
120
|
const data = {
|
|
113
|
-
id,
|
|
121
|
+
id: ++this.id,
|
|
114
122
|
jsonrpc: '2.0',
|
|
115
123
|
method: cmd,
|
|
116
124
|
params
|
|
117
125
|
};
|
|
118
126
|
try {
|
|
119
|
-
const
|
|
120
|
-
|
|
127
|
+
const options = Object.assign(Object.assign({}, this.rpcOptions), rpcOption);
|
|
128
|
+
const callRpc1Signal = this.getSignal();
|
|
129
|
+
const rpc1Result = yield this.callRpc(data, { signal: callRpc1Signal, headersInit: options.headersInit });
|
|
130
|
+
if (callRpc1Signal.aborted) {
|
|
131
|
+
throw new RpcError(RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
132
|
+
}
|
|
133
|
+
if (!TX_CREATION_RPC_LIST.includes(cmd)) {
|
|
121
134
|
return rpc1Result;
|
|
135
|
+
}
|
|
122
136
|
if (!this.accountProvider)
|
|
123
137
|
throw new RpcError(RpcErrorCode.REQUIRE_ACCOUNT, 'No accounts found. Please add an account before tx creation.');
|
|
124
138
|
// rpc1Result가 T(제네릭)이므로 'in' 연산자 사용을 위해 any로 캐스팅하여 체크합니다.
|
|
@@ -128,16 +142,22 @@ class RpcProvider extends Rpc {
|
|
|
128
142
|
throw new RpcError(RpcErrorCode.REQUIRE_ACCOUNT, 'Invalid transaction creation result.');
|
|
129
143
|
}
|
|
130
144
|
// console.debug('rpcResul1>>', txResult)
|
|
131
|
-
const additionalParams = yield this.accountProvider.signTx(txResult, params,
|
|
132
|
-
const newData = Object.assign(Object.assign({}, data), { params: params.map((p, idx) => (idx === 0 ? Object.assign(Object.assign({}, p), additionalParams) : p)) });
|
|
133
|
-
const
|
|
145
|
+
const additionalParams = yield this.accountProvider.signTx(txResult, params, options.address);
|
|
146
|
+
const newData = Object.assign(Object.assign({}, data), { id: ++this.id, params: params.map((p, idx) => (idx === 0 ? Object.assign(Object.assign({}, p), additionalParams) : p)) });
|
|
147
|
+
const callRpc2Signal = this.getSignal();
|
|
148
|
+
const rpcResul2 = yield this.callRpc(newData, { signal: callRpc2Signal, headersInit: options.headersInit });
|
|
149
|
+
if (callRpc2Signal.aborted) {
|
|
150
|
+
throw new RpcError(RpcErrorCode.ABORTED, `rpc aborted by user`);
|
|
151
|
+
}
|
|
152
|
+
return rpcResul2;
|
|
134
153
|
// console.debug('rpcResul2', rpcResult2)
|
|
135
|
-
return rpcResult2;
|
|
136
154
|
}
|
|
137
155
|
catch (err) {
|
|
156
|
+
if (err.code === RpcErrorCode.ABORTED)
|
|
157
|
+
throw err;
|
|
158
|
+
throw new RpcError(RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
138
159
|
// console.trace(err)
|
|
139
160
|
// console.log(err.stack);
|
|
140
|
-
throw new RpcError(RpcErrorCode.RUNTIME_ERROR, err.message);
|
|
141
161
|
}
|
|
142
162
|
});
|
|
143
163
|
}
|
|
@@ -147,7 +167,6 @@ class HttpRpcProvider extends RpcProvider {
|
|
|
147
167
|
constructor(rpcUrl, rpcOptions) {
|
|
148
168
|
super();
|
|
149
169
|
this.id = 0;
|
|
150
|
-
this.canceledLastCallIndex = -1;
|
|
151
170
|
this.rpcUrl = rpcUrl;
|
|
152
171
|
this.rpcOptions = Object.assign({ abortController: new AbortController() }, rpcOptions);
|
|
153
172
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { CallRpcOptions, RpcOptions } from
|
|
2
|
-
import { RpcProvider } from
|
|
1
|
+
import { CallRpcOptions, RpcOptions } from '../types/index.mts';
|
|
2
|
+
import { RpcProvider } from './rpc-provider.mts';
|
|
3
3
|
export declare class HttpRpcProvider extends RpcProvider {
|
|
4
4
|
id: number;
|
|
5
5
|
rpcUrl: string;
|
|
6
6
|
rpcOptions?: RpcOptions;
|
|
7
|
-
canceledLastCallIndex: number;
|
|
8
7
|
constructor(rpcUrl: string, rpcOptions?: RpcOptions);
|
|
9
8
|
callRpc<T>(data: any, options?: CallRpcOptions): Promise<T>;
|
|
10
9
|
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { RpcApiSpec } from
|
|
2
|
-
import { Rpc } from
|
|
3
|
-
import { CallRpcOptions, RpcOptions, Web3APIMethod, Web3APIParams } from
|
|
4
|
-
import { AccountProvider } from
|
|
1
|
+
import { RpcApiSpec } from '../../autogen/rpc-api-spec.mts';
|
|
2
|
+
import { Rpc } from '../../autogen/rpc.mts';
|
|
3
|
+
import { CallRpcOptions, RpcOptions, Web3APIMethod, Web3APIParams } from '../types/index.mts';
|
|
4
|
+
import { AccountProvider } from './account-provider.mts';
|
|
5
5
|
export declare abstract class RpcProvider extends Rpc {
|
|
6
6
|
id: number;
|
|
7
7
|
rpcOptions?: RpcOptions | undefined;
|
|
8
|
-
canceledLastCallIndex: number;
|
|
9
8
|
accountProvider?: AccountProvider;
|
|
10
9
|
abstract callRpc<T>(data: any, options?: CallRpcOptions): Promise<T>;
|
|
11
10
|
abstract get rpcUrl(): string;
|
|
12
11
|
setAccountProvider(accountProvider: AccountProvider): void;
|
|
13
12
|
abort(): boolean;
|
|
13
|
+
private getSignal;
|
|
14
14
|
request<T, M extends Web3APIMethod<RpcApiSpec> = any>(cmd: M, params: Web3APIParams<RpcApiSpec, M>, rpcOption?: RpcOptions): Promise<T>;
|
|
15
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@locuschain/lib",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"author": "bloomtechnology",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "",
|
|
@@ -52,6 +52,14 @@
|
|
|
52
52
|
},
|
|
53
53
|
"import": "./dist/support/index.esm.js",
|
|
54
54
|
"require": "./dist/support/index.cjs.js"
|
|
55
|
+
},
|
|
56
|
+
"./errors": {
|
|
57
|
+
"types": {
|
|
58
|
+
"require": "./dist/errors/index.d.cts",
|
|
59
|
+
"import": "./dist/errors/index.d.mts"
|
|
60
|
+
},
|
|
61
|
+
"import": "./dist/errors/index.esm.js",
|
|
62
|
+
"require": "./dist/errors/index.cjs.js"
|
|
55
63
|
}
|
|
56
64
|
},
|
|
57
65
|
"files": [
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
var RpcErrorCode;
|
|
2
|
-
(function (RpcErrorCode) {
|
|
3
|
-
RpcErrorCode[RpcErrorCode["ERROR"] = -32000] = "ERROR";
|
|
4
|
-
RpcErrorCode[RpcErrorCode["RUNTIME_ERROR"] = -31999] = "RUNTIME_ERROR";
|
|
5
|
-
RpcErrorCode[RpcErrorCode["NET_ERROR"] = -31998] = "NET_ERROR";
|
|
6
|
-
RpcErrorCode[RpcErrorCode["TIMEOUT"] = -31997] = "TIMEOUT";
|
|
7
|
-
RpcErrorCode[RpcErrorCode["ABORTED"] = -31996] = "ABORTED";
|
|
8
|
-
RpcErrorCode[RpcErrorCode["REQUIRE_ACCOUNT"] = -31995] = "REQUIRE_ACCOUNT";
|
|
9
|
-
})(RpcErrorCode || (RpcErrorCode = {}));
|
|
10
|
-
class RpcError extends Error {
|
|
11
|
-
constructor(code, message) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.code = code;
|
|
14
|
-
this.message = message;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { RpcError as R, RpcErrorCode as a };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
define(['exports'], (function (exports) { 'use strict';
|
|
2
|
-
|
|
3
|
-
exports.RpcErrorCode = void 0;
|
|
4
|
-
(function (RpcErrorCode) {
|
|
5
|
-
RpcErrorCode[RpcErrorCode["ERROR"] = -32000] = "ERROR";
|
|
6
|
-
RpcErrorCode[RpcErrorCode["RUNTIME_ERROR"] = -31999] = "RUNTIME_ERROR";
|
|
7
|
-
RpcErrorCode[RpcErrorCode["NET_ERROR"] = -31998] = "NET_ERROR";
|
|
8
|
-
RpcErrorCode[RpcErrorCode["TIMEOUT"] = -31997] = "TIMEOUT";
|
|
9
|
-
RpcErrorCode[RpcErrorCode["ABORTED"] = -31996] = "ABORTED";
|
|
10
|
-
RpcErrorCode[RpcErrorCode["REQUIRE_ACCOUNT"] = -31995] = "REQUIRE_ACCOUNT";
|
|
11
|
-
})(exports.RpcErrorCode || (exports.RpcErrorCode = {}));
|
|
12
|
-
class RpcError extends Error {
|
|
13
|
-
constructor(code, message) {
|
|
14
|
-
super(message);
|
|
15
|
-
this.code = code;
|
|
16
|
-
this.message = message;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
exports.RpcError = RpcError;
|
|
21
|
-
|
|
22
|
-
}));
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
exports.RpcErrorCode = void 0;
|
|
4
|
-
(function (RpcErrorCode) {
|
|
5
|
-
RpcErrorCode[RpcErrorCode["ERROR"] = -32000] = "ERROR";
|
|
6
|
-
RpcErrorCode[RpcErrorCode["RUNTIME_ERROR"] = -31999] = "RUNTIME_ERROR";
|
|
7
|
-
RpcErrorCode[RpcErrorCode["NET_ERROR"] = -31998] = "NET_ERROR";
|
|
8
|
-
RpcErrorCode[RpcErrorCode["TIMEOUT"] = -31997] = "TIMEOUT";
|
|
9
|
-
RpcErrorCode[RpcErrorCode["ABORTED"] = -31996] = "ABORTED";
|
|
10
|
-
RpcErrorCode[RpcErrorCode["REQUIRE_ACCOUNT"] = -31995] = "REQUIRE_ACCOUNT";
|
|
11
|
-
})(exports.RpcErrorCode || (exports.RpcErrorCode = {}));
|
|
12
|
-
class RpcError extends Error {
|
|
13
|
-
constructor(code, message) {
|
|
14
|
-
super(message);
|
|
15
|
-
this.code = code;
|
|
16
|
-
this.message = message;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
exports.RpcError = RpcError;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare enum RpcErrorCode {
|
|
2
|
-
ERROR = -32000,
|
|
3
|
-
RUNTIME_ERROR = -31999,
|
|
4
|
-
NET_ERROR = -31998,
|
|
5
|
-
TIMEOUT = -31997,
|
|
6
|
-
ABORTED = -31996,
|
|
7
|
-
REQUIRE_ACCOUNT = -31995
|
|
8
|
-
}
|
|
9
|
-
export declare class RpcError extends Error {
|
|
10
|
-
code: number;
|
|
11
|
-
message: string;
|
|
12
|
-
constructor(code: number, message: string);
|
|
13
|
-
}
|