From aaf7addda2d67383c25414935f40e12e0b35b224 Mon Sep 17 00:00:00 2001 From: Nicolas Mahe Date: Fri, 5 Apr 2019 18:58:40 +0700 Subject: [PATCH] Update marketplace cli commands to match last version of service --- commands/commands.go | 11 ++-- commands/marketplace_create_offer.go | 20 +++--- commands/marketplace_publish.go | 24 ++++--- commands/marketplace_purchase.go | 21 ++++-- commands/marketplace_root.go | 9 --- commands/provider/marketplace_provider.go | 78 +++++++++++++++-------- commands/provider/marketplace_types.go | 52 +++++++++++---- 7 files changed, 137 insertions(+), 78 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 56f9773e9..0b8a24b13 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -3,6 +3,7 @@ package commands import ( "io" "io/ioutil" + "time" "github.com/mesg-foundation/core/commands/provider" "github.com/mesg-foundation/core/container" @@ -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) } diff --git a/commands/marketplace_create_offer.go b/commands/marketplace_create_offer.go index c2bbc1d22..6b7ff61c4 100644 --- a/commands/marketplace_create_offer.go +++ b/commands/marketplace_create_offer.go @@ -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 } @@ -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 } diff --git a/commands/marketplace_publish.go b/commands/marketplace_publish.go index 9eec25206..0f32a0a3f 100644 --- a/commands/marketplace_publish.go +++ b/commands/marketplace_publish.go @@ -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() { @@ -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 } diff --git a/commands/marketplace_purchase.go b/commands/marketplace_purchase.go index 64df05140..c4dba0554 100644 --- a/commands/marketplace_purchase.go +++ b/commands/marketplace_purchase.go @@ -3,6 +3,7 @@ package commands import ( "fmt" "strconv" + "time" "github.com/mesg-foundation/core/commands/provider" "github.com/mesg-foundation/core/utils/pretty" @@ -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 } @@ -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 } diff --git a/commands/marketplace_root.go b/commands/marketplace_root.go index 68699fbd8..f382a015e 100644 --- a/commands/marketplace_root.go +++ b/commands/marketplace_root.go @@ -1,7 +1,6 @@ package commands import ( - "github.com/mesg-foundation/core/commands/provider" "github.com/spf13/cobra" ) @@ -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) -} diff --git a/commands/provider/marketplace_provider.go b/commands/provider/marketplace_provider.go index eead6bb4d..e2a8e2669 100644 --- a/commands/provider/marketplace_provider.go +++ b/commands/provider/marketplace_provider.go @@ -2,6 +2,7 @@ package provider import ( "encoding/json" + "time" "github.com/docker/docker/pkg/archive" "github.com/mesg-foundation/core/ipfs" @@ -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 } -// 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. diff --git a/commands/provider/marketplace_types.go b/commands/provider/marketplace_types.go index c34f00994..d22089939 100644 --- a/commands/provider/marketplace_types.go +++ b/commands/provider/marketplace_types.go @@ -2,6 +2,7 @@ package provider import ( "encoding/json" + "time" "github.com/mesg-foundation/core/service/importer" ) @@ -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"` } @@ -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 {