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

Update marketplace cli commands with last version of service marketplace #853

Merged
merged 4 commits into from
Apr 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"io"
"io/ioutil"
"time"

"github.com/mesg-foundation/core/commands/provider"
"github.com/mesg-foundation/core/container"
Expand Down Expand Up @@ -41,10 +42,12 @@ type ServiceExecutor interface {
// MarketplaceExecutor is an interface that handles marketplace commands.
type MarketplaceExecutor interface {
UploadSource(path string) (deployment provider.MarketplaceDeployedSource, err error)
PublishServiceVersion(service provider.MarketplaceManifestServiceData, from string) (provider.Transaction, error)
CreateServiceOffer(sid string, price string, duration string, from string) (provider.Transaction, error)
Purchase(sid, offerIndex, from string) ([]provider.Transaction, error)
SendSignedTransaction(signedTransaction string) (provider.TransactionReceipt, error)
PreparePublishServiceVersion(service provider.MarketplaceManifestServiceData, from string) (provider.Transaction, error)
PublishPublishServiceVersion(signedTransaction string) (sid, versionHash, manifest, manifestProtocol string, err error)
PrepareCreateServiceOffer(sid string, price string, duration string, from string) (provider.Transaction, error)
PublishCreateServiceOffer(signedTransaction string) (sid, offerIndex, price, duration string, err error)
PreparePurchase(sid, offerIndex, from string) ([]provider.Transaction, error)
PublishPurchase(signedTransactions []string) (sid, offerIndex, purchaser, price, duration string, expire time.Time, err error)
GetService(sid string) (provider.MarketplaceService, error)
}

Expand Down
20 changes: 12 additions & 8 deletions commands/marketplace_create_offer.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *marketplaceCreateOfferCmd) preRunE(cmd *cobra.Command, args []string) e

var confirmed bool
if err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Are you sure to create offer on service %q with price %q and duration %q?", args[0], c.price, c.duration),
Message: fmt.Sprintf("Are you sure to create offer for service %q with a price of %s MESG Tokens and a duration of %s seconds?", args[0], c.price, c.duration),
}, &confirmed, nil); err != nil {
return err
}
Expand All @@ -86,21 +86,25 @@ func (c *marketplaceCreateOfferCmd) preRunE(cmd *cobra.Command, args []string) e

func (c *marketplaceCreateOfferCmd) runE(cmd *cobra.Command, args []string) error {
var (
tx provider.Transaction
err error
tx provider.Transaction
signedTransaction string
err error
sid, offerIndex string
)
pretty.Progress("Creating offer on the marketplace...", func() {
tx, err = c.e.CreateServiceOffer(args[0], c.price, c.duration, c.account)
if err != nil {
if tx, err = c.e.PrepareCreateServiceOffer(args[0], c.price, c.duration, c.account); err != nil {
return
}
if signedTransaction, err = c.e.Sign(c.account, c.passphrase, tx); err != nil {
return
}
_, err = c.signAndSendTransaction(c.e, tx)
sid, offerIndex, _, _, err = c.e.PublishCreateServiceOffer(signedTransaction)
})
if err != nil {
return err
}
fmt.Printf("%s Offer created with success\n", pretty.SuccessSign)
fmt.Printf("%s See it on the marketplace: https://marketplace.mesg.com/services/%s\n", pretty.SuccessSign, c.service.Sid)
fmt.Printf("%s Offer created with success with index %q\n", pretty.SuccessSign, offerIndex)
fmt.Printf("%s See it on the marketplace: https://marketplace.mesg.com/services/%s#offers\n", pretty.SuccessSign, sid)

return nil
}
24 changes: 14 additions & 10 deletions commands/marketplace_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ func (c *marketplacePublishCmd) preRunE(cmd *cobra.Command, args []string) error

func (c *marketplacePublishCmd) runE(cmd *cobra.Command, args []string) error {
var (
tx provider.Transaction
err error
deployment provider.MarketplaceDeployedSource
tx provider.Transaction
err error
deployment provider.MarketplaceDeployedSource
signedTransaction string
sid, versionHash string
)

pretty.Progress("Uploading service source code on the marketplace...", func() {
Expand All @@ -90,25 +92,27 @@ func (c *marketplacePublishCmd) runE(cmd *cobra.Command, args []string) error {
return err
}
pretty.Progress("Publishing service on the marketplace...", func() {
tx, err = c.e.PublishServiceVersion(provider.MarketplaceManifestServiceData{
if tx, err = c.e.PreparePublishServiceVersion(provider.MarketplaceManifestServiceData{
Definition: *definition,
Hash: c.hash,
HashVersion: marketplaceServiceHashVersion,
Readme: readme,
Deployment: deployment,
}, c.account)
if err != nil {
}, c.account); err != nil {
return
}
_, err = c.signAndSendTransaction(c.e, tx)
if signedTransaction, err = c.e.Sign(c.account, c.passphrase, tx); err != nil {
return
}
sid, versionHash, _, _, err = c.e.PublishPublishServiceVersion(signedTransaction)
})
if err != nil {
return err
}
fmt.Printf("%s Service published with success\n", pretty.SuccessSign)
fmt.Printf("%s See it on the marketplace: https://marketplace.mesg.com/services/%s\n", pretty.SuccessSign, c.sid)
fmt.Printf("%s Service published with success with sid %q and marketplace hash %q\n", pretty.SuccessSign, sid, versionHash)
fmt.Printf("%s See it on the marketplace: https://marketplace.mesg.com/services/%s/%s\n", pretty.SuccessSign, sid, versionHash)

fmt.Printf("%s To create a service offer, execute the command:\n\tmesg-core marketplace create-offer %s\n", pretty.SuccessSign, c.sid)
fmt.Printf("%s To create a service offer, execute the command:\n\tmesg-core marketplace create-offer %s\n", pretty.SuccessSign, sid)

return nil
}
21 changes: 14 additions & 7 deletions commands/marketplace_purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"
"strconv"
"time"

"github.com/mesg-foundation/core/commands/provider"
"github.com/mesg-foundation/core/utils/pretty"
Expand Down Expand Up @@ -72,7 +73,7 @@ func (c *marketplacePurchaseCmd) preRunE(cmd *cobra.Command, args []string) erro

var confirmed bool
if err := survey.AskOne(&survey.Confirm{
Message: fmt.Sprintf("Are you sure to purchase service %q for price %q and duration %q?", args[0], offer.Price, offer.Duration),
Message: fmt.Sprintf("Are you sure to purchase service %q for %s MESG Tokens and for a duration of %s seconds?", args[0], offer.Price, offer.Duration),
}, &confirmed, nil); err != nil {
return err
}
Expand All @@ -85,25 +86,31 @@ func (c *marketplacePurchaseCmd) preRunE(cmd *cobra.Command, args []string) erro

func (c *marketplacePurchaseCmd) runE(cmd *cobra.Command, args []string) error {
var (
transactions []provider.Transaction
err error
txs []provider.Transaction
signedTxs []string
signedTx string
err error
sid string
expire time.Time
)
pretty.Progress("Purchasing offer on the marketplace...", func() {
transactions, err = c.e.Purchase(args[0], c.offerIndex, c.account)
txs, err = c.e.PreparePurchase(args[0], c.offerIndex, c.account)
if err != nil {
return
}
for _, tx := range transactions {
_, err = c.signAndSendTransaction(c.e, tx)
for _, tx := range txs {
signedTx, err = c.e.Sign(c.account, c.passphrase, tx)
if err != nil {
return
}
signedTxs = append(signedTxs, signedTx)
}
sid, _, _, _, _, expire, err = c.e.PublishPurchase(signedTxs)
})
if err != nil {
return err
}
fmt.Printf("%s Offer purchased with success\n", pretty.SuccessSign)
fmt.Printf("%s Offer of service %q purchased with success and will expire on %s\n", pretty.SuccessSign, sid, expire.Local())

return nil
}
9 changes: 0 additions & 9 deletions commands/marketplace_root.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commands

import (
"github.com/mesg-foundation/core/commands/provider"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -52,11 +51,3 @@ func (c *baseMarketplaceCmd) askAccountAndPassphrase() error {
}
return nil
}

func (c *baseMarketplaceCmd) signAndSendTransaction(e Executor, transaction provider.Transaction) (provider.TransactionReceipt, error) {
signedTransaction, err := e.Sign(c.account, c.passphrase, transaction)
if err != nil {
return provider.TransactionReceipt{}, err
}
return e.SendSignedTransaction(signedTransaction)
}
78 changes: 50 additions & 28 deletions commands/provider/marketplace_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"encoding/json"
"time"

"github.com/docker/docker/pkg/archive"
"github.com/mesg-foundation/core/ipfs"
Expand All @@ -18,46 +19,67 @@ func NewMarketplaceProvider(c coreapi.CoreClient) *MarketplaceProvider {
return &MarketplaceProvider{client: client{c}}
}

// PublishServiceVersion executes the create service version task
func (p *MarketplaceProvider) PublishServiceVersion(service MarketplaceManifestServiceData, from string) (Transaction, error) {
input := marketplacePublishServiceVersionTaskInputs{
marketplaceTransactionTaskInputs: marketplaceTransactionTaskInputs{From: from},
Service: service,
// PreparePublishServiceVersion executes the create service version task
func (p *MarketplaceProvider) PreparePublishServiceVersion(service MarketplaceManifestServiceData, from string) (Transaction, error) {
input := marketplacePreparePublishServiceVersionTaskInputs{
marketplacePrepareTaskInputs: marketplacePrepareTaskInputs{From: from},
Service: service,
}
var output Transaction
return output, p.call("publishServiceVersion", input, &output)
return output, p.call("preparePublishServiceVersion", input, &output)
}

// CreateServiceOffer executes the create service offer task
func (p *MarketplaceProvider) CreateServiceOffer(sid string, price string, duration string, from string) (Transaction, error) {
input := marketplaceCreateServiceOfferTaskInputs{
marketplaceTransactionTaskInputs: marketplaceTransactionTaskInputs{From: from},
Sid: sid,
Price: price,
Duration: duration,
// PublishPublishServiceVersion executes the task publish service version task
func (p *MarketplaceProvider) PublishPublishServiceVersion(signedTransaction string) (sid, versionHash, manifest, manifestProtocol string, err error) {
input := marketplacePublishTaskInputs{
SignedTransaction: signedTransaction,
}
var output Transaction
return output, p.call("createServiceOffer", input, &output)
var o marketplacePublishPublishServiceVersionTaskOutputs
err = p.call("publishPublishServiceVersion", input, &o)
return o.Sid, o.VersionHash, o.Manifest, o.ManifestProtocol, err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keeping the same pattern in the code. You could expose marketplacePublishPublishServiceVersionTaskOutputs and have something like that

var o marketplacePublishPublishServiceVersionTaskOutputs
return o, p.call("publishPublishServiceVersion", input, &o)

I feel a function that returns too much data should return a struct.

Same for the PublishCreateServiceOffer and PublishPurchase

Copy link
Member Author

@NicolasMahe NicolasMahe Apr 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the function has too many returned value but:

  • the CLI doesn't directly depend on the package provider, all the function are declared in commands.go
  • it returns only simple types
  • the commands actually only read 2 or 3 returned values. Should I only returned value useful for the commands?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the CLI doesn't directly depend on the package provider, all the function are declared in commands.go

It does on few functions already: UploadSource, PreparePublishServiceVersion, PrepareCreateServiceOffer, PreparePurchase, GetService... basically all except these ones

it returns only simple types

No it doesn't:

PreparePublishServiceVersion(service provider.MarketplaceManifestServiceData, from string) (provider.Transaction, error)
GetService(sid string) (provider.MarketplaceService, error)

the commands actually only read 2 or 3 returned values. Should I only returned value useful for the commands?

If we need only these data we could just return them but if we have the struct let's return the struct like that we have all the data without having a long list of outputs

}

// Purchase executes the purchase task
func (p *MarketplaceProvider) Purchase(sid, offerIndex, from string) ([]Transaction, error) {
input := marketplacePurchaseTaskInputs{
marketplaceTransactionTaskInputs: marketplaceTransactionTaskInputs{From: from},
Sid: sid,
OfferIndex: offerIndex,
// PrepareCreateServiceOffer executes the create service offer task
func (p *MarketplaceProvider) PrepareCreateServiceOffer(sid string, price string, duration string, from string) (Transaction, error) {
input := marketplacePrepareCreateServiceOfferTaskInputs{
marketplacePrepareTaskInputs: marketplacePrepareTaskInputs{From: from},
Sid: sid,
Price: price,
Duration: duration,
}
var output marketplacePurchaseTaskOutputs
return output.Transactions, p.call("purchase", input, &output)
var output Transaction
return output, p.call("prepareCreateServiceOffer", input, &output)
}

// SendSignedTransaction executes the task send signed transaction.
func (p *MarketplaceProvider) SendSignedTransaction(signedTransaction string) (TransactionReceipt, error) {
input := marketplaceSendSignedTransactionTaskInputs{
// PublishCreateServiceOffer executes the task publish service offer task
func (p *MarketplaceProvider) PublishCreateServiceOffer(signedTransaction string) (sid, offerIndex, price, duration string, err error) {
input := marketplacePublishTaskInputs{
SignedTransaction: signedTransaction,
}
var output TransactionReceipt
return output, p.call("sendSignedTransaction", input, &output)
var o marketplacePublishCreateServiceOfferTaskOutputs
err = p.call("publishCreateServiceOffer", input, &o)
return o.Sid, o.OfferIndex, o.Price, o.Duration, err
}

// PreparePurchase executes the purchase task
func (p *MarketplaceProvider) PreparePurchase(sid, offerIndex, from string) ([]Transaction, error) {
input := marketplacePreparePurchaseTaskInputs{
marketplacePrepareTaskInputs: marketplacePrepareTaskInputs{From: from},
Sid: sid,
OfferIndex: offerIndex,
}
var output marketplacePreparePurchaseTaskOutputs
return output.Transactions, p.call("preparePurchase", input, &output)
}

// PublishPurchase executes the task publish service version task
func (p *MarketplaceProvider) PublishPurchase(signedTransactions []string) (sid, offerIndex, purchaser, price, duration string, expire time.Time, err error) {
input := marketplacePublishPurchaseTaskInputs{
SignedTransactions: signedTransactions,
}
var o marketplacePublishPurchaseTaskOutputs
err = p.call("publishPurchase", input, &o)
return o.Sid, o.OfferIndex, o.Purchaser, o.Price, o.Duration, o.Expire, err
}

// GetService executes the task get service.
Expand Down
52 changes: 40 additions & 12 deletions commands/provider/marketplace_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"encoding/json"
"time"

"github.com/mesg-foundation/core/service/importer"
)
Expand Down Expand Up @@ -41,8 +42,8 @@ type MarketplaceService struct {
Active bool `json:"active"`
} `json:"offers"`
Purchases []struct {
Purchaser string `json:"purchaser"`
Expire string `json:"expire"`
Purchaser string `json:"purchaser"`
Expire time.Time `json:"expire"`
} `json:"purchases"`
}

Expand Down Expand Up @@ -77,36 +78,63 @@ func (d *MarketplaceManifestData) UnmarshalJSON(data []byte) error {
return nil
}

type marketplaceTransactionTaskInputs struct {
type marketplacePrepareTaskInputs struct {
From string `json:"from"`
Gas string `json:"gas,omitempty"`
GasPrice string `json:"gasPrice,omitempty"`
}

type marketplacePublishServiceVersionTaskInputs struct {
marketplaceTransactionTaskInputs
type marketplacePublishTaskInputs struct {
SignedTransaction string `json:"signedTransaction"`
}

type marketplacePreparePublishServiceVersionTaskInputs struct {
marketplacePrepareTaskInputs
Service MarketplaceManifestServiceData `json:"service"`
}

type marketplaceCreateServiceOfferTaskInputs struct {
marketplaceTransactionTaskInputs
type marketplacePublishPublishServiceVersionTaskOutputs struct {
Sid string `json:"sid"`
VersionHash string `json:"versionHash"`
Manifest string `json:"manifest"`
ManifestProtocol string `json:"manifestProtocol"`
}

type marketplacePrepareCreateServiceOfferTaskInputs struct {
marketplacePrepareTaskInputs
Sid string `json:"sid"`
Price string `json:"price"`
Duration string `json:"duration"`
}

type marketplacePurchaseTaskInputs struct {
marketplaceTransactionTaskInputs
type marketplacePublishCreateServiceOfferTaskOutputs struct {
Sid string `json:"sid"`
OfferIndex string `json:"offerIndex"`
Price string `json:"price"`
Duration string `json:"duration"`
}

type marketplacePreparePurchaseTaskInputs struct {
marketplacePrepareTaskInputs
Sid string `json:"sid"`
OfferIndex string `json:"offerIndex"`
}

type marketplacePurchaseTaskOutputs struct {
type marketplacePreparePurchaseTaskOutputs struct {
Transactions []Transaction `json:"transactions"`
}

type marketplaceSendSignedTransactionTaskInputs struct {
SignedTransaction string `json:"signedTransaction"`
type marketplacePublishPurchaseTaskInputs struct {
SignedTransactions []string `json:"signedTransactions"`
}

type marketplacePublishPurchaseTaskOutputs struct {
Sid string `json:"sid"`
OfferIndex string `json:"offerIndex"`
Purchaser string `json:"purchaser"`
Price string `json:"price"`
Duration string `json:"duration"`
Expire time.Time `json:"expire"`
}

type marketplaceGetServiceTaskInputs struct {
Expand Down