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 e71e871 + c7d1e4c commit 9189d40
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 107 deletions.
2 changes: 0 additions & 2 deletions src/_docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ export * from "./account";
export * from "./address";
export * from "./nonce";
export * from "./balance";
export * from "./networkConfig";
export * from "./networkStake";
export * from "./networkParams";
export * from "./utils";
12 changes: 5 additions & 7 deletions src/apiProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import axios, { AxiosRequestConfig } from "axios";
import { IApiProvider, IHash } from "./interface";
import * as errors from "./errors";
import { Logger } from "./logger";
import { NetworkStake } from "./networkStake";
import { Stats } from "./stats";
import { Token } from "./token";
import { NFTToken } from "./nftToken";
import { defaultConfig } from "./constants";
import { ApiNetworkProvider } from "./networkProvider/apiNetworkProvider";
import { ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";
import { INetworkStake, INetworkStats, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";

/**
* This is a temporary change, this will be the only provider used, ProxyProvider will be deprecated
Expand All @@ -35,15 +33,15 @@ export class ApiProvider implements IApiProvider {
/**
* Fetches the Network Stake.
*/
async getNetworkStake(): Promise<NetworkStake> {
return this.doGetGeneric("stake", (response) => NetworkStake.fromHttpResponse(response));
async getNetworkStake(): Promise<INetworkStake> {
return await this.backingProvider.getNetworkStakeStatistics();
}

/**
* Fetches the Network Stats.
*/
async getNetworkStats(): Promise<Stats> {
return this.doGetGeneric("stats", (response) => Stats.fromHttpResponse(response));
async getNetworkStats(): Promise<INetworkStats> {
return await this.backingProvider.getNetworkGeneralStatistics();
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export * from "./balanceBuilder";
export * from "./boundaryAdapters";
export * from "./interactive";
export * from "./logger";
export * from "./networkConfig";
export * from "./networkStake";
export * from "./networkParams";
export * from "./signableMessage";
export * from "./utils";
Expand Down
17 changes: 8 additions & 9 deletions src/interface.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Transaction } from "./transaction";
import { NetworkConfig } from "./networkConfig";
import { Signature } from "./signature";
import { Query } from "./smartcontracts";
import { QueryResponse } from "./smartcontracts";
import { NetworkStake } from "./networkStake";
import { Stats } from "./stats";
import { NetworkStatus } from "./networkStatus";
import { Token } from "./token";
import BigNumber from "bignumber.js";
import { IAccountOnNetwork, IFungibleTokenOfAccountOnNetwork, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";
import { IAccountOnNetwork, IFungibleTokenOfAccountOnNetwork, INetworkConfig, INetworkStake, INetworkStats, INetworkStatus, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";

export interface ITransactionFetcher {
/**
Expand All @@ -29,12 +25,12 @@ export interface IProvider extends ITransactionFetcher {
/**
* Fetches the Network configuration.
*/
getNetworkConfig(): Promise<NetworkConfig>;
getNetworkConfig(): Promise<INetworkConfig>;

/**
* Fetches the Network status.
*/
getNetworkStatus(): Promise<NetworkStatus>;
getNetworkStatus(): Promise<INetworkStatus>;

/**
* Fetches the state of an {@link Account}.
Expand Down Expand Up @@ -89,11 +85,11 @@ export interface IApiProvider extends ITransactionFetcher {
/**
* Fetches the Network Stake.
*/
getNetworkStake(): Promise<NetworkStake>;
getNetworkStake(): Promise<INetworkStake>;
/**
* Fetches the Network Stats.
*/
getNetworkStats(): Promise<Stats>;
getNetworkStats(): Promise<INetworkStats>;

getToken(tokenIdentifier: string): Promise<Token>;

Expand Down Expand Up @@ -149,3 +145,6 @@ export interface ITransactionValue { toString(): string; }
export interface IAccountBalance { toString(): string; }
export interface ITransactionPayload { encoded(): string; }
export interface INonce { valueOf(): number; }
export interface IChainID { valueOf(): string; }
export interface IGasLimit { valueOf(): number; }
export interface IGasPrice { valueOf(): number; }
26 changes: 25 additions & 1 deletion src/interfaceOfNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IAccountBalance, IBech32Address, IHash, INonce, ITransactionPayload, ITransactionValue } from "./interface";
import { IAccountBalance, IBech32Address, IChainID, IGasLimit, IHash, INonce, ITransactionPayload, ITransactionValue } from "./interface";

/**
* @deprecated This interface will be removed upon the extraction of networkProvider package.
Expand All @@ -14,6 +14,30 @@ export interface IAccountOnNetwork {
export interface IFungibleTokenOfAccountOnNetwork {
}

/**
* @deprecated This interface will be removed upon the extraction of networkProvider package.
*/
export interface INetworkStatus {
Nonce: number;
}

/**
* @deprecated This interface will be removed upon the extraction of networkProvider package.
*/
export interface INetworkStake {
}

/**
* @deprecated This interface will be removed upon the extraction of networkProvider package.
*/
export interface INetworkStats {
}

export interface INetworkConfig {
MinGasLimit: IGasLimit;
ChainID: IChainID;
}

export interface ITransactionOnNetwork {
hash: IHash;
type: string;
Expand Down
8 changes: 4 additions & 4 deletions src/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/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; }
34 changes: 17 additions & 17 deletions src/networkConfig.ts → src/networkProvider/networkConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BigNumber from "bignumber.js";
import { GasPrice, GasLimit, TransactionVersion, ChainID, GasPriceModifier } from "./networkParams";
import { IChainID, IGasLimit, IGasPrice, IGasPriceModifier, ITransactionVersion } from "./interface";

/**
* An object holding Network configuration parameters.
Expand All @@ -8,7 +8,7 @@ export class NetworkConfig {
/**
* The chain ID. E.g. "1" for the Mainnet.
*/
public ChainID: ChainID;
public ChainID: IChainID;

/**
* The gas required by the Network to process a byte of the {@link TransactionPayload}.
Expand Down Expand Up @@ -36,34 +36,34 @@ export class NetworkConfig {
/**
*
*/
public GasPriceModifier: GasPriceModifier;
public GasPriceModifier: IGasPriceModifier;

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

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

/**
* The oldest {@link TransactionVersion} accepted by the Network.
* The oldest transaction version accepted by the Network.
*/
public MinTransactionVersion: TransactionVersion;
public MinTransactionVersion: ITransactionVersion;

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

/**
Expand All @@ -72,16 +72,16 @@ export class NetworkConfig {
static fromHttpResponse(payload: any): NetworkConfig {
let networkConfig = new NetworkConfig();

networkConfig.ChainID = new ChainID(payload["erd_chain_id"]);
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 = new GasLimit(payload["erd_min_gas_limit"]);
networkConfig.MinGasPrice = new GasPrice(payload["erd_min_gas_price"]);
networkConfig.MinTransactionVersion = new TransactionVersion(payload["erd_min_transaction_version"]);
networkConfig.GasPriceModifier = new GasPriceModifier(payload["erd_gas_price_modifier"]);
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;
}
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/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
File renamed without changes.
19 changes: 5 additions & 14 deletions src/proxyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@ import BigNumber from "bignumber.js";

import { IProvider } from "./interface";
import { Transaction, TransactionHash } from "./transaction";
import { NetworkConfig } from "./networkConfig";
import { Address } from "./address";
import * as errors from "./errors";
import { Query } from "./smartcontracts/query";
import { QueryResponse } from "./smartcontracts/queryResponse";
import { Logger } from "./logger";
import { NetworkStatus } from "./networkStatus";
import { defaultConfig } from "./constants";
import { ProxyNetworkProvider } from "./networkProvider/proxyNetworkProvider";
import { IAccountOnNetwork, IFungibleTokenOfAccountOnNetwork, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";

/**
* This will be deprecated once all the endpoints move to ApiProvider
*/

import { IAccountOnNetwork, IFungibleTokenOfAccountOnNetwork, INetworkConfig, INetworkStatus, ITransactionOnNetwork, ITransactionStatus } from "./interfaceOfNetwork";

export class ProxyProvider implements IProvider {
private url: string;
Expand Down Expand Up @@ -112,17 +105,15 @@ export class ProxyProvider implements IProvider {
/**
* Fetches the Network configuration.
*/
async getNetworkConfig(): Promise<NetworkConfig> {
return this.doGetGeneric("network/config", (response) => NetworkConfig.fromHttpResponse(response.config));
async getNetworkConfig(): Promise<INetworkConfig> {
return await this.backingProvider.getNetworkConfig();
}

/**
* Fetches the network status configuration.
*/
async getNetworkStatus(): Promise<NetworkStatus> {
return this.doGetGeneric("network/status/4294967295", (response) =>
NetworkStatus.fromHttpResponse(response.status)
);
async getNetworkStatus(): Promise<INetworkStatus> {
return await this.backingProvider.getNetworkStatus();
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/smartcontracts/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Nonce } from "../nonce";
import { ESDTNFT_TRANSFER_FUNCTION_NAME, ESDT_TRANSFER_FUNCTION_NAME, MULTI_ESDTNFT_TRANSFER_FUNCTION_NAME } from "../constants";
import { Account } from "../account";
import { CallArguments } from "./interface";
import { IChainID, IGasLimit, IGasPrice } from "../interface";

/**
* Internal interface: the smart contract, as seen from the perspective of an {@link Interaction}.
Expand All @@ -32,9 +33,9 @@ export class Interaction {

private nonce: Nonce = new Nonce(0);
private value: Balance = Balance.Zero();
private gasLimit: GasLimit = new GasLimit(0);
private gasPrice: GasPrice | undefined = undefined;
private chainID: ChainID = ChainID.unspecified();
private gasLimit: IGasLimit = new GasLimit(0);
private gasPrice: IGasPrice | undefined = undefined;
private chainID: IChainID = ChainID.unspecified();
private querent: Address = new Address();

private isWithSingleESDTTransfer: boolean = false;
Expand Down Expand Up @@ -76,7 +77,7 @@ export class Interaction {
return this.tokenTransfers.getTransfers();
}

getGasLimit(): GasLimit {
getGasLimit(): IGasLimit {
return this.gasLimit;
}

Expand Down Expand Up @@ -185,7 +186,7 @@ export class Interaction {
return this.withNonce(account.getNonceThenIncrement());
}

withChainID(chainID: ChainID): Interaction {
withChainID(chainID: IChainID): Interaction {
this.chainID = chainID;
return this;
}
Expand Down
Loading

0 comments on commit 9189d40

Please sign in to comment.