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

Add set & get dedicated node extra fees in chain & client #698

Merged
merged 11 commits into from
Jun 22, 2023
26 changes: 26 additions & 0 deletions packages/grid_client/scripts/extraFees.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { getClient } from "./client_loader";
import { log } from "./utils";

async function main() {
const grid3 = await getClient();

const node = { nodeId: 5 };

const extraFees = await grid3.contracts.setExtraFees({ ...node, extraFee: 5 });

console.log(extraFees);

grid3.contracts
.getDedicatedNodePrice(node)
.then(res => {
log(res);
})
.catch(err => {
throw err;
})
.finally(() => {
grid3.disconnect();
});
}

main();
19 changes: 17 additions & 2 deletions packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ContractsByTwinId,
ContractState,
CreateServiceContractModel,
GetDedicatedNodePriceModel,
GetServiceContractModel,
NameContractCreateModel,
NameContractGetModel,
Expand All @@ -27,6 +28,7 @@ import {
ServiceContractApproveModel,
ServiceContractBillModel,
ServiceContractCancelModel,
SetDedicatedNodeExtraFeesModel,
SetServiceContractFeesModel,
SetServiceContractMetadataModel,
} from "./models";
Expand Down Expand Up @@ -178,14 +180,27 @@ class Contracts {
@expose
@validateInput
@checkBalance
async setMetadataServiceContract(options: SetServiceContractMetadataModel) {
return (await this.client.contracts.setServiceMetadata(options)).apply();
async setExtraFees(options: SetDedicatedNodeExtraFeesModel) {
return (await this.client.contracts.setExtraFees(options)).apply();
}
@expose
@validateInput
async getServiceContract(options: GetServiceContractModel) {
return this.client.contracts.getService(options);
}

@expose
@validateInput
@checkBalance
async setMetadataServiceContract(options: SetServiceContractMetadataModel) {
return (await this.client.contracts.setServiceMetadata(options)).apply();
}

@expose
@validateInput
async getDedicatedNodePrice(options: GetDedicatedNodePriceModel) {
return this.client.contracts.getDedicatedNodePrice(options);
}
/**
* WARNING: Please be careful when executing this method, it will delete all your contracts.
* @returns Promise
Expand Down
11 changes: 11 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,15 @@ class NetworkGetModel {
@Expose() @IsString() @IsNotEmpty() @IsAlphanumeric() @MaxLength(NameLength) name: string;
}

class SetDedicatedNodeExtraFeesModel {
@Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number;
@Expose() @IsInt() @IsNotEmpty() @Min(1) extraFee: number;
}

class GetDedicatedNodePriceModel {
@Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number;
}

export {
AlgorandAccountCreateModel,
AlgorandAccountInitModel,
Expand Down Expand Up @@ -720,4 +729,6 @@ export {
GetServiceContractModel,
NetworkGetModel,
NodeGetModel,
SetDedicatedNodeExtraFeesModel,
GetDedicatedNodePriceModel,
};
48 changes: 32 additions & 16 deletions packages/tfchain_client/src/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ export interface SetServiceFeesOptions {
baseFee: number;
variableFee: number;
}
export interface SetDedicatedNodeExtraFeesOptions {
nodeId: number;
extraFee: number;
}

export interface GetDedicatedNodePriceOptions {
nodeId: number;
}

export interface SetServiceMetadataOptions {
serviceId: number;
Expand All @@ -214,7 +222,7 @@ class Contracts extends QueryContracts {

@checkConnection
async createNode(options: CreateNodeOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.createNodeContract(
const extrinsic = this.client.api.tx.smartContractModule.createNodeContract(
options.nodeId,
options.hash,
options.data,
Expand All @@ -226,23 +234,19 @@ class Contracts extends QueryContracts {

@checkConnection
async updateNode(options: UpdateNodeOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.updateNodeContract(
options.id,
options.hash,
options.data,
);
const extrinsic = this.client.api.tx.smartContractModule.updateNodeContract(options.id, options.hash, options.data);
return this.client.patchExtrinsic<Contract>(extrinsic);
}

@checkConnection
async createName(options: CreateNameOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.createNameContract(options.name);
const extrinsic = this.client.api.tx.smartContractModule.createNameContract(options.name);
return this.client.patchExtrinsic<Contract>(extrinsic);
}

@checkConnection
async createRent(options: CreateRentOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.createRentContract(
const extrinsic = this.client.api.tx.smartContractModule.createRentContract(
options.nodeId,
options.solutionProviderId,
);
Expand All @@ -255,7 +259,7 @@ class Contracts extends QueryContracts {
if (!contract) {
return;
}
const extrinsic = await this.client.api.tx.smartContractModule.cancelContract(options.id);
const extrinsic = this.client.api.tx.smartContractModule.cancelContract(options.id);
return this.client.patchExtrinsic(extrinsic, {
map: () => options.id,
resultEvents: ["NodeContractCanceled", "NameContractCanceled", "RentContractCanceled", "ContractCanceled"],
Expand All @@ -264,7 +268,7 @@ class Contracts extends QueryContracts {

@checkConnection
async createService(options: CreateServiceOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.serviceContractCreate(
const extrinsic = this.client.api.tx.smartContractModule.serviceContractCreate(
options.serviceAccount,
options.consumerAccount,
);
Expand All @@ -275,16 +279,16 @@ class Contracts extends QueryContracts {
async approveService(options: ApproveServiceOptions) {
let extrinsic: any;
if (options.approve) {
extrinsic = await this.client.api.tx.smartContractModule.serviceContractApprove(options.serviceId);
extrinsic = this.client.api.tx.smartContractModule.serviceContractApprove(options.serviceId);
} else {
extrinsic = await this.client.api.tx.smartContractModule.serviceContractReject(options.serviceId);
extrinsic = this.client.api.tx.smartContractModule.serviceContractReject(options.serviceId);
}
return this.client.patchExtrinsic<ServiceContract>(extrinsic);
}

@checkConnection
async billService(options: BillServiceOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.serviceContractBill(
const extrinsic = this.client.api.tx.smartContractModule.serviceContractBill(
options.serviceId,
options.variableAmount,
options.metadata,
Expand All @@ -294,23 +298,35 @@ class Contracts extends QueryContracts {

@checkConnection
async cancelService(options: CancelServiceOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.serviceContractCancel(options.serviceId);
const extrinsic = this.client.api.tx.smartContractModule.serviceContractCancel(options.serviceId);
return this.client.patchExtrinsic(extrinsic, { map: () => options.serviceId });
}

@checkConnection
async setServiceFees(options: SetServiceFeesOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.serviceContractSetFees(
const extrinsic = this.client.api.tx.smartContractModule.serviceContractSetFees(
options.serviceId,
options.baseFee,
options.variableFee,
);
return this.client.patchExtrinsic<ServiceContract>(extrinsic);
}

@checkConnection
async setExtraFees(options: SetDedicatedNodeExtraFeesOptions) {
const extrinsic = this.client.api.tx.smartContractModule.setDedicatedNodeExtraFee(options.nodeId, options.extraFee);
return this.client.patchExtrinsic<RentContract>(extrinsic);
}

@checkConnection
async getDedicatedNodePrice(options: GetDedicatedNodePriceOptions) {
const res = await this.client.api.query.smartContractModule.dedicatedNodesExtraFee(options.nodeId);
return res.toPrimitive() as number;
}
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved

@checkConnection
async setServiceMetadata(options: SetServiceMetadataOptions) {
const extrinsic = await this.client.api.tx.smartContractModule.serviceContractSetMetadata(
const extrinsic = this.client.api.tx.smartContractModule.serviceContractSetMetadata(
options.serviceId,
options.metadata,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/tfchain_client/src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface Node {
virtualized: boolean;
serialNumber: string;
connectionPrice: number;
hasGpu: number;
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
}

interface NetworkInterfaceType {
Expand All @@ -44,6 +45,7 @@ interface NodeResources {
sru: number;
cru: number;
mru: number;
gpu: number;
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
}

interface NodeLocation {
Expand Down