Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlock grace period contracts #2582

Merged
merged 49 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
5eb7137
Feat: add unclock grace period function to tfchain client and grid cl…
0oM4R Apr 18, 2024
7ed75d7
WIP: unlock grace period contracts
0oM4R Apr 21, 2024
d7daeab
Feat: unlock grace period contracts manually
0oM4R Apr 22, 2024
a48c477
Fix: disable unlock contract button on low balance
0oM4R Apr 23, 2024
a7bdc40
WIP: Get total cost of grace period contracts
0oM4R Apr 24, 2024
f69ed1b
Feat: Add getContractsLockDetails method to gridclient
0oM4R May 2, 2024
1dea67e
Feat: add batch unlockContracts and unlockMycontracts to grid client
0oM4R May 2, 2024
3054fd9
Merge branch 'development_2.5' of github.com:threefoldtech/tfgrid-sdk…
0oM4R May 2, 2024
fffa625
add totalAmountLocked to LockContracts
0oM4R May 2, 2024
d63136d
Feat: unlock all contracts
0oM4R May 2, 2024
779b26e
WIP: add unlock features to UI
0oM4R May 7, 2024
a518743
Merge branch 'development_2.5' of github.com:threefoldtech/tfgrid-sdk…
0oM4R May 7, 2024
7cae04c
Feat: unlock all contracts UI
0oM4R May 7, 2024
1b010c8
change single contract unlock botton color
0oM4R May 7, 2024
514d272
change unlock selected contracts UI
0oM4R May 7, 2024
de6fc6e
Enhance:
0oM4R May 9, 2024
301a5ea
enhance unlockMyContracts functions logic
0oM4R May 9, 2024
e06a234
Fix: clear selected contracts on refresh
0oM4R May 12, 2024
5b706a7
Fix: graceperiod node contracts on rented node alert
0oM4R May 12, 2024
e5df487
merge development
0oM4R May 12, 2024
6b0781b
reset: contracts list
0oM4R May 12, 2024
ce47909
revert contracsts list changes
0oM4R May 12, 2024
e8ec0c4
fix build
0oM4R May 12, 2024
0bdbd4a
Feat: add loading to the get lock contract details
0oM4R May 12, 2024
7f3c9b6
WIP: unlock related contracts
0oM4R May 12, 2024
6ae58da
Feat: Unlock related contracts
0oM4R May 13, 2024
d6c1507
add unlock all contracts button back
0oM4R May 13, 2024
1f0d9b4
remove total amoun locked alert
0oM4R May 13, 2024
79a2dae
merge development
0oM4R May 13, 2024
b16b3dc
update contracts table to be algined with vuetify upgrade
0oM4R May 13, 2024
d8dc411
rephrase Unlock all contracts button
0oM4R May 14, 2024
dd6f887
Docs: add documentaion to unlock funtion
0oM4R May 14, 2024
eef1866
Feat: handle loading error while load contracts lock details
0oM4R May 14, 2024
13a02a4
UI: update unlock all contracts dialog style
0oM4R May 14, 2024
7f38c88
rephrase tooltip
0oM4R May 14, 2024
27421b7
rephrase tooltip
0oM4R May 14, 2024
61310cd
Merge branch 'development' of github.com:threefoldtech/tfgrid-sdk-ts …
0oM4R May 14, 2024
342b3de
rephrase success message
0oM4R May 14, 2024
55da1a3
Unselect contracts after unlock submited
0oM4R May 14, 2024
68324e5
Return contracts ids from ContractGracePeriodEnded
0oM4R May 15, 2024
ec8af2f
Update balace before showing the needed fund to resume grace period c…
0oM4R May 15, 2024
8df3bd1
Feat:
0oM4R May 15, 2024
d4a9d22
Update JSdocs
0oM4R May 15, 2024
815def1
Check the amount locked before ask for billing the contract
AhmedHanafy725 May 19, 2024
327ad0b
Fix typo in contracts module
AhmedHanafy725 May 19, 2024
0aa0d01
Remove debug logs
AhmedHanafy725 May 19, 2024
3c69d56
Merge branch 'development' of https://github.com/threefoldtech/tfgrid…
AlaaElattar May 21, 2024
caefc4f
fix lint
AlaaElattar May 21, 2024
5fb636c
fix bug because of merge
AlaaElattar May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
Contract,
ContractLock,
ContractLockOptions,
Contracts,
ExtrinsicResult,
Expand Down Expand Up @@ -100,6 +102,14 @@ export interface CancelMyContractOptions {
graphqlURL: string;
}

export type LockDetails = { [key: number]: ContractLock };
export interface LockContracts {
nameContracts: LockDetails;
nodeContracts: LockDetails;
rentContracts: LockDetails;
totalAmountLocked: number;
}

class TFContracts extends Contracts {
async listContractsByTwinId(options: ListContractByTwinIdOptions): Promise<GqlContracts> {
options.stateList = options.stateList || [ContractStates.Created, ContractStates.GracePeriod];
Expand Down Expand Up @@ -326,6 +336,29 @@ class TFContracts extends Contracts {
return ids;
}

async batchUnlockContracts(ids: number[]) {
const billableContractsIDs: number[] = [];
for (const id of ids) {
if ((await this.contractLock({ id })).amountLocked > 0) billableContractsIDs.push(id);
}
const extrinsics: ExtrinsicResult<number>[] = [];
for (const id of billableContractsIDs) {
extrinsics.push(await this.unlock(id));
}
return this.client.applyAllExtrinsics(extrinsics);
}

async unlockMyContracts(graphqlURL: string) {
const contracts = await this.listMyContracts({
stateList: [ContractStates.GracePeriod],
graphqlURL,
});
const ids: number[] = [...contracts.nameContracts, ...contracts.nodeContracts, ...contracts.rentContracts].map(
contract => parseInt(contract.contractID),
);
return await this.batchUnlockContracts(ids);
}

async getDedicatedNodeExtraFee(options: GetDedicatedNodePriceOptions): Promise<number> {
// converting fees from milli to usd before getting
const fee = new Decimal(await super.getDedicatedNodeExtraFee(options));
Expand Down
53 changes: 52 additions & 1 deletion packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Contract } from "@threefold/tfchain_client";
import { DeploymentKeyDeletionError, InsufficientBalanceError } from "@threefold/types";
import * as PATH from "path";

import { GqlNameContract, GqlNodeContract, GqlRentContract } from "../clients/tf-grid";
import { GqlNameContract, GqlNodeContract, GqlRentContract, LockContracts } from "../clients/tf-grid";
import { TFClient } from "../clients/tf-grid/client";
import { GridClientConfig } from "../config";
import { events } from "../helpers/events";
Expand All @@ -19,6 +20,7 @@ import {
ContractsByAddress,
ContractsByTwinId,
ContractState,
ContractStates,
CreateServiceContractModel,
GetActiveContractsModel,
GetDedicatedNodePriceModel,
Expand Down Expand Up @@ -271,6 +273,55 @@ class Contracts {
async getDeletionTime(options: ContractGetModel): Promise<string | number> {
return this.client.contracts.getDeletionTime(options);
}
/**
* Retrieves lock details of contracts.
* @returns A Promise that resolves to an object of type LockContracts containing details of locked contracts.
*/
@expose
@validateInput
async getContractsLockDetails(): Promise<LockContracts> {
const LockedContracts: LockContracts = {
nameContracts: {},
nodeContracts: {},
rentContracts: {},
totalAmountLocked: 0,
};
const contracts = await this.listMyContracts({ state: [ContractStates.GracePeriod] });

if (contracts == undefined) return LockedContracts;

const contractTypes = ["nameContracts", "nodeContracts", "rentContracts"];

for (const type of contractTypes) {
for (const contract of contracts[type]) {
const contractID = parseInt(contract.contractID);
const contractLockDetails = await this.contractLock({ id: contractID });
LockedContracts[type][contractID] = contractLockDetails;
LockedContracts.totalAmountLocked += contractLockDetails.amountLocked;
}
}
return LockedContracts;
}

/**
* Unlocks multiple contracts.
* @param ids An array of contract IDs to be unlocked.
* @returns A Promise that resolves to an array of billed contracts representing the result of batch unlocking.
*/
@expose
@validateInput
async unlockContracts(ids: number[]): Promise<number[]> {
return await this.client.contracts.batchUnlockContracts(ids);
}
/**
* Unlocks contracts associated with the current user.
* @returns A Promise that resolves to an array of billed contracts.
*/
@expose
@validateInput
async unlockMyContracts(): Promise<number[]> {
return await this.client.contracts.unlockMyContracts(this.config.graphqlURL);
}
}

export { Contracts as contracts };
Loading
Loading