diff --git a/crates/library/std/src/abi.fe b/crates/library/std/src/abi.fe new file mode 100644 index 0000000000..3358da939b --- /dev/null +++ b/crates/library/std/src/abi.fe @@ -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) + 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() +} \ No newline at end of file