1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use better_any::{Tid, TidAble};
use move_deps::{
move_binary_format::errors::PartialVMResult,
move_vm_runtime::native_functions::{NativeContext, NativeFunction},
move_vm_types::{
loaded_data::runtime_types::Type, natives::function::NativeResult, values::Value,
},
};
use smallvec::smallvec;
use std::collections::VecDeque;
use std::sync::Arc;
#[derive(Tid)]
pub struct NativeTransactionContext {
script_hash: Vec<u8>,
}
impl NativeTransactionContext {
pub fn new(script_hash: Vec<u8>) -> Self {
Self { script_hash }
}
}
#[derive(Clone, Debug)]
pub struct GetScriptHashGasParameters {
pub base_cost: u64,
}
fn native_get_script_hash(
gas_params: &GetScriptHashGasParameters,
context: &mut NativeContext,
mut _ty_args: Vec<Type>,
_args: VecDeque<Value>,
) -> PartialVMResult<NativeResult> {
let transaction_context = context.extensions().get::<NativeTransactionContext>();
Ok(NativeResult::ok(
gas_params.base_cost,
smallvec![Value::vector_u8(transaction_context.script_hash.clone())],
))
}
pub fn make_native_get_script_hash(gas_params: GetScriptHashGasParameters) -> NativeFunction {
Arc::new(move |context, ty_args, args| {
native_get_script_hash(&gas_params, context, ty_args, args)
})
}
#[derive(Debug, Clone)]
pub struct GasParameters {
pub get_script_hash: GetScriptHashGasParameters,
}
pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, NativeFunction)> {
let natives = [(
"get_script_hash",
make_native_get_script_hash(gas_params.get_script_hash),
)];
crate::natives::helpers::make_module_natives(natives)
}