Skip to content

Commit

Permalink
Merge pull request #297 from infinitybase/feat/cache/provider
Browse files Browse the repository at this point in the history
Feat/cache/provider
  • Loading branch information
guimroque authored Oct 15, 2024
2 parents dae00a1 + 6b605f8 commit 97cff4c
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider } from 'fuels';
import { FuelProvider } from '@src/utils';
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

const { FUEL_PROVIDER } = process.env;
Expand All @@ -14,7 +14,7 @@ export class AddNetworkOnTransaction1727715693680 implements MigrationInterface
}),
);

const provider = await Provider.create(FUEL_PROVIDER);
const provider = await FuelProvider.create(FUEL_PROVIDER);
const network = {
url: provider.url,
chainId: provider.getChainId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Provider } from 'fuels';
import { FuelProvider } from '@src/utils';
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';

const { FUEL_PROVIDER } = process.env;
Expand All @@ -14,7 +14,7 @@ export class AddNetworkOnApiTokens1727717621119 implements MigrationInterface {
}),
);

const provider = await Provider.create(FUEL_PROVIDER);
const provider = await FuelProvider.create(FUEL_PROVIDER);
const network = {
url: provider.url,
chainId: provider.getChainId(),
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/mocks/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import crypto from 'crypto';

import { IPredicatePayload } from '@src/modules/predicate/types';
import { Vault, VaultConfigurable } from 'bakosafe';
import { Provider } from 'fuels';
import { FuelProvider } from '@src/utils';

const { FUEL_PROVIDER } = process.env;

Expand All @@ -24,7 +24,7 @@ export class PredicateMock {
}

public static async create(signaturesCount: number, signers: string[]) {
const _provider = await Provider.create(FUEL_PROVIDER);
const _provider = await FuelProvider.create(FUEL_PROVIDER);
const _configurable: VaultConfigurable = {
SIGNATURES_COUNT: signaturesCount,
SIGNERS: signers,
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/modules/apiToken/__tests__/apiToken.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { SetupApi } from '@src/utils/testUtils/setup';
import { Predicate, Encoder } from '@src/models';
import { networks } from '@src/mocks/networks';
import { IPredicatePayload } from '@src/modules/predicate/types';

import { FuelProvider } from '@src/utils';
import axios from 'axios';
import { TypeUser, Vault } from 'bakosafe';
import { Provider, Wallet, Address } from 'fuels';
import { Wallet, Address } from 'fuels';
import { accounts } from '@src/mocks/accounts';

const { API_URL } = process.env;
Expand All @@ -25,7 +25,7 @@ describe('[API TOKEN]', () => {
const api = axios.create({
baseURL: API_URL,
});
const provider = await Provider.create(networks['local']);
const provider = await FuelProvider.create(networks['local']);

// create a new user
const wallet = Wallet.generate();
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/modules/auth/__tests__/auth.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Encoder } from '@src/models';

import { Address, Provider, Wallet } from 'fuels';
import { TypeUser } from 'bakosafe';
import { FuelProvider } from '@src/utils';

const { API_URL } = process.env;

Expand All @@ -13,7 +14,7 @@ describe('[AUTH]', () => {
const api = axios.create({
baseURL: API_URL,
});
const provider = await Provider.create(networks['local']);
const provider = await FuelProvider.create(networks['local']);

// create a new user
const wallet = Wallet.generate();
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('[AUTH]', () => {

const { data: updatedNetwork } = await api.get(`/user/latest/info`);

const newProvider = await Provider.create(newNetwork);
const newProvider = await FuelProvider.create(newNetwork);

expect(updatedNetwork).toHaveProperty('network', {
url: newProvider.url,
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/modules/auth/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { RecoverCodeService } from '../recoverCode/services';
import { IAuthService, ICreateRecoverCodeRequest, ISignInRequest } from './types';
import App from '@src/server/app';
import { Request } from 'express';
import { Provider } from 'fuels';
import { FuelProvider } from '@src/utils';
const { FUEL_PROVIDER } = process.env;

export class AuthController {
Expand Down Expand Up @@ -75,7 +75,7 @@ export class AuthController {
});
}

const provider = await Provider.create(networkUrl ?? FUEL_PROVIDER);
const provider = await FuelProvider.create(networkUrl ?? FUEL_PROVIDER);

const response = await new RecoverCodeService().create({
owner,
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/modules/predicate/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import {
IPredicateService,
} from './types';
import { IPredicateOrdination, setOrdination } from './ordination';
import { Network, Provider, ZeroBytes32 } from 'fuels';
import { Network, ZeroBytes32 } from 'fuels';
import { UserService } from '../user/service';
import { IconUtils } from '@src/utils/icons';
import { PredicateVersionService } from '../predicateVersion/services';
import { FuelProvider } from '@src/utils';

export class PredicateService implements IPredicateService {
private _ordination: IPredicateOrdination = {
Expand Down Expand Up @@ -357,7 +358,7 @@ export class PredicateService implements IPredicateService {

async instancePredicate(configurable: string, provider: string): Promise<Vault> {
const conf = JSON.parse(configurable);
const _provider = await Provider.create(provider);
const _provider = await FuelProvider.create(provider);
return new Vault(_provider, conf);
}
}
7 changes: 4 additions & 3 deletions packages/api/src/modules/transaction/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from './types';
import { formatFuelTransaction, formatTransactionsResponse } from './utils';
import { TransactionPagination, TransactionPaginationParams } from './pagination';
import { FuelProvider } from '@src/utils';

export class TransactionService implements ITransactionService {
private _ordination: IOrdination<Transaction> = {
Expand Down Expand Up @@ -518,7 +519,7 @@ export class TransactionService implements ITransactionService {
return await this.findById(id);
}

const provider = await Provider.create(transaction.network.url);
const provider = await FuelProvider.create(transaction.network.url);
const vault = new Vault(provider, JSON.parse(predicate.configurable));

const tx = transactionRequestify({
Expand Down Expand Up @@ -568,7 +569,7 @@ export class TransactionService implements ITransactionService {

for await (const predicate of predicates) {
const address = Address.fromString(predicate.predicateAddress).toB256();
const provider = await Provider.create(providerUrl);
const provider = await FuelProvider.create(providerUrl);

// TODO: change this to use pagination and order DESC
const { transactions } = await getTransactionsSummaries({
Expand Down Expand Up @@ -610,7 +611,7 @@ export class TransactionService implements ITransactionService {
providerUrl: string,
): Promise<ITransactionResponse> {
try {
const provider = await Provider.create(providerUrl);
const provider = await FuelProvider.create(providerUrl);

const tx = await getTransactionSummary({
id,
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/modules/user/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import {
} from './types';
import { Not } from 'typeorm';
import App from '@src/server/app';
import { Provider } from 'fuels';
import { IChangenetworkRequest } from '../auth/types';
import { FuelProvider } from '@src/utils/FuelProvider';

export class UserController {
private userService: IUserService;
Expand Down Expand Up @@ -226,7 +226,7 @@ export class UserController {
});
}

const _provider = await Provider.create(
const _provider = await FuelProvider.create(
provider ?? process.env.FUEL_PROVIDER,
);

Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/modules/user/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Vault } from 'bakosafe';
import { PredicateService } from '../predicate/services';
import { PredicateVersionService } from '../predicateVersion/services';
import { Maybe } from '@src/utils/types/maybe';
import { FuelProvider } from '@src/utils';

const { UI_URL } = process.env;

Expand Down Expand Up @@ -120,7 +121,7 @@ export class UserService implements IUserService {
});

// insert a root wallet predicate
const provider = await Provider.create(payload.provider);
const provider = await FuelProvider.create(payload.provider);
const configurable = {
SIGNATURES_COUNT: 1,
SIGNERS: [user.address],
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { handleErrors } from '@middlewares/index';
import { QuoteStorage, SessionStorage } from './storage';
import Monitoring from './monitoring';
import Bootstrap from './bootstrap';
import RedisWriteClient from '@src/utils/redis/RedisWriteClient';
import RedisReadClient from '@src/utils/redis/RedisReadClient';
import { RedisWriteClient, RedisReadClient, FuelProvider } from '@src/utils';

class App {
private static instance?: App;
Expand Down Expand Up @@ -87,6 +86,7 @@ class App {
await Bootstrap.start();
await RedisWriteClient.start();
await RedisReadClient.start();
await FuelProvider.start();

App.instance = new App();
Object.freeze(App.instance);
Expand Down
11 changes: 8 additions & 3 deletions packages/api/src/server/storage/quote.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { IAsset, IAssetMapById, getAssetsMaps, isDevMode } from '@src/utils';
import RedisReadClient from '@src/utils/redis/RedisReadClient';
import RedisWriteClient from '@src/utils/redis/RedisWriteClient';
import {
RedisReadClient,
RedisWriteClient,
IAsset,
IAssetMapById,
getAssetsMaps,
isDevMode,
} from '@src/utils';
import axios from 'axios';

const { COIN_MARKET_CAP_API_KEY } = process.env;
Expand Down
3 changes: 1 addition & 2 deletions packages/api/src/server/storage/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import UserToken from '@src/models/UserToken';
import { Workspace } from '@src/models/Workspace';
import { AuthService } from '@src/modules/auth/services';
import { ISignInResponse } from '@src/modules/auth/types';
import RedisReadClient from '@src/utils/redis/RedisReadClient';
import RedisWriteClient from '@src/utils/redis/RedisWriteClient';
import { RedisReadClient, RedisWriteClient } from '@src/utils';
import { isPast } from 'date-fns';
import * as redis from 'redis';

Expand Down
46 changes: 46 additions & 0 deletions packages/api/src/utils/FuelProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Provider, ProviderOptions } from 'fuels';

const REFRESH_TIME = 60000 * 60; // 60 minutes
const FUEL_PROVIDER =
process.env.FUEL_PROVIDER || 'https://testnet.fuel.network/v1/graphql';

export class FuelProvider {
private static instance?: FuelProvider;

private providers: Record<string, Provider>;

private constructor() {
this.providers = {};
}

static async create(url: string, options?: ProviderOptions): Promise<Provider> {
if (FuelProvider.instance.providers[url]) {
return FuelProvider.instance.providers[url];
}

const p = await Provider.create(url, options);
FuelProvider.instance.providers[url] = p;

return FuelProvider.instance.providers[url];
}

async reset(): Promise<void> {
const providers: Record<string, Provider> = {};

providers[FUEL_PROVIDER] = await Provider.create(FUEL_PROVIDER);

FuelProvider.instance.providers = providers;
}

static async start(): Promise<void> {
if (!FuelProvider.instance) {
FuelProvider.instance = new FuelProvider();
FuelProvider.instance.reset();

setInterval(() => {
console.log('[PROVIDER] Refreshing providers');
FuelProvider.instance.reset();
}, REFRESH_TIME);
}
}
}
4 changes: 2 additions & 2 deletions packages/api/src/utils/cronJobs/TVL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Asset, Vault } from 'bakosafe';
import cron from 'node-cron';
import { getAssetsMaps } from '../assets';
import App from '@src/server/app';
import { Provider } from 'fuels';
import { FuelProvider } from '../FuelProvider';
const { FUEL_PROVIDER } = process.env;

const VALID_PROVIDERS = [FUEL_PROVIDER];
Expand Down Expand Up @@ -43,7 +43,7 @@ const TVLCronJob = cron.schedule('0 0 * * *', async () => {
// };
// todo: get by session or run a map to get for each valid networks

const provider = await Provider.create(FUEL_PROVIDER);
const provider = await FuelProvider.create(FUEL_PROVIDER);
const conf = JSON.parse(predicate.configurable);

// Instancia cada vault
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ export * from './assets';
export * from './runMode';
export * from './witnessesUpdatedAt';
export * from './formatAssets';
export * from './redis/RedisReadClient';
export * from './redis/RedisWriteClient';
export * from './FuelProvider';
2 changes: 1 addition & 1 deletion packages/api/src/utils/redis/RedisReadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RedisClientType, createClient } from 'redis';

const REDIS_URL_READ = process.env.REDIS_URL_WRITE || 'redis://127.0.0.1:6379';

export default class RedisReadClient {
export class RedisReadClient {
private static client: RedisClientType;

private constructor() {}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/utils/redis/RedisWriteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RedisClientType, createClient } from 'redis';

const REDIS_URL_WRITE = process.env.REDIS_URL_WRITE || 'redis://127.0.0.1:6379';

export default class RedisWriteClient {
export class RedisWriteClient {
private static client: RedisClientType;

private constructor() {}
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/utils/testUtils/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios, { AxiosInstance } from 'axios';
// import { IBakoSafeApi } from 'bakosafe';
import { Provider, Wallet } from 'fuels';
import { Wallet } from 'fuels';

import { TypeUser } from '@src/models';

import { IDefaultAccount } from '../../mocks/accounts';
import GeneralError from '@utils/error/GeneralError';
import { FuelProvider } from '../FuelProvider';

const { API_URL } = process.env;

Expand Down Expand Up @@ -111,7 +111,7 @@ export class AuthValidations {
}

async signer(message: string) {
const provider = await Provider.create(this.provider);
const provider = await FuelProvider.create(this.provider);
const signer = Wallet.fromPrivateKey(this.account.privateKey, provider);
return await signer.signMessage(message);
}
Expand Down
7 changes: 4 additions & 3 deletions packages/api/src/utils/testUtils/Wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { networks } from '@src/mocks/networks';
import { Vault } from 'bakosafe';
import { BN, Wallet, Provider } from 'fuels';
import { BN, Wallet } from 'fuels';
import { FuelProvider } from '../FuelProvider';

export const txParams = {
maxFee: 1000,
Expand All @@ -15,14 +16,14 @@ export const sendPredicateCoins = async (
) => {
const wallet = Wallet.fromPrivateKey(
rootWallet,
await Provider.create(networks['local']),
await FuelProvider.create(networks['local']),
);
const deposit = await wallet.transfer(predicate.address, amount, asset, txParams);
await deposit.wait();
};

export const signBypK = async (message: string, privateKey: string) => {
const provider = await Provider.create(networks['local']);
const provider = await FuelProvider.create(networks['local']);
const signer = Wallet.fromPrivateKey(privateKey, provider);
return signer.signMessage(message);
};
Loading

0 comments on commit 97cff4c

Please sign in to comment.