forked from daostack/alchemy
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pluginUtils.ts
221 lines (204 loc) · 7.4 KB
/
pluginUtils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* eslint-disable no-bitwise */
import {
Address,
IContractInfo,
IPluginState,
IGenericPluginState,
IContributionRewardExtState,
} from "@daostack/arc.js";
import { rewarderContractName } from "components/Plugin/ContributionRewardExtRewarders/rewardersProps";
import { GenericPluginRegistry } from "genericPluginRegistry";
/**
* gotta load moment in order to use moment-timezone directly
*/
import "moment";
import * as moment from "moment-timezone";
import { getArc } from "../arc";
export enum PluginPermissions {
None = 0,
IsRegistered = 1, // Always added by default in the controller
CanRegisterPlugins = 2,
CanAddRemoveGlobalConstraints = 4,
CanUpgradeController = 8,
CanCallDelegateCall = 0x10,
All = 0x1f,
}
/**
* These are the permissions that are the minimum that each plugin must have to
* be able to perform its full range of functionality.
*
* Note that '1' is always assigned to a plugin by the Controller when the
* plugin is registered with the controller.
*/
export const REQUIRED_PLUGIN_PERMISSIONS: any = {
"ContributionReward": PluginPermissions.IsRegistered,
"GlobalConstraintRegistrar": PluginPermissions.IsRegistered | PluginPermissions.CanAddRemoveGlobalConstraints,
"SchemeRegistrar": PluginPermissions.All, // TODO: is this correct?
"SchemeFactory": PluginPermissions.All, //TODO: is this correct?
"UpgradeScheme": PluginPermissions.IsRegistered | PluginPermissions.CanRegisterPlugins | PluginPermissions.CanUpgradeController,
"VestingScheme": PluginPermissions.IsRegistered,
"VoteInOrganizationScheme": PluginPermissions.IsRegistered | PluginPermissions.CanCallDelegateCall,
"Join": PluginPermissions.IsRegistered,
"FundingRequest": PluginPermissions.IsRegistered,
"TokenTrade": PluginPermissions.IsRegistered,
};
/** plugins that we know how to interpret */
export const PLUGIN_NAMES = {
ContributionReward: "Funding and Voting Power",
GenericScheme: "Generic Plugin",
ReputationFromToken: "Reputation from Token",
SchemeRegistrar: "Plugin Registrar",
SchemeFactory: "Plugin Manager",
Competition: "Competition",
TokenTrade: "Token Trade",
ContributionRewardExt: "Contribution Reward Ext",
Join: "Join",
FundingRequest: "Funding Request",
};
/**
* return true if the address is the address of a known plugin (which we know how to represent)
* @param address [description]
* @return [description]
*/
export function isKnownPlugin(address: Address): boolean {
const arc = getArc();
let contractInfo;
try {
contractInfo = arc.getContractInfo(address);
} catch (err) {
if (err.message.match(/no contract/i)) {
return false;
}
throw err;
}
if (Object.keys(PLUGIN_NAMES).includes(contractInfo.name)) {
return true;
} else {
return false;
}
}
export function pluginName(plugin: IPluginState|IContractInfo, fallback?: string): string {
let name: string;
if (plugin.name === "GenericScheme") {
const generic = plugin as IGenericPluginState;
if (generic.pluginParams && generic.pluginParams.contractToCall) {
const genericPluginRegistry = new GenericPluginRegistry();
const contractToCall = generic.pluginParams.contractToCall;
const genericPluginInfo = genericPluginRegistry.getPluginInfo(contractToCall);
if (genericPluginInfo) {
name = genericPluginInfo.specs.name;
} else {
// Adding the address is a bit long for a title
// name = `Blockchain Interaction (${contractToCall})`;
name = "Blockchain Interaction";
}
} else {
// this should never happen...
name = "Blockchain Interaction";
}
} else if (plugin.name === "ContributionRewardExt") {
name = rewarderContractName(plugin as IContributionRewardExtState);
if (!name) {
name = plugin.name;
} // else name is the rewarder contract alias, or its name split by camel case
} else {
name = PLUGIN_NAMES[plugin.name as keyof typeof PLUGIN_NAMES];
if (name === undefined){
name = fallback;
}
}
return name;
}
/**
* Given plugin name and address, returns a friendly string representing the plugin's address and it's name
* @param name
* @param address
*/
function getNameAndAddressString(address: string, name?: string) {
if (name) {
return `${address.slice(0, 4)}...${address.slice(-4)} (${name})`;
}
return `${address.slice(0, 4)}...${address.slice(-4)}`;
}
/**
* Given a plugin or a plugin address, returns a friendly string representing the plugin's address and it's name
* @param plugin plugin or plugin address
*/
export function pluginNameAndAddress(plugin: string|IPluginState): string {
let name;
if (typeof plugin === "string"){ // Plugin address
const arc = getArc();
try {
const contractInfo = arc.getContractInfo(plugin);
name = pluginName(contractInfo);
if (name) {
return getNameAndAddressString(plugin, name);
}
return getNameAndAddressString(plugin);
} catch (err) {
if (err.message.match(/No contract/)) {
return getNameAndAddressString(plugin);
}
}
}
else { // Plugin
name = pluginName(plugin);
return getNameAndAddressString(plugin.address, name);
}
}
export enum GetPluginIsActiveActions {
Register=1,
Remove,
Replace
}
const pluginActionPropNames = new Map<string, Map<GetPluginIsActiveActions, string>>([
[
"SchemeRegistrar", new Map<GetPluginIsActiveActions, string>([
[GetPluginIsActiveActions.Register, "voteRegisterParams"],
[GetPluginIsActiveActions.Remove, "voteRemoveParams"],
]),
],
[
"SchemeFactory", new Map<GetPluginIsActiveActions, string>([
[GetPluginIsActiveActions.Register, "voteParams"],
[GetPluginIsActiveActions.Remove, "voteParams"],
[GetPluginIsActiveActions.Replace, "voteParams"],
]),
],
]);
export function getPluginIsActive(plugin: IPluginState, action?: GetPluginIsActiveActions): boolean {
let votingMachineParamsPropertyName: string;
if (action) { // then the name of the voting machine properties property depends on the action
const pluginActionsMap = pluginActionPropNames.get(plugin.name);
if (!pluginActionsMap) {
throw new Error(`getPluginIsActive: unknown plugin: ${plugin.name}`);
}
const propName = pluginActionsMap.get(action);
if (!propName) {
throw new Error(`getPluginIsActive: unknown action: ${plugin.name}:${action}`);
}
votingMachineParamsPropertyName = propName;
} else {
/**
* if plugin is SchemeRegistrar, then it is active if any of its actions are active
*/
if (plugin.name === "SchemeRegistrar") {
return getPluginIsActive(plugin, GetPluginIsActiveActions.Register) || getPluginIsActive(plugin, GetPluginIsActiveActions.Remove);
} else {
votingMachineParamsPropertyName = "voteParams";
}
}
const votingMachineParams = (plugin as any).pluginParams?.[votingMachineParamsPropertyName];
if (!votingMachineParams) {
// eslint-disable-next-line no-console
console.warn(`getPluginIsActive: voting machine parameters parameters not found for ${plugin?.name ?? plugin.address}`);
return true;
}
if ((typeof(votingMachineParams.activationTime) === undefined) || (votingMachineParams.activationTime === null)) {
// eslint-disable-next-line no-console
console.warn(`getPluginIsActive: voting machine appears not to be GenesisProtocol: ${plugin.name}`);
return true;
} else {
return moment(votingMachineParams.activationTime*1000).isSameOrBefore(moment());
}
}