Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Created February 26, 2025 14:35
Show Gist options
  • Save Turupawn/cfe96242737d687347a0373b3d1ae683 to your computer and use it in GitHub Desktop.
Save Turupawn/cfe96242737d687347a0373b3d1ae683 to your computer and use it in GitHub Desktop.
Fe jam 25 feb 2025
// std/evm.fe
mod std {
pub mod evm {
pub type Address = u256
pub struct Wei { value: usize }
pub struct Ptr { pub location: usize }
pub struct Buf { pub offset: Ptr, pub len: usize }
const HASH_SCRATCH_OFFSET: u256 = 0x0
extern {
pub fn __call(gas: Wei, address: Address, value: Wei, args: Buf, ret: Buf)
pub fn __mstore(_ loc: u256, _ data: u256)
pub fn __keccak256(args: Buf) -> u256
pub fn __caller() -> Address
}
pub fn alloc(len: usize) -> Buf {
let offset = __mload(0x40) // 0
// ...
}
}
}
// mstore 0x40 0x60
// let mybuf = alloc(10000) // Buf { offset: 0, len: 10000 }
// use core::todo
struct Map<T, U> {
slot: u8
}
impl<T, U> Map<T, U>
where T: Encode, U: Decode
{
pub fn get(self, _ key: T) -> U {
let buffer = alloc(key.encode_size())
let x : u256 = std::evm::__keccak256(args: buffer)
let mut y : U
//y = x.into()
y
}
pub fn set(self, _ key: T, _ value: U) {
todo()
}
}
use std::evm::abi::SolEncodedUint64
#[derive(SolAbi)]
msg Erc20Msg {
TotalSupply -> u256,
BalanceOf { account: Address } -> u256 // keccak("balanceOf(address)"),
IsGreaterThan10 { val: SolEncodedUint64 } -> bool,
Attack { power: SolEncodedUint8, targets: *Address, collatoral_damage: *Address } -> bool,
}
contract ERC20 {
pub balance: Map<std::evm::Address, u256>,
pub allowance: <(std::evm::Address, std::evm::Address), std::evm::Address>,
pub TOTAL_SUPPLY: u256
}
impl ERC20 {
mstore(0x40, 0x60)
// read from calldata
recv Erc20Msg {
TotalSupply -> u256 {
self.TOTAL_SUPPLOY
}
BalanceOf { account } -> u256 {
return 100
}
IsGreaterThan10 { val } -> bool {
// val is 32 bytes in calldata
val.as_u64() > 10
}
Attack { power: SolEncodedUint8, targets: ArbitraryList<Address> } -> bool {
for target in targets {
}
}
}
recv Erc888 {
}
}
impl ERC20 {
pub fn constructor(mut self) {
self.TOTAL_SUPPLY = 21_000_000 * 1**18
self.balance.set(std::evm::__caller(), self.TOTAL_SUPPLY)
}
pub fn totalSupply(self) -> u256 {
self.TOTAL_SUPPLY
}
pub fn balanceOf(self, account : std::evm::Address) -> u256 {
self.balance.get(account)
}
pub fn transfer(mut self, to : std::evm::Address, value : u256) -> bool {
self.balance.set(std::evm::__caller(), self.balance.get(std::evm::__caller()) - value)
self.balance.set(to, self.balance.get(to) + value)
true
}
pub fn allowance(self, owner : std::evm::Address, spender : std::evm::Address) -> u256 {
self.allowance.get(owner).get(spender)
}
pub fn approve(mut self, spender : std::evm::Address, value : u256) -> bool {
self.allowance.get(std::evm::__caller()).set(spender, value)
true
}
pub fn transferFrom(mut self, from : std::evm::Address, to : std::evm::Address, value : u256) -> bool {
self.balance.set(from, self.balance.get(from) - value)
self.balance.set(to, self.balance.get(to) + value)
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment