Created
January 18, 2022 14:20
-
-
Save danielwpz/75cfcc54d7f6cce54237e03c850220c3 to your computer and use it in GitHub Desktop.
nft owner
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
use near_contract_standards::non_fungible_token::{Token, TokenId}; | |
use near_sdk::borsh::{self, BorshSerialize, BorshDeserialize}; | |
use near_sdk::{ | |
env, near_bindgen, require, AccountId, PanicOnDefault, Promise, ext_contract, | |
Gas, promise_result_as_success | |
}; | |
#[near_bindgen] | |
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)] | |
pub struct Foo { | |
} | |
#[ext_contract(ext_contract)] | |
trait ExtContract { | |
fn nft_token( | |
&mut self, | |
token_id: TokenId, | |
); | |
} | |
#[ext_contract(ext_self)] | |
trait ExtSelf { | |
fn on_nft_token_callback(&mut self) -> String; | |
} | |
#[near_bindgen] | |
impl Foo { | |
#[init] | |
pub fn new() -> Self { | |
require!(!env::state_exists(), "Already initialized"); | |
Self { | |
} | |
} | |
#[payable] | |
pub fn show( | |
&mut self, | |
token_address: AccountId, | |
token_id: TokenId, | |
) -> Promise { | |
ext_contract::nft_token( | |
token_id, | |
token_address, | |
0, | |
Gas(5_000_000_000_000) | |
) | |
.then(ext_self::on_nft_token_callback( | |
env::current_account_id(), | |
0, | |
Gas(5_000_000_000_000) | |
)) | |
} | |
#[private] | |
pub fn on_nft_token_callback( | |
&mut self | |
) -> String { | |
let result = promise_result_as_success().and_then(|value| { | |
let parsed_result = near_sdk::serde_json::from_slice::<Token>(&value); | |
if parsed_result.is_ok() { | |
parsed_result.ok() | |
.and_then(|token| { | |
Some(token.owner_id) | |
}) | |
} else { | |
None | |
} | |
}); | |
result.unwrap().to_string() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment