Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit 0ca2bd3

Browse files
authored
feat: offer-remove --offer-ids flag support, set default stakerReward to 20 (#1051)
* feat: offer-remove --offer-ids flag support, set default stakerReward to 20 * Apply automatic changes --------- Co-authored-by: shamsartem <shamsartem@users.noreply.github.com>
1 parent 5a093b3 commit 0ca2bd3

10 files changed

Lines changed: 153 additions & 39 deletions

File tree

cli/docs/commands/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,12 +1673,14 @@ Remove offers
16731673

16741674
```
16751675
USAGE
1676-
$ fluence provider offer-remove [--no-input] [--offers <offer-1,offer-2>] [--env <testnet | mainnet | stage | local |
1677-
custom>] [--priv-key <private-key>]
1676+
$ fluence provider offer-remove [--no-input] [--offers <offer-1,offer-2> | --offer-ids <id-1,id-2>] [--env <testnet |
1677+
mainnet | stage | local | custom>] [--priv-key <private-key>]
16781678
16791679
FLAGS
16801680
--env=<testnet | mainnet | stage | local | custom> Fluence Environment to use when running the command
16811681
--no-input Don't interactively ask for any input from the user
1682+
--offer-ids=<id-1,id-2> Comma-separated list of offer ids. Can't be used together with
1683+
--offers flag
16821684
--offers=<offer-1,offer-2> Comma-separated list of offer names. To use all of your offers:
16831685
--offers all
16841686
--priv-key=<private-key> !WARNING! for debug purposes only. Passing private keys through

cli/src/commands/provider/offer-remove.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717

1818
import { BaseCommand, baseFlags } from "../../baseCommand.js";
1919
import { removeOffers } from "../../lib/chain/offer/updateOffers.js";
20-
import { CHAIN_FLAGS, OFFER_FLAG } from "../../lib/const.js";
20+
import { CHAIN_FLAGS, OFFER_FLAGS } from "../../lib/const.js";
2121
import { initCli } from "../../lib/lifeCycle.js";
2222

2323
export default class OfferRemove extends BaseCommand<typeof OfferRemove> {
2424
static override aliases = ["provider:or"];
2525
static override description = "Remove offers";
2626
static override flags = {
2727
...baseFlags,
28-
...OFFER_FLAG,
28+
...OFFER_FLAGS,
2929
...CHAIN_FLAGS,
3030
};
3131

cli/src/lib/chain/chainValidators.ts

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,38 @@ export async function validateAddress(
7171
return `Must be a valid address. Got: ${color.yellow(stringifyUnknown(input))}`;
7272
}
7373

74-
async function getProtocolVersions() {
75-
let minProtocolVersion = BigInt(versions.protocolVersion);
76-
let maxProtocolVersion = minProtocolVersion;
77-
78-
if ((await ensureChainEnv()) !== "local") {
79-
const { readonlyDealClient } = await getReadonlyDealClient();
80-
const core = readonlyDealClient.getCore();
81-
82-
[minProtocolVersion, maxProtocolVersion] = await Promise.all([
83-
core.minProtocolVersion(),
84-
core.maxProtocolVersion(),
85-
]);
74+
let protocolVersions:
75+
| undefined
76+
| Promise<{ minProtocolVersion: bigint; maxProtocolVersion: bigint }>;
77+
78+
export async function getProtocolVersions() {
79+
if (protocolVersions === undefined) {
80+
protocolVersions = (async () => {
81+
let minProtocolVersion = BigInt(versions.protocolVersion);
82+
let maxProtocolVersion = minProtocolVersion;
83+
84+
if ((await ensureChainEnv()) !== "local") {
85+
const { readonlyDealClient } = await getReadonlyDealClient();
86+
const core = readonlyDealClient.getCore();
87+
88+
[minProtocolVersion, maxProtocolVersion] = await Promise.all([
89+
core.minProtocolVersion(),
90+
core.maxProtocolVersion(),
91+
]);
92+
}
93+
94+
return { minProtocolVersion, maxProtocolVersion };
95+
})();
8696
}
8797

88-
return { minProtocolVersion, maxProtocolVersion };
98+
return protocolVersions;
8999
}
90100

91-
let protocolVersions: undefined | ReturnType<typeof getProtocolVersions>;
92-
93101
export async function validateProtocolVersion(
94102
protocolVersion: number,
95103
): Promise<ValidationResult> {
96-
protocolVersions =
97-
protocolVersions === undefined ? getProtocolVersions() : protocolVersions;
98-
99-
const { minProtocolVersion, maxProtocolVersion } = await protocolVersions;
104+
const { minProtocolVersion, maxProtocolVersion } =
105+
await getProtocolVersions();
100106

101107
if (
102108
protocolVersion < minProtocolVersion ||

cli/src/lib/chain/commitment.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import {
6363

6464
import {
6565
peerIdHexStringToBase58String,
66-
peerIdToUint8Array,
66+
peerIdBase58ToUint8Array,
6767
} from "./conversions.js";
6868
import { fltFormatWithSymbol, fltParse } from "./currencies.js";
6969

@@ -98,7 +98,9 @@ export async function getComputePeersWithCC(
9898
const computePeersWithChainInfo = (
9999
await Promise.all(
100100
computePeers.map(async (computePeer) => {
101-
const peerIdUint8Array = await peerIdToUint8Array(computePeer.peerId);
101+
const peerIdUint8Array = await peerIdBase58ToUint8Array(
102+
computePeer.peerId,
103+
);
102104

103105
const commitmentCreatedEvent =
104106
commitmentCreatedEvents[computePeer.peerId];
@@ -225,7 +227,7 @@ export async function createCommitments(flags: {
225227
let peerIdUint8Arr;
226228

227229
try {
228-
peerIdUint8Arr = await peerIdToUint8Array(peerId);
230+
peerIdUint8Arr = await peerIdBase58ToUint8Array(peerId);
229231
} catch (e) {
230232
return {
231233
error: `Invalid peerId: ${peerId}. Error: ${stringifyUnknown(e)}`,

cli/src/lib/chain/conversions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ import type { CIDV1Struct } from "@fluencelabs/deal-ts-clients/dist/typechain-ty
2020
const PREFIX = new Uint8Array([0, 36, 8, 1, 18, 32]);
2121
const BASE_58_PREFIX = "z";
2222

23-
export async function peerIdToUint8Array(peerId: string) {
23+
export async function peerIdBase58ToUint8Array(peerIdBase58: string) {
2424
const [{ digest }, { base58btc }] = await Promise.all([
2525
import("multiformats"),
2626

2727
import("multiformats/bases/base58"),
2828
]);
2929

3030
return digest
31-
.decode(base58btc.decode(BASE_58_PREFIX + peerId))
31+
.decode(base58btc.decode(BASE_58_PREFIX + peerIdBase58))
3232
.bytes.subarray(PREFIX.length);
3333
}
3434

cli/src/lib/chain/offer/offer.ts

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ import {
6161
} from "../../helpers/utils.js";
6262
import { checkboxes } from "../../prompt.js";
6363
import { ensureFluenceEnv } from "../../resolveFluenceEnv.js";
64+
import { getProtocolVersions } from "../chainValidators.js";
6465
import {
6566
cidStringToCIDV1Struct,
6667
peerIdHexStringToBase58String,
67-
peerIdToUint8Array,
68+
peerIdBase58ToUint8Array,
6869
} from "../conversions.js";
6970
import { ptParse } from "../currencies.js";
7071
import { assertProviderIsRegistered } from "../providerInfo.js";
@@ -74,6 +75,7 @@ const OFFER_ID_PROPERTY = "offerId";
7475

7576
export type OffersArgs = {
7677
[OFFER_FLAG_NAME]?: string | undefined;
78+
[OFFER_IDS_FLAG_NAME]?: string | undefined;
7779
force?: boolean | undefined;
7880
};
7981

@@ -530,8 +532,110 @@ async function formatOfferInfo(
530532
export async function resolveOffersFromProviderConfig(
531533
flags: OffersArgs,
532534
): Promise<EnsureOfferConfig[]> {
535+
if (
536+
flags[OFFER_FLAG_NAME] !== undefined &&
537+
flags[OFFER_IDS_FLAG_NAME] !== undefined
538+
) {
539+
commandObj.error(
540+
`You can't use both ${color.yellow(
541+
`--${OFFER_FLAG_NAME}`,
542+
)} and ${color.yellow(
543+
`--${OFFER_IDS_FLAG_NAME}`,
544+
)} flags at the same time. Please pick one of them`,
545+
);
546+
}
547+
533548
const allOffers = await ensureOfferConfigs();
534549

550+
if (flags[OFFER_IDS_FLAG_NAME] !== undefined) {
551+
const offerIdsFromFlags = commaSepStrToArr(flags[OFFER_IDS_FLAG_NAME]);
552+
const offerIdsFromFlagsSet = new Set(offerIdsFromFlags);
553+
554+
const offersDefinedLocally = allOffers.filter(({ offerId }) => {
555+
return offerId !== undefined && offerIdsFromFlagsSet.has(offerId);
556+
});
557+
558+
const offersDefinedLocallySet = new Set(
559+
offersDefinedLocally.map(({ offerId }) => {
560+
return offerId;
561+
}),
562+
);
563+
564+
const offerIdsNotDefinedLocally = offerIdsFromFlags.filter((offerId) => {
565+
return !offersDefinedLocallySet.has(offerId);
566+
});
567+
568+
const [offerInfosErrors, offerInfos] = await getOffersInfo(
569+
offerIdsNotDefinedLocally.map((offerId) => {
570+
return { offerId };
571+
}),
572+
);
573+
574+
if (offerInfosErrors.length > 0) {
575+
commandObj.warn(
576+
`Wasn't able to get info about the following offers from indexer:\n\n${offerInfosErrors
577+
.map(({ offerId }) => {
578+
return offerId;
579+
})
580+
.join("\n")}`,
581+
);
582+
}
583+
584+
const protocolVersionsFromChain = await getProtocolVersions();
585+
586+
const offersNotDefinedLocally = await Promise.all(
587+
offerInfos.map(async ({ offerId, offerIndexerInfo }) => {
588+
return {
589+
offerName: `Offer ${offerId}`,
590+
minPricePerCuPerEpochBigInt: await ptParse(
591+
offerIndexerInfo.pricePerEpoch,
592+
),
593+
effectorPrefixesAndHash: await Promise.all(
594+
offerIndexerInfo.effectors.map(({ cid }) => {
595+
return cidStringToCIDV1Struct(cid);
596+
}),
597+
),
598+
effectors: offerIndexerInfo.effectors.map(({ cid }) => {
599+
return cid;
600+
}),
601+
computePeersFromProviderConfig: await Promise.all(
602+
offerIndexerInfo.peers.map(async ({ computeUnits, id }, i) => {
603+
const peerIdBase58 = await peerIdHexStringToBase58String(id);
604+
return {
605+
name: `Peer #${numToStr(i)}`,
606+
peerIdBase58,
607+
peerId: await peerIdBase58ToUint8Array(peerIdBase58),
608+
unitIds: computeUnits.map(({ id }) => {
609+
return new Uint8Array(Buffer.from(id.slice(2), "hex"));
610+
}),
611+
owner: offerIndexerInfo.providerId,
612+
};
613+
}),
614+
),
615+
offerId,
616+
minProtocolVersion: Number(
617+
protocolVersionsFromChain.minProtocolVersion,
618+
),
619+
maxProtocolVersion: Number(
620+
protocolVersionsFromChain.maxProtocolVersion,
621+
),
622+
};
623+
}),
624+
);
625+
626+
return offerIdsFromFlags.flatMap((offerId) => {
627+
const offer =
628+
offersDefinedLocally.find((o) => {
629+
return o.offerId === offerId;
630+
}) ??
631+
offersNotDefinedLocally.find((o) => {
632+
return o.offerId === offerId;
633+
});
634+
635+
return offer === undefined ? [] : [offer];
636+
});
637+
}
638+
535639
if (flags[OFFER_FLAG_NAME] === ALL_FLAG_VALUE) {
536640
return allOffers;
537641
}
@@ -630,7 +734,7 @@ async function ensureOfferConfigs() {
630734
return {
631735
name,
632736
peerIdBase58: peerId,
633-
peerId: await peerIdToUint8Array(peerId),
737+
peerId: await peerIdBase58ToUint8Array(peerId),
634738
unitIds: times(computeUnits).map(() => {
635739
return ethers.randomBytes(32);
636740
}),

cli/src/lib/configs/project/provider.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import {
5757
IPFS_PORT,
5858
defaultNumberProperties,
5959
DEFAULT_CC_DURATION,
60-
DEFAULT_CC_REWARD_DELEGATION_RATE,
60+
DEFAULT_CC_STAKER_REWARD,
6161
DURATION_EXAMPLE,
6262
DEFAULT_NUMBER_OF_COMPUTE_UNITS_ON_NOX,
6363
WS_CHAIN_URLS,
@@ -128,7 +128,7 @@ const capacityCommitmentSchemaV0 = {
128128
minimum: 0,
129129
maximum: 100,
130130
description: "Reward delegation rate in percent",
131-
default: DEFAULT_CC_REWARD_DELEGATION_RATE,
131+
default: DEFAULT_CC_STAKER_REWARD,
132132
},
133133
},
134134
} as const satisfies JSONSchemaType<CapacityCommitmentV0>;
@@ -161,7 +161,7 @@ const capacityCommitmentSchemaV1 = {
161161
minimum: 0,
162162
maximum: 100,
163163
description: "Staker reward in percent",
164-
default: DEFAULT_CC_REWARD_DELEGATION_RATE,
164+
default: DEFAULT_CC_STAKER_REWARD,
165165
},
166166
},
167167
} as const satisfies JSONSchemaType<CapacityCommitmentV1>;
@@ -1407,7 +1407,7 @@ function getDefault(args: ProviderConfigArgs) {
14071407
noxName,
14081408
{
14091409
duration: DEFAULT_CC_DURATION,
1410-
stakerReward: DEFAULT_CC_REWARD_DELEGATION_RATE,
1410+
stakerReward: DEFAULT_CC_STAKER_REWARD,
14111411
},
14121412
] as const;
14131413
}),

cli/src/lib/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ export const READMEs: Record<Template, string> = {
863863

864864
export const DEFAULT_OFFER_NAME = "defaultOffer";
865865

866-
export const DEFAULT_CC_REWARD_DELEGATION_RATE = 7;
866+
export const DEFAULT_CC_STAKER_REWARD = 20;
867867
export const DEFAULT_CC_DURATION = "100 days";
868868
export const DURATION_EXAMPLE =
869869
"in human-readable format. Example: 1 months 1 days";

cli/src/lib/deal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { versions } from "../versions.js";
2323

2424
import {
2525
cidStringToCIDV1Struct,
26-
peerIdToUint8Array,
26+
peerIdBase58ToUint8Array,
2727
} from "./chain/conversions.js";
2828
import { ptFormatWithSymbol, ptParse } from "./chain/currencies.js";
2929
import { commandObj } from "./commandObj.js";
@@ -178,7 +178,7 @@ export async function createAndMatchDealsForPeerIds({
178178

179179
const offersWithCUs = await Promise.all(
180180
peerIds.map(async (peerId) => {
181-
const peerIdUint8Array = await peerIdToUint8Array(peerId);
181+
const peerIdUint8Array = await peerIdBase58ToUint8Array(peerId);
182182

183183
const computeUnits = (
184184
await Promise.all(

cli/src/lib/generateUserProviderConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
defaultNumberProperties,
3232
type CurrencyProperty,
3333
currencyProperties,
34-
DEFAULT_CC_REWARD_DELEGATION_RATE,
34+
DEFAULT_CC_STAKER_REWARD,
3535
DURATION_EXAMPLE,
3636
DEFAULT_NUMBER_OF_COMPUTE_UNITS_ON_NOX,
3737
} from "./const.js";
@@ -108,7 +108,7 @@ export async function addComputePeers(
108108

109109
const capacityCommitmentStakerReward = await input({
110110
message: `Enter capacity commitment staker reward (in %)`,
111-
default: numToStr(DEFAULT_CC_REWARD_DELEGATION_RATE),
111+
default: numToStr(DEFAULT_CC_STAKER_REWARD),
112112
validate: validatePercent,
113113
});
114114

0 commit comments

Comments
 (0)