-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Grant Wuerker
committed
Mar 8, 2023
1 parent
f62afac
commit 2a77608
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(primitive) => { return primitive.decode(location, offset) } | ||
AbiType::Array(array) => { return array.decode(location, offset) } | ||
} | ||
} | ||
} | ||
|
||
#test | ||
unsafe fn test_decode_u256() { | ||
// evm::mstore() | ||
} |