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

Add NFT Endpoints: /api/get-nfts-by-did and /api/fetch-nft #258

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 31 additions & 0 deletions client/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ type CreateNFTReq struct {
Artifact string
}

type FetchNFTRequest struct {
NFT string
NFTPath string
}

func (c *Client) CreateNFT(createNFTReq *CreateNFTReq) (*model.BasicResponse, error) {
fields := make(map[string]string)
files := make(map[string]string)
Expand Down Expand Up @@ -81,3 +86,29 @@ func (c *Client) GetAllNFTs(did string) (*model.NFTTokens, error) {
}
return &tkns, nil
}

func (c *Client) GetNFTsByDid(did string) (*model.NFTTokens, error) {
q := make(map[string]string)
q["did"] = did
var tkns model.NFTTokens
err := c.sendJSONRequest("GET", setup.APIGetNftsByDid, q, nil, &tkns)
if err != nil {
return nil, err
}
return &tkns, nil
}

func (c *Client) FetchNFT(fetchNft *FetchNFTRequest) (*model.BasicResponse, error) {
fields := make(map[string]string)
if fetchNft.NFT != "" {
fields["nft"] = fetchNft.NFT
}

var basicResponse model.BasicResponse
err := c.sendJSONRequest("GET", setup.APIFetchNft, fields, nil, &basicResponse)
if err != nil {
return nil, err
}
return &basicResponse, nil

}
12 changes: 10 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const (
DeployNFTCmd string = "deploy-nft"
ExecuteNFTCmd string = "execute-nft"
SubscribeNFTCmd string = "subscribe-nft"
FetchNftCmd string = "fetch-nft"
GetNftsByDidCmd string = "get-nfts-by-did"
)

var commands = []string{VersionCmd,
Expand Down Expand Up @@ -156,6 +158,8 @@ var commands = []string{VersionCmd,
DeployNFTCmd,
ExecuteNFTCmd,
SubscribeNFTCmd,
FetchNftCmd,
GetNftsByDidCmd,
}

var commandsHelp = []string{"To get tool version",
Expand Down Expand Up @@ -632,8 +636,8 @@ func Run(args []string) {
cmd.SubscribeContract()
case CreateNFTCmd:
cmd.createNFT()
// case GetAllNFTCmd:
// cmd.getAllNFTs()
case GetAllNFTCmd:
cmd.getAllNFTs()
case DeploySmartContractCmd:
cmd.deploySmartcontract()
case GenerateSmartContractToken:
Expand Down Expand Up @@ -688,6 +692,10 @@ func Run(args []string) {
cmd.dumpNFTTokenChain()
case SubscribeNFTCmd:
cmd.SubscribeNFT()
case FetchNftCmd:
cmd.fetchNFT()
case GetNftsByDidCmd:
cmd.getNFTsByDid()
default:
cmd.log.Error("Invalid command")
}
Expand Down
53 changes: 53 additions & 0 deletions command/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/rubixchain/rubixgoplatform/client"
"github.com/rubixchain/rubixgoplatform/core"
"github.com/rubixchain/rubixgoplatform/core/model"
)

Expand Down Expand Up @@ -193,3 +194,55 @@ func (cmd *Command) getAllNFTs() {
}
cmd.log.Info("Got all NFTs successfully")
}

func (cmd *Command) getNFTsByDid() {
if cmd.did == "" {
cmd.log.Error("Failed to get NFTs, DID is required to get NFTs")
return
}
tkns, err := cmd.c.GetNFTsByDid(cmd.did)
if err != nil {
cmd.log.Error("Failed to get NFTs, " + err.Error())
return
}
for _, tkn := range tkns.Tokens {
fmt.Printf("NFT : %s, Status : %d\n", tkn.Token, tkn.TokenStatus)
}
cmd.log.Info("Got all NFTs successfully")
}

func (cmd *Command) fetchNFT() {
if cmd.nft == "" {
cmd.log.Info("nft id cannot be empty")
fmt.Print("Enter NFT Token Id : ")
_, err := fmt.Scan(&cmd.nft)
if err != nil {
cmd.log.Error("Failed to get NFT Token ID")
return
}
}
isAlphanumeric := regexp.MustCompile(`^[a-zA-Z0-9]*$`).MatchString(cmd.nft)

if len(cmd.nft) != 46 || !strings.HasPrefix(cmd.nft, "Qm") || !isAlphanumeric {
cmd.log.Error("Invalid smart contract token")
return
}
nftRequest := core.FetchNFTRequest{
NFT: cmd.nft,
}

request := client.FetchNFTRequest{
NFT: nftRequest.NFT,
}

basicResponse, err := cmd.c.FetchNFT(&request)
if err != nil {
cmd.log.Error("Failed to fetch nft", "err", err)
return
}
if !basicResponse.Status {
cmd.log.Error("Failed to fetch nft", "err", err)
return
}
cmd.log.Info("NFT fetched successfully")
}
25 changes: 25 additions & 0 deletions core/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,28 @@ func (c *Core) GetAllNFT() model.NFTList {
return response

}

func (c *Core) GetNFTsByDid(did string) model.NFTList {
response := model.NFTList{
BasicResponse: model.BasicResponse{
Status: false,
},
}
nftList, err := c.w.GetNFTsByDid(did)
if err != nil {
errorMsg := fmt.Sprintf("Failed to get NFT list of the did: ", did, "err", err)
c.log.Error(errorMsg)
response.Message = errorMsg
return response
}
nftDetails := make([]model.NFTInfo, 0)
for _, nft := range nftList {
nftDetails = append(nftDetails, model.NFTInfo{NFTId: nft.TokenID, Owner: nft.DID, Value: nft.TokenValue})
}
response.NFTs = nftDetails
response.Status = true
response.Message = "Got All NFTs"

return response

}
12 changes: 11 additions & 1 deletion core/wallet/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (w *Wallet) CreateNFT(nt *NFT, local bool) error {
return nil
}

// GetNFTByDid get all NFTs from db
// GetAllNFT get all NFTs from db
func (w *Wallet) GetAllNFT() ([]NFT, error) {
var tkns []NFT
err := w.s.Read(NFTTokenStorage, &tkns, "token_id != ?", "")
Expand All @@ -38,6 +38,16 @@ func (w *Wallet) GetAllNFT() ([]NFT, error) {
return tkns, nil
}

// GetNFTsByDid get all the NFTs of that did from db
func (w *Wallet) GetNFTsByDid(did string) ([]NFT, error) {
var tkns []NFT
err := w.s.Read(NFTTokenStorage, &tkns, "did=?", did)
if err != nil {
return nil, err
}
return tkns, nil
}

// GetNFT get NFT from db
func (w *Wallet) GetNFT(did string, nft string, lock bool) (*NFT, error) {
var tkns NFT
Expand Down
61 changes: 61 additions & 0 deletions docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,37 @@
}
}
},
"/api/fetch-nft": {
"get": {
"description": "This API will Fetch NFT",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"NFT"
],
"summary": "Fetch NFT",
"operationId": "fetch-nft",
"parameters": [
{
"type": "string",
"name": "nft",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.BasicResponse"
}
}
}
}
},
"/api/fetch-smart-contract": {
"get": {
"description": "This API will Fetch smart contract",
Expand Down Expand Up @@ -687,6 +718,36 @@
}
}
},
"/api/get-nfts-by-did": {
"get": {
"description": "This API will get all NFTs owned by the particular did",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"NFT"
],
"summary": "Get NFTs owned by the particular did",
"parameters": [
{
"type": "string",
"name": "did",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/model.NFTList"
}
}
}
}
},
"/api/get-pledgedtoken-details": {
"get": {
"description": "This API allows the user to get details about the tokens the quorums have pledged i.e. which token is pledged for which token state",
Expand Down
Loading
Loading