Exemple #1
0
def wasm_contract():
    return utils.compile_rust_contract('''
const N: u32 = 100;

metadata! {
    #[near_bindgen]
    #[derive(Default, BorshSerialize, BorshDeserialize)]
    pub struct LoadContract {}
}

#[near_bindgen]
impl LoadContract {
    pub fn do_work(&self) {
        // Do some pointless work.
        // In this case we bubble sort a reversed list.
        // Thus, this is O(N) in space and O(N^2) in time.
        let xs: Vec<u32> = (0..N).rev().collect();
        let _ = Self::bubble_sort(xs);
        env::log(b"Done.");
    }

    fn bubble_sort(mut xs: Vec<u32>) -> Vec<u32> {
        let n = xs.len();
        for i in 0..n {
            for j in 1..(n - i) {
                if xs[j - 1] > xs[j] {
                    let tmp = xs[j - 1];
                    xs[j - 1] = xs[j];
                    xs[j] = tmp;
                }
            }
        }
        xs
    }
}''')
status2 = nodes[1].get_status()
hash_2 = status2['sync_info']['latest_block_hash']
hash_2 = base58.b58decode(hash_2.encode('utf8'))
tx2 = sign_function_call_tx(nodes[0].signer_key,
                            nodes[0].signer_key.account_id, 'log_something',
                            [], 100000000000, 100000000000, 20, hash_2)
res = nodes[1].send_tx_and_wait(tx2, 20)
assert res['result']['receipts_outcome'][0]['outcome']['logs'][0] == 'hello'

wasm_file = compile_rust_contract('''
metadata! {
  #[near_bindgen]
  #[derive(Default, BorshDeserialize, BorshSerialize)]
  pub struct StatusMessage {}
}

#[near_bindgen]
impl StatusMessage {
  pub fn log_world(&self) {
    env::log(b"world");
  }
}
''')

status3 = nodes[2].get_status()
hash_3 = status3['sync_info']['latest_block_hash']
hash_3 = base58.b58decode(hash_3.encode('utf8'))
tx3 = sign_deploy_contract_tx(nodes[2].signer_key, load_binary_file(wasm_file),
                              10, hash_3)
res = nodes[3].send_tx(tx3)
time.sleep(3)