Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Mar 8, 2023
1 parent f62afac commit c1284ea
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions crates/library/std/src/abi.fe
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// use ingot::evm
use std::evm

trait AbiDecode {
fn decode(self, location: DecodeLocation, offset: u256) -> DecodeOutput;
}

pub enum DecodeOutput {
MPtr(u256)
Value(u256)

pub fn unwrap(self) -> u256 {
match self {
// Self::Mptr(ptr) => { return ptr }
DecodeOutput::MPtr(ptr) => { return ptr }
DecodeOutput::Value(ptr) => { return ptr }
}
}
}

enum DecodeLocation {
Memory
Calldata

pub unsafe fn load(self, offset: u256) -> u256 {
match self {
DecodeLocation::Memory => {
return evm::call_data_load(offset)
}
DecodeLocation::Calldata => {
return evm::mload(offset)
}
}
}
}

pub enum AbiPrimitive {
U256
U128
}

pub struct AbiArray {
pub length: u256
pub inner: AbiPrimitive
}

impl AbiDecode for AbiPrimitive {
fn decode(self, location: DecodeLocation, offset: u256) -> DecodeOutput {
unsafe {
return DecodeOutput::Value(location.load(offset))
}
}
}

impl AbiDecode for AbiArray {
fn decode(self, location: DecodeLocation, offset: u256) -> DecodeOutput {
let mem_start: u256 = 42
let mut i: u256 = 0
while i < self.length {
let curr_offset: u256 = i * 256
let value: u256 = self.inner.decode(location, offset: offset + curr_offset).unwrap()
unsafe { evm::mstore(offset: mem_start + curr_offset, value) }
i += 1
}
return DecodeOutput::MPtr(mem_start)
}
}

pub enum AbiType {
Primitive(AbiPrimitive)
// Tuple(Array<AbiPrimitive, 32>)
Array(AbiArray)

pub fn decode(self, location: DecodeLocation, offset: u256) -> DecodeOutput {
match self {
AbiType::Primitive(primative) => { return primative.decode(location, offset) }
AbiType::Array(array) => { return array.decode(location, offset) }
}
}
}

#test
unsafe fn test_decode_u256() {
// evm::mstore()
}

0 comments on commit c1284ea

Please sign in to comment.