From 2a77608cbed95e7d5efb53bf3608e70d5a7a8e4b Mon Sep 17 00:00:00 2001
From: Grant  Wuerker <grantwuerker@pop-os.localdomain>
Date: Sat, 4 Mar 2023 13:55:57 -0700
Subject: [PATCH] hacking

---
 crates/library/std/src/abi.fe | 85 +++++++++++++++++++++++++++++++++++
 1 file changed, 85 insertions(+)
 create mode 100644 crates/library/std/src/abi.fe

diff --git a/crates/library/std/src/abi.fe b/crates/library/std/src/abi.fe
new file mode 100644
index 0000000000..af4805bca6
--- /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<AbiPrimitive, 32>)
+    Array(AbiArray)
+
+    pub fn decode(self, location: DecodeLocation, offset: u256) -> DecodeOutput {
+        match self {
+            AbiType::Primitive(primitive) => { return primitive.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