17
17
18
18
import { color } from "@oclif/color" ;
19
19
import omit from "lodash-es/omit.js" ;
20
+ import { stringify } from "yaml" ;
20
21
21
22
import { commandObj } from "../../commandObj.js" ;
22
23
import type { ResourceType } from "../../configs/project/provider/provider4.js" ;
@@ -31,6 +32,7 @@ import { numToStr } from "../../helpers/typesafeStringify.js";
31
32
import { splitErrorsAndResults } from "../../helpers/utils.js" ;
32
33
import { confirm } from "../../prompt.js" ;
33
34
import { ensureFluenceEnv } from "../../resolveFluenceEnv.js" ;
35
+ import type { Required } from "../../typeHelpers.js" ;
34
36
import { uint8ArrayToPeerIdHexString } from "../conversions.js" ;
35
37
import {
36
38
peerIdHexStringToBase58String ,
@@ -58,7 +60,9 @@ type PeersOnChain = {
58
60
hexPeerId : string ;
59
61
} [ ] ;
60
62
61
- type Txs = { description ?: string ; tx : ReturnType < typeof populateTx > } [ ] ;
63
+ type Tx = { description ?: string ; tx : ReturnType < typeof populateTx > } ;
64
+ type Txs = Tx [ ] ;
65
+ type TxsWithDescription = Required < Tx > [ ] ;
62
66
63
67
export async function updateOffers ( flags : OffersArgs ) {
64
68
const offers = await resolveOffersFromProviderConfig ( flags ) ;
@@ -79,7 +83,25 @@ export async function updateOffers(flags: OffersArgs) {
79
83
] . flat ( ) ;
80
84
81
85
if ( firstUpdateOffersTx === undefined ) {
82
- commandObj . logToStderr ( "No changes found for selected offers" ) ;
86
+ const addedCPs = populatedTxs . reduce < Record < string , string [ ] > > (
87
+ ( acc , { addedCPsNames, offerName } ) => {
88
+ if ( addedCPsNames . length > 0 ) {
89
+ acc [ offerName ] = addedCPsNames ;
90
+ }
91
+
92
+ return acc ;
93
+ } ,
94
+ { } ,
95
+ ) ;
96
+
97
+ if ( Object . values ( addedCPs ) . length === 0 ) {
98
+ commandObj . logToStderr ( "No changes found for selected offers" ) ;
99
+ } else {
100
+ commandObj . logToStderr (
101
+ `Added the compute peers to the following offers:\n${ stringify ( addedCPs ) } ` ,
102
+ ) ;
103
+ }
104
+
83
105
return ;
84
106
}
85
107
@@ -229,10 +251,14 @@ function populateUpdateOffersTxs(offersFoundOnChain: OnChainOffer[]) {
229
251
peersOnChain ,
230
252
) ) satisfies Txs ;
231
253
232
- await addMissingComputePeers ( offer , peersOnChain ) ;
254
+ const { addedCPsNames } = await addMissingComputePeers (
255
+ offer ,
256
+ peersOnChain ,
257
+ ) ;
233
258
234
259
const txs = (
235
260
await Promise . all ( [
261
+ populateDataCenterTx ( offer ) ,
236
262
populateChangeResourceSupplyAndDetailsTx ( offer ) ,
237
263
populateCUToRemoveTxs ( offer , peersOnChain ) ,
238
264
populateCUToAddTxs ( offer , peersOnChain ) ,
@@ -244,7 +270,13 @@ function populateUpdateOffersTxs(offersFoundOnChain: OnChainOffer[]) {
244
270
] )
245
271
) . flat ( ) satisfies Txs ;
246
272
247
- return { offerName, offerId, removePeersFromOffersTxs, txs } ;
273
+ return {
274
+ offerName,
275
+ offerId,
276
+ removePeersFromOffersTxs,
277
+ txs,
278
+ addedCPsNames,
279
+ } ;
248
280
} ) ,
249
281
) ;
250
282
}
@@ -344,6 +376,28 @@ async function populatePaymentTokenTx({
344
376
] ;
345
377
}
346
378
379
+ async function populateDataCenterTx ( {
380
+ offerIndexerInfo,
381
+ offerId,
382
+ dataCenter,
383
+ } : OnChainOffer ) {
384
+ const { contracts } = await getContracts ( ) ;
385
+ return offerIndexerInfo . dataCenter ?. id === dataCenter . id
386
+ ? [ ]
387
+ : [
388
+ {
389
+ description : `\nchanging data center from ${ color . yellow (
390
+ offerIndexerInfo . dataCenter ?. id ,
391
+ ) } to ${ color . yellow ( dataCenter . id ) } `,
392
+ tx : populateTx (
393
+ contracts . diamond . setOfferDatacenter ,
394
+ offerId ,
395
+ dataCenter . id ,
396
+ ) ,
397
+ } ,
398
+ ] ;
399
+ }
400
+
347
401
async function populateCUToRemoveTxs (
348
402
{ computePeersFromProviderConfig } : OnChainOffer ,
349
403
peersOnChain : PeersOnChain ,
@@ -492,7 +546,7 @@ async function createResourceSupplyAndDetailsUpdateTx(
492
546
peerId : string ,
493
547
{ resourceType, onChainResource, configuredResource } : ResourceSupplyUpdate ,
494
548
) {
495
- const txs : { description : string ; tx : ReturnType < typeof populateTx > } [ ] = [ ] ;
549
+ const txs : TxsWithDescription = [ ] ;
496
550
497
551
if ( onChainResource . resourceId !== configuredResource . resourceId ) {
498
552
return txs ;
@@ -548,7 +602,7 @@ async function populateChangeResourceSupplyAndDetailsTx({
548
602
computePeersFromProviderConfig,
549
603
offerIndexerInfo,
550
604
} : OnChainOffer ) {
551
- const txs : { description ?: string ; tx : ReturnType < typeof populateTx > } [ ] = [ ] ;
605
+ const txs : Txs = [ ] ;
552
606
553
607
for ( const peer of offerIndexerInfo . peers ) {
554
608
const peerIdBase58 = await peerIdHexStringToBase58String ( peer . id ) ;
@@ -638,7 +692,7 @@ async function createResourceUpdateTx(
638
692
peerId : string ,
639
693
{ resourceType, onChainResource, configuredResource } : ResourceUpdate ,
640
694
) {
641
- const txs : { description : string ; tx : ReturnType < typeof populateTx > } [ ] = [ ] ;
695
+ const txs : TxsWithDescription = [ ] ;
642
696
const { contracts } = await getContracts ( ) ;
643
697
644
698
function removeResource ( ) {
0 commit comments