-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfarm.abi.factory.ts
More file actions
73 lines (67 loc) · 2.58 KB
/
farm.abi.factory.ts
File metadata and controls
73 lines (67 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { Injectable } from '@nestjs/common';
import { farmVersion } from 'src/utils/farm.utils';
import { FarmAbiService } from './base-module/services/farm.abi.service';
import { FarmVersion } from './models/farm.model';
import { FarmAbiServiceV1_2 } from './v1.2/services/farm.v1.2.abi.service';
import { FarmAbiServiceV1_3 } from './v1.3/services/farm.v1.3.abi.service';
import { FarmAbiServiceV2 } from './v2/services/farm.v2.abi.service';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { Constants } from '@multiversx/sdk-nestjs-common';
import { FarmFactoryService } from './farm.factory';
@Injectable()
export class FarmAbiFactory {
constructor(
private readonly abiServiceV1_2: FarmAbiServiceV1_2,
private readonly abiServiceV1_3: FarmAbiServiceV1_3,
private readonly abiServiceV2: FarmAbiServiceV2,
private readonly farmFactory: FarmFactoryService,
private readonly cachingService: CacheService,
) {}
useAbi(farmAddress: string): FarmAbiService {
switch (farmVersion(farmAddress)) {
case FarmVersion.V1_2:
return this.abiServiceV1_2;
case FarmVersion.V1_3:
return this.abiServiceV1_3;
case FarmVersion.V2:
return this.abiServiceV2;
}
}
async isFarmToken(tokenID: string): Promise<boolean> {
const farmsAddresses = await this.farmFactory.getFarmsAddresses();
for (const farmAddress of farmsAddresses) {
const farmTokenID = await this.useAbi(farmAddress).farmTokenID(
farmAddress,
);
if (tokenID === farmTokenID) {
return true;
}
}
return false;
}
async getFarmAddressByFarmTokenID(
tokenID: string,
): Promise<string | undefined> {
const cachedValue: string = await this.cachingService.get(
`${tokenID}.farmAddress`,
);
if (cachedValue && cachedValue !== undefined) {
return cachedValue;
}
const farmsAddresses = await this.farmFactory.getFarmsAddresses();
for (const farmAddress of farmsAddresses) {
const farmTokenID = await this.useAbi(farmAddress).farmTokenID(
farmAddress,
);
if (farmTokenID === tokenID) {
await this.cachingService.set(
`${tokenID}.farmAddress`,
farmAddress,
Constants.oneHour(),
);
return farmAddress;
}
}
return undefined;
}
}