Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix(lyra-avalon): Add unique keys on Lyra Avalon positions (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
immasandwich committed Jul 8, 2022
1 parent 833c26c commit 78ff09f
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/app-toolkit/app-toolkit.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { AppDefinition } from '~app/app.definition';
import { IContractFactory } from '~contract/contracts';
import { EthersMulticall } from '~multicall';
import { DefaultDataProps } from '~position/display.interface';
import { AppTokenPosition, ContractPosition } from '~position/position.interface';
import { AppTokenPosition, ContractPosition, NonFungibleToken } from '~position/position.interface';
import { AppGroupsDefinition } from '~position/position.service';
import { BaseToken } from '~position/token.interface';
import { Network } from '~types/network.interface';
Expand Down Expand Up @@ -44,6 +44,13 @@ export interface IAppToolkit {
...appTokenDefinition: AppGroupsDefinition[]
): Promise<ContractPosition<T>[]>;

// Position Key

getPositionKey(
position: ContractPosition | AppTokenPosition | BaseToken | NonFungibleToken,
pickFields?: string[],
): string;

// Cache

getFromCache<T = any>(key: string): Promise<T | undefined>;
Expand Down
13 changes: 13 additions & 0 deletions src/app-toolkit/app-toolkit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { ContractFactory } from '~contract';
import { MulticallService } from '~multicall/multicall.service';
import { NetworkProviderService } from '~network-provider/network-provider.service';
import { DefaultDataProps } from '~position/display.interface';
import { PositionKeyService } from '~position/position-key.service';
import { AppTokenPosition, ContractPosition, NonFungibleToken } from '~position/position.interface';
import { AppGroupsDefinition, PositionService } from '~position/position.service';
import { BaseToken } from '~position/token.interface';
import { TokenService } from '~token/token.service';
import { Network } from '~types/network.interface';

Expand All @@ -24,6 +27,7 @@ export class AppToolkit implements IAppToolkit {
@Inject(AppService) private readonly appService: AppService,
@Inject(NetworkProviderService) private readonly networkProviderService: NetworkProviderService,
@Inject(PositionService) private readonly positionService: PositionService,
@Inject(PositionKeyService) private readonly positionKeyService: PositionKeyService,
@Inject(TokenService) private readonly tokenService: TokenService,
@Inject(MulticallService) private readonly multicallService: MulticallService,
@Inject(CACHE_MANAGER) private readonly cacheManager: Cache,
Expand Down Expand Up @@ -75,6 +79,15 @@ export class AppToolkit implements IAppToolkit {
return this.positionService.getAppContractPositions<T>(...appTokenDefinitions);
}

// Position Key

getPositionKey(
position: ContractPosition | AppTokenPosition | BaseToken | NonFungibleToken,
pickFields: string[] = [],
) {
return this.positionKeyService.getPositionKey(position, pickFields);
}

// Cache

async getFromCache<T = any>(key: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ export class MasterChefContractPositionHelper {
displayProps,
};

position.key = this.appToolkit.getPositionKey(position, ['poolIndex']);
return position;
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class OptimismLyraAvalonOptionsContractPositionFetcher implements Positio
},
dataProps: {},
};

const positions = _.keys(OPTION_TYPES).map(key => {
return {
...position,
Expand All @@ -73,7 +74,12 @@ export class OptimismLyraAvalonOptionsContractPositionFetcher implements Positio
},
} as ContractPosition<LyraAvalonOptionContractPositionDataProps>;
});
return positions;

const positionsWithKey = positions.map(p => ({
key: this.appToolkit.getPositionKey(p, ['optionType', 'strikeId']),
...p,
}));
return positionsWithKey;
});
return _.flatten(strikes);
});
Expand Down
53 changes: 53 additions & 0 deletions src/position/position-key.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Injectable } from '@nestjs/common';
import murmur from 'murmurhash-js';

import { Network } from '~types/network.interface';

import { ContractType } from './contract.interface';
import { AppTokenPosition, ContractPosition, MetaType, NonFungibleToken } from './position.interface';
import { BaseToken } from './token.interface';

export type AppGroupsDefinition = {
appId: string;
groupIds: string[];
network: Network;
};

@Injectable()
export class PositionKeyService {
generateKey(input: string) {
return murmur.murmur3(input).toString();
}

getPositionKey(
position: ContractPosition | AppTokenPosition | BaseToken | NonFungibleToken,
pickFields: string[] = [],
) {
if ('key' in position) return position.key!;

switch (position.type) {
case ContractType.POSITION:
return this.generateKey(
[
position.address,
position.network,
position.appId,
position.tokens.map(token => [token.address, token.network, token.metaType].join(':')),
pickFields.map(v => position.dataProps[v]).join(':'),
].join(':'),
);
case ContractType.APP_TOKEN:
return this.generateKey(
[
position.appId,
position.address,
position.network,
MetaType.SUPPLIED,
pickFields.map(v => position.dataProps[v]).join(':'),
].join(':'),
);
default:
return this.generateKey([position.address, position.network, MetaType.SUPPLIED].join(':'));
}
}
}
11 changes: 9 additions & 2 deletions src/position/position.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import { CacheModule } from '~cache/cache.module';

import { PositionBalanceFetcherRegistry } from './position-balance-fetcher.registry';
import { PositionFetcherRegistry } from './position-fetcher.registry';
import { PositionKeyService } from './position-key.service';
import { PositionSources } from './position-source';
import { PositionController } from './position.controller';
import { PositionService } from './position.service';

@Module({
imports: [DiscoveryModule, CacheModule],
providers: [...PositionSources, PositionService, PositionFetcherRegistry, PositionBalanceFetcherRegistry],
providers: [
...PositionSources,
PositionService,
PositionFetcherRegistry,
PositionBalanceFetcherRegistry,
PositionKeyService,
],
controllers: [PositionController],
exports: [PositionService, PositionFetcherRegistry, PositionBalanceFetcherRegistry],
exports: [PositionService, PositionFetcherRegistry, PositionBalanceFetcherRegistry, PositionKeyService],
})
export class PositionModule {}

0 comments on commit 78ff09f

Please sign in to comment.