Skip to content

Commit 0c16935

Browse files
committed
feat(cmd/rofl): Show total price when topping up
1 parent d43cf1a commit 0c16935

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

cmd/rofl/machine/mgmt.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@ package machine
33
import (
44
"context"
55
"fmt"
6+
"os"
67

78
"github.com/spf13/cobra"
89

910
"github.com/oasisprotocol/oasis-core/go/common/cbor"
11+
"github.com/oasisprotocol/oasis-core/go/common/quantity"
1012
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/client"
13+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config"
1114
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection"
1215
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/rofl"
1316
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/modules/roflmarket"
17+
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"
1418

1519
buildRofl "github.com/oasisprotocol/cli/build/rofl"
20+
buildRoflProvider "github.com/oasisprotocol/cli/build/rofl/provider"
1621
"github.com/oasisprotocol/cli/build/rofl/scheduler"
1722
"github.com/oasisprotocol/cli/cmd/common"
1823
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
@@ -201,6 +206,12 @@ var (
201206
NeedAppID: true,
202207
NeedAdmin: false,
203208
})
209+
ctx := context.Background()
210+
211+
// This is required for the price pretty printer to work...
212+
if npa.ParaTime != nil {
213+
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
214+
}
204215

205216
machine, machineName, machineID := resolveMachine(args, deployment)
206217

@@ -220,16 +231,47 @@ var (
220231
}
221232

222233
// When not in offline mode, connect to the given network endpoint.
223-
ctx := context.Background()
224234
var conn connection.Connection
235+
var offer *roflmarket.Offer
225236
if !txCfg.Offline {
226237
conn, err = connection.Connect(ctx, npa.Network)
227238
cobra.CheckErr(err)
239+
240+
// Fetch chosen offer, so we can calculate price.
241+
offers, err := conn.Runtime(npa.ParaTime).ROFLMarket.Offers(ctx, client.RoundLatest, *providerAddr)
242+
if err != nil {
243+
cobra.CheckErr(fmt.Errorf("failed to query provider: %s", err))
244+
}
245+
246+
for _, of := range offers {
247+
if of.Metadata[buildRoflProvider.SchedulerMetadataOfferKey] == machine.Offer {
248+
offer = of
249+
break
250+
}
251+
}
252+
if offer == nil {
253+
cobra.CheckErr(fmt.Errorf("unable to find existing machine offer (%s) among market offers", machine.Offer))
254+
}
228255
}
229256

230257
fmt.Printf("Using provider: %s (%s)\n", machine.Provider, providerAddr)
231258
fmt.Printf("Top-up machine: %s [%s]\n", machineName, machine.ID)
232-
fmt.Printf("Top-up term: %d x %s\n", roflCommon.TermCount, roflCommon.Term)
259+
if txCfg.Offline {
260+
fmt.Printf("Top-up term: %d x %s\n", roflCommon.TermCount, roflCommon.Term)
261+
} else {
262+
// Calculate total price and print term info.
263+
qTermCount := quantity.NewFromUint64(roflCommon.TermCount)
264+
totalPrice, ok := offer.Payment.Native.Terms[term]
265+
if !ok {
266+
cobra.CheckErr("previously selected term not found in offer terms")
267+
}
268+
cobra.CheckErr(totalPrice.Mul(qTermCount))
269+
tp := types.NewBaseUnits(totalPrice, offer.Payment.Native.Denomination)
270+
fmt.Printf("Top-up term: %d x %s (", roflCommon.TermCount, roflCommon.Term)
271+
tp.PrettyPrint(ctx, "", os.Stdout)
272+
fmt.Println(" total)")
273+
}
274+
233275
roflCommon.PrintRentRefundWarning()
234276

235277
// Prepare transaction.

0 commit comments

Comments
 (0)