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

Implemented the locked contract feature, added inside the playground. #479

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 8 additions & 1 deletion packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Contracts } from "@threefold/tfchain_client";
import { ContractLockOptions, Contracts } from "@threefold/tfchain_client";
import { Decimal } from "decimal.js";

import { ContractStates } from "../../modules";
Expand Down Expand Up @@ -150,6 +150,13 @@ class TFContracts extends Contracts {
});
}

async contractLock(options: ContractLockOptions) {
const res = await super.contractLock(options);
const amountLocked = new Decimal(res.amountLocked);
res.amountLocked = amountLocked.div(10 ** 7).toNumber();
return res;
}

/**
* WARNING: Please be careful when executing this method, it will delete all your contracts.
* @param {CancelMyContractOptions} options
Expand Down
7 changes: 7 additions & 0 deletions packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ContractConsumption,
ContractGetByNodeIdAndHashModel,
ContractGetModel,
ContractLockModel,
ContractsByAddress,
ContractsByTwinId,
ContractState,
Expand Down Expand Up @@ -98,6 +99,12 @@ class Contracts {
return this.client.contracts.getContractIdByActiveRentForNode(options);
}

@expose
@validateInput
async contractLock(options: ContractLockModel) {
return this.client.contracts.contractLock(options);
}

@expose
@validateInput
@checkBalance
Expand Down
3 changes: 3 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ class ContractConsumption {
@Expose() @IsInt() @Min(1) id: number;
}

class ContractLockModel extends ContractConsumption {}

class TwinCreateModel {
@Expose() @IsString() @IsNotEmpty() relay: string;
}
Expand Down Expand Up @@ -647,6 +649,7 @@ export {
ContractsByTwinId,
ContractsByAddress,
ContractConsumption,
ContractLockModel,
TwinCreateModel,
TwinGetModel,
TwinGetByAccountIdModel,
Expand Down
62 changes: 61 additions & 1 deletion packages/playground/src/weblets/tf_contracts_list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@
</template>

<template #[`item.state`]="{ item }">
<v-chip :color="getStateColor(item.value.state)">
<v-tooltip
v-if="item && item.value.state === ContractStates.GracePeriod"
:text="'the number of tokens required to remove your contract from the grace period and restore functionality to your workloads.'"
location="top center"
>
<template #activator="{ props }">
<v-chip
@click.stop="contractLockDetails(item.value.contractId)"
v-bind="props"
:color="getStateColor(item.value.state)"
>
{{ item.value.state }}
</v-chip>
</template>
</v-tooltip>
<v-chip v-else :color="getStateColor(item.value.state)">
{{ item.value.state }}
</v-chip>
</template>
Expand Down Expand Up @@ -82,6 +97,30 @@
</v-card-actions>
</v-card>
</v-dialog>

<v-dialog width="70%" v-model="contractStateDialog">
<v-card>
<v-card-title class="text-h5">Contract lock Detalis</v-card-title>
<v-card-text>
<p v-if="loading" class="text-center">
<strong>Loading The Locked Amount...</strong>
</p>
<p v-else class="text-center">
Amount Locked <strong>{{ contractLocked?.amountLocked }} TFT</strong>
</p>
<br />
<v-alert type="info" variant="tonal">
The contract is in a GracePeriod condition, which means that your workloads are suspended but not deleted; in
order to resume your workloads and restore their functionality, you must pay your account with the necessary
tokens.
</v-alert>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="error" variant="tonal" @click="contractStateDialog = false"> Close </v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>

<script lang="ts" setup>
Expand Down Expand Up @@ -121,6 +160,8 @@ async function onMount() {
}

const loadingContractId = ref<number>();
const contractLocked = ref<ContractLock>();

async function onShowDetails(contractId: number) {
loading.value = true;
loadingContractId.value = contractId;
Expand Down Expand Up @@ -148,7 +189,24 @@ function getStateColor(state: ContractStates): string {
}
}

async function contractLockDetails(contractId: number) {
contractStateDialog.value = true;
loading.value = true;
const grid = await getGrid(profileManager.profile!);
await grid?.contracts
.contractLock({ id: contractId })
.then((data: ContractLock) => {
contractLocked.value = data;
})
.catch(err => {
layout.value.setStatus("failed", normalizeError(err, `Failed to fetch the contract ${contractId} lock details.`));
contractStateDialog.value = false;
});
loading.value = false;
}

const deletingDialog = ref(false);
const contractStateDialog = ref(false);
const deleting = ref(false);
async function onDelete() {
deletingDialog.value = false;
Expand All @@ -172,6 +230,8 @@ async function onDelete() {
</script>

<script lang="ts">
import type { ContractLock } from "@threefold/tfchain_client";

import ListTable from "../components/list_table.vue";
import { normalizeError } from "../utils/helpers";

Expand Down
16 changes: 16 additions & 0 deletions packages/tfchain_client/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ interface NameContract {
name: string;
}

export interface ContractLockOptions {
id: number;
}

export interface ContractLock {
amountLocked: number;
lockUpdated: number;
xmonader marked this conversation as resolved.
Show resolved Hide resolved
cycles: number;
}

interface NodeContract {
nodeId: number;
deploymentHash: string;
Expand Down Expand Up @@ -106,6 +116,12 @@ class QueryContracts {
return res.toPrimitive() as number;
}

@checkConnection
async contractLock(options: ContractLockOptions): Promise<ContractLock> {
const res = await this.client.api.query.smartContractModule.contractLock(options.id);
return res.toPrimitive() as unknown as ContractLock;
}

@checkConnection
async getDeletionTime(options: QueryContractsGetOptions): Promise<number> {
const contract = await this.get(options);
Expand Down