Skip to content

Commit

Permalink
Merge pull request #182 from ElrondNetwork/network-obj
Browse files Browse the repository at this point in the history
Breaking change: unifying provider interfaces, preparing network providers for extraction - step 3
  • Loading branch information
andreibancioiu authored Apr 6, 2022
2 parents e119bd2 + 5edfba1 commit 97793ed
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src-network-providers/networkProvider/apiNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import axios, { AxiosRequestConfig } from "axios";
import { AccountOnNetwork } from "./accounts";
import { IAddress, IContractQueryResponse, IDefinitionOfFungibleTokenOnNetwork, IDefinitionOfTokenCollectionOnNetwork, IFungibleTokenOfAccountOnNetwork, IHash, INetworkProvider, INonce, INonFungibleTokenOfAccountOnNetwork, ITransaction, Pagination } from "./interface";
import { NetworkConfig } from "../networkConfig";
import { NetworkStake } from "../networkStake";
import { NetworkStatus } from "../networkStatus";
import { NetworkConfig } from "./networkConfig";
import { NetworkStake } from "./networkStake";
import { Query } from "../smartcontracts";
import { Stats } from "../stats";
import { Stats } from "./stats";
import { ContractQueryResponse } from "./contractResults";
import { ProxyNetworkProvider } from "./proxyNetworkProvider";
import { DefinitionOfFungibleTokenOnNetwork, DefinitionOfTokenCollectionOnNetwork } from "./tokenDefinitions";
Expand All @@ -15,6 +14,7 @@ import { TransactionStatus } from "./transactionStatus";
import { Hash } from "./primitives";
import { ErrNetworkProvider } from "./errors";
import { defaultAxiosConfig } from "./config";
import { NetworkStatus } from "./networkStatus";

// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
export class ApiNetworkProvider implements INetworkProvider {
Expand Down
11 changes: 7 additions & 4 deletions src-network-providers/networkProvider/interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BigNumber } from "bignumber.js";
import { AccountOnNetwork } from "./accounts";
import { NetworkConfig } from "../networkConfig";
import { NetworkStake } from "../networkStake";
import { NetworkStatus } from "../networkStatus";
import { NetworkConfig } from "./networkConfig";
import { NetworkStake } from "./networkStake";
import { Query } from "../smartcontracts";
import { Stats } from "../stats";
import { Stats } from "./stats";
import { TransactionOnNetwork } from "./transactions";
import { TransactionStatus } from "./transactionStatus";
import { NetworkStatus } from "./networkStatus";

/**
* An interface that defines the endpoints of an HTTP API Provider.
Expand Down Expand Up @@ -195,4 +195,7 @@ export interface INonce extends IHexable { valueOf(): number; }
export interface ITransactionPayload { encoded(): string; }
export interface IGasLimit { valueOf(): number; }
export interface IGasPrice { valueOf(): number; }
export interface IChainID { valueOf(): string; }
export interface IGasPriceModifier { valueOf(): number; }
export interface ITransactionVersion { valueOf(): number; }
export interface IAccountBalance { toString(): string; }
88 changes: 88 additions & 0 deletions src-network-providers/networkProvider/networkConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import BigNumber from "bignumber.js";
import { IChainID, IGasLimit, IGasPrice, IGasPriceModifier, ITransactionVersion } from "./interface";

/**
* An object holding Network configuration parameters.
*/
export class NetworkConfig {
/**
* The chain ID. E.g. "1" for the Mainnet.
*/
public ChainID: IChainID;

/**
* The gas required by the Network to process a byte of the {@link TransactionPayload}.
*/
public GasPerDataByte: number;
/**
* The round duration.
*/
public RoundDuration: number;
/**
* The number of rounds per epoch.
*/
public RoundsPerEpoch: number;

/**
* The Top Up Factor for APR calculation
*/
public TopUpFactor: number;

/**
* The Top Up Factor for APR calculation
*/
public TopUpRewardsGradientPoint: BigNumber;

/**
*
*/
public GasPriceModifier: IGasPriceModifier;

/**
* The minimum gas limit required to be set when broadcasting a {@link Transaction}.
*/
public MinGasLimit: IGasLimit;

/**
* The minimum gas price required to be set when broadcasting a {@link Transaction}.
*/
public MinGasPrice: IGasPrice;

/**
* The oldest transaction version accepted by the Network.
*/
public MinTransactionVersion: ITransactionVersion;

constructor() {
this.ChainID = "T";
this.GasPerDataByte = 1500;
this.TopUpFactor = 0;
this.RoundDuration = 0;
this.RoundsPerEpoch = 0;
this.TopUpRewardsGradientPoint = new BigNumber(0);
this.MinGasLimit = 50000;
this.MinGasPrice = 1000000000;
this.GasPriceModifier = 1;
this.MinTransactionVersion = 1;
}

/**
* Constructs a configuration object from a HTTP response (as returned by the provider).
*/
static fromHttpResponse(payload: any): NetworkConfig {
let networkConfig = new NetworkConfig();

networkConfig.ChainID = String(payload["erd_chain_id"]);
networkConfig.GasPerDataByte = Number(payload["erd_gas_per_data_byte"]);
networkConfig.TopUpFactor = Number(payload["erd_top_up_factor"]);
networkConfig.RoundDuration = Number(payload["erd_round_duration"]);
networkConfig.RoundsPerEpoch = Number(payload["erd_rounds_per_epoch"]);
networkConfig.TopUpRewardsGradientPoint = new BigNumber(payload["erd_rewards_top_up_gradient_point"]);
networkConfig.MinGasLimit = Number(payload["erd_min_gas_limit"]);
networkConfig.MinGasPrice = Number(payload["erd_min_gas_price"]);
networkConfig.MinTransactionVersion = Number(payload["erd_min_transaction_version"]);
networkConfig.GasPriceModifier = Number(payload["erd_gas_price_modifier"]);

return networkConfig;
}
}
47 changes: 47 additions & 0 deletions src-network-providers/networkProvider/networkStake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import BigNumber from "bignumber.js";

/**
* An object holding Network stake parameters.
*/
export class NetworkStake {
private static default: NetworkStake;

/**
* The Total Validators Number.
*/
public TotalValidators: number;

/**
* The Active Validators Number.
*/
public ActiveValidators: number;
/**
* The Queue Size.
*/
public QueueSize: number;
/**
* The Total Validators Number.
*/
public TotalStaked: BigNumber;

constructor() {
this.TotalValidators = 0;
this.ActiveValidators = 0;
this.QueueSize = 0;
this.TotalStaked = new BigNumber(0);
}

/**
* Constructs a configuration object from a HTTP response (as returned by the provider).
*/
static fromHttpResponse(payload: any): NetworkStake {
let networkStake = new NetworkStake();

networkStake.TotalValidators = Number(payload["totalValidators"]);
networkStake.ActiveValidators = Number(payload["activeValidators"]);
networkStake.QueueSize = Number(payload["queueSize"]);
networkStake.TotalStaked = new BigNumber(payload["totalStaked"]);

return networkStake;
}
}
82 changes: 82 additions & 0 deletions src-network-providers/networkProvider/networkStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* An object holding network status configuration parameters.
*/
export class NetworkStatus {
private static default: NetworkStatus;

/**
* The current round.
*/
public CurrentRound: number;

/**
* The epoch number.
*/
public EpochNumber: number;

/**
* The Highest final nonce.
*/
public HighestFinalNonce: number;

/**
* The erd nonce.
*/
public Nonce: number;

/**
* The nonce at epoch start.
*/
public NonceAtEpochStart: number;

/**
* The nonces passed in current epoch.
*/
public NoncesPassedInCurrentEpoch: number;

/**
* The round at epoch start
*/
public RoundAtEpochStart: number;

/**
* The rounds passed in current epoch
*/
public RoundsPassedInCurrentEpoch: number;

/**
* The rounds per epoch
*/
public RoundsPerEpoch: number;

constructor() {
this.CurrentRound = 0;
this.EpochNumber = 0;
this.HighestFinalNonce = 0;
this.Nonce = 0;
this.NonceAtEpochStart = 0;
this.NoncesPassedInCurrentEpoch = 0;
this.RoundAtEpochStart = 0;
this.RoundsPassedInCurrentEpoch = 0;
this.RoundsPerEpoch = 0;
}

/**
* Constructs a configuration object from a HTTP response (as returned by the provider).
*/
static fromHttpResponse(payload: any): NetworkStatus {
let networkStatus = new NetworkStatus();

networkStatus.CurrentRound = Number(payload["erd_current_round"]);
networkStatus.EpochNumber = Number(payload["erd_epoch_number"]);
networkStatus.HighestFinalNonce = Number(payload["erd_highest_final_nonce"]);
networkStatus.Nonce = Number(payload["erd_nonce"]);
networkStatus.NonceAtEpochStart = Number(payload["erd_nonce_at_epoch_start"]);
networkStatus.NoncesPassedInCurrentEpoch = Number(payload["erd_nonces_passed_in_current_epoch"]);
networkStatus.RoundAtEpochStart = Number(payload["erd_round_at_epoch_start"]);
networkStatus.RoundsPassedInCurrentEpoch = Number(payload["erd_rounds_passed_in_current_epoch"]);
networkStatus.RoundsPerEpoch = Number(payload["erd_rounds_per_epoch"]);

return networkStatus;
}
}
8 changes: 4 additions & 4 deletions src-network-providers/networkProvider/proxyNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import axios, { AxiosRequestConfig } from "axios";
import { AccountOnNetwork } from "./accounts";
import { IAddress, IContractQueryResponse, IDefinitionOfFungibleTokenOnNetwork, IDefinitionOfTokenCollectionOnNetwork, IFungibleTokenOfAccountOnNetwork, IHash, INetworkProvider, INonce, INonFungibleTokenOfAccountOnNetwork, ITransaction, Pagination } from "./interface";
import { NetworkConfig } from "../networkConfig";
import { NetworkStake } from "../networkStake";
import { NetworkStatus } from "../networkStatus";
import { NetworkConfig } from "./networkConfig";
import { NetworkStake } from "./networkStake";
import { Query } from "../smartcontracts";
import { Stats } from "../stats";
import { Stats } from "./stats";
import { ContractQueryResponse } from "./contractResults";
import { FungibleTokenOfAccountOnNetwork, NonFungibleTokenOfAccountOnNetwork } from "./tokens";
import { TransactionOnNetwork } from "./transactions";
import { TransactionStatus } from "./transactionStatus";
import { Hash } from "./primitives";
import { ErrNetworkProvider } from "./errors";
import { defaultAxiosConfig } from "./config";
import { NetworkStatus } from "./networkStatus";

// TODO: Find & remove duplicate code between "ProxyNetworkProvider" and "ApiNetworkProvider".
export class ProxyNetworkProvider implements INetworkProvider {
Expand Down
69 changes: 69 additions & 0 deletions src-network-providers/networkProvider/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* An object holding Network stats parameters.
*/
export class Stats {
private static default: Stats;

/**
* The number of Shards.
*/
public Shards: number;

/**
* The Number of Blocks.
*/
public Blocks: number;
/**
* The Number of Accounts.
*/
public Accounts: number;
/**
* The Number of transactions.
*/
public Transactions: number;
/**
* The Refresh rate.
*/
public RefreshRate: number;
/**
* The Number of the current Epoch.
*/
public Epoch: number;
/**
* The Number of rounds passed.
*/
public RoundsPassed: number;
/**
* The Number of Rounds per epoch.
*/
public RoundsPerEpoch: number;

constructor() {
this.Shards = 0;
this.Blocks = 0;
this.Accounts = 0;
this.Transactions = 0;
this.RefreshRate = 0;
this.Epoch = 0;
this.RoundsPassed = 0;
this.RoundsPerEpoch = 0;
}

/**
* Constructs a stats object from a HTTP response (as returned by the provider).
*/
static fromHttpResponse(payload: any): Stats {
let stats = new Stats();

stats.Shards = Number(payload["shards"]);
stats.Blocks = Number(payload["blocks"]);
stats.Accounts = Number(payload["accounts"]);
stats.Transactions = Number(payload["transactions"]);
stats.RefreshRate = Number(payload["refreshRate"]);
stats.Epoch = Number(payload["epoch"]);
stats.RoundsPassed = Number(payload["roundsPassed"]);
stats.RoundsPerEpoch = Number(payload["roundsPerEpoch"]);

return stats;
}
}

0 comments on commit 97793ed

Please sign in to comment.