Skip to content

Commit

Permalink
feat(hapi): use an array of endpoints to query producers
Browse files Browse the repository at this point in the history
  • Loading branch information
Torresmorah committed Oct 31, 2022
1 parent fbae50e commit f53977c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ services:
environment:
HAPI_EOS_API_NETWORK_NAME: '${HAPI_EOS_API_NETWORK_NAME}'
HAPI_EOS_API_ENDPOINT: '${HAPI_EOS_API_ENDPOINT}'
HAPI_EOS_API_ENDPOINTS: '${HAPI_EOS_API_ENDPOINTS}'
HAPI_EOS_STATE_HISTORY_PLUGIN_ENDPOINT: '${HAPI_EOS_STATE_HISTORY_PLUGIN_ENDPOINT}'
HAPI_EOS_API_CHAIN_ID: '${HAPI_EOS_API_CHAIN_ID}'
HAPI_EOS_BASE_ACCOUNT: '${HAPI_EOS_BASE_ACCOUNT}'
Expand Down
3 changes: 3 additions & 0 deletions hapi/src/config/eos.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = {
networkName: process.env.HAPI_EOS_API_NETWORK_NAME,
apiEndpoint: process.env.HAPI_EOS_API_ENDPOINT,
apiEndpoints: process.env.HAPI_EOS_API_ENDPOINTS
? JSON.parse(process.env.HAPI_EOS_API_ENDPOINTS)
: [],
stateHistoryPluginEndpoint:
process.env.HAPI_EOS_STATE_HISTORY_PLUGIN_ENDPOINT,
chainId: process.env.HAPI_EOS_API_CHAIN_ID,
Expand Down
38 changes: 29 additions & 9 deletions hapi/src/utils/eos.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,26 @@ const eosApi = EosApi({
verbose: false,
fetchConfiguration: {}
})
const eosApis = eosConfig.apiEndpoints.map((endpoint) => {
return EosApi({
httpEndpoint: endpoint,
verbose: false,
fetchConfiguration: {}
})
})

const callEosApi = async (method) => {
for (let i = 0; i < eosApis.length; i++) {
try {
const response = await method(eosApis[i])

const newAccount = async accountName => {
return response
} catch (error) {
console.log(error)
}
}
}
const newAccount = async (accountName) => {
const password = await walletUtil.create(accountName)
const key = await walletUtil.createKey(accountName)

Expand Down Expand Up @@ -140,9 +158,9 @@ const generateRandomAccountName = async (prefix = '') => {
}
}

const getAbi = account => eosApi.getAbi(account)
const getAbi = (account) => eosApi.getAbi(account)

const getAccount = async account => {
const getAccount = async (account) => {
try {
const accountInfo = await eosApi.getAccount(account)

Expand All @@ -152,18 +170,19 @@ const getAccount = async account => {
}
}

const getBlock = async blockNumber => {
const getBlock = async (blockNumber) => {
const block = await eosApi.getBlock(blockNumber)

return block
}

const getCodeHash = account => eosApi.getCodeHash(account)
const getCodeHash = (account) => eosApi.getCodeHash(account)

const getCurrencyBalance = (code, account, symbol) =>
eosApi.getCurrencyBalance(code, account, symbol)

const getTableRows = options => eosApi.getTableRows({ json: true, ...options })
const getTableRows = (options) =>
eosApi.getTableRows({ json: true, ...options })

const getProducerSchedule = () => eosApi.getProducerSchedule({})

Expand Down Expand Up @@ -196,11 +215,12 @@ const transact = async (actions, account, password) => {
return transaction
}

const getCurrencyStats = options => eosApi.getCurrencyStats(options)
const getCurrencyStats = (options) => eosApi.getCurrencyStats(options)

const getProducers = options => eosApi.getProducers(options)
const getProducers = async (options) =>
callEosApi(async (eosApi) => eosApi.getProducers(options))

const getInfo = options => eosApi.getInfo(options || {})
const getInfo = (options) => eosApi.getInfo(options || {})

module.exports = {
newAccount,
Expand Down

0 comments on commit f53977c

Please sign in to comment.