-
Notifications
You must be signed in to change notification settings - Fork 28
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 owner to API Server response #1858
Open
OBorce
wants to merge
2
commits into
master
Choose a base branch
from
feature/api-server-nft-owner
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+289
−74
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ use crate::storage::{ | |
storage_api::{ | ||
block_aux_data::{BlockAuxData, BlockWithExtraData}, | ||
ApiServerStorageError, BlockInfo, CoinOrTokenStatistic, Delegation, FungibleTokenData, | ||
LockedUtxo, Order, PoolBlockStats, TransactionInfo, Utxo, UtxoWithExtraInfo, | ||
LockedUtxo, NftWithOwner, Order, PoolBlockStats, TransactionInfo, Utxo, UtxoWithExtraInfo, | ||
}, | ||
}; | ||
|
||
|
@@ -240,7 +240,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
|
||
pub async fn set_address_balance_at_height( | ||
&mut self, | ||
address: &str, | ||
address: &Address<Destination>, | ||
amount: Amount, | ||
coin_or_token_id: CoinOrTokenId, | ||
block_height: BlockHeight, | ||
|
@@ -260,12 +260,62 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
.await | ||
.map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||
|
||
let CoinOrTokenId::TokenId(token_id) = coin_or_token_id else { | ||
return Ok(()); | ||
}; | ||
|
||
self.update_nft_owner(token_id, height, (amount > Amount::ZERO).then_some(address)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn update_nft_owner( | ||
&mut self, | ||
token_id: TokenId, | ||
height: i64, | ||
owner: Option<&Address<Destination>>, | ||
) -> Result<(), ApiServerStorageError> { | ||
self.tx | ||
.execute( | ||
Comment on lines
+267
to
+280
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments as for the other |
||
r#" | ||
WITH LastRow AS ( | ||
SELECT | ||
nft_id, | ||
block_height, | ||
ticker, | ||
issuance | ||
FROM | ||
ml.nft_issuance | ||
WHERE | ||
nft_id = $1 | ||
ORDER BY | ||
block_height DESC | ||
LIMIT 1 | ||
) | ||
INSERT INTO ml.nft_issuance (nft_id, block_height, ticker, issuance, owner) | ||
SELECT | ||
lr.nft_id, | ||
$2, | ||
lr.ticker, | ||
lr.issuance, | ||
$3 | ||
FROM | ||
LastRow lr | ||
ON CONFLICT (nft_id, block_height) DO UPDATE | ||
SET | ||
ticker = EXCLUDED.ticker, | ||
owner = EXCLUDED.owner;"#, | ||
&[&token_id.encode(), &height, &owner.map(|o| o.as_object().encode())], | ||
) | ||
.await | ||
.map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||
Ok(()) | ||
} | ||
|
||
pub async fn set_address_locked_balance_at_height( | ||
&mut self, | ||
address: &str, | ||
address: &Address<Destination>, | ||
amount: Amount, | ||
coin_or_token_id: CoinOrTokenId, | ||
block_height: BlockHeight, | ||
|
@@ -285,6 +335,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
.await | ||
.map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||
|
||
let CoinOrTokenId::TokenId(token_id) = coin_or_token_id else { | ||
return Ok(()); | ||
}; | ||
|
||
self.update_nft_owner(token_id, height, (amount > Amount::ZERO).then_some(address)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
|
@@ -631,7 +688,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
block_height bigint NOT NULL, | ||
ticker bytea NOT NULL, | ||
issuance bytea NOT NULL, | ||
PRIMARY KEY (nft_id) | ||
owner bytea, | ||
PRIMARY KEY (nft_id, block_height) | ||
);", | ||
) | ||
.await?; | ||
|
@@ -2030,11 +2088,11 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
pub async fn get_nft_token_issuance( | ||
&self, | ||
token_id: TokenId, | ||
) -> Result<Option<NftIssuance>, ApiServerStorageError> { | ||
) -> Result<Option<NftWithOwner>, ApiServerStorageError> { | ||
let row = self | ||
.tx | ||
.query_opt( | ||
"SELECT issuance FROM ml.nft_issuance WHERE nft_id = $1 | ||
"SELECT issuance, owner FROM ml.nft_issuance WHERE nft_id = $1 | ||
ORDER BY block_height DESC | ||
LIMIT 1;", | ||
&[&token_id.encode()], | ||
|
@@ -2048,22 +2106,34 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
}; | ||
|
||
let serialized_data: Vec<u8> = row.get(0); | ||
let owner: Option<Vec<u8>> = row.get(1); | ||
|
||
let issuance = NftIssuance::decode_all(&mut serialized_data.as_slice()).map_err(|e| { | ||
let nft = NftIssuance::decode_all(&mut serialized_data.as_slice()).map_err(|e| { | ||
ApiServerStorageError::DeserializationError(format!( | ||
"Nft issuance data for nft id {} deserialization failed: {}", | ||
token_id, e | ||
)) | ||
})?; | ||
|
||
Ok(Some(issuance)) | ||
let owner = owner | ||
.map(|owner| { | ||
Destination::decode_all(&mut owner.as_slice()).map_err(|e| { | ||
ApiServerStorageError::DeserializationError(format!( | ||
"Deserialization failed for nft owner {token_id}: {e}" | ||
)) | ||
}) | ||
}) | ||
.transpose()?; | ||
|
||
Ok(Some(NftWithOwner { nft, owner })) | ||
} | ||
|
||
pub async fn set_nft_token_issuance( | ||
&mut self, | ||
token_id: TokenId, | ||
block_height: BlockHeight, | ||
issuance: NftIssuance, | ||
owner: &Destination, | ||
) -> Result<(), ApiServerStorageError> { | ||
let height = Self::block_height_to_postgres_friendly(block_height); | ||
|
||
|
@@ -2073,8 +2143,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> { | |
|
||
self.tx | ||
.execute( | ||
"INSERT INTO ml.nft_issuance (nft_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4);", | ||
&[&token_id.encode(), &height, &issuance.encode(), ticker], | ||
"INSERT INTO ml.nft_issuance (nft_id, block_height, issuance, ticker, owner) VALUES ($1, $2, $3, $4, $5);", | ||
&[&token_id.encode(), &height, &issuance.encode(), ticker, &owner.encode()], | ||
) | ||
.await | ||
.map_err(|e| ApiServerStorageError::LowLevelStorageError(e.to_string()))?; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update_tables_from_transaction_inputs
is called beforeupdate_tables_from_transaction_outputs
(as I can see), IMO it's not good for this function to depend on this.update_tables_from_transaction_outputs
, in theTxOutput::IssueNft
case,set_nft_token_issuance
is called afterincrease_address_amount
. How is it supposed to work?