From db8cce62f58c1086ed62f09d6a78532cf78351c5 Mon Sep 17 00:00:00 2001 From: Tobeyw Date: Fri, 2 Feb 2024 11:11:37 +0800 Subject: [PATCH] add GetRedEnvelopeUsers and update go version --- neo3fura_http/Dockerfile | 2 +- .../biz/api/re.GetRedEnvelopeUsers.go | 161 ++++++++++++++++++ neo3fura_http/config/config.go | 1 + neo3fura_http/go.mod | 2 +- neo3fura_ws/Dockerfile | 2 +- neo3fura_ws/go.mod | 19 ++- 6 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 neo3fura_http/biz/api/re.GetRedEnvelopeUsers.go diff --git a/neo3fura_http/Dockerfile b/neo3fura_http/Dockerfile index 5187e42f..dac4d867 100644 --- a/neo3fura_http/Dockerfile +++ b/neo3fura_http/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.17 +FROM golang:1.21 ENV GO111MODULE="on" diff --git a/neo3fura_http/biz/api/re.GetRedEnvelopeUsers.go b/neo3fura_http/biz/api/re.GetRedEnvelopeUsers.go new file mode 100644 index 00000000..1daea43b --- /dev/null +++ b/neo3fura_http/biz/api/re.GetRedEnvelopeUsers.go @@ -0,0 +1,161 @@ +package api + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "github.com/joeqian10/neo3-gogogo/crypto" + "github.com/joeqian10/neo3-gogogo/helper" + "github.com/joeqian10/neo3-gogogo/rpc" + "github.com/joeqian10/neo3-gogogo/sc" + "go.mongodb.org/mongo-driver/bson/primitive" + log2 "neo3fura_http/lib/log" + "neo3fura_http/lib/type/Contract" + "neo3fura_http/lib/type/h160" + "neo3fura_http/var/stderr" + "os" + + "go.mongodb.org/mongo-driver/bson" +) + +func (me *T) GetRedEnvelopeUsers(args struct { + Asset h160.T + StartTime uint64 + EndTime uint64 + Filter map[string]interface{} + Raw *[]map[string]interface{} +}, ret *json.RawMessage) error { + if args.Asset.Valid() == false { + return stderr.ErrInvalidArgs + } + + if args.EndTime < args.StartTime { + return stderr.ErrInvalidArgs + } + rt := os.ExpandEnv("${RUNTIME}") + NetEndPoint := "http://seed2.neo.org:10332" + nnsContract := Contract.Test_NNS + switch rt { + case "test": + NetEndPoint = "http://seed2t5.neo.org:20332" + nnsContract = Contract.Test_NNS + case "test2": + NetEndPoint = "http://seed2t5.neo.org:20332" + nnsContract = Contract.Test_NNS + case "staging": + NetEndPoint = "http://seed2.neo.org:10332" + nnsContract = Contract.Main_NNS + default: + log2.Fatalf("runtime environment mismatch") + } + + flag, err := isExpiresNNS(NetEndPoint, nnsContract, "Y3J5cHRvem9tYmllLm5lbw==") + fmt.Println(flag, err) + r1, err := me.Client.QueryAggregate(struct { + Collection string + Index string + Sort bson.M + Filter bson.M + Pipeline []bson.M + Query []string + }{Collection: "Nep11TransferNotification", + Index: "GetRedEnvelopeUsers", + Sort: bson.M{}, + Filter: bson.M{}, + Pipeline: []bson.M{ + bson.M{"$match": bson.M{"contract": args.Asset, + "$and": []interface{}{ + bson.M{"timestamp": bson.M{"$gte": args.StartTime}}, + bson.M{"timestamp": bson.M{"$lte": args.EndTime}}, + }}}, + + bson.M{"$lookup": bson.M{ + "from": "Address-Asset", + "let": bson.M{"to": "$to"}, + "pipeline": []bson.M{ + bson.M{"$match": bson.M{"asset": nnsContract.Val(), "$expr": bson.M{"$and": []interface{}{ + bson.M{"$eq": []interface{}{"$address", "$$to"}}, + }}}}, + bson.M{"$project": bson.M{"asset": 1, "tokenid": 1, "address": 1}}, // bson.M{"$eq": []interface{}{"$address", "$$to"}}, + }, + "as": "nns"}, + }, + + bson.M{"$group": bson.M{"_id": "$tokenId", "transferList": bson.M{"$push": "$$ROOT"}}}, + bson.M{"$sort": bson.M{"from": 1}}, + }, + Query: []string{}, + }, ret) + if err != nil { + return err + } + + result := make(map[string]interface{}) + for _, item := range r1 { + var minter string + var minterInfo []primitive.A + transferList := item["transferList"].(primitive.A) + if len(transferList) > 1 { + for _, transfer := range transferList { + transferItem := transfer.(map[string]interface{}) + if transferItem["from"] == nil { //mint + minter = transferItem["to"].(string) + if transferItem["nns"] != nil { + nnsList := transferItem["nns"].(primitive.A) + for _, nns := range nnsList { + nnsItem := nns.(map[string]interface{}) + tokenid, err := base64.URLEncoding.DecodeString(nnsItem["tokenid"].(string)) + if err != nil { + return fmt.Errorf("tokenid base64.URLEncoding.DecodeString error %s", err) + } + isExpired, _ := isExpiresNNS(NetEndPoint, nnsContract, string(tokenid)) + if isExpired { + minterInfo = append(minterInfo, transferList) + goto endfor + } + } + } + } + + } + endfor: + } + result[minter] = minterInfo + } + + //r2, err := me.FilterArrayAndAppendCount(result, 0, args.Filter) + r2, err := me.Filter(result, args.Filter) + if err != nil { + return err + } + + r, err := json.Marshal(r2) + if err != nil { + return err + } + *ret = json.RawMessage(r) + return nil +} + +func isExpiresNNS(endPoint string, contract Contract.T, nns string) (bool, error) { + client := rpc.NewClient(endPoint) + sb := sc.NewScriptBuilder() + sh, err := helper.UInt160FromString(contract.Val()) + if err != nil { + return false, err + } + var arg = []interface{}{nns} + sb.EmitDynamicCall(sh, "ownerOf", arg) + script, err := sb.ToArray() + if err != nil { + return false, err + } + + response := client.InvokeScript(crypto.Base64Encode(script), nil) + if response.Result.State == "HALT" { + return true, nil + } else { + return false, nil + } + return false, nil +} diff --git a/neo3fura_http/config/config.go b/neo3fura_http/config/config.go index 19fef6be..bc04f886 100644 --- a/neo3fura_http/config/config.go +++ b/neo3fura_http/config/config.go @@ -158,4 +158,5 @@ var Apis = []string{ "GetNFTByAssetClassPrimaryMarket", "GetInfoByNFTPrimaryMarket", "GetMarketCollectionWhitelist", + "GetRedEnvelopeUsers", } diff --git a/neo3fura_http/go.mod b/neo3fura_http/go.mod index 4219c454..b7e5d96c 100644 --- a/neo3fura_http/go.mod +++ b/neo3fura_http/go.mod @@ -1,6 +1,6 @@ module neo3fura_http -go 1.17 +go 1.21.3 require ( github.com/go-redis/redis/v8 v8.11.3 diff --git a/neo3fura_ws/Dockerfile b/neo3fura_ws/Dockerfile index d5652ba2..a2e8ab53 100644 --- a/neo3fura_ws/Dockerfile +++ b/neo3fura_ws/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.15.6 +FROM golang:1.21 ENV GO111MODULE="on" diff --git a/neo3fura_ws/go.mod b/neo3fura_ws/go.mod index 34fed671..5d857e6a 100644 --- a/neo3fura_ws/go.mod +++ b/neo3fura_ws/go.mod @@ -1,16 +1,27 @@ module neo3fura_ws -go 1.15 +go 1.21 require ( - github.com/google/go-cmp v0.5.6 // indirect github.com/gorilla/websocket v1.4.2 - github.com/stretchr/testify v1.7.0 // indirect go.mongodb.org/mongo-driver v1.7.1 + gopkg.in/yaml.v2 v2.4.0 +) + +require ( + github.com/go-stack/stack v1.8.0 // indirect + github.com/golang/snappy v0.0.1 // indirect + github.com/google/go-cmp v0.5.6 // indirect + github.com/klauspost/compress v1.9.5 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/stretchr/testify v1.7.0 // indirect + github.com/xdg-go/pbkdf2 v1.0.0 // indirect + github.com/xdg-go/scram v1.0.2 // indirect + github.com/xdg-go/stringprep v1.0.2 // indirect + github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect golang.org/x/text v0.3.6 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect - gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect )