Skip to content

Commit

Permalink
Add FF_COUNTERFACTUAL_BALANCES (#1625)
Browse files Browse the repository at this point in the history
Add FF_COUNTERFACTUAL_BALANCES
  • Loading branch information
hectorgomezv authored Jun 10, 2024
1 parent 8f90c03 commit da063e4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default (): ReturnType<typeof configuration> => ({
confirmationView: false,
eventsQueue: false,
delegatesV2: false,
counterfactualBalances: false,
},
httpClient: { requestTimeout: faker.number.int() },
locking: {
Expand Down
2 changes: 2 additions & 0 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export default () => ({
process.env.FF_CONFIRMATION_VIEW?.toLowerCase() === 'true',
eventsQueue: process.env.FF_EVENTS_QUEUE?.toLowerCase() === 'true',
delegatesV2: process.env.FF_DELEGATES_V2?.toLowerCase() === 'true',
counterfactualBalances:
process.env.FF_COUNTERFACTUAL_BALANCES?.toLowerCase() === 'true',
},
httpClient: {
// Timeout in milliseconds to be used for the HTTP client.
Expand Down
2 changes: 2 additions & 0 deletions src/datasources/balances-api/balances-api.manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ beforeEach(() => {
configurationServiceMock.getOrThrow.mockImplementation((key) => {
if (key === 'features.zerionBalancesChainIds')
return ZERION_BALANCES_CHAIN_IDS;
if (key === 'features.counterfactualBalances') return true;
});
});

Expand Down Expand Up @@ -156,6 +157,7 @@ describe('Balances API Manager Tests', () => {
return notFoundExpireTimeSeconds;
else if (key === 'features.zerionBalancesChainIds')
return ZERION_BALANCES_CHAIN_IDS;
else if (key === 'features.counterfactualBalances') return true;
throw new Error(`Unexpected key: ${key}`);
});
configApiMock.getChain.mockResolvedValue(chain);
Expand Down
27 changes: 18 additions & 9 deletions src/datasources/balances-api/balances-api.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ITransactionApiManager } from '@/domain/interfaces/transaction-api.mana
@Injectable()
export class BalancesApiManager implements IBalancesApiManager {
private safeBalancesApiMap: Record<string, SafeBalancesApi> = {};
private readonly isCounterFactualBalancesEnabled: boolean;
private readonly zerionChainIds: string[];
private readonly zerionBalancesApi: IBalancesApi;
private readonly useVpcUrl: boolean;
Expand All @@ -34,6 +35,10 @@ export class BalancesApiManager implements IBalancesApiManager {
@Inject(ITransactionApiManager)
private readonly transactionApiManager: ITransactionApiManager,
) {
this.isCounterFactualBalancesEnabled =
this.configurationService.getOrThrow<boolean>(
'features.counterfactualBalances',
);
this.zerionChainIds = this.configurationService.getOrThrow<string[]>(
'features.zerionBalancesChainIds',
);
Expand All @@ -51,16 +56,20 @@ export class BalancesApiManager implements IBalancesApiManager {
return this.zerionBalancesApi;
}

// SafeBalancesApi will be returned only if TransactionApi returns the Safe data.
// Otherwise ZerionBalancesApi will be returned as the Safe is considered counterfactual/not deployed.
try {
const transactionApi =
await this.transactionApiManager.getTransactionApi(chainId);
await transactionApi.getSafe(safeAddress);
return this._getSafeBalancesApi(chainId);
} catch {
return this.zerionBalancesApi;
if (this.isCounterFactualBalancesEnabled) {
// SafeBalancesApi will be returned only if TransactionApi returns the Safe data.
// Otherwise ZerionBalancesApi will be returned as the Safe is considered counterfactual/not deployed.
try {
const transactionApi =
await this.transactionApiManager.getTransactionApi(chainId);
await transactionApi.getSafe(safeAddress);
return this._getSafeBalancesApi(chainId);
} catch {
return this.zerionBalancesApi;
}
}

return this._getSafeBalancesApi(chainId);
}

async getFiatCodes(): Promise<string[]> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ describe('Balances Controller (Unit)', () => {
},
},
},
features: {
...defaultConfiguration.features,
counterfactualBalances: true,
},
});

const moduleFixture: TestingModule = await Test.createTestingModule({
Expand Down
11 changes: 10 additions & 1 deletion src/routes/balances/balances.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ describe('Balances Controller (Unit)', () => {
beforeEach(async () => {
jest.resetAllMocks();

const defaultConfiguration = configuration();
const testConfiguration = (): typeof defaultConfiguration => ({
...defaultConfiguration,
features: {
...defaultConfiguration.features,
counterfactualBalances: true,
},
});

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(AccountDataSourceModule)
.useModule(TestAccountDataSourceModule)
Expand Down
15 changes: 12 additions & 3 deletions src/routes/collectibles/collectibles.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@ describe('Collectibles Controller (Unit)', () => {
beforeEach(async () => {
jest.resetAllMocks();

const defaultConfiguration = configuration();
const testConfiguration = (): typeof defaultConfiguration => ({
...defaultConfiguration,
features: {
...defaultConfiguration.features,
counterfactualBalances: false,
},
});

const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule.register(configuration)],
imports: [AppModule.register(testConfiguration)],
})
.overrideModule(AccountDataSourceModule)
.useModule(TestAccountDataSourceModule)
Expand Down Expand Up @@ -154,7 +163,7 @@ describe('Collectibles Controller (Unit)', () => {
)
.expect(200);

expect(networkService.get.mock.calls[2][0].networkRequest).toStrictEqual({
expect(networkService.get.mock.calls[1][0].networkRequest).toStrictEqual({
params: {
limit: 10,
offset: 20,
Expand Down Expand Up @@ -200,7 +209,7 @@ describe('Collectibles Controller (Unit)', () => {
)
.expect(200);

expect(networkService.get.mock.calls[2][0].networkRequest).toStrictEqual({
expect(networkService.get.mock.calls[1][0].networkRequest).toStrictEqual({
params: {
limit: PaginationData.DEFAULT_LIMIT,
offset: PaginationData.DEFAULT_OFFSET,
Expand Down
4 changes: 4 additions & 0 deletions src/routes/safes/safes.controller.overview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe('Safes Controller Overview (Unit)', () => {
maxOverviews: 3,
},
},
features: {
...configuration().features,
counterfactualBalances: true,
},
});

const moduleFixture: TestingModule = await Test.createTestingModule({
Expand Down

0 comments on commit da063e4

Please sign in to comment.