|
| 1 | +use cairo_vm::{ |
| 2 | + types::relocatable::{MaybeRelocatable, Relocatable}, |
| 3 | + vm::vm_core::VirtualMachine, |
| 4 | + Felt252, |
| 5 | +}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use types::cairo::{ |
| 8 | + new_syscalls::{CallContractRequest, CallContractResponse}, |
| 9 | + traits::CairoType, |
| 10 | + FELT_10, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::{traits, SyscallResult, WriteResponseResult}; |
| 14 | + |
| 15 | +pub const CONTRACT_ADDRESS: Felt252 = Felt252::from_hex_unchecked("0x616e795f74797065"); // 'any_type' in hex |
| 16 | + |
| 17 | +#[derive(Debug, Default, Serialize, Deserialize, Clone)] |
| 18 | +pub struct AnyTypeCallContractHandler; |
| 19 | + |
| 20 | +impl traits::SyscallHandler for AnyTypeCallContractHandler { |
| 21 | + type Request = CallContractRequest; |
| 22 | + type Response = CallContractResponse; |
| 23 | + |
| 24 | + fn read_request(&mut self, vm: &VirtualMachine, ptr: &mut Relocatable) -> SyscallResult<Self::Request> { |
| 25 | + let ret = Self::Request::from_memory(vm, *ptr)?; |
| 26 | + *ptr = (*ptr + Self::Request::cairo_size())?; |
| 27 | + Ok(ret) |
| 28 | + } |
| 29 | + |
| 30 | + async fn execute(&mut self, request: Self::Request, vm: &mut VirtualMachine) -> SyscallResult<Self::Response> { |
| 31 | + let field_len = (request.calldata_end - request.calldata_start)?; |
| 32 | + let fields = vm |
| 33 | + .get_integer_range(request.calldata_start, field_len)? |
| 34 | + .into_iter() |
| 35 | + .map(|f| (*f.as_ref())) |
| 36 | + .collect::<Vec<Felt252>>(); |
| 37 | + |
| 38 | + // we need here macro to deserialize the cairo1 way the CairoType derived structure |
| 39 | + let input = AnyTypeInput { |
| 40 | + item_a: fields[0], |
| 41 | + item_b: fields[1], |
| 42 | + item_c: fields[2], |
| 43 | + item_d: fields[3], |
| 44 | + }; |
| 45 | + |
| 46 | + // we need here macro to serialize the cairo1 way the CairoType derived structure |
| 47 | + let output = AnyTypeOutput { |
| 48 | + item_a: input.item_a, |
| 49 | + item_b: input.item_b, |
| 50 | + item_c: input.item_c, |
| 51 | + item_d: input.item_d, |
| 52 | + item_e: FELT_10, |
| 53 | + item_f: FELT_10, |
| 54 | + }; |
| 55 | + |
| 56 | + let retdata_start = vm.add_memory_segment(); |
| 57 | + let retdata_end = vm.load_data( |
| 58 | + retdata_start, |
| 59 | + &[ |
| 60 | + output.item_a, |
| 61 | + output.item_b, |
| 62 | + output.item_c, |
| 63 | + output.item_d, |
| 64 | + output.item_e, |
| 65 | + output.item_f, |
| 66 | + ] |
| 67 | + .map(MaybeRelocatable::from), |
| 68 | + )?; |
| 69 | + |
| 70 | + Ok(Self::Response { |
| 71 | + retdata_start, |
| 72 | + retdata_end, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + fn write_response(&mut self, response: Self::Response, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult { |
| 77 | + response.to_memory(vm, *ptr)?; |
| 78 | + *ptr = (*ptr + Self::Response::cairo_size())?; |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +struct AnyTypeInput { |
| 84 | + pub item_a: Felt252, |
| 85 | + pub item_b: Felt252, |
| 86 | + pub item_c: Felt252, |
| 87 | + pub item_d: Felt252, |
| 88 | +} |
| 89 | + |
| 90 | +struct AnyTypeOutput { |
| 91 | + pub item_a: Felt252, |
| 92 | + pub item_b: Felt252, |
| 93 | + pub item_c: Felt252, |
| 94 | + pub item_d: Felt252, |
| 95 | + pub item_e: Felt252, |
| 96 | + pub item_f: Felt252, |
| 97 | +} |
0 commit comments