-
Notifications
You must be signed in to change notification settings - Fork 17
/
getBlockItemStatus.ts
125 lines (110 loc) · 4.26 KB
/
getBlockItemStatus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { parseEndpoint } from '../shared/util.js';
import {
BlockItemStatus,
CcdAmount,
TransactionHash,
} from '@concordium/web-sdk';
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs';
import { credentials } from '@grpc/grpc-js';
import meow from 'meow';
const cli = meow(
`
Usage
$ yarn run-example <path-to-this-file> [options]
Required
--transaction, -t A transaction to get status from
Options
--help, -h Displays this message
--endpoint, -e Specify endpoint of the form "address:port", defaults to localhost:20000
`,
{
importMeta: import.meta,
flags: {
endpoint: {
type: 'string',
alias: 'e',
default: 'localhost:20000',
},
transaction: {
type: 'string',
alias: 't',
isRequired: true,
},
},
}
);
const [address, port] = parseEndpoint(cli.flags.endpoint);
const client = new ConcordiumGRPCNodeClient(
address,
Number(port),
credentials.createInsecure()
);
/**
* Retrieves status information about a block item (transaction).
* The outcome contains the blockHash and the summary of
* the block item. The summary can be of three different types,
* `accountTransaction`, `accountCreation` or `UpdateTransaction`,
* which is denoted by the type field.
*/
(async () => {
// #region documentation-snippet
const blockItemStatus: BlockItemStatus = await client.getBlockItemStatus(
TransactionHash.fromHexString(cli.flags.transaction)
);
console.log('Status of the transaction:', cli.flags.transaction, '\n');
// Note that there will be no outcomes for a transaction that has only been received:
if (blockItemStatus.status === 'received') {
console.log(
'blockItemStatus is "received" and therefore has no "status" field'
);
}
// If the transaction has only been committed, then there is a list of outcomes:
if (blockItemStatus.status === 'committed') {
console.log(
'blockItemStatus is "committed" and therefore there are potentially multiple outcomes'
);
}
// If the transaction has been finalized, then there is exactly one outcome:
if (blockItemStatus.status === 'finalized') {
console.log(
'blockItemStatus is "finalized" and therefore there is exactly one outcome \n'
);
const { summary } = blockItemStatus.outcome;
if (summary.type === 'accountTransaction') {
console.log('The block item is an account transaction');
switch (summary.transactionType) {
case 'transfer':
// The transaction is a simple transfer
const { amount, to } = summary.transfer;
const ccdAmount = CcdAmount.toCcd(amount);
console.log(ccdAmount, 'CCD sent to', to);
break;
case 'failed':
// The transaction was rejected, in which case the transaction
// type is still available under the failedTransactionType field
const { failedTransactionType, rejectReason } = summary;
console.log(
'Transaction of type "' +
failedTransactionType +
'" failed because:',
rejectReason.tag
);
break;
default:
// Another transaction kind encountered
const otherType = summary.transactionType;
console.log('The transaction is of type:', otherType);
}
} else if (summary.type === 'updateTransaction') {
console.log('The block item is a chain update');
const { effectiveTime, payload } = summary;
console.log('EffectiveTime:', effectiveTime);
console.log('Payload:', payload);
console;
} else if (summary.type === 'accountCreation') {
console.log('The block item is an account creation');
console.log('Account created with address:', summary.address);
}
}
// #endregion documentation-snippet
})();