Skip to content

Instantly share code, notes, and snippets.

@gagdiez
Created September 19, 2021 19:05
Show Gist options
  • Select an option

  • Save gagdiez/65265fe56c013eae3f0dc5e44656406c to your computer and use it in GitHub Desktop.

Select an option

Save gagdiez/65265fe56c013eae3f0dc5e44656406c to your computer and use it in GitHub Desktop.
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::{U128};
use near_sdk::{env, log, PromiseResult, near_bindgen, ext_contract, Gas, PanicOnDefault, Promise};
near_sdk::setup_alloc!();
const POOL: &str = "test-account-1632065640639-6140484";
const TGAS: Gas = 1_000_000_000_000;
const NO_DEPOSIT: u128 = 0;
#[derive(BorshDeserialize, BorshSerialize, Deserialize, Serialize)]
#[serde(crate = "near_sdk::serde")]
pub struct PoolInfo {
pub staked_balance: U128,
pub unstaked_balance: U128,
pub available: bool
}
// Our contracts interface
#[ext_contract(this_contract)]
trait Callbacks {
fn get_pool_information_callback(&mut self);
}
// Pool interface
#[ext_contract(pool_contract)]
trait Pool {
fn get_account(&self, account_id: AccountId) -> PoolInfo;
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct Contract {}
#[near_bindgen]
impl Contract {
#[init]
pub fn new( ) -> Self {
assert!(!env::state_exists(), "Already initialized");
Self {}
}
pub fn get_pool_information(&mut self) -> Promise {
pool_contract::get_account(
env::current_account_id(),
&POOL,
NO_DEPOSIT,
5*TGAS
).then(this_contract::get_pool_information_callback(
&env::predecessor_account_id(),
NO_DEPOSIT,
5*TGAS,
))
}
#[private]
pub fn get_pool_information_callback(&mut self) -> bool {
if env::promise_results_count() != 1 {
log!("Expected a result on the callback");
return false;
}
// Get response, return false if failed
let pool_info: PoolInfo = match env::promise_result(0) {
PromiseResult::Successful(value) => near_sdk::serde_json::from_slice::<PoolInfo>(&value).unwrap(),
_ => { log!("Getting info from Pool Party failed"); return false; },
};
log!("{}", pool_info.available);
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment