Skip to content

Commit

Permalink
NFT metadata for synthetic devices under /synthetic/device/{id}
Browse files Browse the repository at this point in the history
  • Loading branch information
elffjs committed Dec 23, 2023
1 parent 7893804 commit 306f106
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/devices-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ func startWebAPI(logger zerolog.Logger, settings *config.Settings, pdb db.Store,
v1.Get("/aftermarket/device/:tokenID/image", nftController.GetAftermarketDeviceNFTImage)
v1.Get("/manufacturer/:tokenID", nftController.GetManufacturerNFTMetadata)

v1.Get("/synthetic/device/:tokenID", cacheHandler, nftController.GetSyntheticDeviceNFTMetadata)

v1.Get("/dcn/:nodeID", nftController.GetDcnNFTMetadata)
v1.Get("/dcn/:nodeID/image", nftController.GetDCNNFTImage)
v1.Get("/integration/:tokenID", nftController.GetIntegrationNFTMetadata)
Expand Down
40 changes: 40 additions & 0 deletions internal/controllers/nft_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,46 @@ func (nc *NFTController) GetAftermarketDeviceNFTMetadataByAddress(c *fiber.Ctx)
})
}

// GetSyntheticDeviceNFTMetadata godoc
// @Description Retrieves NFT metadata for a given synthetic device.
// @Tags nfts
// @Param tokenId path int true "token id"
// @Produce json
// @Success 200 {object} controllers.NFTMetadataResp
// @Failure 404
// @Router /synthetic/device/{tokenId} [get]
func (nc *NFTController) GetSyntheticDeviceNFTMetadata(c *fiber.Ctx) error {
tidStr := c.Params("tokenID")

tid, ok := new(big.Int).SetString(tidStr, 10)
if !ok {
return fiber.NewError(fiber.StatusBadRequest, "Couldn't parse token id.")
}

sd, err := models.SyntheticDevices(
models.SyntheticDeviceWhere.TokenID.EQ(types.NullDecimal(utils.BigToDecimal(tid))),
).One(c.Context(), nc.DBS().Reader)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return fiber.NewError(fiber.StatusNotFound, "No synthetic device with that id.")
}
return err
}

var name string
if three, err := mnemonic.EntropyToMnemonicThreeWords(sd.WalletAddress); err == nil {
name = strings.Join(three, " ")
}

return c.JSON(NFTMetadataResp{
Name: name,
Description: name + ", a DIMO synthetic device.",
Attributes: []NFTAttribute{
{TraitType: "Ethereum Address", Value: common.BytesToAddress(sd.WalletAddress).String()},
},
})
}

// GetAftermarketDeviceNFTMetadata godoc
// @Description Retrieves NFT metadata for a given aftermarket device.
// @Tags nfts
Expand Down

0 comments on commit 306f106

Please sign in to comment.