Trait near_vm_logic::External[][src]

pub trait External {
Show 18 methods fn storage_set(
        &mut self,
        key: &[u8],
        value: &[u8]
    ) -> Result<(), VMLogicError>;
fn storage_get<'a>(
        &'a self,
        key: &[u8]
    ) -> Result<Option<Box<dyn ValuePtr + 'a>>, VMLogicError>;
fn storage_remove(&mut self, key: &[u8]) -> Result<(), VMLogicError>;
fn storage_remove_subtree(
        &mut self,
        prefix: &[u8]
    ) -> Result<(), VMLogicError>;
fn storage_has_key(&mut self, key: &[u8]) -> Result<bool, VMLogicError>;
fn create_receipt(
        &mut self,
        receipt_indices: Vec<ReceiptIndex>,
        receiver_id: AccountId
    ) -> Result<ReceiptIndex, VMLogicError>;
fn append_action_create_account(
        &mut self,
        receipt_index: ReceiptIndex
    ) -> Result<(), VMLogicError>;
fn append_action_deploy_contract(
        &mut self,
        receipt_index: ReceiptIndex,
        code: Vec<u8>
    ) -> Result<(), VMLogicError>;
fn append_action_function_call(
        &mut self,
        receipt_index: ReceiptIndex,
        method_name: Vec<u8>,
        arguments: Vec<u8>,
        attached_deposit: Balance,
        prepaid_gas: Gas
    ) -> Result<(), VMLogicError>;
fn append_action_transfer(
        &mut self,
        receipt_index: ReceiptIndex,
        amount: Balance
    ) -> Result<(), VMLogicError>;
fn append_action_stake(
        &mut self,
        receipt_index: ReceiptIndex,
        stake: Balance,
        public_key: PublicKey
    ) -> Result<(), VMLogicError>;
fn append_action_add_key_with_full_access(
        &mut self,
        receipt_index: ReceiptIndex,
        public_key: PublicKey,
        nonce: u64
    ) -> Result<(), VMLogicError>;
fn append_action_add_key_with_function_call(
        &mut self,
        receipt_index: ReceiptIndex,
        public_key: PublicKey,
        nonce: u64,
        allowance: Option<Balance>,
        receiver_id: AccountId,
        method_names: Vec<Vec<u8>>
    ) -> Result<(), VMLogicError>;
fn append_action_delete_key(
        &mut self,
        receipt_index: ReceiptIndex,
        public_key: PublicKey
    ) -> Result<(), VMLogicError>;
fn append_action_delete_account(
        &mut self,
        receipt_index: ReceiptIndex,
        beneficiary_id: AccountId
    ) -> Result<(), VMLogicError>;
fn get_touched_nodes_count(&self) -> u64;
fn validator_stake(
        &self,
        account_id: &AccountId
    ) -> Result<Option<Balance>, VMLogicError>;
fn validator_total_stake(&self) -> Result<Balance, VMLogicError>;
}
Expand description

An external blockchain interface for the Runtime logic

Required methods

Write value to the key of the storage trie associated with the current account.

Example

assert_eq!(external.storage_set(b"key42", b"value1337"), Ok(()));
// Should return an old value if the key exists
assert_eq!(external.storage_set(b"key42", b"new_value"), Ok(()));

Read key from the storage trie associated with the current account.

Arguments
  • key - the key to read
Errors

This function could return [VMError::ExternalError].

Example

external.storage_set(b"key42", b"value1337").unwrap();
assert_eq!(external.storage_get(b"key42").unwrap().map(|ptr| ptr.deref().unwrap()), Some(b"value1337".to_vec()));
// Returns Ok(None) if there is no value for a key
assert_eq!(external.storage_get(b"no_key").unwrap().map(|ptr| ptr.deref().unwrap()), None);

Removes the key from the storage trie associated with the current account.

The operation will succeed even if the key does not exist.

Arguments
  • key - the key to remove
Example

external.storage_set(b"key42", b"value1337").unwrap();
// Returns Ok if exists
assert_eq!(external.storage_remove(b"key42"), Ok(()));
// Returns Ok if there was no value
assert_eq!(external.storage_remove(b"no_value_key"), Ok(()));

Removes all keys with a given prefix from the storage trie associated with current account.

Arguments
  • prefix - a prefix for all keys to remove
Errors

This function could return [VMError].

Example

external.storage_set(b"key1", b"value1337").unwrap();
external.storage_set(b"key2", b"value1337").unwrap();
assert_eq!(external.storage_remove_subtree(b"key"), Ok(()));
assert!(!external.storage_has_key(b"key1").unwrap());
assert!(!external.storage_has_key(b"key2").unwrap());

Check whether the key is present in the storage trie associated with the current account.

Returns Ok(true) if key is present, Ok(false) if the key is not present.

Arguments
  • key - a key to check
Errors

This function could return [VMError::RuntimeError].

Example

external.storage_set(b"key42", b"value1337").unwrap();
// Returns value if exists
assert_eq!(external.storage_has_key(b"key42"), Ok(true));
// Returns None if there was no value
assert_eq!(external.storage_has_key(b"no_value_key"), Ok(false));

Create a receipt which will be executed after all the receipts identified by receipt_indices are complete.

If any of the [RecepitIndex]es do not refer to a known receipt, this function will fail with an error.

Arguments
  • receipt_indices - a list of receipt indices the new receipt is depend on
Example

let receipt_index_one = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
let receipt_index_two = external.create_receipt(vec![receipt_index_one], "bob.near".parse().unwrap());

Attach the [CreateAccountAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_create_account(receipt_index).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [DeployContractAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
  • code - a Wasm code to attach
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_deploy_contract(receipt_index, b"some valid Wasm code".to_vec()).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [FunctionCallAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
  • method_name - a name of the contract method to call
  • arguments - a Wasm code to attach
  • attached_deposit - amount of tokens to transfer with the call
  • prepaid_gas - amount of prepaid gas to attach to the call
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_function_call(
    receipt_index,
    b"method_name".to_vec(),
    b"{serialised: arguments}".to_vec(),
    100000u128,
    100u64
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [TransferAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
  • amount - amount of tokens to transfer
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_transfer(
    receipt_index,
    100000u128,
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [StakeAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
  • stake - amount of tokens to stake
  • public_key - a validator public key
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_stake(
    receipt_index,
    100000u128,
    b"some public key".to_vec()
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [AddKeyAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
  • public_key - a public key for an access key
  • nonce - a nonce
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_add_key_with_full_access(
    receipt_index,
    b"some public key".to_vec(),
    0u64
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [AddKeyAction] action an existing receipt.

The access key associated with the action will have the [AccessKeyPermission::FunctionCall] permission scope.

Arguments
  • receipt_index - an index of Receipt to append an action
  • public_key - a public key for an access key
  • nonce - a nonce
  • allowance - amount of tokens allowed to spend by this access key
  • receiver_id - a contract witch will be allowed to call with this access key
  • method_names - a list of method names is allowed to call with this access key (empty = any method)
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_add_key_with_function_call(
    receipt_index,
    b"some public key".to_vec(),
    0u64,
    None,
    "bob.near".parse().unwrap(),
    vec![b"foo".to_vec(), b"bar".to_vec()]
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [DeleteKeyAction] action to an existing receipt.

Arguments
  • receipt_index - an index of Receipt to append an action
  • public_key - a public key for an access key to delete
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_delete_key(
    receipt_index,
    b"some public key".to_vec()
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Attach the [DeleteAccountAction] action to an existing receipt

Arguments
  • receipt_index - an index of Receipt to append an action
  • beneficiary_id - an account id to which the rest of the funds of the removed account will be transferred
Example

let receipt_index = external.create_receipt(vec![], "charli.near".parse().unwrap()).unwrap();
external.append_action_delete_account(
    receipt_index,
    "sam".parse().unwrap()
).unwrap();
Panics

Panics if the receipt_index does not refer to a known receipt.

Returns amount of touched trie nodes by storage operations

Returns the validator stake for given account in the current epoch. If the account is not a validator, returns None.

Returns total stake of validators in the current epoch.

Implementors