Skip to content

Instantly share code, notes, and snippets.

@mfornet
Created April 29, 2020 23:20
Show Gist options
  • Save mfornet/96cd389749662402b28846e1406ef5aa to your computer and use it in GitHub Desktop.
Save mfornet/96cd389749662402b28846e1406ef5aa to your computer and use it in GitHub Desktop.
Forking Contract
use borsh::{BorshDeserialize, BorshSerialize};
use near_bindgen::{env, ext_contract, near_bindgen};
use serde::{Deserialize, Serialize};
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[ext_contract(ext)]
pub trait ExtCrossContract {
fn fork(&mut self);
}
#[near_bindgen]
#[derive(BorshSerialize, BorshDeserialize, Default)]
pub struct CrossContract {
prepaid_gas: u64,
used_gas: u64,
calls: u64,
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
pub struct GasOutput {
prepaid_gas: u64,
used_gas: u64,
}
#[near_bindgen]
impl CrossContract {
pub fn fork(&mut self) {
let prepaid_gas = env::prepaid_gas();
self.calls += 1;
env::log(
format!(
"Call fork with gas: {:?} Calls: {:?}",
prepaid_gas, self.calls
)
.as_bytes(),
);
ext::fork(&env::current_account_id(), 0, prepaid_gas / 3);
ext::fork(&env::current_account_id(), 0, prepaid_gas / 3);
}
pub fn save_data(&mut self) {
self.prepaid_gas = env::prepaid_gas();
self.used_gas = env::used_gas();
}
pub fn total_gas(&self) -> GasOutput {
GasOutput {
prepaid_gas: self.prepaid_gas,
used_gas: self.used_gas,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment