2022-10-12 07:34:44 +00:00
|
|
|
package gql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-15 04:20:38 +00:00
|
|
|
"fmt" // #nosec G702
|
2022-10-12 07:34:44 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
auctiontypes "github.com/cerc-io/laconicd/x/auction/types"
|
|
|
|
bondtypes "github.com/cerc-io/laconicd/x/bond/types"
|
2022-12-09 04:17:14 +00:00
|
|
|
registrytypes "github.com/cerc-io/laconicd/x/registry/types"
|
2022-10-12 07:34:44 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2024-01-15 04:20:38 +00:00
|
|
|
"github.com/ipld/go-ipld-prime"
|
|
|
|
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
2022-10-12 07:34:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// OwnerAttributeName denotes the owner attribute name for a bond.
|
|
|
|
const OwnerAttributeName = "owner"
|
|
|
|
|
|
|
|
// BondIDAttributeName denotes the record bond ID.
|
|
|
|
const BondIDAttributeName = "bondId"
|
|
|
|
|
|
|
|
// ExpiryTimeAttributeName denotes the record expiry time.
|
|
|
|
const ExpiryTimeAttributeName = "expiryTime"
|
|
|
|
|
|
|
|
func getGQLCoin(coin sdk.Coin) *Coin {
|
|
|
|
gqlCoin := Coin{
|
|
|
|
Type: coin.Denom,
|
|
|
|
Quantity: coin.Amount.BigInt().String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return &gqlCoin
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGQLCoins(coins sdk.Coins) []*Coin {
|
|
|
|
gqlCoins := make([]*Coin, len(coins))
|
|
|
|
for index, coin := range coins {
|
|
|
|
gqlCoins[index] = getGQLCoin(coin)
|
|
|
|
}
|
|
|
|
|
|
|
|
return gqlCoins
|
|
|
|
}
|
2022-10-17 06:47:56 +00:00
|
|
|
|
2022-12-09 04:17:14 +00:00
|
|
|
func GetGQLNameAuthorityRecord(record *registrytypes.NameAuthority) (*AuthorityRecord, error) {
|
2022-10-12 07:34:44 +00:00
|
|
|
if record == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &AuthorityRecord{
|
|
|
|
OwnerAddress: record.OwnerAddress,
|
|
|
|
OwnerPublicKey: record.OwnerPublicKey,
|
|
|
|
Height: strconv.FormatUint(record.Height, 10),
|
|
|
|
Status: record.Status,
|
|
|
|
BondID: record.GetBondId(),
|
|
|
|
ExpiryTime: record.GetExpiryTime().String(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 04:17:14 +00:00
|
|
|
func getGQLRecord(ctx context.Context, resolver QueryResolver, record registrytypes.Record) (*Record, error) {
|
2022-10-12 07:34:44 +00:00
|
|
|
// Nil record.
|
|
|
|
if record.Deleted {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:20:38 +00:00
|
|
|
node, err := ipld.Decode(record.Attributes, dagjson.Decode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if node.Kind() != ipld.Kind_Map {
|
|
|
|
return nil, fmt.Errorf("invalid record attributes")
|
|
|
|
}
|
|
|
|
|
|
|
|
var links []string
|
|
|
|
attributes, err := resolveIPLDNode(node, &links)
|
2022-10-12 07:34:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:20:38 +00:00
|
|
|
references, err := resolver.GetRecordsByIds(ctx, links)
|
2022-10-12 07:34:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Record{
|
|
|
|
ID: record.Id,
|
|
|
|
BondID: record.GetBondId(),
|
|
|
|
CreateTime: record.GetCreateTime(),
|
|
|
|
ExpiryTime: record.GetExpiryTime(),
|
|
|
|
Owners: record.GetOwners(),
|
|
|
|
Names: record.GetNames(),
|
2024-01-15 04:20:38 +00:00
|
|
|
Attributes: attributes.(MapValue).Value,
|
2022-10-12 07:34:44 +00:00
|
|
|
References: references,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:20:38 +00:00
|
|
|
func resolveIPLDNode(node ipld.Node, links *[]string) (Value, error) {
|
|
|
|
switch node.Kind() {
|
|
|
|
case ipld.Kind_Map:
|
|
|
|
var entries []*Attribute
|
|
|
|
for itr := node.MapIterator(); !itr.Done(); {
|
|
|
|
k, v, err := itr.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if k.Kind() != ipld.Kind_String {
|
|
|
|
return nil, fmt.Errorf("invalid record attribute key type: %s", k.Kind())
|
|
|
|
}
|
|
|
|
s, err := k.AsString()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
val, err := resolveIPLDNode(v, links)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entries = append(entries, &Attribute{
|
|
|
|
Key: s,
|
|
|
|
Value: val,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return MapValue{entries}, nil
|
|
|
|
case ipld.Kind_List:
|
|
|
|
var values []Value
|
|
|
|
for itr := node.ListIterator(); !itr.Done(); {
|
|
|
|
_, v, err := itr.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
val, err := resolveIPLDNode(v, links)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
values = append(values, val)
|
|
|
|
}
|
|
|
|
return ArrayValue{values}, nil
|
|
|
|
case ipld.Kind_Null:
|
|
|
|
return nil, nil
|
|
|
|
case ipld.Kind_Bool:
|
|
|
|
val, err := node.AsBool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return BooleanValue{val}, nil
|
|
|
|
case ipld.Kind_Int:
|
|
|
|
val, err := node.AsInt()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// TODO: handle bigger ints
|
|
|
|
return IntValue{int(val)}, nil
|
|
|
|
case ipld.Kind_Float:
|
|
|
|
val, err := node.AsFloat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return FloatValue{val}, nil
|
|
|
|
case ipld.Kind_String:
|
|
|
|
val, err := node.AsString()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return StringValue{val}, nil
|
|
|
|
case ipld.Kind_Bytes:
|
|
|
|
val, err := node.AsBytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return BytesValue{string(val)}, nil
|
|
|
|
case ipld.Kind_Link:
|
|
|
|
val, err := node.AsLink()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
*links = append(*links, val.String())
|
|
|
|
return LinkValue{Link(val.String())}, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid node kind")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 04:17:14 +00:00
|
|
|
func getGQLNameRecord(record *registrytypes.NameRecord) (*NameRecord, error) {
|
2022-10-12 07:34:44 +00:00
|
|
|
if record == nil {
|
2022-10-17 07:15:01 +00:00
|
|
|
return nil, fmt.Errorf("got nil record")
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
records := make([]*NameRecordEntry, len(record.History))
|
|
|
|
for index, entry := range record.History {
|
|
|
|
records[index] = getNameRecordEntry(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &NameRecord{
|
|
|
|
Latest: getNameRecordEntry(record.Latest),
|
|
|
|
History: records,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-09 04:17:14 +00:00
|
|
|
func getNameRecordEntry(record *registrytypes.NameRecordEntry) *NameRecordEntry {
|
2022-10-12 07:34:44 +00:00
|
|
|
return &NameRecordEntry{
|
|
|
|
ID: record.Id,
|
|
|
|
Height: strconv.FormatUint(record.Height, 10),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGQLBond(bondObj *bondtypes.Bond) (*Bond, error) {
|
|
|
|
// Nil record.
|
|
|
|
if bondObj == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Bond{
|
|
|
|
ID: bondObj.Id,
|
|
|
|
Owner: bondObj.Owner,
|
|
|
|
Balance: getGQLCoins(bondObj.Balance),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAuctionBid(bid *auctiontypes.Bid) *AuctionBid {
|
|
|
|
return &AuctionBid{
|
|
|
|
BidderAddress: bid.BidderAddress,
|
|
|
|
Status: bid.Status,
|
|
|
|
CommitHash: bid.CommitHash,
|
|
|
|
CommitTime: bid.GetCommitTime(),
|
|
|
|
RevealTime: bid.GetRevealTime(),
|
|
|
|
CommitFee: getGQLCoin(bid.CommitFee),
|
|
|
|
RevealFee: getGQLCoin(bid.RevealFee),
|
|
|
|
BidAmount: getGQLCoin(bid.BidAmount),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetGQLAuction(auction *auctiontypes.Auction, bids []*auctiontypes.Bid) (*Auction, error) {
|
|
|
|
if auction == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gqlAuction := Auction{
|
|
|
|
ID: auction.Id,
|
|
|
|
Status: auction.Status,
|
|
|
|
OwnerAddress: auction.OwnerAddress,
|
|
|
|
CreateTime: auction.GetCreateTime(),
|
|
|
|
CommitsEndTime: auction.GetCommitsEndTime(),
|
|
|
|
RevealsEndTime: auction.GetRevealsEndTime(),
|
|
|
|
CommitFee: getGQLCoin(auction.CommitFee),
|
|
|
|
RevealFee: getGQLCoin(auction.RevealFee),
|
|
|
|
MinimumBid: getGQLCoin(auction.MinimumBid),
|
|
|
|
WinnerAddress: auction.WinnerAddress,
|
|
|
|
WinnerBid: getGQLCoin(auction.WinningBid),
|
|
|
|
WinnerPrice: getGQLCoin(auction.WinningPrice),
|
|
|
|
}
|
|
|
|
|
|
|
|
auctionBids := make([]*AuctionBid, len(bids))
|
|
|
|
for index, entry := range bids {
|
|
|
|
auctionBids[index] = getAuctionBid(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
gqlAuction.Bids = auctionBids
|
|
|
|
|
|
|
|
return &gqlAuction, nil
|
|
|
|
}
|
|
|
|
|
2024-01-15 04:20:38 +00:00
|
|
|
func toRPCValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
|
|
|
|
var rpcval registrytypes.QueryListRecordsRequest_ValueInput
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case value == nil:
|
|
|
|
return nil
|
|
|
|
case value.Int != nil:
|
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Int{Int: int64(*value.Int)}
|
|
|
|
case value.Float != nil:
|
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Float{Float: *value.Float}
|
|
|
|
case value.String != nil:
|
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_String_{String_: *value.String}
|
|
|
|
case value.Boolean != nil:
|
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Boolean{Boolean: *value.Boolean}
|
|
|
|
case value.Link != nil:
|
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Link{Link: value.Link.String()}
|
|
|
|
case value.Array != nil:
|
|
|
|
var contents registrytypes.QueryListRecordsRequest_ArrayInput
|
|
|
|
for _, val := range value.Array {
|
|
|
|
contents.Values = append(contents.Values, toRPCValue(val))
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
2024-01-15 04:20:38 +00:00
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Array{Array: &contents}
|
|
|
|
case value.Map != nil:
|
|
|
|
var contents registrytypes.QueryListRecordsRequest_MapInput
|
|
|
|
for _, kv := range value.Map {
|
|
|
|
contents.Values[kv.Key] = toRPCValue(kv.Value)
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
2024-01-15 04:20:38 +00:00
|
|
|
rpcval.Value = ®istrytypes.QueryListRecordsRequest_ValueInput_Map{Map: &contents}
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
2024-01-15 04:20:38 +00:00
|
|
|
return &rpcval
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 04:20:38 +00:00
|
|
|
func toRPCAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
|
2022-12-09 04:17:14 +00:00
|
|
|
kvPairs := []*registrytypes.QueryListRecordsRequest_KeyValueInput{}
|
2022-10-12 07:34:44 +00:00
|
|
|
|
|
|
|
for _, value := range attrs {
|
2024-01-15 04:20:38 +00:00
|
|
|
parsedValue := toRPCValue(value.Value)
|
2022-12-09 04:17:14 +00:00
|
|
|
kvPair := ®istrytypes.QueryListRecordsRequest_KeyValueInput{
|
2022-10-12 07:34:44 +00:00
|
|
|
Key: value.Key,
|
2024-01-15 04:20:38 +00:00
|
|
|
Value: parsedValue,
|
2022-10-12 07:34:44 +00:00
|
|
|
}
|
|
|
|
kvPairs = append(kvPairs, kvPair)
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvPairs
|
|
|
|
}
|