-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from pendulum-chain/24-create-totaltransferabl…
…etotallockedtotalreserved-api 24 create total transferable, total locked, totalreserved endpoints
- Loading branch information
Showing
4 changed files
with
7,300 additions
and
10,114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { ApiPromise, WsProvider } = require("@polkadot/api"); | ||
const { amplitudeWss, pendulumWss } = require("../../config/vars"); | ||
|
||
const apiInstanceDict = {}; | ||
|
||
const connectApi = async (socketUrl) => { | ||
|
||
const wsProvider = new WsProvider(socketUrl); | ||
return ApiPromise.create({ provider: wsProvider, noInitWarn: true }); | ||
|
||
} | ||
|
||
const getApi = async (network) => { | ||
|
||
const websocketUrl = network === "amplitude" ? amplitudeWss : pendulumWss; | ||
|
||
if (!apiInstanceDict[network]) { | ||
console.log(`Connecting to node ${websocketUrl}...`); | ||
apiInstanceDict[network] = await connectApi(websocketUrl); | ||
console.log(`Connected to node ${websocketUrl}`); | ||
} | ||
|
||
return apiInstanceDict[network]; | ||
}; | ||
|
||
const executeApiCall = async (network, apiCall) => { | ||
|
||
const apiInstance = await getApi(network); | ||
|
||
const websocketUrl = network === "amplitude" ? amplitudeWss : pendulumWss; | ||
|
||
try { | ||
return await apiCall(apiInstance); | ||
} catch (initialError) { | ||
try { | ||
console.log(`Attempting to reconnect to node ${websocketUrl}...`); | ||
apiInstanceDict[network] = await connectApi(websocketUrl); | ||
console.log(`Reconnected to node ${websocketUrl}`); | ||
|
||
return await apiCall(apiInstance); | ||
} catch (reconnectError) { | ||
console.error('Failed to reconnect and execute the API call', reconnectError); | ||
throw initialError; | ||
} | ||
} | ||
}; | ||
|
||
exports.executeApiCall = executeApiCall; |
Oops, something went wrong.