Database API for NFT metadata and enumerables

This commit is contained in:
Srdjan Obucina 2020-07-02 00:23:09 +02:00
parent e2c375e2a2
commit 3a8d8b9e98
3 changed files with 161 additions and 0 deletions

View file

@ -189,6 +189,12 @@ class database_api_impl : public std::enable_shared_from_this<database_api_impl>
optional<account_id_type> nft_owner_of(const nft_id_type token_id) const;
optional<account_id_type> nft_get_approved(const nft_id_type token_id) const;
bool nft_is_approved_for_all(const account_id_type owner, const account_id_type operator_) const;
string nft_get_name(const nft_metadata_id_type nft_metadata_id) const;
string nft_get_symbol(const nft_metadata_id_type nft_metadata_id) const;
string nft_get_token_uri(const nft_id_type token_id) const;
uint64_t nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const;
nft_object nft_token_by_index(const nft_metadata_id_type nft_metadata_id, const uint64_t token_idx) const;
nft_object nft_token_of_owner_by_index(const nft_metadata_id_type nft_metadata_id, const account_id_type owner, const uint64_t token_idx) const;
//private:
const account_object* get_account_from_string( const std::string& name_or_id,
@ -2377,6 +2383,104 @@ bool database_api_impl::nft_is_approved_for_all(const account_id_type owner, con
return result;
}
string database_api::nft_get_name(const nft_metadata_id_type nft_metadata_id) const
{
return my->nft_get_name(nft_metadata_id);
}
string database_api_impl::nft_get_name(const nft_metadata_id_type nft_metadata_id) const
{
const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>();
auto itr_nft_md = idx_nft_md.find(nft_metadata_id);
if (itr_nft_md != idx_nft_md.end()) {
return itr_nft_md->name;
}
return "";
}
string database_api::nft_get_symbol(const nft_metadata_id_type nft_metadata_id) const
{
return my->nft_get_symbol(nft_metadata_id);
}
string database_api_impl::nft_get_symbol(const nft_metadata_id_type nft_metadata_id) const
{
const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>();
auto itr_nft_md = idx_nft_md.find(nft_metadata_id);
if (itr_nft_md != idx_nft_md.end()) {
return itr_nft_md->symbol;
}
return "";
}
string database_api::nft_get_token_uri(const nft_id_type token_id) const
{
return my->nft_get_token_uri(token_id);
}
string database_api_impl::nft_get_token_uri(const nft_id_type token_id) const
{
const auto &idx_nft = _db.get_index_type<nft_index>().indices().get<by_id>();
auto itr_nft = idx_nft.find(token_id);
if (itr_nft != idx_nft.end()) {
const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>();
auto itr_nft_md = idx_nft_md.find(itr_nft->nft_metadata_id);
if (itr_nft_md != idx_nft_md.end()) {
return itr_nft_md->base_uri + itr_nft->token_uri;
}
}
return "";
}
uint64_t database_api::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const
{
return my->nft_get_total_supply(nft_metadata_id);
}
uint64_t database_api_impl::nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const
{
const auto &idx_nft_md = _db.get_index_type<nft_metadata_index>().indices().get<by_id>();
return idx_nft_md.size();
}
nft_object database_api::nft_token_by_index(const nft_metadata_id_type nft_metadata_id, const uint64_t token_idx) const
{
return my->nft_token_by_index(nft_metadata_id, token_idx);
}
nft_object database_api_impl::nft_token_by_index(const nft_metadata_id_type nft_metadata_id, const uint64_t token_idx) const
{
const auto &idx_nft = _db.get_index_type<nft_index>().indices().get<by_metadata>();
auto idx_nft_range = idx_nft.equal_range(nft_metadata_id);
uint64_t tmp_idx = token_idx;
for (auto itr = idx_nft_range.first; itr != idx_nft_range.second; ++itr) {
if (tmp_idx == 0) {
return *itr;
}
tmp_idx = tmp_idx - 1;
}
return {};
}
nft_object database_api::nft_token_of_owner_by_index(const nft_metadata_id_type nft_metadata_id, const account_id_type owner, const uint64_t token_idx) const
{
return my->nft_token_of_owner_by_index(nft_metadata_id, owner, token_idx);
}
nft_object database_api_impl::nft_token_of_owner_by_index(const nft_metadata_id_type nft_metadata_id, const account_id_type owner, const uint64_t token_idx) const
{
const auto &idx_nft = _db.get_index_type<nft_index>().indices().get<by_metadata_and_owner>();
auto idx_nft_range = idx_nft.equal_range(std::make_tuple(nft_metadata_id, owner));
uint64_t tmp_idx = token_idx;
for (auto itr = idx_nft_range.first; itr != idx_nft_range.second; ++itr) {
if (tmp_idx == 0) {
return *itr;
}
tmp_idx = tmp_idx - 1;
}
return {};
}
//////////////////////////////////////////////////////////////////////
// //
// Private methods //

View file

@ -743,6 +743,50 @@ class database_api
*/
bool nft_is_approved_for_all(const account_id_type owner, const account_id_type operator_) const;
/**
* @brief Returns NFT name from NFT metadata
* @param nft_metadata_id NFT metadata ID
* @return NFT name
*/
string nft_get_name(const nft_metadata_id_type nft_metadata_id) const;
/**
* @brief Returns NFT symbol from NFT metadata
* @param nft_metadata_id NFT metadata ID
* @return NFT symbol
*/
string nft_get_symbol(const nft_metadata_id_type nft_metadata_id) const;
/**
* @brief Returns NFT URI
* @param token_id NFT ID
* @return NFT URI
*/
string nft_get_token_uri(const nft_id_type token_id) const;
/**
* @brief Returns total number of NFTs assigned to NFT metadata
* @param nft_metadata_id NFT metadata ID
* @return Total number of NFTs assigned to NFT metadata
*/
uint64_t nft_get_total_supply(const nft_metadata_id_type nft_metadata_id) const;
/**
* @brief Returns NFT by index from NFT metadata
* @param nft_metadata_id NFT metadata ID
* @param token_idx NFT index in the list of tokens
* @return NFT symbol
*/
nft_object nft_token_by_index(const nft_metadata_id_type nft_metadata_id, const uint64_t token_idx) const;
/**
* @brief Returns NFT URI
* @param nft_metadata_id NFT metadata ID
* @param owner NFT owner
* @param token_idx NFT index in the list of tokens
* @return NFT URI
*/
nft_object nft_token_of_owner_by_index(const nft_metadata_id_type nft_metadata_id, const account_id_type owner, const uint64_t token_idx) const;
private:
std::shared_ptr< database_api_impl > my;
@ -887,5 +931,11 @@ FC_API(graphene::app::database_api,
(nft_owner_of)
(nft_get_approved)
(nft_is_approved_for_all)
(nft_get_name)
(nft_get_symbol)
(nft_get_token_uri)
(nft_get_total_supply)
(nft_token_by_index)
(nft_token_of_owner_by_index)
)

View file

@ -50,6 +50,7 @@ namespace graphene { namespace chain {
using nft_metadata_index = generic_index<nft_metadata_object, nft_metadata_multi_index_type>;
struct by_metadata;
struct by_metadata_and_owner;
struct by_owner;
struct by_owner_and_id;
using nft_multi_index_type = multi_index_container<
@ -61,6 +62,12 @@ namespace graphene { namespace chain {
ordered_non_unique< tag<by_metadata>,
member<nft_object, nft_metadata_id_type, &nft_object::nft_metadata_id>
>,
ordered_non_unique< tag<by_metadata_and_owner>,
composite_key<nft_object,
member<nft_object, nft_metadata_id_type, &nft_object::nft_metadata_id>,
member<nft_object, account_id_type, &nft_object::owner>
>
>,
ordered_non_unique< tag<by_owner>,
member<nft_object, account_id_type, &nft_object::owner>
>,