-
Notifications
You must be signed in to change notification settings - Fork 7
/
contracts.ts
251 lines (228 loc) · 7.37 KB
/
contracts.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { Decimal } from "decimal.js";
import * as PATH from "path";
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { events } from "../helpers/events";
import { expose } from "../helpers/expose";
import { validateInput } from "../helpers/validator";
import { Nodes } from "../primitives/nodes";
import { BaseModule } from "./base";
import {
BatchCancelContractsModel,
ContractCancelModel,
ContractConsumption,
ContractGetByNodeIdAndHashModel,
ContractGetModel,
ContractLockModel,
ContractsByAddress,
ContractsByTwinId,
ContractState,
CreateServiceContractModel,
GetDedicatedNodePriceModel,
GetServiceContractModel,
NameContractCreateModel,
NameContractGetModel,
NodeContractCreateModel,
NodeContractUpdateModel,
RentContractGetModel,
ServiceContractApproveModel,
ServiceContractBillModel,
ServiceContractCancelModel,
SetDedicatedNodeExtraFeesModel,
SetServiceContractFeesModel,
SetServiceContractMetadataModel,
} from "./models";
import { checkBalance } from "./utils";
class Contracts {
client: TFClient;
nodes: Nodes;
constructor(public config: GridClientConfig) {
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType);
this.nodes = new Nodes(config.graphqlURL, config.proxyURL, config.rmbClient);
}
private async invalidateDeployment(contractId: number) {
const baseModule = new BaseModule(this.config);
const contractPath = PATH.join(this.config.storePath, "contracts", `${contractId}.json`);
let contractInfo;
try {
contractInfo = await baseModule.backendStorage.load(contractPath);
} catch (e) {
events.emit("logs", `Couldn't delete the deployment's cached data for contract id: ${contractId} due to ${e}`);
}
if (contractInfo) {
baseModule.moduleName = contractInfo["moduleName"];
baseModule.projectName = contractInfo["projectName"];
await baseModule._get(contractInfo["deploymentName"]);
}
}
@expose
@validateInput
@checkBalance
async create_node(options: NodeContractCreateModel) {
return (
await this.client.contracts.createNode({
nodeId: options.node_id,
hash: options.hash,
data: options.data,
numberOfPublicIps: options.public_ip,
solutionProviderId: options.solutionProviderId,
})
).apply();
}
@expose
@validateInput
@checkBalance
async create_name(options: NameContractCreateModel) {
return (await this.client.contracts.createName(options)).apply();
}
@expose
@validateInput
async get(options: ContractGetModel) {
return this.client.contracts.get(options);
}
@expose
@validateInput
async get_contract_id_by_node_id_and_hash(options: ContractGetByNodeIdAndHashModel) {
return this.client.contracts.getContractIdByNodeIdAndHash({ nodeId: options.node_id, hash: options.hash });
}
@expose
@validateInput
async get_name_contract(options: NameContractGetModel) {
return this.client.contracts.getContractIdByName(options);
}
@expose
@validateInput
async getDedicatedNodeExtraFee(options: GetDedicatedNodePriceModel) {
return await this.client.contracts.getDedicatedNodeExtraFee(options);
}
@expose
@validateInput
async activeRentContractForNode(options: RentContractGetModel) {
return this.client.contracts.getContractIdByActiveRentForNode(options);
}
@expose
@validateInput
async contractLock(options: ContractLockModel) {
return this.client.contracts.contractLock(options);
}
@expose
@validateInput
@checkBalance
async update_node(options: NodeContractUpdateModel) {
return (await this.client.contracts.updateNode(options)).apply();
}
@expose
@validateInput
@checkBalance
async cancel(options: ContractCancelModel) {
const deletedContract = await (await this.client.contracts.cancel(options)).apply();
await this.invalidateDeployment(options.id);
return deletedContract;
}
@expose
@validateInput
async listMyContracts(options?: ContractState) {
return this.client.contracts.listMyContracts({ graphqlURL: this.config.graphqlURL, stateList: options?.state });
}
@expose
@validateInput
async listContractsByTwinId(options: ContractsByTwinId) {
return this.client.contracts.listContractsByTwinId({ graphqlURL: this.config.graphqlURL, twinId: options.twinId });
}
@expose
@validateInput
async listContractsByAddress(options: ContractsByAddress) {
return this.client.contracts.listContractsByAddress({
graphqlURL: this.config.graphqlURL,
accountId: options.address,
});
}
@expose
@validateInput
@checkBalance
async createServiceContract(options: CreateServiceContractModel) {
return (await this.client.contracts.createService(options)).apply();
}
@expose
@validateInput
@checkBalance
async approveServiceContract(options: ServiceContractApproveModel) {
return (await this.client.contracts.approveService(options)).apply();
}
@expose
@validateInput
@checkBalance
async billServiceContract(options: ServiceContractBillModel) {
return (await this.client.contracts.billService(options)).apply();
}
@expose
@validateInput
@checkBalance
async cancelServiceContract(options: ServiceContractCancelModel) {
return (await this.client.contracts.cancelService(options)).apply();
}
@expose
@validateInput
@checkBalance
async setFeesServiceContract(options: SetServiceContractFeesModel) {
return (await this.client.contracts.setServiceFees(options)).apply();
}
@expose
@validateInput
@checkBalance
async setMetadataServiceContract(options: SetServiceContractMetadataModel) {
return (await this.client.contracts.setServiceMetadata(options)).apply();
}
@expose
@validateInput
async getServiceContract(options: GetServiceContractModel) {
return this.client.contracts.getService(options);
}
/**
* WARNING: Please be careful when executing this method, it will delete all your contracts.
* @returns Promise
*/
@expose
@validateInput
@checkBalance
async cancelMyContracts(): Promise<Record<string, number>[]> {
const contracts = await this.client.contracts.cancelMyContracts({ graphqlURL: this.config.graphqlURL });
for (const contract of contracts) {
await this.invalidateDeployment(contract.contractId);
}
return contracts;
}
@expose
@validateInput
@checkBalance
async batchCancelContracts(options: BatchCancelContractsModel): Promise<number[]> {
const contracts = await this.client.contracts.batchCancelContracts(options.ids);
for (const id of options.ids) {
await this.invalidateDeployment(id);
}
return contracts;
}
@expose
@validateInput
@checkBalance
async setDedicatedNodeExtraFee(options: SetDedicatedNodeExtraFeesModel) {
return (await this.client.contracts.setDedicatedNodeExtraFee(options)).apply();
}
/**
* Get contract consumption per hour in TFT.
*
* @param {ContractConsumption} options
* @returns {Promise<number>}
*/
@expose
@validateInput
async getConsumption(options: ContractConsumption): Promise<number> {
return this.client.contracts.getConsumption({ id: options.id, graphqlURL: this.config.graphqlURL });
}
@expose
@validateInput
async getDeletionTime(options: ContractGetModel): Promise<string | number> {
return this.client.contracts.getDeletionTime(options);
}
}
export { Contracts as contracts };