Skip to content

Commit 3ad596c

Browse files
Merge branch 'feature-thorest' into 1886-remove-all-but-core-and-thorest-packages
2 parents 3207154 + d94f839 commit 3ad596c

10 files changed

+75
-42
lines changed

packages/core/src/vcdm/Gas.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { InvalidDataType } from '@vechain/sdk-errors';
2+
import { VTHO } from './currency/VTHO';
3+
import { Units } from './currency/Units';
4+
import { UInt } from './UInt';
5+
6+
class Gas extends UInt {
7+
protected constructor(value: number) {
8+
super(value);
9+
}
10+
11+
static of(exp: number): Gas {
12+
if (exp >= 0 && Number.isInteger(exp)) {
13+
return new Gas(exp);
14+
}
15+
throw new InvalidDataType(
16+
'Gas.of',
17+
'not an unsigned integer expression',
18+
{
19+
exp: `${exp}`
20+
}
21+
);
22+
}
23+
24+
/**
25+
*
26+
* @returns The value of this Gas object as a VTHO.
27+
* 10^5 gas = 1 VTHO
28+
* 1 gas = 10^13 weiVTHO
29+
*/
30+
toVTHO(): VTHO {
31+
return VTHO.of(this.valueOf() ** Math.pow(10, 13), Units.wei);
32+
}
33+
}
34+
35+
export { Gas };

packages/core/src/vcdm/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './BloomFilter';
66
export * from './currency';
77
export * from './encoding';
88
export * from './FixedPointNumber';
9+
export * from './Gas';
910
export * from './hash';
1011
export * from './Hex';
1112
export * from './HexInt';

packages/thorest/src/thor/accounts/ExecuteCodesRequest.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Clause, type ClauseJSON } from '../transactions';
2-
import { Address, BlockRef, UInt, Units, VTHO } from '@vechain/sdk-core';
2+
import { Address, BlockRef, Gas, UInt, Units, VTHO } from '@vechain/sdk-core';
33

44
class ExecuteCodesRequest {
55
readonly provedWork?: string;
66
readonly gasPayer?: Address;
77
readonly expiration?: UInt;
88
readonly blockRef?: BlockRef;
99
readonly clauses?: Clause[];
10-
readonly gas?: VTHO;
10+
readonly gas?: Gas;
1111
readonly gasPrice?: VTHO;
1212
readonly caller?: Address;
1313

@@ -29,8 +29,7 @@ class ExecuteCodesRequest {
2929
: json.clauses.map(
3030
(clauseJSON: ClauseJSON): Clause => new Clause(clauseJSON)
3131
);
32-
this.gas =
33-
json.gas === undefined ? undefined : VTHO.of(json.gas, Units.wei);
32+
this.gas = json.gas === undefined ? undefined : Gas.of(json.gas);
3433
this.gasPrice =
3534
json.gasPrice === undefined
3635
? undefined
@@ -46,7 +45,7 @@ class ExecuteCodesRequest {
4645
expiration: this.expiration?.valueOf(),
4746
blockRef: this.blockRef?.toString(),
4847
clauses: this.clauses?.map((clause: Clause) => clause.toJSON()),
49-
gas: this.gas === undefined ? undefined : Number(this.gas.wei),
48+
gas: this.gas === undefined ? undefined : this.gas.valueOf(),
5049
gasPrice:
5150
this.gasPrice === undefined
5251
? undefined

packages/thorest/src/thor/accounts/ExecuteCodesResponse.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import {
44
Transfer,
55
type TransferJSON
66
} from '../transactions';
7-
import { HexUInt, VTHO } from '@vechain/sdk-core';
7+
import { HexUInt, Gas } from '@vechain/sdk-core';
88

99
class ExecuteCodeResponse {
1010
readonly data: HexUInt;
1111
readonly events: Event[];
1212
readonly transfers: Transfer[];
13-
readonly gasUsed: VTHO;
13+
readonly gasUsed: Gas;
1414
readonly reverted: boolean;
1515
readonly vmError: string;
1616

@@ -22,7 +22,7 @@ class ExecuteCodeResponse {
2222
this.transfers = json.transfers.map(
2323
(transferJSON: TransferJSON): Transfer => new Transfer(transferJSON)
2424
);
25-
this.gasUsed = VTHO.of(json.gasUsed);
25+
this.gasUsed = Gas.of(json.gasUsed);
2626
this.reverted = json.reverted;
2727
this.vmError = json.vmError;
2828
}
@@ -36,7 +36,7 @@ class ExecuteCodeResponse {
3636
transfers: this.transfers.map(
3737
(transfer: Transfer): TransferJSON => transfer.toJSON()
3838
),
39-
gasUsed: Number(this.gasUsed.wei),
39+
gasUsed: this.gasUsed.valueOf(),
4040
reverted: this.reverted,
4141
vmError: this.vmError
4242
} satisfies ExecuteCodeResponseJSON;

packages/thorest/src/thor/blocks/CommonBlockResponse.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Address, BlockId, ThorId, UInt, Units, VTHO } from '@vechain/sdk-core';
1+
import { Address, BlockId, Gas, ThorId, UInt } from '@vechain/sdk-core';
22

33
interface CommmonBlockResponseJSON {
44
number: number;
@@ -26,10 +26,9 @@ class CommmonBlockResponse {
2626
readonly size: UInt;
2727
readonly parentID: BlockId;
2828
readonly timestamp: UInt;
29-
// to do: use the Gas unit class when it will be available
30-
readonly gasLimit: VTHO;
29+
readonly gasLimit: Gas;
3130
readonly beneficiary: Address;
32-
readonly gasUsed: VTHO;
31+
readonly gasUsed: Gas;
3332
readonly totalScore: UInt;
3433
readonly txsRoot: ThorId;
3534
readonly txsFeatures: UInt;
@@ -46,9 +45,9 @@ class CommmonBlockResponse {
4645
this.size = UInt.of(json.size);
4746
this.parentID = BlockId.of(json.parentID);
4847
this.timestamp = UInt.of(json.timestamp);
49-
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
48+
this.gasLimit = Gas.of(json.gasLimit);
5049
this.beneficiary = Address.of(json.beneficiary);
51-
this.gasUsed = VTHO.of(json.gasUsed, Units.wei);
50+
this.gasUsed = Gas.of(json.gasUsed);
5251
this.totalScore = UInt.of(json.totalScore);
5352
this.txsRoot = ThorId.of(json.txsRoot);
5453
this.txsFeatures = UInt.of(json.txsFeatures);
@@ -67,9 +66,9 @@ class CommmonBlockResponse {
6766
size: this.size.valueOf(),
6867
parentID: this.parentID.toString(),
6968
timestamp: this.timestamp.valueOf(),
70-
gasLimit: Number(this.gasLimit.wei),
69+
gasLimit: this.gasLimit.valueOf(),
7170
beneficiary: this.beneficiary.toString(),
72-
gasUsed: Number(this.gasUsed.wei),
71+
gasUsed: this.gasUsed.valueOf(),
7372
totalScore: this.totalScore.valueOf(),
7473
txsRoot: this.txsRoot.toString(),
7574
txsFeatures: this.txsFeatures.valueOf(),

packages/thorest/src/thor/debug/PostDebugTracerCallRequest.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Tracer, type TracerName } from './TracerName';
22
import {
33
Address,
44
type BlockRef,
5+
Gas,
56
HexUInt,
67
UInt,
78
Units,
@@ -15,7 +16,7 @@ class PostDebugTracerCallRequest {
1516
readonly value: VET;
1617
readonly data: HexUInt;
1718
readonly to?: Address;
18-
readonly gas?: VTHO;
19+
readonly gas?: Gas;
1920
readonly gasPrice?: VTHO;
2021
readonly caller?: Address;
2122
readonly provedWork?: string;
@@ -29,8 +30,7 @@ class PostDebugTracerCallRequest {
2930
this.value = VET.of(json.value);
3031
this.data = HexUInt.of(json.data);
3132
this.to = json.to === undefined ? undefined : Address.of(json.to);
32-
this.gas =
33-
json.gas === undefined ? undefined : VTHO.of(json.gas, Units.wei);
33+
this.gas = json.gas === undefined ? undefined : Gas.of(json.gas);
3434
this.gasPrice =
3535
json.gasPrice === undefined
3636
? undefined
@@ -53,7 +53,7 @@ class PostDebugTracerCallRequest {
5353
value: HexUInt.of(this.value.wei).toString(),
5454
data: this.data.toString(),
5555
to: this.to?.toString(),
56-
gas: this.gas === undefined ? undefined : Number(this.gas.wei),
56+
gas: this.gas === undefined ? undefined : this.gas.valueOf(),
5757
gasPrice:
5858
this.gasPrice === undefined
5959
? undefined

packages/thorest/src/thor/subscriptions/SubscriptionBeat2Response.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { BlockId, HexUInt, UInt, Units, VTHO } from '@vechain/sdk-core';
1+
import { BlockId, Gas, HexUInt, UInt } from '@vechain/sdk-core';
22

33
class SubscriptionBeat2Response {
4-
readonly gasLimit: VTHO;
4+
readonly gasLimit: Gas;
55
readonly obsolete: boolean;
66
readonly number: UInt;
77
readonly id: BlockId;
@@ -12,7 +12,7 @@ class SubscriptionBeat2Response {
1212
readonly k: UInt;
1313

1414
constructor(json: SubscriptionBeat2ResponseJSON) {
15-
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
15+
this.gasLimit = Gas.of(json.gasLimit);
1616
this.obsolete = json.obsolete;
1717
this.number = UInt.of(json.number);
1818
this.id = BlockId.of(json.id);
@@ -25,7 +25,7 @@ class SubscriptionBeat2Response {
2525

2626
toJSON(): SubscriptionBeat2ResponseJSON {
2727
return {
28-
gasLimit: Number(this.gasLimit.wei),
28+
gasLimit: this.gasLimit.valueOf(),
2929
obsolete: this.obsolete,
3030
number: this.number.valueOf(),
3131
id: this.id.toString(),

packages/thorest/src/thor/subscriptions/SubscriptionBlockResponse.ts

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import {
22
Address,
33
BlockId,
4+
Gas,
45
ThorId,
56
type TxId,
6-
UInt,
7-
Units,
8-
VTHO
7+
UInt
98
} from '@vechain/sdk-core';
109

1110
class SubscriptionBlockResponse {
@@ -14,9 +13,9 @@ class SubscriptionBlockResponse {
1413
readonly size: UInt;
1514
readonly parentID: BlockId;
1615
readonly timestamp: UInt;
17-
readonly gasLimit: VTHO;
16+
readonly gasLimit: Gas;
1817
readonly beneficiary: Address;
19-
readonly gasUsed: VTHO;
18+
readonly gasUsed: Gas;
2019
readonly totalScore: UInt;
2120
readonly txsRoot: ThorId;
2221
readonly txsFeatures: UInt;
@@ -33,9 +32,9 @@ class SubscriptionBlockResponse {
3332
this.size = UInt.of(json.size);
3433
this.parentID = BlockId.of(json.parentID);
3534
this.timestamp = UInt.of(json.timestamp);
36-
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
35+
this.gasLimit = Gas.of(json.gasLimit);
3736
this.beneficiary = Address.of(json.beneficiary);
38-
this.gasUsed = VTHO.of(json.gasUsed, Units.wei);
37+
this.gasUsed = Gas.of(json.gasUsed);
3938
this.totalScore = UInt.of(json.totalScore);
4039
this.txsRoot = ThorId.of(json.txsRoot);
4140
this.txsFeatures = UInt.of(json.txsFeatures);
@@ -56,9 +55,9 @@ class SubscriptionBlockResponse {
5655
size: this.size.valueOf(),
5756
parentID: this.parentID.toString(),
5857
timestamp: this.timestamp.valueOf(),
59-
gasLimit: Number(this.gasLimit.wei),
58+
gasLimit: this.gasLimit.valueOf(),
6059
beneficiary: this.beneficiary.toString(),
61-
gasUsed: Number(this.gasUsed.wei),
60+
gasUsed: this.gasUsed.valueOf(),
6261
totalScore: this.totalScore.valueOf(),
6362
txsRoot: this.txsRoot.toString(),
6463
txsFeatures: this.txsFeatures.valueOf(),

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Clause, type ClauseJSON } from './Clause';
22
import { TxMeta, type TxMetaJSON } from './TxMeta';
3-
import { Address, BlockId, Nonce, TxId, UInt, VTHO } from '@vechain/sdk-core';
3+
import { Address, BlockId, Gas, Nonce, TxId, UInt } from '@vechain/sdk-core';
44

55
class GetTxResponse {
66
readonly id: TxId;
@@ -12,7 +12,7 @@ class GetTxResponse {
1212
readonly expiration: UInt;
1313
readonly clauses: Clause[];
1414
readonly gasPriceCoef: UInt;
15-
readonly gas: VTHO;
15+
readonly gas: Gas;
1616
readonly dependsOn?: TxId | null;
1717
readonly nonce: Nonce;
1818
readonly meta: TxMeta;
@@ -30,7 +30,7 @@ class GetTxResponse {
3030
return new Clause(clauseJSON);
3131
});
3232
this.gasPriceCoef = UInt.of(json.gasPriceCoef);
33-
this.gas = VTHO.of(json.gas);
33+
this.gas = Gas.of(json.gas);
3434
this.dependsOn =
3535
json.dependsOn !== undefined && json.dependsOn !== null
3636
? TxId.of(json.dependsOn)
@@ -51,7 +51,7 @@ class GetTxResponse {
5151
expiration: this.expiration.valueOf(),
5252
clauses: this.clauses?.map((clause) => clause.toJSON()),
5353
gasPriceCoef: this.gasPriceCoef.valueOf(),
54-
gas: Number(this.gas.wei),
54+
gas: this.gas.valueOf(),
5555
dependsOn:
5656
this.dependsOn !== undefined && this.dependsOn !== null
5757
? this.dependsOn.toString()

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import { ReceiptOutput, type ReceiptOutputJSON } from './ReceiptOutput';
2-
import { Address, Hex, HexUInt, Units, VTHO } from '@vechain/sdk-core';
2+
import { Address, Gas, Hex, HexUInt, Units, VTHO } from '@vechain/sdk-core';
33

44
class Receipt {
5-
readonly gasUsed: VTHO;
5+
readonly gasUsed: Gas;
66
readonly gasPayer: Address;
77
readonly paid: VTHO;
88
readonly reward: VTHO;
99
readonly reverted: boolean;
1010
readonly outputs: ReceiptOutput[];
1111

1212
constructor(json: ReceiptJSON) {
13-
this.gasUsed = VTHO.of(json.gasUsed);
13+
this.gasUsed = Gas.of(json.gasUsed);
1414
this.gasPayer = Address.of(json.gasPayer);
1515
this.paid = VTHO.of(Hex.of(json.paid).bi, Units.wei);
1616
this.reward = VTHO.of(Hex.of(json.reward).bi, Units.wei);
@@ -20,7 +20,7 @@ class Receipt {
2020

2121
toJSON(): ReceiptJSON {
2222
return {
23-
gasUsed: Number(this.gasUsed.wei),
23+
gasUsed: this.gasUsed.valueOf(),
2424
gasPayer: this.gasPayer.toString(),
2525
paid: HexUInt.of(this.paid.wei).toString(),
2626
reward: HexUInt.of(this.reward.wei).toString(),

0 commit comments

Comments
 (0)