Skip to content

Commit

Permalink
simplify subnet_api and parentchain_api to xdc_api
Browse files Browse the repository at this point in the history
  • Loading branch information
wanwiset25 committed Jan 15, 2024
1 parent 469aa7f commit 7628424
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 46 deletions.
38 changes: 2 additions & 36 deletions backend/src/client/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,17 @@ export interface NetworkInfo {
}

export interface Web3WithExtension extends Web3 {
xdcSubnet: {
xdcApi: {
getV2Block: (type: 'committed') => Promise<FetchedV2BlockInfo>;
getV2BlockByNumber: (blockNum: string) => Promise<FetchedV2BlockInfo>;
getV2BlockByHash: (blockHash: string) => Promise<FetchedV2BlockInfo>;
getMasternodesByNumber: (blockStatus: BlockStatus) => Promise<MasternodesInfo>;
getCandidates: (param: 'latest') => Promise<Candidates>;
getNetworkInfo: () => Promise<NetworkInfo>;
};
xdcParentnet: {
getV2Block: (type: 'committed') => Promise<FetchedV2BlockInfo>;
getV2BlockByNumber: (blockNum: string) => Promise<FetchedV2BlockInfo>;
getV2BlockByHash: (blockHash: string) => Promise<FetchedV2BlockInfo>;
getNetworkInfo: () => Promise<NetworkInfo>;
}
}

export const subnetExtensions = (extensionName = 'xdcSubnet') => {
export const xdcExtensions = (extensionName = 'xdcApi') => {
return {
property: extensionName,
methods: [
Expand Down Expand Up @@ -101,31 +95,3 @@ export const subnetExtensions = (extensionName = 'xdcSubnet') => {
],
};
};

export const parentnetExtensions = (extensionName = 'xdcParentnet') => {
return {
property: extensionName,
methods: [
{
name: 'getV2Block',
params: 1,
call: 'XDPoS_getV2BlockByNumber',
},
{
name: 'getV2BlockByNumber',
params: 1,
call: 'XDPoS_getV2BlockByNumber',
},
{
name: 'getV2BlockByHash',
params: 1,
call: 'XDPoS_getV2BlockByHash',
},
{
name: 'getNetworkInfo',
params: 0,
call: 'XDPoS_networkInformation',
},
],
};
};
6 changes: 3 additions & 3 deletions backend/src/client/parentchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { abi } from './contract';
import { logger } from '../../utils/logger';
import { CHECKPOINT_CONTRACT, PARENTNET_URL } from '../../config';
import { HttpException } from '../../exceptions/httpException';
import { parentnetExtensions, Web3WithExtension } from '../extensions';
import { xdcExtensions, Web3WithExtension } from '../extensions';

export interface SmartContractAuditedBlockInfo {
smartContractHash: string;
Expand All @@ -21,7 +21,7 @@ export class ParentChainClient {
constructor() {
const keepaliveAgent = new HttpsAgent();
const provider = new Web3.providers.HttpProvider(PARENTNET_URL, { keepAlive: true, agent: { https: keepaliveAgent } });
this.web3 = new Web3(provider).extend(parentnetExtensions());
this.web3 = new Web3(provider).extend(xdcExtensions());
this.smartContractInstance = new this.web3.eth.Contract(abi as any[], CHECKPOINT_CONTRACT);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ export class ParentChainClient {
timestamp: 0,
}
}
const { Committed, Hash, Miner, Timestamp } = await this.web3.xdcParentnet.getV2BlockByNumber(Web3.utils.numberToHex(mainnet_num));
const { Committed, Hash, Miner, Timestamp } = await this.web3.xdcApi.getV2BlockByNumber(Web3.utils.numberToHex(mainnet_num));
return {
isCommitted: Committed && finalized,
parentchainHash: Hash,
Expand Down
14 changes: 7 additions & 7 deletions backend/src/client/subnet/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Web3 from 'web3';
import { HttpsAgent } from 'agentkeepalive';
import { subnetExtensions, Web3WithExtension } from '../extensions';
import { xdcExtensions, Web3WithExtension } from '../extensions';
import { logger } from '../../utils/logger';
import { HttpException } from '../../exceptions/httpException';
import { SUBNET_URL } from '../../config';
Expand All @@ -19,12 +19,12 @@ export class SubnetClient {
const keepaliveAgent = new HttpsAgent();
const provider = new Web3.providers.HttpProvider(SUBNET_URL, { keepAlive: true, agent: { https: keepaliveAgent } });

this.web3 = new Web3(provider).extend(subnetExtensions());
this.web3 = new Web3(provider).extend(xdcExtensions());
}

async getCandidates() {
try {
const { candidates, success } = await this.web3.xdcSubnet.getCandidates('latest');
const { candidates, success } = await this.web3.xdcApi.getCandidates('latest');
if (!success) {
throw new Error('Failed on getting the candidates data');
}
Expand All @@ -37,7 +37,7 @@ export class SubnetClient {

async getNetworkInfo() {
try {
const { NetworkId, XDCValidatorAddress, Denom, NetworkName } = await this.web3.xdcSubnet.getNetworkInfo();
const { NetworkId, XDCValidatorAddress, Denom, NetworkName } = await this.web3.xdcApi.getNetworkInfo();
return {
networkId: NetworkId,
validatorSmartContractAddress: XDCValidatorAddress,
Expand All @@ -52,7 +52,7 @@ export class SubnetClient {

async getLastMasternodesInformation(): Promise<MasternodesInfo> {
try {
const { Number, Round, Masternodes, Penalty } = await this.web3.xdcSubnet.getMasternodesByNumber('latest');
const { Number, Round, Masternodes, Penalty } = await this.web3.xdcApi.getMasternodesByNumber('latest');
return {
currentBlockNumber: Number,
currentRound: Round,
Expand All @@ -67,7 +67,7 @@ export class SubnetClient {

async getLatestCommittedBlockInfo(): Promise<{ hash: string; number: number; round: number }> {
try {
const { Hash, Number, Round } = await this.web3.xdcSubnet.getV2Block('committed');
const { Hash, Number, Round } = await this.web3.xdcApi.getV2Block('committed');
return {
hash: Hash,
number: Number,
Expand All @@ -80,7 +80,7 @@ export class SubnetClient {
}

async getBlockInfoByHash(hash: string) {
const { Hash, Number, Committed, Miner, Timestamp } = await this.web3.xdcSubnet.getV2BlockByHash(hash);
const { Hash, Number, Committed, Miner, Timestamp } = await this.web3.xdcApi.getV2BlockByHash(hash);
if (!Hash || !Number) {
logger.warn(`Invalid block hash or height or ParentHash received, hash: ${hash}, number: ${Number}`);
throw new HttpException(404, 'No such block exit in subnet');
Expand Down

0 comments on commit 7628424

Please sign in to comment.