Skip to content

Commit 73309a6

Browse files
authored
v2.3.0
* feat: export validator utils * chore: bump a version * fix(typescript): represent transaction id as a string (#52) * fix: remove preinstall npm script (#53) * fix: remove preinstall npm script * chore: bump a version * Fix/transaction query (#51) * chore(CHANGELOG): v2.3.0
1 parent d451231 commit 73309a6

File tree

7 files changed

+240
-42
lines changed

7 files changed

+240
-42
lines changed

CHANGELOG.md

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
# Changelog
22

3+
## [2.3.0] - 2024-03-01
4+
5+
### Added
6+
7+
- `transformTransactionQuery` function:
8+
9+
```ts
10+
const transformed = transformTransactionQuery({
11+
fromHeight: 7585271,
12+
and: {
13+
toHeight: 7586280,
14+
},
15+
or: {
16+
senderId: 'U18132012621449491414',
17+
},
18+
});
19+
20+
/**
21+
* {
22+
* and:fromHeight: 7585271,
23+
* and:toHeight: 7586280,
24+
* or:senderId: 'U18132012621449491414'
25+
* }
26+
*/
27+
console.log(transformed);
28+
```
29+
30+
### Fixed
31+
32+
- TypeScript: `id` should have `string` type in the methods: `api.getTransaction()`, `api.getQueuedTransaction()`, `api.getUnconfirmedTransaction()`
33+
34+
- Global installation of packages that use `adamant-api` as its dependency
35+
36+
- Transaction query methods:
37+
38+
```ts
39+
const blocks = await api.getTransactions({
40+
fromHeight: 7585271,
41+
and: {
42+
toHeight: 7586280,
43+
},
44+
or: {
45+
senderId: 'U18132012621449491414',
46+
},
47+
});
48+
```
49+
350
## [2.2.0] - 2023-12-01
451

552
## Added
@@ -12,11 +59,13 @@
1259
function isAdmPublicKey(publicKey: unknown): publicKey is string;
1360
function isAdmVoteForPublicKey(publicKey: unknown): publicKey is string;
1461
function isAdmVoteForAddress(address: unknown): boolean;
15-
function isAdmVoteForDelegateName(delegateName: unknown): delegateName is string;
62+
function isAdmVoteForDelegateName(
63+
delegateName: unknown
64+
): delegateName is string;
1665
function validateMessage(
1766
message: string,
1867
messageType: MessageType = MessageType.Chat
19-
): { success: false, error: string } | { success: true };
68+
): {success: false; error: string} | {success: true};
2069
function isDelegateName(name: unknown): name is string;
2170
function admToSats(amount: number): number;
2271
```
@@ -28,11 +77,13 @@
2877
- `api.initSocket()` now accepts an instance of `WebSocketClient` as an argument:
2978

3079
```js
31-
const socket = new WebSocketClient({ /* ... */ })
80+
const socket = new WebSocketClient({
81+
/* ... */
82+
});
3283

33-
api.initSocket(socket)
84+
api.initSocket(socket);
3485
// instead of
35-
api.socket = socket
86+
api.socket = socket;
3687
```
3788

3889
- Improved the `encodeMessage()` and `decodeMessage()` functions to accept public keys as Uint8Array or Buffer
@@ -62,7 +113,7 @@
62113
For example, you can now use `'log'` instead of `LogLevel.Log` in TypeScript:
63114

64115
```ts
65-
const api = new AdamantApi({ /* ... */ logLevel: 'log' })
116+
const api = new AdamantApi({/* ... */ logLevel: 'log'});
66117
```
67118

68119
- TypeScript: Added missing declaration modules to npm that led to the error:
@@ -95,7 +146,7 @@
95146

96147
```js
97148
// before
98-
const block = await api.get('blocks/get', { id });
149+
const block = await api.get('blocks/get', {id});
99150

100151
// after
101152
const block = await api.getBlock(id);
@@ -104,17 +155,16 @@
104155
and `post()` method:
105156

106157
```js
107-
await api.post('transactions/process', { transaction });
158+
await api.post('transactions/process', {transaction});
108159
```
109160

110-
111161
- **getTransactionId()** method
112162

113163
Pass signed transaction with signature to get a transaction id as a string:
114164

115165
```js
116-
import {getTransactionId} from 'adamant-api'
117-
const id = getTransactionId(signedTransaction)
166+
import {getTransactionId} from 'adamant-api';
167+
const id = getTransactionId(signedTransaction);
118168
```
119169

120170
_See [documentation](https://github.com/Adamant-im/adamant-api-jsclient/wiki/Calculating-transaction-id) for more information._
@@ -136,9 +186,13 @@
136186
Now you will create new instances of `adamant-api` using keyword `new`:
137187

138188
```js
139-
import { AdamantApi } from 'adamant-api';
189+
import {AdamantApi} from 'adamant-api';
140190

141-
const api = new AdamantApi({ nodes: [/* ... */] });
191+
const api = new AdamantApi({
192+
nodes: [
193+
/* ... */
194+
],
195+
});
142196
```
143197

144198
- **Socket Initialization**
@@ -157,7 +211,7 @@
157211
});
158212

159213
// after
160-
api.initSocket({ admAddress: 'U1234..' });
214+
api.initSocket({admAddress: 'U1234..'});
161215

162216
api.socket.on((transaction: AnyTransaction) => {
163217
// ...
@@ -168,29 +222,34 @@
168222

169223
```ts
170224
// socket.ts
171-
import { WebSocketClient, TransactionType } from 'adamant-api';
225+
import {WebSocketClient, TransactionType} from 'adamant-api';
172226

173-
const socket = new WebSocketClient({ admAddress: 'U1234..' });
227+
const socket = new WebSocketClient({admAddress: 'U1234..'});
174228

175-
socket.on([TransactionType.CHAT_MESSAGE, TransactionType.SEND], (transaction) => {
176-
// handle chat messages and transfer tokens transactions
177-
});
229+
socket.on(
230+
[TransactionType.CHAT_MESSAGE, TransactionType.SEND],
231+
transaction => {
232+
// handle chat messages and transfer tokens transactions
233+
}
234+
);
178235

179-
socket.on(TransactionType.VOTE, (transaction) => {
236+
socket.on(TransactionType.VOTE, transaction => {
180237
// handle vote for delegate transaction
181238
});
182239

183-
export { socket };
240+
export {socket};
184241
```
185242

186243
```ts
187244
// api.ts
188-
import { AdamantApi } from 'adamant-api';
189-
import { socket } from './socket';
245+
import {AdamantApi} from 'adamant-api';
246+
import {socket} from './socket';
190247

191248
export const api = new AdamantApi({
192249
socket,
193-
nodes: [/* ... */],
250+
nodes: [
251+
/* ... */
252+
],
194253
});
195254
```
196255

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "adamant-api",
3-
"version": "2.2.1",
3+
"version": "2.3.0",
44
"description": "JavaScript API library for the ADAMANT Blockchain",
55
"keywords": [
66
"adm",

src/api/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ import {
7373
createVoteTransaction,
7474
} from '../helpers/transactions';
7575
import {encodeMessage} from '../helpers/encryptor';
76-
import {VoteDirection, parseVote} from './votes';
76+
import {
77+
type VoteDirection,
78+
transformTransactionQuery,
79+
parseVote,
80+
} from './utils';
7781

7882
export type AdamantAddress = `U${string}`;
7983

@@ -99,9 +103,9 @@ export type AddressOrPublicKeyObject = AddressObject | PublicKeyObject;
99103
*/
100104
export type UsernameOrPublicKeyObject = UsernameObject | PublicKeyObject;
101105

102-
export type TransactionQuery<T extends {}> = T & {
103-
or: T;
104-
and: T;
106+
export type TransactionQuery<T extends object> = Partial<T> & {
107+
or?: Partial<T>;
108+
and?: Partial<T>;
105109
};
106110

107111
export interface GetBlocksOptions {
@@ -571,7 +575,10 @@ export class AdamantApi extends NodeManager {
571575
address: string,
572576
options?: TransactionQuery<ChatroomsOptions>
573577
) {
574-
return this.get<GetChatRoomsResponseDto>(`chatrooms/${address}`, options);
578+
return this.get<GetChatRoomsResponseDto>(
579+
`chatrooms/${address}`,
580+
transformTransactionQuery(options)
581+
);
575582
}
576583

577584
/**
@@ -584,7 +591,7 @@ export class AdamantApi extends NodeManager {
584591
) {
585592
return this.get<GetChatMessagesResponseDto>(
586593
`chatrooms/${address1}/${address2}`,
587-
query
594+
transformTransactionQuery(query)
588595
);
589596
}
590597

@@ -778,7 +785,10 @@ export class AdamantApi extends NodeManager {
778785
* Returns list of transactions
779786
*/
780787
async getTransactions(options?: TransactionQuery<TransactionsOptions>) {
781-
return this.get<GetTransactionsResponseDto>('transactions', options);
788+
return this.get<GetTransactionsResponseDto>(
789+
'transactions',
790+
transformTransactionQuery(options)
791+
);
782792
}
783793

784794
/**
@@ -790,7 +800,7 @@ export class AdamantApi extends NodeManager {
790800
) {
791801
return this.get<GetTransactionByIdResponseDto>('transactions/get', {
792802
id,
793-
...options,
803+
...transformTransactionQuery(options),
794804
});
795805
}
796806

@@ -841,4 +851,4 @@ export class AdamantApi extends NodeManager {
841851
}
842852

843853
export * from './generated';
844-
export * from './votes';
854+
export * from './utils';

src/api/tests/utils.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import {transformTransactionQuery} from '../utils';
2+
3+
describe('transformTransactionQuery', () => {
4+
it('should transform `or` and `and` object properties', () => {
5+
const transformed = transformTransactionQuery({
6+
or: {
7+
maxAmount: 50000000,
8+
senderPublicKey:
9+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
10+
senderPublicKeys: [
11+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
12+
'4ef885053c630041f57493343d7f6023107c5dc8b8148147e732c93',
13+
],
14+
},
15+
and: {
16+
blockId: '7917597195203393333',
17+
fromHeight: 10336065,
18+
toHeight: 11,
19+
minAmount: 1000000000000001,
20+
senderId: 'U15423595369615486571',
21+
senderIds: ['U18132012621449491414', 'U15881344309699504778'],
22+
recipientId: 'U15423595369615486571',
23+
recipientIds: ['U18132012621449491414', 'U15881344309699504778'],
24+
recipientPublicKey:
25+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
26+
recipientPublicKeys: [
27+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
28+
'4ef885053c630041f57493343d7f6023107c5dc8b8148147e732c93',
29+
],
30+
inId: 'U100739400829575109',
31+
type: 2,
32+
types: [9, 0],
33+
key: 'eth:address',
34+
keyIds: ['eth:address', 'doge:address', 'dash:address'],
35+
},
36+
});
37+
38+
expect(transformed).toStrictEqual({
39+
'or:maxAmount': 50000000,
40+
'or:senderPublicKey':
41+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
42+
'or:senderPublicKeys': [
43+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
44+
'4ef885053c630041f57493343d7f6023107c5dc8b8148147e732c93',
45+
],
46+
'and:blockId': '7917597195203393333',
47+
'and:fromHeight': 10336065,
48+
'and:toHeight': 11,
49+
'and:minAmount': 1000000000000001,
50+
'and:senderId': 'U15423595369615486571',
51+
'and:senderIds': ['U18132012621449491414', 'U15881344309699504778'],
52+
'and:recipientId': 'U15423595369615486571',
53+
'and:recipientIds': ['U18132012621449491414', 'U15881344309699504778'],
54+
'and:recipientPublicKey':
55+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
56+
'and:recipientPublicKeys': [
57+
'801846655523f5e21f2d454b2f98f70aaf5d3887c806463100a1764a4e7c1457',
58+
'4ef885053c630041f57493343d7f6023107c5dc8b8148147e732c93',
59+
],
60+
'and:inId': 'U100739400829575109',
61+
'and:type': 2,
62+
'and:types': [9, 0],
63+
'and:key': 'eth:address',
64+
'and:keyIds': ['eth:address', 'doge:address', 'dash:address'],
65+
});
66+
});
67+
68+
it('should NOT transform object properties other than `or` and `and`', () => {
69+
const transformed = transformTransactionQuery({
70+
minAmount: 5000,
71+
senderId: 'U15423595369615486571',
72+
senderIds: ['U18132012621449491414', 'U15881344309699504778'],
73+
and: {
74+
maxAmount: 6000,
75+
},
76+
or: {
77+
recipientIds: ['U18132012621449491414', 'U15881344309699504778'],
78+
},
79+
});
80+
81+
expect(transformed).toStrictEqual({
82+
minAmount: 5000,
83+
senderId: 'U15423595369615486571',
84+
senderIds: ['U18132012621449491414', 'U15881344309699504778'],
85+
'and:maxAmount': 6000,
86+
'or:recipientIds': ['U18132012621449491414', 'U15881344309699504778'],
87+
});
88+
});
89+
});

0 commit comments

Comments
 (0)