Skip to content

Commit 6549723

Browse files
authored
1723 new net p1 code review and test thortransactions package (#1881)
* feat: chaing gas unit handling and added unit tests * fix: fixed gas handling * fix: fixed gas used for Receipt class * fix: updated gas price convertion for GetTxResponseJSON * fix: removed EXP * fix: renamed EXP unit test to a more comprehensive name and added verify
1 parent b200285 commit 6549723

6 files changed

+417
-2
lines changed

packages/thorest/src/thor/transactions/Clause.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Address, HexUInt, VET } from '@vechain/sdk-core';
1+
import { Address, HexUInt, Units, VET } from '@vechain/sdk-core';
22

33
class Clause {
44
readonly to?: Address;
@@ -10,7 +10,7 @@ class Clause {
1010
json.to !== undefined && json.to != null
1111
? Address.of(json.to)
1212
: undefined;
13-
this.value = VET.of(json.value);
13+
this.value = VET.of(json.value, Units.wei);
1414
this.data = HexUInt.of(json.data);
1515
}
1616

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, test } from '@jest/globals';
2+
import { TxId } from '@vechain/sdk-core';
3+
import {
4+
type FetchHttpClient,
5+
type GetRawTxResponseJSON,
6+
RetrieveRawTransactionByID
7+
} from '../../../src';
8+
9+
const mockHttpClient = <T>(response: T): FetchHttpClient => {
10+
return {
11+
get: jest.fn().mockImplementation(() => {
12+
return {
13+
json: jest.fn().mockImplementation(() => {
14+
return response;
15+
})
16+
};
17+
})
18+
} as unknown as FetchHttpClient;
19+
};
20+
21+
/**
22+
* VeChain transaction - unit
23+
*
24+
* @group unit/transaction
25+
*/
26+
describe('RetrieveRawTransactionByID unit tests', () => {
27+
test('ok <- askTo', async () => {
28+
const txId = TxId.of(
29+
'0xb6b5b47a5eee8b14e5222ac1bb957c0bbdc3d489850b033e3e544d9ca0cef934'
30+
);
31+
32+
const mockResponse = {
33+
raw: '0xf8970184aabbccdd20f840df947d8bf18c7ce84b3e175b339c4ca93aed1dd4885501a69400000000000000000000000000000000000000000880de0b6b3a76400008028a0a8134669348e8a7ad34a64a193def01cc0d0f77935f134120ef507a87dcfe39da0046ae0acb1b9c47aadb38c2f0ce5789be81d194471378b14c53f3c6677f209c4',
34+
meta: {
35+
blockID:
36+
'0x0000000000000000000000000000000000000000000000000000000000000000',
37+
blockNumber: 1,
38+
blockTimestamp: 1000000
39+
}
40+
} satisfies GetRawTxResponseJSON;
41+
42+
const mockClient = mockHttpClient(mockResponse);
43+
const response =
44+
await RetrieveRawTransactionByID.of(txId).askTo(mockClient);
45+
expect(response.response.toJSON()).toEqual(mockResponse);
46+
});
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, test } from '@jest/globals';
2+
import { TxId } from '@vechain/sdk-core';
3+
import {
4+
type FetchHttpClient,
5+
type GetTxResponseJSON,
6+
RetrieveTransactionByID
7+
} from '../../../src';
8+
9+
const mockHttpClient = <T>(response: T): FetchHttpClient => {
10+
return {
11+
get: jest.fn().mockImplementation(() => {
12+
return {
13+
json: jest.fn().mockImplementation(() => {
14+
return response;
15+
})
16+
};
17+
})
18+
} as unknown as FetchHttpClient;
19+
};
20+
21+
/**
22+
* VeChain transaction - unit
23+
*
24+
* @group unit/transaction
25+
*/
26+
describe('RetrieveTransactionByID unit tests', () => {
27+
test('ok <- askTo', async () => {
28+
const txId = TxId.of(
29+
'0xb6b5b47a5eee8b14e5222ac1bb957c0bbdc3d489850b033e3e544d9ca0cef934'
30+
);
31+
32+
const mockResponse = {
33+
id: '0xb6b5b47a5eee8b14e5222ac1bb957c0bbdc3d489850b033e3e544d9ca0cef934',
34+
chainTag: 39,
35+
blockRef:
36+
'0x0000000000000000000000000000000000000000000000000000000000000000',
37+
expiration: 32,
38+
clauses: [
39+
{
40+
to: '0x7d8Bf18C7ce84B3e175B339C4cA93Aed1dD488Aa',
41+
// We are adding a leading zero to the value because of how VCDM's
42+
// Hex is parsing and because we need an even number of hex-digits in other places
43+
value: '0x05ec3db77eba739400',
44+
data: '0x'
45+
}
46+
],
47+
gasPriceCoef: 0,
48+
gas: 21000,
49+
origin: '0x7d8Bf18C7ce84B3e175B339C4cA93Aed1dD488Aa',
50+
nonce: '0x12345678',
51+
dependsOn: undefined,
52+
delegator: null,
53+
size: 128,
54+
meta: {
55+
blockID:
56+
'0x0000000000000000000000000000000000000000000000000000000000000000',
57+
blockNumber: 1,
58+
blockTimestamp: 1000000
59+
}
60+
} satisfies GetTxResponseJSON;
61+
62+
const mockClient = mockHttpClient(mockResponse);
63+
const response =
64+
await RetrieveTransactionByID.of(txId).askTo(mockClient);
65+
expect(response.response.toJSON()).toEqual(mockResponse);
66+
});
67+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, test } from '@jest/globals';
2+
import { TxId } from '@vechain/sdk-core';
3+
import {
4+
type FetchHttpClient,
5+
type GetTxReceiptResponseJSON,
6+
RetrieveTransactionReceipt
7+
} from '../../../src';
8+
9+
const mockHttpClient = <T>(response: T): FetchHttpClient => {
10+
return {
11+
get: jest.fn().mockImplementation(() => {
12+
return {
13+
json: jest.fn().mockImplementation(() => {
14+
return response;
15+
})
16+
};
17+
})
18+
} as unknown as FetchHttpClient;
19+
};
20+
21+
/**
22+
* VeChain transaction - unit
23+
*
24+
* @group unit/transaction
25+
*/
26+
describe('RetrieveTransactionReceipt unit tests', () => {
27+
test('ok <- askTo', async () => {
28+
const txId = TxId.of(
29+
'0xb6b5b47a5eee8b14e5222ac1bb957c0bbdc3d489850b033e3e544d9ca0cef934'
30+
);
31+
32+
const mockResponse = {
33+
gasUsed: 2100,
34+
gasPayer: '0x7d8Bf18C7ce84B3e175B339C4cA93Aed1dD488Aa',
35+
paid: '0x1234567890',
36+
reward: '0x0987654321',
37+
meta: {
38+
blockID:
39+
'0x0000000000000000000000000000000000000000000000000000000000000000',
40+
blockNumber: 0,
41+
blockTimestamp: 0,
42+
txID: '0x0000000000000000000000000000000000000000000000000000000000000000',
43+
txOrigin:
44+
'0x0000000000000000000000000000000000000000000000000000000000000000'
45+
},
46+
reverted: false,
47+
outputs: [
48+
{
49+
contractAddress:
50+
'0x0000000000000000000000000000000000000000',
51+
events: [],
52+
transfers: []
53+
}
54+
]
55+
} satisfies GetTxReceiptResponseJSON;
56+
57+
const mockClient = mockHttpClient(mockResponse);
58+
const response =
59+
await RetrieveTransactionReceipt.of(txId).askTo(mockClient);
60+
expect(response.response.toJSON()).toEqual(mockResponse);
61+
});
62+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, test } from '@jest/globals';
2+
import { transferTransactionBody } from '../../../../network/tests/thor-client/transactions/fixture';
3+
import { TEST_ACCOUNTS } from '../../../../network/tests/fixture';
4+
import { HexUInt, Transaction } from '@vechain/sdk-core';
5+
import { type FetchHttpClient, SendTransaction, TXID } from '../../../src';
6+
7+
const mockHttpClient = <T>(response: T): FetchHttpClient => {
8+
return {
9+
post: jest.fn().mockImplementation(() => {
10+
return {
11+
json: jest.fn().mockImplementation(() => {
12+
return response;
13+
})
14+
};
15+
})
16+
} as unknown as FetchHttpClient;
17+
};
18+
19+
/**
20+
* VeChain transaction - unit
21+
*
22+
* @group unit/transaction
23+
*/
24+
describe('SendTransaction solo tests', () => {
25+
test('ok <- askTo', async () => {
26+
const gasResult = {
27+
totalGas: 21000,
28+
reverted: false,
29+
revertReasons: [],
30+
vmErrors: []
31+
};
32+
const tx = Transaction.of({
33+
...transferTransactionBody,
34+
gas: gasResult.totalGas,
35+
nonce: 10000000
36+
}).sign(
37+
HexUInt.of(TEST_ACCOUNTS.TRANSACTION.TRANSACTION_SENDER.privateKey)
38+
.bytes
39+
).encoded;
40+
const response = await SendTransaction.of(tx).askTo(
41+
mockHttpClient(new TXID({ id: '0x123' }))
42+
);
43+
44+
expect(response.response.toJSON()).toEqual(
45+
new TXID({ id: '0x123' }).toJSON()
46+
);
47+
});
48+
});

0 commit comments

Comments
 (0)