Skip to content
This repository was archived by the owner on May 26, 2023. It is now read-only.

Commit e4386e5

Browse files
authored
Merge pull request #244 from Zilliqa/feat/pendingtxn
feat/pendingtxn
2 parents f4cbd7b + b52d3b8 commit e4386e5

File tree

6 files changed

+74
-24
lines changed

6 files changed

+74
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dist
77
includes
88
tsconfig.tsbuildinfo
99
.env
10+
.idea/
1011
coverage
1112
devTestOnly/
1213
@zilliqa-js*

packages/zilliqa-js-blockchain/src/chain.ts

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
DsBlockObj,
2626
GET_TX_ATTEMPTS,
2727
PendingTxnResult,
28+
TransactionStatus,
2829
PendingTxns,
2930
Provider,
3031
RPCMethod,
@@ -43,6 +44,30 @@ import { toTxParams } from './util';
4344
export class Blockchain implements ZilliqaModule {
4445
signer: Wallet;
4546
provider: Provider;
47+
pendingErrorMap: { [key: number]: string } = {
48+
0: 'Txn was already processed and confirmed',
49+
1: 'Pending - nonce too high',
50+
2: 'Pending - blk gas limit exceeded',
51+
3: 'Pending - consensus failure',
52+
4: 'Error - txn not found',
53+
10: 'Dropped - math error',
54+
11: 'Dropped - scilla invocation error',
55+
12: 'Dropped - account init error',
56+
13: 'Dropped - invalid source account',
57+
14: 'Dropped - gas limit too high',
58+
15: 'Dropped - txn type unknown',
59+
16: 'Dropped - txn in wrong shard',
60+
17: 'Dropped - account in wrong shard',
61+
18: 'Dropped - code size too large',
62+
19: 'Dropped - txn verification error',
63+
20: 'Dropped - gas limit too low',
64+
21: 'Dropped - insuff balance',
65+
22: 'Dropped - insuff gas for checker',
66+
23: 'Dropped - duplicate txn found',
67+
24: 'Dropped - txn w/ higher gas found',
68+
25: 'Dropped - invalid dest account',
69+
26: 'Dropped - state addition error',
70+
};
4671

4772
constructor(provider: Provider, signer: Wallet) {
4873
this.provider = provider;
@@ -474,28 +499,48 @@ export class Blockchain implements ZilliqaModule {
474499
* See the pending status of transaction
475500
* @param txId
476501
*/
477-
getPendingTxn(txId: string): Promise<RPCResponse<PendingTxnResult, string>> {
478-
return this.provider.send(
479-
RPCMethod.GetPendingTxn,
480-
txId.replace('0x', '').toLowerCase(),
481-
);
502+
async getPendingTxn(txId: string): Promise<PendingTxnResult> {
503+
try {
504+
const response = await this.provider.send(
505+
RPCMethod.GetPendingTxn,
506+
txId.replace('0x', '').toLowerCase(),
507+
);
508+
509+
if (response.error) {
510+
return Promise.reject(response.error);
511+
}
512+
513+
response.result.info = this.pendingErrorMap[response.result.code];
514+
515+
return response.result;
516+
} catch (err) {
517+
throw err;
518+
}
482519
}
483520

484521
/**
485522
* getPendingTxns
486523
*
487524
* Returns the pending status of all unvalidated Transactions.
488525
*
489-
* For each entry, the possible results are:
490-
*
491-
* confirmed code info
492-
* false 0 Txn not pending
493-
* false 1 Nonce too high
494-
* false 2 Could not fit in as microblock gas limit reached
495-
* false 3 Transaction valid but consensus not reached
496526
*/
497-
getPendingTxns(): Promise<RPCResponse<PendingTxns, any>> {
498-
return this.provider.send(RPCMethod.GetPendingTxns);
527+
async getPendingTxns(): Promise<PendingTxns> {
528+
try {
529+
const response = await this.provider.send(RPCMethod.GetPendingTxns);
530+
if (response.error) {
531+
return Promise.reject(response.error);
532+
}
533+
534+
if (response.result.Txns.length) {
535+
response.result.Txns.forEach((txn: TransactionStatus) => {
536+
txn.info = this.pendingErrorMap[txn.code];
537+
});
538+
}
539+
540+
return response.result;
541+
} catch (err) {
542+
throw err;
543+
}
499544
}
500545

501546
/**

packages/zilliqa-js-blockchain/test/chain.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe('Module: Blockchain', () => {
292292
result: {
293293
Txns: [
294294
{
295-
Status: 1,
295+
code: 1,
296296
TxnHash:
297297
'ec5ef8110a285563d0104269081aa77820058067091a9b3f3ae70f38b94abda3',
298298
},
@@ -303,13 +303,14 @@ describe('Module: Blockchain', () => {
303303

304304
fetch.mockResponses(...responses);
305305
const result = await blockchain.getPendingTxns();
306-
expect(result.result).toBeDefined();
306+
expect(result.Txns).toBeDefined();
307307
// @ts-ignore
308-
expect(result.result.Txns).toEqual([
308+
expect(result.Txns).toEqual([
309309
{
310-
Status: 1,
310+
code: 1,
311311
TxnHash:
312312
'ec5ef8110a285563d0104269081aa77820058067091a9b3f3ae70f38b94abda3',
313+
info: 'Pending - nonce too high',
313314
},
314315
]);
315316
});

packages/zilliqa-js-core/src/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export interface BlockList {
115115
maxPages: number;
116116
}
117117

118-
const enum TxBlockType {
118+
enum TxBlockType {
119119
MICRO,
120120
FINAL,
121121
}
@@ -209,6 +209,7 @@ export interface EventLogEntry {
209209
}
210210

211211
export interface TransitionEntry {
212+
accepted: boolean;
212213
addr: string;
213214
depth: number;
214215
msg: TransitionMsg;
@@ -232,8 +233,9 @@ export interface PendingTxns {
232233
}
233234

234235
export interface TransactionStatus {
235-
Status: number;
236+
code: number;
236237
TxnHash: string;
238+
info: string;
237239
}
238240

239241
export interface PendingTxnResult {

packages/zilliqa-js-crypto/src/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,9 @@ export const normaliseAddress = (address: string): string => {
216216
};
217217

218218
/**
219-
* encodeBase58
219+
* encodeBase58 -the function currently not in use, so we will deprecate it
220220
*
221+
* @deprecated since version 1.0.0
221222
* @param {string} hex - base 16 encoded string
222223
* @returns {string} - big endian base 58 encoded string
223224
*/
@@ -251,8 +252,9 @@ export const encodeBase58 = (hex: string): string => {
251252
};
252253

253254
/**
254-
* decodeBase58
255+
* decodeBase58 - the function currently not in use, so we will deprecate it
255256
*
257+
* @deprecated since version 1.0.0
256258
* @param {string} raw - base 58 string
257259
* @returns {string} - big endian base 16 string
258260
*/

packages/zilliqa-js-crypto/test/util.spec.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1717

1818
import { decodeBase58, encodeBase58, normalizePrivateKey } from '../src/util';
19-
import { randomBytes } from '../src/random';
2019
import testCases from './b58.fixtures.json';
2120

2221
describe('crypto utils', () => {
@@ -35,7 +34,7 @@ describe('crypto utils', () => {
3534
});
3635

3736
it('should encode and decode to the same strings', () => {
38-
const b16 = randomBytes(20);
37+
const b16 = 'fa629547ab288cdd7f726fc0610abe944c982a1d';
3938
const b58 = encodeBase58(b16);
4039
expect(decodeBase58(b58)).toEqual(b16);
4140
});

0 commit comments

Comments
 (0)