Skip to content

Instantly share code, notes, and snippets.

@encody
Created February 10, 2024 05:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save encody/367720dfe372f1da46bd07b81e3b6b55 to your computer and use it in GitHub Desktop.
Save encody/367720dfe372f1da46bd07b81e3b6b55 to your computer and use it in GitHub Desktop.
NEAR Spinner
use near_sdk::{
borsh::{self, BorshDeserialize, BorshSerialize},
env,
json_types::U64,
log, near_bindgen, Promise,
};
#[derive(BorshSerialize, BorshDeserialize, Default, Debug)]
#[near_bindgen]
struct Contract {}
#[near_bindgen]
impl Contract {
pub fn with_transfers(&mut self, count: u32) -> Promise {
let mut p = Promise::new(env::current_account_id()).transfer(1);
for _ in 0..count {
p = p.then(Promise::new(env::current_account_id()).transfer(1));
}
p.then(Self::ext(env::current_account_id()).end(env::block_height().into()))
}
pub fn with_callbacks(&mut self, count: u32, start_block_height: Option<U64>) -> Promise {
let start_block_height = start_block_height.unwrap_or_else(|| env::block_height().into());
if count == 0 {
Self::ext(env::current_account_id()).end(start_block_height)
} else {
Self::ext(env::current_account_id()).with_callbacks(count - 1, Some(start_block_height))
}
}
pub fn end(&mut self, start_block_height: U64) -> U64 {
let block_height = env::block_height();
let delta = block_height - start_block_height.0;
log!("Block height delta: {}", delta);
log!("Used gas: {}", env::used_gas().0);
log!("Prepaid gas: {}", env::prepaid_gas().0);
delta.into()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment