From 8f1d86a7bdb06956ade034d1fa1f9f84ae08ae6e Mon Sep 17 00:00:00 2001 From: Andy Haynes Date: Thu, 5 May 2022 14:59:35 -0700 Subject: [PATCH] feat(indexer): use flagged indexer service --- packages/frontend/src/redux/actions/staking.js | 9 ++++++++- packages/frontend/src/services/FungibleTokens.js | 13 +++++++++---- packages/frontend/src/services/NonFungibleTokens.js | 7 ++++++- packages/frontend/src/utils/account-with-lockup.js | 9 ++++++++- packages/frontend/src/utils/explorer-api.js | 9 ++++++++- packages/frontend/src/utils/helper-api.js | 7 ++++++- packages/frontend/src/utils/staking.js | 9 ++++++++- 7 files changed, 53 insertions(+), 10 deletions(-) diff --git a/packages/frontend/src/redux/actions/staking.js b/packages/frontend/src/redux/actions/staking.js index f9bfb43c7e..d86de377b7 100644 --- a/packages/frontend/src/redux/actions/staking.js +++ b/packages/frontend/src/redux/actions/staking.js @@ -2,6 +2,7 @@ import BN from 'bn.js'; import * as nearApiJs from 'near-api-js'; import { createActions } from 'redux-actions'; +import { USE_INDEXER_SERVICE } from '../../../../../features'; import { ACCOUNT_HELPER_URL, REACT_APP_USE_TESTINGLOCKUP, @@ -11,6 +12,7 @@ import { LOCKUP_ACCOUNT_ID_SUFFIX } from '../../config'; import { fungibleTokensService, FT_MINIMUM_STORAGE_BALANCE_LARGE } from '../../services/FungibleTokens'; +import { listStakingPools } from '../../services/indexer'; import StakingFarmContracts from '../../services/StakingFarmContracts'; import { getLockupAccountId, getLockupMinBalanceForStorage } from '../../utils/account-with-lockup'; import { showAlert } from '../../utils/alerts'; @@ -401,7 +403,12 @@ export const { staking } = createActions({ const rpcValidators = [...current_validators, ...next_validators, ...current_proposals].map(({ account_id }) => account_id); const networkId = wallet.connection.provider.connection.url.indexOf(MAINNET) > -1 ? MAINNET : TESTNET; - const allStakingPools = (await fetch(`${ACCOUNT_HELPER_URL}/stakingPools`).then((r) => r.json())); + let allStakingPools; + if (USE_INDEXER_SERVICE) { + allStakingPools = await listStakingPools(); + } else { + allStakingPools = (await fetch(`${ACCOUNT_HELPER_URL}/stakingPools`).then((r) => r.json())); + } const prefix = getValidatorRegExp(networkId); accountIds = [...new Set([...rpcValidators, ...allStakingPools])] .filter((v) => v.indexOf('nfvalidator') === -1 && v.match(prefix)); diff --git a/packages/frontend/src/services/FungibleTokens.js b/packages/frontend/src/services/FungibleTokens.js index fda9fe6d7a..a6817b89ed 100644 --- a/packages/frontend/src/services/FungibleTokens.js +++ b/packages/frontend/src/services/FungibleTokens.js @@ -1,6 +1,7 @@ import BN from 'bn.js'; import * as nearApiJs from 'near-api-js'; +import { USE_INDEXER_SERVICE } from '../../../../features'; import { ACCOUNT_HELPER_URL, NEAR_TOKEN_ID } from '../config'; import sendJson from '../tmp_fetch_send_json'; import { @@ -9,6 +10,7 @@ import { removeTrailingZeros, } from '../utils/amounts'; import { wallet } from '../utils/wallet'; +import { listLikelyTokens } from './indexer'; const { transactions: { functionCall }, @@ -61,10 +63,13 @@ export default class FungibleTokens { } static async getLikelyTokenContracts({ accountId }) { - return sendJson( - 'GET', - `${ACCOUNT_HELPER_URL}/account/${accountId}/likelyTokens` - ); + if (!USE_INDEXER_SERVICE) { + return sendJson( + 'GET', + `${ACCOUNT_HELPER_URL}/account/${accountId}/likelyTokens` + ); + } + return listLikelyTokens(accountId); } static async getStorageBalance({ contractName, accountId }) { diff --git a/packages/frontend/src/services/NonFungibleTokens.js b/packages/frontend/src/services/NonFungibleTokens.js index 9672c1dd13..dfdb642d34 100644 --- a/packages/frontend/src/services/NonFungibleTokens.js +++ b/packages/frontend/src/services/NonFungibleTokens.js @@ -1,8 +1,10 @@ import * as nearAPI from 'near-api-js'; +import { USE_INDEXER_SERVICE } from '../../../../features'; import { ACCOUNT_HELPER_URL } from '../config'; import sendJson from '../tmp_fetch_send_json'; import { wallet } from '../utils/wallet'; +import { listLikelyNfts } from './indexer'; export const TOKENS_PER_PAGE = 4; export const NFT_TRANSFER_GAS = nearAPI.utils.format.parseNearAmount('0.00000000003'); @@ -17,7 +19,10 @@ export default class NonFungibleTokens { static viewFunctionAccount = wallet.getAccountBasic('dontcare') static getLikelyTokenContracts = async (accountId) => { - return sendJson('GET', `${ACCOUNT_HELPER_URL}/account/${accountId}/likelyNFTs`); + if (!USE_INDEXER_SERVICE) { + return sendJson('GET', `${ACCOUNT_HELPER_URL}/account/${accountId}/likelyNFTs`); + } + return listLikelyNfts(accountId); } static getMetadata = async (contractName) => { diff --git a/packages/frontend/src/utils/account-with-lockup.js b/packages/frontend/src/utils/account-with-lockup.js index ac73864676..b2e8a93308 100644 --- a/packages/frontend/src/utils/account-with-lockup.js +++ b/packages/frontend/src/utils/account-with-lockup.js @@ -5,12 +5,14 @@ import { InMemoryKeyStore } from 'near-api-js/lib/key_stores'; import { parseNearAmount } from 'near-api-js/lib/utils/format'; import { BinaryReader } from 'near-api-js/lib/utils/serialize'; +import { USE_INDEXER_SERVICE } from '../../../../features'; import { ACCOUNT_HELPER_URL, LOCKUP_ACCOUNT_ID_SUFFIX, MIN_BALANCE_FOR_GAS, REACT_APP_USE_TESTINGLOCKUP, } from '../config'; +import { listStakingDeposits } from '../services/indexer'; import { WalletError } from './walletError'; // TODO: Should gas allowance be dynamically calculated @@ -177,7 +179,12 @@ async function getAccountBalance(limitedAccountData = false) { }; } - let stakingDeposits = await fetch(ACCOUNT_HELPER_URL + '/staking-deposits/' + this.accountId).then((r) => r.json()); + let stakingDeposits; + if (USE_INDEXER_SERVICE) { + stakingDeposits = await listStakingDeposits(this.accountId); + } else { + stakingDeposits = await fetch(ACCOUNT_HELPER_URL + '/staking-deposits/' + this.accountId).then((r) => r.json()); + } let stakedBalanceMainAccount = new BN(0); await Promise.all( stakingDeposits.map(async ({ validator_id }) => { diff --git a/packages/frontend/src/utils/explorer-api.js b/packages/frontend/src/utils/explorer-api.js index 6a72fb0a10..060d14781d 100644 --- a/packages/frontend/src/utils/explorer-api.js +++ b/packages/frontend/src/utils/explorer-api.js @@ -1,10 +1,17 @@ +import { USE_INDEXER_SERVICE } from '../../../../features'; import { ACCOUNT_HELPER_URL } from '../config'; +import { listRecentTransactions } from '../services/indexer'; import { wallet } from './wallet'; export async function getTransactions({ accountId }) { if (!accountId) return {}; - const txs = await fetch(`${ACCOUNT_HELPER_URL}/account/${accountId}/activity`).then((res) => res.json()); + let txs; + if (USE_INDEXER_SERVICE) { + txs = await listRecentTransactions(accountId); + } else { + txs = await fetch(`${ACCOUNT_HELPER_URL}/account/${accountId}/activity`).then((res) => res.json()); + } return txs.map((t, i) => ({ ...t, diff --git a/packages/frontend/src/utils/helper-api.js b/packages/frontend/src/utils/helper-api.js index 52c889dd1e..064017b680 100644 --- a/packages/frontend/src/utils/helper-api.js +++ b/packages/frontend/src/utils/helper-api.js @@ -2,13 +2,18 @@ import * as nearApiJs from 'near-api-js'; import { parseSeedPhrase } from 'near-seed-phrase'; +import { USE_INDEXER_SERVICE } from '../../../../features'; import { ACCOUNT_HELPER_URL } from '../config'; +import { listAccountsByPublicKey } from '../services/indexer'; export let controller; export async function getAccountIds(publicKey) { controller = new AbortController(); - return await fetch(`${ACCOUNT_HELPER_URL}/publicKey/${publicKey}/accounts`, { signal: controller.signal }).then((res) => res.json()); + if (!USE_INDEXER_SERVICE) { + return await fetch(`${ACCOUNT_HELPER_URL}/publicKey/${publicKey}/accounts`, { signal: controller.signal }).then((res) => res.json()); + } + return listAccountsByPublicKey(publicKey); } export async function getAccountIdsBySeedPhrase(seedPhrase) { diff --git a/packages/frontend/src/utils/staking.js b/packages/frontend/src/utils/staking.js index 1d1481d0fc..f517a97e46 100644 --- a/packages/frontend/src/utils/staking.js +++ b/packages/frontend/src/utils/staking.js @@ -1,7 +1,9 @@ import BN from 'bn.js'; import * as nearApiJs from 'near-api-js'; +import { USE_INDEXER_SERVICE } from '../../../../features'; import { ACCOUNT_HELPER_URL, NEAR_TOKEN_ID } from '../config'; +import { listStakingDeposits } from '../services/indexer'; import { nearTo } from '../utils/amounts'; import { wallet } from './wallet'; @@ -78,7 +80,12 @@ export async function updateStakedBalance(validatorId, account_id, contract) { } export async function getStakingDeposits(accountId) { - let stakingDeposits = await fetch(ACCOUNT_HELPER_URL + '/staking-deposits/' + accountId).then((r) => r.json()); + let stakingDeposits; + if (USE_INDEXER_SERVICE) { + stakingDeposits = await listStakingDeposits(accountId); + } else { + stakingDeposits = await fetch(ACCOUNT_HELPER_URL + '/staking-deposits/' + accountId).then((r) => r.json()); + } const validatorDepositMap = {}; stakingDeposits.forEach(({ validator_id, deposit }) => {