|
| 1 | +extern crate alloc; |
| 2 | +use alloy_core::primitives::{Address, Bytes, U32}; |
| 3 | +use alloy_sol_types::{SolType, SolValue}; |
| 4 | +use ext_alloc::vec::Vec; |
| 5 | +use core::{arch::asm, marker::PhantomData, u64}; |
| 6 | +use eth_riscv_syscalls::Syscall; |
| 7 | + |
| 8 | +use crate::{FromBuilder, InitInterface, MethodCtx, ReadWrite}; |
| 9 | + |
| 10 | +pub trait Deployable { |
| 11 | + type Interface: InitInterface; |
| 12 | + |
| 13 | + /// Returns the contract's runtime bytecode |
| 14 | + fn __runtime() -> &'static [u8]; |
| 15 | + |
| 16 | + /// Returns the contract's runtime bytecode |
| 17 | + fn bytecode() -> Bytes { |
| 18 | + Bytes::from(Self::__runtime()) |
| 19 | + } |
| 20 | + |
| 21 | + // Creates a deployment builder that captures the constructor args |
| 22 | + fn deploy<Args>(args: Args) -> DeploymentBuilder<Self, Args> |
| 23 | + where |
| 24 | + Self: Sized, |
| 25 | + Args: SolValue + core::convert::From<<<Args as SolValue>::SolType as SolType>::RustType> |
| 26 | + { |
| 27 | + DeploymentBuilder { |
| 28 | + args, |
| 29 | + _phantom: PhantomData, |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +pub struct DeploymentBuilder<D: Deployable + ?Sized, Args> |
| 35 | +where |
| 36 | + Args: SolValue + core::convert::From<<<Args as SolValue>::SolType as SolType>::RustType> |
| 37 | +{ |
| 38 | + args: Args, |
| 39 | + _phantom: PhantomData<D>, |
| 40 | +} |
| 41 | + |
| 42 | +impl<D: Deployable, Args> DeploymentBuilder<D, Args> |
| 43 | +where |
| 44 | + Args: SolValue + core::convert::From<<<Args as SolValue>::SolType as SolType>::RustType> |
| 45 | +{ |
| 46 | + |
| 47 | + // Return the interface with the appropriate context |
| 48 | + pub fn with_ctx<M, T>(self, ctx: M) -> T |
| 49 | + where |
| 50 | + M: MethodCtx<Allowed = ReadWrite>, // Constrain to mutable contexts only |
| 51 | + D::Interface: InitInterface, |
| 52 | + T: FromBuilder<Context = M::Allowed>, |
| 53 | + D::Interface: crate::IntoInterface<T> |
| 54 | + { |
| 55 | + let bytecode = D::__runtime(); |
| 56 | + let encoded_args = self.args.abi_encode(); |
| 57 | + |
| 58 | + // Craft R55 initcode: [0xFF][codesize][bytecode][constructor_args] |
| 59 | + let codesize = U32::from(bytecode.len()); |
| 60 | + |
| 61 | + let mut init_code = Vec::new(); |
| 62 | + init_code.push(0xff); |
| 63 | + init_code.extend_from_slice(&Bytes::from(codesize.to_be_bytes_vec())); |
| 64 | + init_code.extend_from_slice(&bytecode); |
| 65 | + init_code.extend_from_slice(&encoded_args); |
| 66 | + |
| 67 | + let offset = init_code.as_ptr() as u64; |
| 68 | + let size = init_code.len() as u64; |
| 69 | + |
| 70 | + // TODO: think of an ergonomic API to handle deployments with values |
| 71 | + create(0, offset, size); |
| 72 | + |
| 73 | + // Get deployment address |
| 74 | + let mut ret_data = Vec::with_capacity(20); |
| 75 | + ret_data.resize(20 as usize, 0); |
| 76 | + return_create_address(ret_data.as_ptr() as u64); |
| 77 | + |
| 78 | + let address = Address::from_slice(&ret_data); |
| 79 | + |
| 80 | + // Create the interface builder |
| 81 | + let builder = D::Interface::new(address); |
| 82 | + |
| 83 | + // Convert to the actual interface with context |
| 84 | + builder.with_ctx(ctx) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +fn create(value: u64, data_offset: u64, data_size: u64) { |
| 89 | + unsafe { |
| 90 | + asm!( |
| 91 | + "ecall", |
| 92 | + in("a0") value, in("a1") data_offset, in("a2") data_size, |
| 93 | + in("t0") u8::from(Syscall::Create) |
| 94 | + ); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn return_create_address(data_offset: u64) { |
| 99 | + unsafe { |
| 100 | + asm!( |
| 101 | + "ecall", in("a0") data_offset, in("t0") u8::from(Syscall::ReturnCreateAddress)); |
| 102 | + } |
| 103 | +} |
0 commit comments