From 5fdd6f16ff71fb44a865c6b028aa8970731ccebf Mon Sep 17 00:00:00 2001 From: Laurent Le Houerou Date: Mon, 25 Apr 2022 22:05:52 +0400 Subject: [PATCH] add all_collections & buy_token message to deviants faction market --- protocols/deviantfactions/market.go | 50 +++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/protocols/deviantfactions/market.go b/protocols/deviantfactions/market.go index 6573978..f61dfa6 100644 --- a/protocols/deviantfactions/market.go +++ b/protocols/deviantfactions/market.go @@ -3,9 +3,8 @@ package deviantfactions import ( "context" - "github.com/galacticship/terra/cosmos" - "github.com/galacticship/terra" + "github.com/galacticship/terra/cosmos" "github.com/pkg/errors" "github.com/shopspring/decimal" ) @@ -34,6 +33,43 @@ type Listing struct { BlockExpires int64 } +func (m *Market) AllCollections(ctx context.Context) ([]string, error) { + var res []string + startAfter := "" + for { + tmp, err := m.allCollectionsPage(ctx, 30, startAfter) + if err != nil { + return nil, errors.Wrapf(err, "getting page with startafter %s", startAfter) + } + res = append(res, tmp...) + if len(tmp) < 30 { + break + } + startAfter = tmp[len(tmp)-1] + } + return res, nil +} + +func (m *Market) allCollectionsPage(ctx context.Context, limit int, startAfter string) ([]string, error) { + var q struct { + AllCollections struct { + Limit int `json:"limit"` + StartAfter string `json:"start_after"` + } `json:"all_collections"` + } + q.AllCollections.Limit = limit + q.AllCollections.StartAfter = startAfter + var r struct { + Collections []string `json:"collections"` + } + err := m.QueryStore(ctx, q, &r) + if err != nil { + return nil, errors.Wrap(err, "querying contract store") + } + + return r.Collections, nil +} + func (m *Market) AllListedTokens(ctx context.Context, collectionIds []string) ([]Listing, error) { var res []Listing startAfter := "" @@ -195,3 +231,13 @@ func (m *Market) NewListTokenMessage(sender cosmos.AccAddress, tokenId string, A q.ListToken.BlocksToListFor = BlocksToListFor return m.NewMsgExecuteContract(sender, q) } + +func (m *Market) NewBuyTokenMessage(sender cosmos.AccAddress, tokenId string, token terra.NativeToken, price decimal.Decimal) (cosmos.Msg, error) { + var q struct { + BuyToken struct { + TokenId string `json:"token_id"` + } `json:"buy_token"` + } + q.BuyToken.TokenId = tokenId + return token.NewMsgSendExecute(sender, m.Contract, price, q) +}