Skip to content

Commit

Permalink
simple fallback for engine nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
feruzm committed Aug 12, 2024
1 parent 594a450 commit 876ce93
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions src/server/handlers/wallet-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const ENGINE_NODES = [
"https://api2.hive-engine.com/rpc"
];

const BASE_ENGINE_URL = ENGINE_NODES[0];
// min and max included
const randomIntFromInterval = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
}

let BASE_ENGINE_URL = `${ENGINE_NODES[randomIntFromInterval(0,6)]}/contracts`;
const BASE_SPK_URL = 'https://spk.good-karma.xyz';

const ENGINE_REWARDS_URL = 'https://scot-api.hive-engine.com/';
Expand All @@ -34,7 +39,7 @@ export const PATH_CONTRACTS = 'contracts';
//client engine api requests
export const eapi = async (req: express.Request, res: express.Response) => {
const data = req.body;
pipe(engineContractsRequest(data), res);
pipe(engineContractsRequest(data, BASE_ENGINE_URL), res);
}


Expand Down Expand Up @@ -64,8 +69,7 @@ export const engineAccountHistory = (req: express.Request, res: express.Response


//raw engine api call
const engineContractsRequest = (data: EngineRequestPayload) => {
const url = `${BASE_ENGINE_URL}/${PATH_CONTRACTS}`;
const engineContractsRequest = (data: EngineRequestPayload, url: string) => {
const headers = { 'Content-type': 'application/json', 'User-Agent': 'Ecency' };

return baseApiRequest(url, "POST", headers, data)
Expand All @@ -80,8 +84,6 @@ const engineRewardsRequest = (username:string, params:any) => {
}




//engine contracts methods
export const fetchEngineBalances = async (account: string): Promise<TokenBalance[]> => {
const data: EngineRequestPayload = {
Expand All @@ -96,14 +98,25 @@ export const fetchEngineBalances = async (account: string): Promise<TokenBalance
},
id: EngineIds.ONE,
};
try {
const response = await engineContractsRequest(data, BASE_ENGINE_URL);

const response = await engineContractsRequest(data);
if (!response.data?.result) {
throw new Error("Failed to get engine balances")
}

if (!response.data?.result) {
throw new Error("Failed to get engine balances")
return response.data.result;
}
catch (e) {
BASE_ENGINE_URL = `${ENGINE_NODES[randomIntFromInterval(0,6)]}/contracts`;
const response = await engineContractsRequest(data, BASE_ENGINE_URL);

if (!response.data?.result) {
throw new Error("Failed to get engine balances")
}

return response.data.result;
return response.data.result;
}
};


Expand All @@ -120,14 +133,24 @@ export const fetchEngineTokens = async (tokens: string[]): Promise<Token[]> => {
},
id: EngineIds.ONE,
};
try {
const response = await engineContractsRequest(data, BASE_ENGINE_URL);

const response = await engineContractsRequest(data);
if (!response.data?.result) {
throw new Error("Failed to get engine tokens data")
}

if (!response.data?.result) {
throw new Error("Failed to get engine tokens data")
}
return response.data.result;
} catch(e) {
BASE_ENGINE_URL = `${ENGINE_NODES[randomIntFromInterval(0,6)]}/contracts`;
const response = await engineContractsRequest(data, BASE_ENGINE_URL);

return response.data.result;
if (!response.data?.result) {
throw new Error("Failed to get engine tokens data")
}

return response.data.result;
}
}


Expand All @@ -144,14 +167,23 @@ export const fetchEngineMetics = async (tokens: string[]): Promise<EngineMetric[
},
id: EngineIds.ONE,
};
try {
const response = await engineContractsRequest(data, BASE_ENGINE_URL);

const response = await engineContractsRequest(data);
if (!response.data?.result) {
throw new Error("Failed to get engine metrics data")
}

if (!response.data?.result) {
throw new Error("Failed to get engine metrics data")
}
return response.data.result;
} catch(e) {
BASE_ENGINE_URL = `${ENGINE_NODES[randomIntFromInterval(0,6)]}/contracts`;
const response = await engineContractsRequest(data, BASE_ENGINE_URL);

return response.data.result;
if (!response.data?.result) {
throw new Error("Failed to get engine metrics data")
}
return response.data.result;
}
}


Expand Down

0 comments on commit 876ce93

Please sign in to comment.