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

Commit b5fd8cb

Browse files
authored
Merge pull request #269 from Zilliqa/feat/transactionstatus
feat/transactionstatus
2 parents 9c038c3 + c9cefb9 commit b5fd8cb

File tree

6 files changed

+146
-1
lines changed

6 files changed

+146
-1
lines changed

examples/callContract.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ async function testBlockchain() {
8888
false,
8989
);
9090

91+
console.log(callTx.bytes);
9192
// check the pending status
9293
const pendingStatus = await zilliqa.blockchain.getPendingTxn(callTx.id);
9394
console.log(`Pending status is: `);

examples/queryTransactionStatus.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (C) 2020 Zilliqa
2+
//
3+
// This file is part of Zilliqa-Javascript-Library.
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
18+
const { Zilliqa } = require('@zilliqa-js/zilliqa');
19+
20+
const zilliqa = new Zilliqa('https://dev-api.zilliqa.com');
21+
22+
async function test() {
23+
try {
24+
const status = await zilliqa.blockchain.getTransactionStatus(
25+
'96ba63371a57c9d4d7cbf448dbdd78d209c888a76a30413abd72691c16efffbb',
26+
);
27+
console.log(status);
28+
} catch (e) {
29+
console.log(e);
30+
}
31+
}
32+
33+
test();

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
DsBlockObj,
2626
GET_TX_ATTEMPTS,
2727
PendingTxnResult,
28+
TransactionStatusObj,
2829
TransactionStatus,
2930
PendingTxns,
3031
Provider,
@@ -70,6 +71,38 @@ export class Blockchain implements ZilliqaModule {
7071
255: 'Error - Internal database error',
7172
};
7273

74+
transactionStatusMap: { [key: number]: { [key: number]: string } } = {
75+
0: { 0: 'Transaction not found', 1: ' Pending - Dispatched' },
76+
1: {
77+
2: 'Pending - Soft-confirmed (awaiting Tx block generation)',
78+
4: 'Pending - Nonce is higher than expected',
79+
5: 'Pending - Microblock gas limit exceeded',
80+
6: 'Pending - Consensus failure in network',
81+
},
82+
2: {
83+
3: 'Confirmed',
84+
10: 'Rejected - Transaction caused math error',
85+
11: 'Rejected - Scilla invocation error',
86+
12: 'Rejected - Contract account initialization error',
87+
13: 'Rejected - Invalid source account',
88+
14: 'Rejected - Gas limit higher than shard gas limit',
89+
15: 'Rejected - Unknown transaction type',
90+
16: 'Rejected - Transaction sent to wrong shard',
91+
17: 'Rejected - Contract & source account cross-shard issue',
92+
18: 'Rejected - Code size exceeded limit',
93+
19: 'Rejected - Transaction verification failed',
94+
20: 'Rejected - Gas limit too low',
95+
21: 'Rejected - Insufficient balance',
96+
22: 'Rejected - Insufficient gas to invoke Scilla checker',
97+
23: 'Rejected - Duplicate transaction exists',
98+
24: 'Rejected - Transaction with higher gas price exists',
99+
25: 'Rejected - Invalid destination address',
100+
26: 'Rejected - Failed to add contract account to state',
101+
27: 'Rejected - Nonce is lower than expected',
102+
255: 'Rejected - Internal error',
103+
},
104+
};
105+
73106
constructor(provider: Provider, signer: Wallet) {
74107
this.provider = provider;
75108
this.provider.middleware.request.use(
@@ -396,7 +429,7 @@ export class Blockchain implements ZilliqaModule {
396429
* a rejected Transaction instance is returned.
397430
*
398431
* @param {string} txHash
399-
* @returns {Promise<any>}
432+
* @returns {Promise<Transaction>}
400433
*/
401434
async getTransaction(txHash: string): Promise<Transaction> {
402435
try {
@@ -417,6 +450,35 @@ export class Blockchain implements ZilliqaModule {
417450
}
418451
}
419452

453+
/**
454+
* Returns the status of a specified transaction.
455+
* This API is available from Zilliqa `V7.0.0` onwards and supports all transaction statuses
456+
* (unconfirmed, confirmed, and rejected).
457+
*
458+
* @param txHash
459+
* @returns {Promise<TransactionStatusObj>}
460+
*/
461+
async getTransactionStatus(txHash: string): Promise<TransactionStatusObj> {
462+
try {
463+
const response = await this.provider.send<TransactionStatusObj>(
464+
RPCMethod.GetTransactionStatus,
465+
txHash,
466+
);
467+
if (response.error) {
468+
return Promise.reject(response.error);
469+
}
470+
471+
const modificationState = response.result.modificationState;
472+
const status = response.result.status;
473+
response.result.statusMessage = this.transactionStatusMap[
474+
modificationState
475+
][status];
476+
return response.result;
477+
} catch (err) {
478+
throw err;
479+
}
480+
}
481+
420482
/**
421483
* getRecentTransactions
422484
*

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,30 @@ describe('Module: Blockchain', () => {
284284
expect(result.result[0].ID).toEqual('some_hash');
285285
});
286286

287+
it('should receive transaction status with confirmed', async () => {
288+
const responses = [
289+
{
290+
id: 1,
291+
jsonrpc: '2.0',
292+
result: {
293+
ID:
294+
'96ba63371a57c9d4d7cbf448dbdd78d209c888a76a30413abd72691c16efffbb',
295+
gasLimit: '1',
296+
gasPrice: '2000000000',
297+
modificationState: 2,
298+
status: 3,
299+
},
300+
},
301+
].map((res) => [JSON.stringify(res)] as [string]);
302+
303+
fetch.mockResponses(...responses);
304+
305+
const result = await blockchain.getTransactionStatus(
306+
'96ba63371a57c9d4d7cbf448dbdd78d209c888a76a30413abd72691c16efffbb',
307+
);
308+
expect(result.statusMessage).toEqual('Confirmed');
309+
});
310+
287311
it('should receive pending transaction list', async () => {
288312
const responses = [
289313
{

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export enum RPCMethod {
5151
// Transaction-related methods
5252
CreateTransaction = 'CreateTransaction',
5353
GetTransaction = 'GetTransaction',
54+
GetTransactionStatus = 'GetTransactionStatus',
5455
GetRecentTransactions = 'GetRecentTransactions',
5556
GetTransactionsForTxBlock = 'GetTransactionsForTxBlock',
5657
GetTxnBodiesForTxBlock = 'GetTxnBodiesForTxBlock',

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ export interface TransactionObj {
8888
receipt: TransactionReceiptObj;
8989
}
9090

91+
export interface TransactionStatusObj {
92+
ID: string;
93+
_id: StatusID;
94+
amount: string;
95+
epochInserted: string;
96+
epochUpdated: string;
97+
gasLimit: string;
98+
gasPrice: string;
99+
lastModified: string;
100+
modificationState: number;
101+
nonce: string;
102+
senderAddr: string;
103+
signature: string;
104+
status: number;
105+
success: boolean;
106+
toAddr: string;
107+
version: string;
108+
statusMessage: string;
109+
}
110+
111+
export interface StatusID {
112+
$oid: string;
113+
}
114+
91115
export interface DsBlockHeader {
92116
BlockNum: string;
93117
Difficulty: number;

0 commit comments

Comments
 (0)