Skip to content

Commit

Permalink
hacking
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Jun 27, 2023
1 parent bab11bb commit 90dad9e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
6 changes: 3 additions & 3 deletions crates/library/std/src/abi.fe
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ingot::evm
use ingot::context::CalldataReader
use ingot::traits::Max
use ingot::buf::{MemoryBuffer, CalldataReader, MemoryBufferReader, MemoryBufferWriter}
use ingot::buf::{MemoryBuffer, MemoryBufferReader, MemoryBufferWriter}

pub trait AbiDecode {
fn decode(mut reader: CalldataReader) -> Self;
Expand All @@ -19,7 +19,7 @@ impl AbiDecode for u256 {

impl AbiEncode for u256 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write_u256(value: self)
return writer.write(value: self)
}
}

Expand All @@ -36,7 +36,7 @@ impl AbiDecode for u128 {

impl AbiEncode for u128 {
fn encode(self, mut writer: MemoryBufferWriter) {
return writer.write_u256(value: u256(self))
return writer.write(value: u256(self))
}
}

Expand Down
18 changes: 18 additions & 0 deletions crates/library/std/src/context.fe
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ pub struct CalldataReader {
return value
}
}

pub fn read_u16(mut self) -> u16 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.cur_offset += 2
assert self.cur_offset <= self.len
return u16(evm::shr(bits: 240, value))
}
}

pub fn read_u32(mut self) -> u32 {
unsafe {
let value: u256 = evm::call_data_load(offset: self.cur_offset)
self.cur_offset += 4
assert self.cur_offset <= self.len
return u32(evm::shr(bits: 224, value))
}
}
}

pub struct Context {
Expand Down
22 changes: 11 additions & 11 deletions crates/tests/fixtures/files/abi.fe
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// error: cannot glob import from ingot
// use std::{abi::Abi, evm}
use std::abi::{AbiDecode, AbiEncode}
use std::buf::{MemoryBuffer, MemoryBufferWriter}
use std::buf::{MemoryBuffer, MemoryBufferWriter, MemoryBufferReader}
use std::evm

#test
unsafe fn u256_decode() {
let mut buf: MemoryBuffer = MemoryBuffer::new(len: 64)
// #test
// unsafe fn u256_decode() {
// let mut buf: MemoryBuffer = MemoryBuffer::new(len: 64)

let mut writer: MemoryBufferWriter = buf.writer()
writer.write(26)
writer.write(42)
// let mut writer: MemoryBufferWriter = buf.writer()
// writer.write(value: 26)
// writer.write(value: 42)

let mut reader: MemoryBufferReader = buf.reader()
assert u256::decode(reader) == 26
assert u256::decode(reader) == 42
}
// let mut reader: MemoryBufferReader = buf.reader()
// assert u256::decode(reader) == 26
// assert u256::decode(reader) == 42
// }

#test
unsafe fn u256_encode() {
Expand Down
26 changes: 13 additions & 13 deletions crates/tests/fixtures/files/contract_abi.fe
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::buf::{CalldataBuffer, MemoryBuffer}
use std::abi::{Abi, AbiCalldata}
use std::buf::{MemoryBuffer, MemoryBufferReader, MemoryBufferWriter}
use std::abi::{AbiEncode, AbiDecode}
use std::context::CalldataReader
use std::evm

struct UnknownSelector {
Expand All @@ -20,20 +21,19 @@ contract MyContract {
foo: u256

pub unsafe fn __call__(mut self, ctx: Context) {
let selector: u256 = ctx.msg_sig()
let mut reader: CalldataReader = ctx.calldata_reader()

let selector: u32 = reader.read_u32()

if selector == 0xe5d5dfbc {
// set_foo
let mut in_buf: CalldataBuffer = CalldataBuffer::new(start: 4, end: 36)
self.foo = u256::decode_calldata(buf: in_buf)
self.foo = u256::decode(reader)
} else if selector == 0x62e2d0de {
// get_foo
let mut out_buf: MemoryBuffer = MemoryBuffer::new(start: 0, end: 32)
// doesnt work
// self.foo.encode(buf: out_buf)
let mut buf: MemoryBuffer = MemoryBuffer::new(len: 32)
let mut writer: MemoryBufferWriter = buf.writer()
// self.foo.encode(writer)
let value: u256 = self.foo
value.encode(buf: out_buf)
evm::return_mem(offset: 0, len: 32)
value.encode(writer)
evm::return_mem(offset: buf.offset(), len: buf.len())
} else {
revert UnknownSelector(selector)
}
Expand All @@ -46,5 +46,5 @@ unsafe fn contract_call() {
let mut my_contract: MyContractInterface = MyContractInterface(address(MyContract.create(ctx, 0)))
assert my_contract.get_foo() == 0
my_contract.set_foo(foo: 42)
assert my_contract.get_foo() == 42
assert my_contract.get_foo() == 42
}

0 comments on commit 90dad9e

Please sign in to comment.