Skip to content

Commit

Permalink
feat: up deal-ts-clients, show additional cc-info for providers (#1023)
Browse files Browse the repository at this point in the history
* feat: up deal-ts-clients, show additional cc-info for providers

* format flt
  • Loading branch information
shamsartem authored Sep 9, 2024
1 parent 03c6e03 commit 6a37315
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 31 deletions.
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@fluencelabs/air-beautify-wasm": "0.3.9",
"@fluencelabs/aqua-api": "0.14.10",
"@fluencelabs/aqua-to-js": "0.3.13",
"@fluencelabs/deal-ts-clients": "0.18.0",
"@fluencelabs/deal-ts-clients": "0.18.5",
"@fluencelabs/fluence-network-environment": "1.2.2",
"@fluencelabs/js-client": "0.9.0",
"@fluencelabs/npm-aqua-compiler": "0.0.3",
Expand Down
121 changes: 99 additions & 22 deletions cli/src/lib/chain/commitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
populateTx,
getReadonlyDealClient,
sign,
getDealExplorerClient,
} from "../dealClient.js";
import { bigintSecondsToDate } from "../helpers/bigintOps.js";
import { bigintToStr, numToStr } from "../helpers/typesafeStringify.js";
Expand All @@ -61,7 +62,7 @@ import {
peerIdHexStringToBase58String,
peerIdToUint8Array,
} from "./conversions.js";
import { fltFormatWithSymbol } from "./currencies.js";
import { fltFormatWithSymbol, fltParse } from "./currencies.js";

const HUNDRED_PERCENT = 100;

Expand Down Expand Up @@ -590,13 +591,21 @@ export async function getCommitmentsInfo(flags: CCFlags) {
const capacity = readonlyDealClient.getCapacity();
const core = readonlyDealClient.getCore();

const [commitments, currentEpoch, epochDuration, initTimestamp] =
await Promise.all([
getCommitments(flags),
core.currentEpoch(),
core.epochDuration(),
core.initTimestamp(),
]);
const [
commitments,
currentEpoch,
epochDuration,
initTimestamp,
maxFailedRatio,
] = await Promise.all([
getCommitments(flags),
core.currentEpoch(),
core.epochDuration(),
core.initTimestamp(),
core.maxFailedRatio(),
]);

const dealExplorerClient = await getDealExplorerClient();

return Promise.all(
commitments.map(async (c) => {
Expand All @@ -614,7 +623,32 @@ export async function getCommitmentsInfo(flags: CCFlags) {

try {
commitment = await capacity.getCommitment(c.commitmentId);
} catch {}
} catch (e) {
dbg(
`Failed to get commitment from chain ${c.commitmentId}. Error: ${stringifyUnknown(e)}`,
);
}

const cuFailThreshold =
commitment.unitCount === undefined
? undefined
: maxFailedRatio * commitment.unitCount;

let ccFromExplorer: Awaited<
ReturnType<typeof dealExplorerClient.getCapacityCommitment>
> = null;

try {
ccFromExplorer = await dealExplorerClient.getCapacityCommitment(
c.commitmentId,
);
} catch (e) {
dbg(
`Failed to get commitment ${c.commitmentId} from explorer. Error: ${stringifyUnknown(
e,
)}`,
);
}

const ccStartDate =
commitment.startEpoch === undefined
Expand All @@ -637,12 +671,13 @@ export async function getCommitmentsInfo(flags: CCFlags) {
peerId: c.providerConfigComputePeer.peerId,
}
: {}),
ccFromExplorer,
commitmentId: c.commitmentId,
status:
commitment.status === undefined
? undefined
: Number(commitment.status),
currentEpoch: optBigIntToStr(currentEpoch),
currentEpoch: bigintToStr(currentEpoch),
startEpoch: optBigIntToStr(commitment.startEpoch),
startDate: ccStartDate,
endEpoch: optBigIntToStr(commitment.endEpoch),
Expand All @@ -654,6 +689,7 @@ export async function getCommitmentsInfo(flags: CCFlags) {
totalCU: optBigIntToStr(commitment.unitCount),
failedEpoch: optBigIntToStr(commitment.failedEpoch),
totalCUFailCount: optBigIntToStr(commitment.totalFailCount),
cuFailThreshold: optBigIntToStr(cuFailThreshold),
collateralPerUnit: commitment.collateralPerUnit,
exitedUnitCount: optBigIntToStr(commitment.exitedUnitCount),
};
Expand Down Expand Up @@ -706,6 +742,20 @@ export async function getCommitmentInfoString(
) {
const { ethers } = await import("ethers");

const staker =
ccInfo.delegator ?? ccInfo.ccFromExplorer?.delegatorAddress ?? undefined;

const startEndCurrentEpoch =
ccInfo.startEpoch === undefined || ccInfo.endEpoch === undefined
? undefined
: [ccInfo.startEpoch, ccInfo.endEpoch, ccInfo.currentEpoch].join(" / ");

const missedProofsThreshold =
ccInfo.totalCUFailCount === undefined ||
ccInfo.cuFailThreshold === undefined
? undefined
: [ccInfo.totalCUFailCount, ccInfo.cuFailThreshold].join(" / ");

return yamlDiffPatch(
"",
{},
Expand All @@ -714,25 +764,52 @@ export async function getCommitmentInfoString(
PeerId: ccInfo.peerId,
"Capacity commitment ID": ccInfo.commitmentId,
Status: await ccStatusToString(ccInfo.status),
"Current epoch": ccInfo.currentEpoch,
"Start epoch": ccInfo.startEpoch,
"End epoch": ccInfo.endEpoch,
"Start date": ccInfo.startDate?.toLocaleString(),
"End date": ccInfo.endDate?.toLocaleString(),
"Staker reward %": ccInfo.stakerReward,
Delegator:
ccInfo.delegator === ethers.ZeroAddress
Staker:
staker === ethers.ZeroAddress
? "Anyone can activate capacity commitment"
: ccInfo.delegator,
: staker,
"Staker reward":
ccInfo.stakerReward === undefined ? undefined : ccInfo.stakerReward,
"Start / End / Current epoch": startEndCurrentEpoch,
"Start date": ccInfo.startDate?.toLocaleString(),
"Expiration date": ccInfo.endDate?.toLocaleString(),
"Total CU": ccInfo.totalCU,
"Failed epoch": ccInfo.failedEpoch,
"Total CU Fail Count": ccInfo.totalCUFailCount,
"Missed proofs / Threshold": missedProofsThreshold,
"Collateral per unit":
ccInfo.collateralPerUnit === undefined
? undefined
: await fltFormatWithSymbol(ccInfo.collateralPerUnit),
"Exited unit count": ccInfo.exitedUnitCount,
},
...(ccInfo.ccFromExplorer === null
? {}
: {
"Total CC rewards over time": await fltFormatWithSymbol(
await fltParse(ccInfo.ccFromExplorer.rewards.total),
),
"In vesting / Available / Total claimed (Provider)": (
await Promise.all(
[
ccInfo.ccFromExplorer.rewards.provider.inVesting,
ccInfo.ccFromExplorer.rewards.provider.availableToClaim,
ccInfo.ccFromExplorer.rewards.provider.claimed,
].map((val) => {
return fltFormatWithSymbol(BigInt(val));
}),
)
).join(" / "),
"In vesting / Available / Total claimed (Staker)": (
await Promise.all(
[
ccInfo.ccFromExplorer.rewards.delegator.inVesting,
ccInfo.ccFromExplorer.rewards.delegator.availableToClaim,
ccInfo.ccFromExplorer.rewards.delegator.claimed,
].map((val) => {
return fltFormatWithSymbol(BigInt(val));
}),
)
).join(" / "),
}),
} satisfies Record<string, string | undefined>,
isUndefined,
),
);
Expand Down
22 changes: 22 additions & 0 deletions cli/src/lib/dealClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import assert from "node:assert";
import type {
DealClient,
DealMatcherClient,
DealExplorerClient,
} from "@fluencelabs/deal-ts-clients";
import type { DealCliClient } from "@fluencelabs/deal-ts-clients/dist/dealCliClient/dealCliClient.js";
import type {
Expand Down Expand Up @@ -126,6 +127,27 @@ export async function getDealMatcherClient() {
return dealMatcherClient;
}

let dealExplorerClient: DealExplorerClient | undefined = undefined;

export async function getDealExplorerClient() {
if (provider === undefined) {
provider = await ensureProvider();
}

if (dealExplorerClient === undefined) {
const { DealExplorerClient } = await import("@fluencelabs/deal-ts-clients");
const env = await ensureChainEnv();

dealExplorerClient = await DealExplorerClient.create(
env,
undefined,
provider,
);
}

return dealExplorerClient;
}

let dealCliClient: DealCliClient | undefined = undefined;

export async function getDealCliClient() {
Expand Down
6 changes: 3 additions & 3 deletions cli/src/versions.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"protocolVersion": 1,
"nox": "fluencelabs/nox:0.26.1",
"chain-rpc": "fluencelabs/chain-rpc:0.18.0",
"chain-deploy-script": "fluencelabs/chain-deploy-script:0.18.0",
"subgraph-deploy-script": "fluencelabs/subgraph-deploy-script:0.18.0",
"chain-rpc": "fluencelabs/chain-rpc:0.18.5",
"chain-deploy-script": "fluencelabs/chain-deploy-script:0.18.5",
"subgraph-deploy-script": "fluencelabs/subgraph-deploy-script:0.18.5",
"rust-toolchain": "nightly-2024-06-10",
"npm": {
"@fluencelabs/aqua-lib": "0.11.0",
Expand Down
10 changes: 5 additions & 5 deletions cli/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ __metadata:
"@fluencelabs/air-beautify-wasm": "npm:0.3.9"
"@fluencelabs/aqua-api": "npm:0.14.10"
"@fluencelabs/aqua-to-js": "npm:0.3.13"
"@fluencelabs/deal-ts-clients": "npm:0.18.0"
"@fluencelabs/deal-ts-clients": "npm:0.18.5"
"@fluencelabs/fluence-network-environment": "npm:1.2.2"
"@fluencelabs/js-client": "npm:0.9.0"
"@fluencelabs/npm-aqua-compiler": "npm:0.0.3"
Expand Down Expand Up @@ -509,9 +509,9 @@ __metadata:
languageName: unknown
linkType: soft

"@fluencelabs/deal-ts-clients@npm:0.18.0":
version: 0.18.0
resolution: "@fluencelabs/deal-ts-clients@npm:0.18.0"
"@fluencelabs/deal-ts-clients@npm:0.18.5":
version: 0.18.5
resolution: "@fluencelabs/deal-ts-clients@npm:0.18.5"
dependencies:
"@graphql-typed-document-node/core": "npm:^3.2.0"
debug: "npm:^4.3.4"
Expand All @@ -523,7 +523,7 @@ __metadata:
graphql-tag: "npm:^2.12.6"
ipfs-http-client: "npm:^60.0.1"
multiformats: "npm:^13.0.1"
checksum: 10c0/9f68446aa3555056abf16e176a9ccaf4e72c9dd283cb3df21bc099ead3f24315629a45f72a41cf9a18e17f8b76a65fb1c07aee8d16fda226c7808c16504071d4
checksum: 10c0/9a82483b5012f14e705c1dbcbbdad61cad1690b4043b7557a6b1d2cf73db9626fd8a90ec1abf099f0e69f6a8004af382f29a56c82378a9b4054f2e9958e1c8a1
languageName: node
linkType: hard

Expand Down

0 comments on commit 6a37315

Please sign in to comment.