@leofcoin/chain 1.6.8 → 1.6.10
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/exports/browser/browser-store.js +14 -11
- package/exports/browser/chain.js +688 -686
- package/exports/chain.d.ts +3 -2
- package/exports/chain.js +29 -11
- package/exports/contract.d.ts +3 -3
- package/exports/machine.d.ts +1 -0
- package/exports/transaction.d.ts +1 -1
- package/package.json +1 -1
package/exports/chain.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import '@vandeurenglenn/debug';
|
|
2
|
+
import { BigNumber } from '@leofcoin/utils';
|
|
2
3
|
import { BigNumberish } from '@ethersproject/bignumber';
|
|
3
4
|
import { Address } from './types.js';
|
|
4
5
|
import { VersionControl } from './version-control.js';
|
|
@@ -20,14 +21,14 @@ export default class Chain extends VersionControl {
|
|
|
20
21
|
sendTransaction(transaction: any): Promise<{
|
|
21
22
|
hash: any;
|
|
22
23
|
data: any;
|
|
23
|
-
fee:
|
|
24
|
+
fee: string | number | BigNumber;
|
|
24
25
|
wait: Promise<unknown>;
|
|
25
26
|
message: any;
|
|
26
27
|
}>;
|
|
27
28
|
addContract(transaction: any, contractMessage: any): Promise<{
|
|
28
29
|
hash: any;
|
|
29
30
|
data: any;
|
|
30
|
-
fee:
|
|
31
|
+
fee: string | number | BigNumber;
|
|
31
32
|
wait: Promise<unknown>;
|
|
32
33
|
message: any;
|
|
33
34
|
}>;
|
package/exports/chain.js
CHANGED
|
@@ -10,11 +10,11 @@ import { ContractDeploymentError, ExecutionError, isResolveError, ResolveError,
|
|
|
10
10
|
|
|
11
11
|
const limit = 1800;
|
|
12
12
|
const transactionLimit = 1000;
|
|
13
|
-
const requestTimeout =
|
|
14
|
-
const syncTimeout =
|
|
13
|
+
const requestTimeout = 30_000;
|
|
14
|
+
const syncTimeout = 30_000;
|
|
15
15
|
class Protocol {
|
|
16
16
|
constructor(config) {
|
|
17
|
-
this.resolveTimeout =
|
|
17
|
+
this.resolveTimeout = 10_000;
|
|
18
18
|
if (config?.resolveTimeout)
|
|
19
19
|
this.resolveTimeout = config.resolveTimeout;
|
|
20
20
|
}
|
|
@@ -114,7 +114,13 @@ class Transaction extends Protocol {
|
|
|
114
114
|
* @returns {Number} nonce
|
|
115
115
|
*/
|
|
116
116
|
async getNonce(address) {
|
|
117
|
-
|
|
117
|
+
try {
|
|
118
|
+
if (!(await globalThis.accountsStore.has(address))) {
|
|
119
|
+
const nonce = await this.#getNonceFallback(address);
|
|
120
|
+
await globalThis.accountsStore.put(address, new TextEncoder().encode(String(nonce)));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
118
124
|
const nonce = await this.#getNonceFallback(address);
|
|
119
125
|
await globalThis.accountsStore.put(address, new TextEncoder().encode(String(nonce)));
|
|
120
126
|
}
|
|
@@ -132,8 +138,7 @@ class Transaction extends Protocol {
|
|
|
132
138
|
return Number(nonce);
|
|
133
139
|
}
|
|
134
140
|
async validateNonce(address, nonce) {
|
|
135
|
-
|
|
136
|
-
previousNonce = Number(new TextDecoder().decode(previousNonce));
|
|
141
|
+
const previousNonce = await this.getNonce(address);
|
|
137
142
|
if (previousNonce > nonce)
|
|
138
143
|
throw new Error(`a transaction with a higher nonce already exists`);
|
|
139
144
|
if (previousNonce === nonce)
|
|
@@ -191,7 +196,7 @@ class Transaction extends Protocol {
|
|
|
191
196
|
result.status === 'fulfilled' ? resolve(result.hash) : reject({ hash: result.hash, error: result.error });
|
|
192
197
|
setTimeout(async () => {
|
|
193
198
|
pubsub.unsubscribe(`transaction.completed.${hash}`, completed);
|
|
194
|
-
},
|
|
199
|
+
}, 10_000);
|
|
195
200
|
};
|
|
196
201
|
pubsub.subscribe(`transaction.completed.${hash}`, completed);
|
|
197
202
|
}
|
|
@@ -276,7 +281,8 @@ class Machine {
|
|
|
276
281
|
lastBlock: {
|
|
277
282
|
index: 0,
|
|
278
283
|
hash: ''
|
|
279
|
-
}
|
|
284
|
+
},
|
|
285
|
+
accounts: {}
|
|
280
286
|
};
|
|
281
287
|
// @ts-ignore
|
|
282
288
|
return this.#init(blocks);
|
|
@@ -366,8 +372,14 @@ class Machine {
|
|
|
366
372
|
const value = await this.#askWorker('get', { contract, method: 'state', params: [] });
|
|
367
373
|
state[contract] = value;
|
|
368
374
|
}));
|
|
369
|
-
await
|
|
370
|
-
|
|
375
|
+
const accounts = await Promise.all((await accountsStore.keys()).map((address) => accountsStore.get(address)));
|
|
376
|
+
const tasks = [
|
|
377
|
+
stateStore.put('lastBlock', JSON.stringify(await this.lastBlock)),
|
|
378
|
+
stateStore.put('states', JSON.stringify(state)),
|
|
379
|
+
stateStore.put('accounts', JSON.stringify(accounts))
|
|
380
|
+
// accountsStore.clear()
|
|
381
|
+
];
|
|
382
|
+
await Promise.all(tasks);
|
|
371
383
|
}
|
|
372
384
|
}
|
|
373
385
|
catch (error) {
|
|
@@ -401,6 +413,12 @@ class Machine {
|
|
|
401
413
|
if (await stateStore.has('lastBlock')) {
|
|
402
414
|
this.states.lastBlock = JSON.parse(new TextDecoder().decode(await stateStore.get('lastBlock')));
|
|
403
415
|
this.states.states = JSON.parse(new TextDecoder().decode(await stateStore.get('states')));
|
|
416
|
+
try {
|
|
417
|
+
this.states.accounts = JSON.parse(new TextDecoder().decode(await stateStore.get('accounts')));
|
|
418
|
+
}
|
|
419
|
+
catch {
|
|
420
|
+
this.states.accounts = {};
|
|
421
|
+
}
|
|
404
422
|
console.log({ balances: this.states.states[addresses.nativeToken].balances });
|
|
405
423
|
}
|
|
406
424
|
const message = {
|
|
@@ -1392,7 +1410,6 @@ class Chain extends VersionControl {
|
|
|
1392
1410
|
const lastBlock = await this.#makeRequest(peer, 'lastBlock');
|
|
1393
1411
|
const localBlock = await this.lastBlock;
|
|
1394
1412
|
const higherThenCurrentLocal = !localBlock.index ? true : lastBlock.index > localBlock.index;
|
|
1395
|
-
const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
|
|
1396
1413
|
if (Object.keys(lastBlock).length > 0) {
|
|
1397
1414
|
if (!this.lastBlock || higherThenCurrentLocal) {
|
|
1398
1415
|
this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
@@ -1401,6 +1418,7 @@ class Chain extends VersionControl {
|
|
|
1401
1418
|
else if (!this.knownBlocks)
|
|
1402
1419
|
this.knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
1403
1420
|
}
|
|
1421
|
+
const peerTransactionPool = (higherThenCurrentLocal && (await this.getPeerTransactionPool(peer))) || [];
|
|
1404
1422
|
if (this.#participating && peerTransactionPool.length > 0)
|
|
1405
1423
|
return this.#runEpoch();
|
|
1406
1424
|
}
|
package/exports/contract.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export default class Contract extends Transaction {
|
|
|
13
13
|
* @param {Array} constructorParameters
|
|
14
14
|
* @returns lib.createContractMessage
|
|
15
15
|
*/
|
|
16
|
-
createContractMessage(creator: any, contract: any, constructorParameters?: any[]): Promise<
|
|
16
|
+
createContractMessage(creator: any, contract: any, constructorParameters?: any[]): Promise<import("@leofcoin/messages").ContractMessage>;
|
|
17
17
|
/**
|
|
18
18
|
*
|
|
19
19
|
* @param {Address} creator
|
|
@@ -31,14 +31,14 @@ export default class Contract extends Transaction {
|
|
|
31
31
|
deployContract(signer: MultiWallet, contract: any, constructorParameters?: any[]): Promise<{
|
|
32
32
|
hash: any;
|
|
33
33
|
data: any;
|
|
34
|
-
fee:
|
|
34
|
+
fee: string | number | import("@ethersproject/bignumber").BigNumber;
|
|
35
35
|
wait: Promise<unknown>;
|
|
36
36
|
message: any;
|
|
37
37
|
}>;
|
|
38
38
|
deployContractMessage(signer: any, message: any): Promise<{
|
|
39
39
|
hash: any;
|
|
40
40
|
data: any;
|
|
41
|
-
fee:
|
|
41
|
+
fee: string | number | import("@ethersproject/bignumber").BigNumber;
|
|
42
42
|
wait: Promise<unknown>;
|
|
43
43
|
message: any;
|
|
44
44
|
}>;
|
package/exports/machine.d.ts
CHANGED
package/exports/transaction.d.ts
CHANGED