Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning res.result on Transaction Call #5

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pyrevm.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ class EVM:
to: str,
value: Optional[int] = None,
data: Optional[list[int]] = None,
) -> None: ...
) -> str: ...
def call_raw(
self: "EVM",
caller: str,
to: str,
value: Optional[int] = None,
data: Optional[list[int]] = None,
) -> None: ...
) -> str: ...
def deploy(
self: "EVM",
deployer: str,
Expand Down
1 change: 1 addition & 0 deletions pytest/contracts/full_math.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
608060405234801561001057600080fd5b50600436106100365760003560e01c80630af8b27f1461003b578063aa9a091214610091575b600080fd5b61007b6004803603606081101561005157600080fd5b810190808035906020019092919080359060200190929190803590602001909291905050506100e7565b6040518082815260200191505060405180910390f35b6100d1600480360360608110156100a757600080fd5b81019080803590602001909291908035906020019092919080359060200190929190505050610145565b6040518082815260200191505060405180910390f35b60006100f4848484610145565b90506000828061010057fe5b848609111561013e577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff811061013557600080fd5b80806001019150505b9392505050565b600080600080198587098587029250828110838203039150506000811415610180576000841161017457600080fd5b83820492505050610218565b80841161018c57600080fd5b600084868809905082811182039150808303925060008586600003169050808604955080840493506001818260000304019050808302841793506000600287600302189050808702600203810290508087026002038102905080870260020381029050808702600203810290508087026002038102905080870260020381029050808502955050505050505b939250505056fea2646970667358221220b256a7bccd674ce18ce412dab2791542b912a007f39cfeb8c76e0e5e1e6f7bf864736f6c63430007060033
33 changes: 33 additions & 0 deletions pytest/test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os.path

import pytest

from pyrevm import *
Expand All @@ -8,6 +10,17 @@
fork_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"


def load_contract_bin(contract_name: str) -> bytes:
with open(f"{os.path.dirname(__file__)}/contracts/{contract_name}", "r") as readfile:
hexstring = readfile.readline()
return bytes.fromhex(hexstring)


def encode_uint(num: int):
encoded = hex(num)[2:]
return ("0" * (64 - len(encoded))) + encoded


def test_revm():
# set up an evm
evm = EVM(
Expand Down Expand Up @@ -66,3 +79,23 @@ def test_balances():

assert evm.get_balance(address) == AMT
assert evm.basic(address).balance == AMT


def test_call_raw():
evm = EVM()
evm.insert_account_info(address, AccountInfo(code=load_contract_bin("full_math.bin")))

# mulDiv() -> 64 * 8 / 2
result = evm.call_raw(caller=address2, to=address, data=bytes.fromhex(f"aa9a0912{encode_uint(64)}{encode_uint(8)}{encode_uint(2)}"))

assert int(result, 16) == 256


def test_call_committing():
evm = EVM()
evm.insert_account_info(address, AccountInfo(code=load_contract_bin("full_math.bin")))

# mulDivRoundingUp() -> 64 * 8 / 3
result = evm.call_raw(caller=address2, to=address, data=bytes.fromhex(f"0af8b27f{encode_uint(64)}{encode_uint(8)}{encode_uint(3)}"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result = evm.call_raw(caller=address2, to=address, data=bytes.fromhex(f"0af8b27f{encode_uint(64)}{encode_uint(8)}{encode_uint(3)}"))
result = evm.call_raw_committing(caller=address2, to=address, data=bytes.fromhex(f"0af8b27f{encode_uint(64)}{encode_uint(8)}{encode_uint(3)}"))


assert int(result, 16) == 171
8 changes: 4 additions & 4 deletions src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl EVM {
to: &str,
value: Option<U256>,
data: Option<Vec<u8>>,
) -> PyResult<()> {
) -> PyResult<String> {
let res = _self
.0
.call_raw_committing(
Expand All @@ -128,7 +128,7 @@ impl EVM {

// TODO: Return the traces back to the user.
dbg!(&res.traces);
Ok(())
Ok(format!("{:x}", res.result))
}

fn call_raw(
Expand All @@ -137,7 +137,7 @@ impl EVM {
to: &str,
value: Option<U256>,
data: Option<Vec<u8>>,
) -> PyResult<()> {
) -> PyResult<String> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be left as a byte array so that it can be converted with int.frombytes(..., byteorder='big')

let res = _self
.0
.call_raw(
Expand All @@ -153,7 +153,7 @@ impl EVM {
}

dbg!(&res.traces);
Ok(())
Ok(format!("{:x}", res.result))
}

/// Deploy a contract with the given code.
Expand Down