Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: created gas class #1894

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions packages/core/src/vcdm/Gas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { InvalidDataType } from '@vechain/sdk-errors';
import { VTHO } from './currency/VTHO';
import { Units } from './currency/Units';
import { UInt } from './UInt';

class Gas extends UInt {
protected constructor(value: number) {
super(value);
}

static of(exp: number): Gas {
if (exp >= 0 && Number.isInteger(exp)) {
return new Gas(exp);
}
throw new InvalidDataType(
'Gas.of',
'not an unsigned integer expression',
{
exp: `${exp}`
}
);
}

/**
*
* @returns The value of this Gas object as a VTHO.
* 10^5 gas = 1 VTHO
* 1 gas = 10^13 weiVTHO
*/
toVTHO(): VTHO {
return VTHO.of(this.valueOf() ** Math.pow(10, 13), Units.wei);
}
}

export { Gas };
1 change: 1 addition & 0 deletions packages/core/src/vcdm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './BloomFilter';
export * from './currency';
export * from './encoding';
export * from './FixedPointNumber';
export * from './Gas';
export * from './hash';
export * from './Hex';
export * from './HexInt';
Expand Down
9 changes: 4 additions & 5 deletions packages/thorest/src/thor/accounts/ExecuteCodesRequest.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Clause, type ClauseJSON } from '../transactions';
import { Address, BlockRef, UInt, Units, VTHO } from '@vechain/sdk-core';
import { Address, BlockRef, Gas, UInt, Units, VTHO } from '@vechain/sdk-core';

class ExecuteCodesRequest {
readonly provedWork?: string;
readonly gasPayer?: Address;
readonly expiration?: UInt;
readonly blockRef?: BlockRef;
readonly clauses?: Clause[];
readonly gas?: VTHO;
readonly gas?: Gas;
readonly gasPrice?: VTHO;
readonly caller?: Address;

Expand All @@ -29,8 +29,7 @@ class ExecuteCodesRequest {
: json.clauses.map(
(clauseJSON: ClauseJSON): Clause => new Clause(clauseJSON)
);
this.gas =
json.gas === undefined ? undefined : VTHO.of(json.gas, Units.wei);
this.gas = json.gas === undefined ? undefined : Gas.of(json.gas);
this.gasPrice =
json.gasPrice === undefined
? undefined
Expand All @@ -46,7 +45,7 @@ class ExecuteCodesRequest {
expiration: this.expiration?.valueOf(),
blockRef: this.blockRef?.toString(),
clauses: this.clauses?.map((clause: Clause) => clause.toJSON()),
gas: this.gas === undefined ? undefined : Number(this.gas.wei),
gas: this.gas === undefined ? undefined : this.gas.valueOf(),
gasPrice:
this.gasPrice === undefined
? undefined
Expand Down
8 changes: 4 additions & 4 deletions packages/thorest/src/thor/accounts/ExecuteCodesResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
Transfer,
type TransferJSON
} from '../transactions';
import { HexUInt, VTHO } from '@vechain/sdk-core';
import { HexUInt, Gas } from '@vechain/sdk-core';

class ExecuteCodeResponse {
readonly data: HexUInt;
readonly events: Event[];
readonly transfers: Transfer[];
readonly gasUsed: VTHO;
readonly gasUsed: Gas;
readonly reverted: boolean;
readonly vmError: string;

Expand All @@ -22,7 +22,7 @@ class ExecuteCodeResponse {
this.transfers = json.transfers.map(
(transferJSON: TransferJSON): Transfer => new Transfer(transferJSON)
);
this.gasUsed = VTHO.of(json.gasUsed);
this.gasUsed = Gas.of(json.gasUsed);
this.reverted = json.reverted;
this.vmError = json.vmError;
}
Expand All @@ -36,7 +36,7 @@ class ExecuteCodeResponse {
transfers: this.transfers.map(
(transfer: Transfer): TransferJSON => transfer.toJSON()
),
gasUsed: Number(this.gasUsed.wei),
gasUsed: this.gasUsed.valueOf(),
reverted: this.reverted,
vmError: this.vmError
} satisfies ExecuteCodeResponseJSON;
Expand Down
15 changes: 7 additions & 8 deletions packages/thorest/src/thor/blocks/CommonBlockResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, BlockId, ThorId, UInt, Units, VTHO } from '@vechain/sdk-core';
import { Address, BlockId, Gas, ThorId, UInt } from '@vechain/sdk-core';

interface CommmonBlockResponseJSON {
number: number;
Expand Down Expand Up @@ -26,10 +26,9 @@ class CommmonBlockResponse {
readonly size: UInt;
readonly parentID: BlockId;
readonly timestamp: UInt;
// to do: use the Gas unit class when it will be available
readonly gasLimit: VTHO;
readonly gasLimit: Gas;
readonly beneficiary: Address;
readonly gasUsed: VTHO;
readonly gasUsed: Gas;
readonly totalScore: UInt;
readonly txsRoot: ThorId;
readonly txsFeatures: UInt;
Expand All @@ -46,9 +45,9 @@ class CommmonBlockResponse {
this.size = UInt.of(json.size);
this.parentID = BlockId.of(json.parentID);
this.timestamp = UInt.of(json.timestamp);
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
this.gasLimit = Gas.of(json.gasLimit);
this.beneficiary = Address.of(json.beneficiary);
this.gasUsed = VTHO.of(json.gasUsed, Units.wei);
this.gasUsed = Gas.of(json.gasUsed);
this.totalScore = UInt.of(json.totalScore);
this.txsRoot = ThorId.of(json.txsRoot);
this.txsFeatures = UInt.of(json.txsFeatures);
Expand All @@ -67,9 +66,9 @@ class CommmonBlockResponse {
size: this.size.valueOf(),
parentID: this.parentID.toString(),
timestamp: this.timestamp.valueOf(),
gasLimit: Number(this.gasLimit.wei),
gasLimit: this.gasLimit.valueOf(),
beneficiary: this.beneficiary.toString(),
gasUsed: Number(this.gasUsed.wei),
gasUsed: this.gasUsed.valueOf(),
totalScore: this.totalScore.valueOf(),
txsRoot: this.txsRoot.toString(),
txsFeatures: this.txsFeatures.valueOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Tracer, type TracerName } from './TracerName';
import {
Address,
type BlockRef,
Gas,
HexUInt,
UInt,
Units,
Expand All @@ -15,7 +16,7 @@ class PostDebugTracerCallRequest {
readonly value: VET;
readonly data: HexUInt;
readonly to?: Address;
readonly gas?: VTHO;
readonly gas?: Gas;
readonly gasPrice?: VTHO;
readonly caller?: Address;
readonly provedWork?: string;
Expand All @@ -29,8 +30,7 @@ class PostDebugTracerCallRequest {
this.value = VET.of(json.value);
this.data = HexUInt.of(json.data);
this.to = json.to === undefined ? undefined : Address.of(json.to);
this.gas =
json.gas === undefined ? undefined : VTHO.of(json.gas, Units.wei);
this.gas = json.gas === undefined ? undefined : Gas.of(json.gas);
this.gasPrice =
json.gasPrice === undefined
? undefined
Expand All @@ -53,7 +53,7 @@ class PostDebugTracerCallRequest {
value: HexUInt.of(this.value.wei).toString(),
data: this.data.toString(),
to: this.to?.toString(),
gas: this.gas === undefined ? undefined : Number(this.gas.wei),
gas: this.gas === undefined ? undefined : this.gas.valueOf(),
gasPrice:
this.gasPrice === undefined
? undefined
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BlockId, HexUInt, UInt, Units, VTHO } from '@vechain/sdk-core';
import { BlockId, Gas, HexUInt, UInt } from '@vechain/sdk-core';

class SubscriptionBeat2Response {
readonly gasLimit: VTHO;
readonly gasLimit: Gas;
readonly obsolete: boolean;
readonly number: UInt;
readonly id: BlockId;
Expand All @@ -12,7 +12,7 @@ class SubscriptionBeat2Response {
readonly k: UInt;

constructor(json: SubscriptionBeat2ResponseJSON) {
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
this.gasLimit = Gas.of(json.gasLimit);
this.obsolete = json.obsolete;
this.number = UInt.of(json.number);
this.id = BlockId.of(json.id);
Expand All @@ -25,7 +25,7 @@ class SubscriptionBeat2Response {

toJSON(): SubscriptionBeat2ResponseJSON {
return {
gasLimit: Number(this.gasLimit.wei),
gasLimit: this.gasLimit.valueOf(),
obsolete: this.obsolete,
number: this.number.valueOf(),
id: this.id.toString(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {
Address,
BlockId,
Gas,
ThorId,
type TxId,
UInt,
Units,
VTHO
UInt
} from '@vechain/sdk-core';

class SubscriptionBlockResponse {
Expand All @@ -14,9 +13,9 @@ class SubscriptionBlockResponse {
readonly size: UInt;
readonly parentID: BlockId;
readonly timestamp: UInt;
readonly gasLimit: VTHO;
readonly gasLimit: Gas;
readonly beneficiary: Address;
readonly gasUsed: VTHO;
readonly gasUsed: Gas;
readonly totalScore: UInt;
readonly txsRoot: ThorId;
readonly txsFeatures: UInt;
Expand All @@ -33,9 +32,9 @@ class SubscriptionBlockResponse {
this.size = UInt.of(json.size);
this.parentID = BlockId.of(json.parentID);
this.timestamp = UInt.of(json.timestamp);
this.gasLimit = VTHO.of(json.gasLimit, Units.wei);
this.gasLimit = Gas.of(json.gasLimit);
this.beneficiary = Address.of(json.beneficiary);
this.gasUsed = VTHO.of(json.gasUsed, Units.wei);
this.gasUsed = Gas.of(json.gasUsed);
this.totalScore = UInt.of(json.totalScore);
this.txsRoot = ThorId.of(json.txsRoot);
this.txsFeatures = UInt.of(json.txsFeatures);
Expand All @@ -56,9 +55,9 @@ class SubscriptionBlockResponse {
size: this.size.valueOf(),
parentID: this.parentID.toString(),
timestamp: this.timestamp.valueOf(),
gasLimit: Number(this.gasLimit.wei),
gasLimit: this.gasLimit.valueOf(),
beneficiary: this.beneficiary.toString(),
gasUsed: Number(this.gasUsed.wei),
gasUsed: this.gasUsed.valueOf(),
totalScore: this.totalScore.valueOf(),
txsRoot: this.txsRoot.toString(),
txsFeatures: this.txsFeatures.valueOf(),
Expand Down
8 changes: 4 additions & 4 deletions packages/thorest/src/thor/transactions/GetTxResponse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Clause, type ClauseJSON } from './Clause';
import { TxMeta, type TxMetaJSON } from './TxMeta';
import { Address, BlockId, Nonce, TxId, UInt, VTHO } from '@vechain/sdk-core';
import { Address, BlockId, Gas, Nonce, TxId, UInt } from '@vechain/sdk-core';

class GetTxResponse {
readonly id: TxId;
Expand All @@ -12,7 +12,7 @@ class GetTxResponse {
readonly expiration: UInt;
readonly clauses: Clause[];
readonly gasPriceCoef: UInt;
readonly gas: VTHO;
readonly gas: Gas;
readonly dependsOn?: TxId | null;
readonly nonce: Nonce;
readonly meta: TxMeta;
Expand All @@ -30,7 +30,7 @@ class GetTxResponse {
return new Clause(clauseJSON);
});
this.gasPriceCoef = UInt.of(json.gasPriceCoef);
this.gas = VTHO.of(json.gas);
this.gas = Gas.of(json.gas);
this.dependsOn =
json.dependsOn !== undefined && json.dependsOn !== null
? TxId.of(json.dependsOn)
Expand All @@ -51,7 +51,7 @@ class GetTxResponse {
expiration: this.expiration.valueOf(),
clauses: this.clauses?.map((clause) => clause.toJSON()),
gasPriceCoef: this.gasPriceCoef.valueOf(),
gas: Number(this.gas.wei),
gas: this.gas.valueOf(),
dependsOn:
this.dependsOn !== undefined && this.dependsOn !== null
? this.dependsOn.toString()
Expand Down
8 changes: 4 additions & 4 deletions packages/thorest/src/thor/transactions/Receipt.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ReceiptOutput, type ReceiptOutputJSON } from './ReceiptOutput';
import { Address, Hex, HexUInt, Units, VTHO } from '@vechain/sdk-core';
import { Address, Gas, Hex, HexUInt, Units, VTHO } from '@vechain/sdk-core';

class Receipt {
readonly gasUsed: VTHO;
readonly gasUsed: Gas;
readonly gasPayer: Address;
readonly paid: VTHO;
readonly reward: VTHO;
readonly reverted: boolean;
readonly outputs: ReceiptOutput[];

constructor(json: ReceiptJSON) {
this.gasUsed = VTHO.of(json.gasUsed);
this.gasUsed = Gas.of(json.gasUsed);
this.gasPayer = Address.of(json.gasPayer);
this.paid = VTHO.of(Hex.of(json.paid).bi, Units.wei);
this.reward = VTHO.of(Hex.of(json.reward).bi, Units.wei);
Expand All @@ -20,7 +20,7 @@ class Receipt {

toJSON(): ReceiptJSON {
return {
gasUsed: Number(this.gasUsed.wei),
gasUsed: this.gasUsed.valueOf(),
gasPayer: this.gasPayer.toString(),
paid: HexUInt.of(this.paid.wei).toString(),
reward: HexUInt.of(this.reward.wei).toString(),
Expand Down
Loading