Skip to content

Commit

Permalink
Improve pool type mapper safety by checking for unsupported types
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoguerios committed Jan 17, 2024
1 parent ef5c88d commit 47f952b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BalancerApiClient } from '../../client';
import { NestedPool, NestedPoolState } from '../../../../../entities';
import { MinimalToken } from '../../../../types';
import { Address, Hex, PoolType } from '../../../../../types';
import { mapPoolType } from '@/utils/poolTypeMapper';

type PoolGetPool = {
id: Hex;
Expand Down Expand Up @@ -181,7 +182,7 @@ export class NestedPools {
{
id: pool.id,
address: pool.address,
type: this.mapPoolType(pool.type),
type: mapPoolType(pool.type),
level: 1,
tokens: pool.tokens.map((t) => {
const minimalToken: MinimalToken = {
Expand All @@ -203,7 +204,7 @@ export class NestedPools {
id: token.pool.id,
address: token.pool.address,
level: 0,
type: this.mapPoolType(token.pool.type),
type: mapPoolType(token.pool.type),
tokens: token.pool.tokens.map((t) => {
const minimalToken: MinimalToken = {
address: t.address,
Expand Down
4 changes: 2 additions & 2 deletions src/data/providers/balancer-api/modules/pool-state/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BalancerApiClient } from '../../client';
import { PoolState } from '../../../../../entities';
import { poolTypeFromApi } from '../../../../../utils/poolTypeMapper';
import { mapPoolType } from '../../../../../utils/poolTypeMapper';

export class Pools {
readonly poolStateQuery = `query GetPool($id: String!){
Expand Down Expand Up @@ -86,6 +86,6 @@ export class Pools {
},
});
const poolGetPool: PoolState = data.poolGetPool;
return { ...poolGetPool, type: poolTypeFromApi[poolGetPool.type] };
return { ...poolGetPool, type: mapPoolType(poolGetPool.type) };
}
}
13 changes: 12 additions & 1 deletion src/utils/poolTypeMapper.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { PoolType } from '../types';

export const poolTypeFromApi = {
// map pool type from the api to the sdk
const poolTypeMap = {
WEIGHTED: PoolType.Weighted,
COMPOSABLE_STABLE: PoolType.ComposableStable,
GYRO3: PoolType.Gyro3,
GYRO2: PoolType.Gyro2,
GYROE: PoolType.GyroE,
};

export const mapPoolType = (type: string): PoolType => {
const supportedPoolTypes = Object.keys(poolTypeMap);

if (!supportedPoolTypes.includes(type)) {
throw new Error(`Unsupported pool type ${type}`);

This comment has been minimized.

Copy link
@agualis

agualis Jan 17, 2024

Contributor

@brunoguerios I would include ${supportedPoolTypes.join(',')} in the error so that the consumer knows about the concrete supported types.

This comment has been minimized.

Copy link
@brunoguerios

brunoguerios Jan 17, 2024

Author Member

Sure! Nice suggestion! I got a bit ahead of myself and ended up merging the PR. I'll include this on a next one with some minor refactor that I have listed here. Thanks!

}

return poolTypeMap[type];
};
2 changes: 1 addition & 1 deletion test/v2/data/balancerApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BalancerApi, ChainId, NestedPoolState, PoolState } from '../../../src';

// Placeholder test to help validate the impact of API updates
// Note: should not be included to CI checks
describe.skip(
describe(
'BalancerApi Provider',
() => {
test('CS Pool - Should add BPT to tokens', async () => {
Expand Down

0 comments on commit 47f952b

Please sign in to comment.