forked from akure/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
features.ts
71 lines (67 loc) · 2.26 KB
/
features.ts
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
import semverGte from 'semver/functions/gte'
import { ContractVersion, Feature, SupportedFeatureMap } from '@dao-dao/types'
/**
* Checks if a specific feature is supported by a given contract version.
*
* @param {Feature} feature - The feature to check.
* @param {ContractVersion} version - The contract version to check against.
* @return {boolean} Returns true if the feature is supported by the version,
* false otherwise.
*/
export const isFeatureSupportedByVersion = (
feature: Feature,
version: ContractVersion
): boolean => {
switch (feature) {
case Feature.StorageItemValueKey:
return versionGte(version, ContractVersion.V2Beta)
case Feature.MultipleChoiceProposals:
case Feature.SubDaos:
case Feature.PrePropose:
case Feature.StaticProposalModulePrefixes:
case Feature.VoteUntilExpiration:
return versionGte(version, ContractVersion.V2Alpha)
case Feature.ModuleInstantiateFunds:
case Feature.Instantiate2:
return versionGte(version, ContractVersion.V230)
default:
return true
}
}
/**
* Pre-computes supported features based on the given contract version.
*
* @param {ContractVersion} version - The contract version to check.
* @return {SupportedFeatureMap} - A map of supported features where the key is
* the feature name and the value is a boolean indicating if the feature is
* supported.
*/
export const getSupportedFeatures = (
version: ContractVersion
): SupportedFeatureMap =>
Object.values(Feature).reduce(
(acc, feature) => ({
...acc,
[feature]: isFeatureSupportedByVersion(feature as Feature, version),
}),
{} as SupportedFeatureMap
)
/**
* Checks if a given version is greater than or equal to a specified version.
*
* @param {ContractVersion} version - The version to check.
* @param {ContractVersion} gteThis - The version to compare with.
* @return {boolean} Returns true if the given version is higher than the specified version, otherwise false.
*/
export const versionGte = (
version: ContractVersion,
gteThis: ContractVersion
): boolean => {
try {
return semverGte(version, gteThis)
} catch {
// If throws an error, one of the versions was invalid (likely not a
// version, like ContractVersion.Gov), so just return false.
return false
}
}