Coder Social home page Coder Social logo

aurora-is-near / evm2near Goto Github PK

View Code? Open in Web Editor NEW
22.0 5.0 4.0 569 KB

evm2near compiles Solidity contracts into NEAR WebAssembly contracts.

License: Creative Commons Zero v1.0 Universal

Makefile 1.21% Rust 94.64% Solidity 2.37% Python 1.79%
compiler evm near-protocol solidity webassembly

evm2near's Introduction

EVM โ†’ NEAR

evm2near is a project for compiling EVM bytecode into wasm bytecode, with the particular goal of having that wasm artifact be executable on the NEAR blockchain. For ease of testing locally, evm2near also currently supports wasi as a target platform. The wasi output can be run locally using a wasm runtime, for example wasmtime. This can be useful for debugging contracts without deploying to NEAR.

Even though evm2near is a general EVM bytecode to wasm bytecode transpiler, the CLI interface accepts a Solidity source file as input for convenience. The source file is compiled to EVM bytecode using solc. Using Solidity and solc, means evm2near also has access to the contract ABI. This allows the output wasm artifact to contain functions that match the ones given in the contract. For example, test/calc.sol contains a contract with a function multiply(int a, int b), and the compiled wasm artifact will also contain a function called multiply which takes a JSON string as input. The JSON input is expected to be an object with fields matching the function argument names (a and b in the example). These functions generated based on the ABI are in addition to a general function called execute, which accepts binary input following the usual Solidity ABI (i.e. the first four bytes are the "selector" derived from the function signature, the remaining bytes are the input arguments encoded using Solidity's ABI format).

Usage

Compiling to wasi (for running locally)

./evm2near INPUT_SOLIDITY_CONTRACT -o OUTPUT_WASM_FILE -b wasi

Example:

./evm2near test/calc.sol -o calc.wasm -b wasi

Running the output in wasmtime:

wasmtime --allow-unknown-exports calc.wasm --invoke multiply -- '{"a":6, "b": 7}'

Compiling to NEAR

./evm2near INPUT_SOLIDITY_CONTRACT -o OUTPUT_WASM_FILE -b near

Example:

./evm2near test/calc.sol -o calc.wasm -b near

Running the output using near-cli:

near --networkId testnet dev-deploy calc.wasm
near --networkId testnet --accountId $NEAR_ACCOUNT_ID call $DEV_CONTACT_ID multiply '{"a": 7, "b": 6}'

Note: you will need to set the value of $DEV_CONTACT_ID from the output of the prior dev-deploy command (you will see something like Account id: dev-1663014663747-27418521013742 included in the output, then you would set DEV_CONTACT_ID=dev-1663014663747-27418521013742).

Note: you will need to use your own NEAR account for $NEAR_ACCOUNT_ID. If you do not have one, you can create it using the NEAR wallet, then access it via the CLI using the near login command.

Help

./evm2near --help

Development

Prerequisites

  • Rust toolchain (nightly 2022-09-07)
  • Solidity compiler solc (0.8.16+)
  • wasm-strip from WABT

Prerequisites on macOS

brew install rustup solidity wabt

Prerequisites on Ubuntu

curl -sSf https://sh.rustup.rs | sh

sudo apt-add-repository ppa:ethereum/ethereum
sudo apt update
sudo apt install solc

sudo apt install wabt

Development Builds

rustup target add wasm32-wasi
rustup target add wasm32-unknown-unknown
make
./evm2near --help

Release

Prerequisites

  • Rust toolchain (nightly 2022-09-07)
  • MinGW-w64 (10.0.0+)
  • wasm-strip from WABT

Prerequisites on macOS

brew install rustup mingw-w64 wabt

Prerequisites on Ubuntu

curl -sSf https://sh.rustup.rs | sh

apt install mingw-w64 wabt

Release Builds

rustup target add wasm32-wasi
rustup target add wasm32-unknown-unknown
rustup target add aarch64-apple-darwin
rustup target add x86_64-apple-darwin
rustup target add aarch64-pc-windows-msvc
rustup target add x86_64-pc-windows-gnu
rustup target add aarch64-unknown-linux-musl
rustup target add x86_64-unknown-linux-musl
make clean release

Benchmarks

You can see results of gas-spent measurements here

evm2near's People

Contributors

artob avatar birchmd avatar shishkin-pavel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

evm2near's Issues

"ExecutionError":"WebAssembly trap: An `unreachable` opcode was executed."

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.16;

contract Calc {

    address public owner;
    uint256 public num;


    function multiply(address addr) public returns (address) {

        return addr;
    }
}

The contract can be deployed, but when I call the multiply method:
near call name_contract.testnet multiply '{"addr": "account1.testnet"}' --accountId name_contract.testnet

I get an error:
Error: {"index":0,"kind":{"ExecutionError":"WebAssembly trap: An unreachable opcode was executed."}}

After the call:
near call name_contract.testnet multiply '{"addr": "account1.testnet"}' --accountId name_contract.testnet
I expect to be returned the account passed in the function parameter:
account1.testnet

EVM Flashloan on Aurora. but get "A status code indicating if the top-level call succeeded or failed (applicable for Post BYZANTIUM blocks only)

I am had successfully deploy a flashloan contract on both Aurora & Aurora+ network (an EVM on Near protocol).

and also successfully tested with its RPC on hardhat, there are no issues, able to borrow and return fund.

However, when executing on mainnet, it shows a failed transaction with the following error:
"A status code indicating if the top-level call succeeded or failed (applicable for Post BYZANTIUM blocks only).

what could be the possible root cause?

Empty result

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.16;


contract Calc {

    function multiply(int ui) public returns (int) {

        return ui;
    }
}

After deploying this contract, I call the multiply method:
near call name_contract.testnet multiply '{"ui": 16}' --accountId name_contract.testnet

I get result: '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10'

Ideally, the expected result should be: 16

But if you change the code as follows:

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.16;


contract Calc {

    address public owner;
    uint256 public num;


    function multiply(int ui) public returns (int) {

        return ui;
    }
}

I get result:
Empty result ''

Expected result: 16

Failed to load source for dependency `clap` & zsh: no such file or directory: ./evm2near

Hi all,

I'm using the compiler branch. I have downloaded the dependencies but now when I execute make I get this:

(base) sidsridhar@Siddarths-MacBook-Pro evm2near-compiler % make
cargo build --package=evmlib --release --frozen --target=wasm32-wasi --no-default-features --features=gas,pc
error: failed to get `clap` as a dependency of package `evm2near v0.1.0 (/Users/sidsridhar/Downloads/evm2near-compiler/bin/evm2near)`

Caused by:
  failed to load source for dependency `clap`

Caused by:
  Unable to update registry `crates-io`

Caused by:
  attempting to make an HTTP request, but --frozen was specified
make: *** [evmlib.wasi] Error 101

Then when I execute ./evm2near --help I get this:

zsh: no such file or directory: ./evm2near

Please advise and lmk how I can fix this

bug when using ./evm2near (.sol to .wasm)

Using command ./evm2near my_contract.sol -o my_contract.wasm -b near, I get the following error: Failed to compile Solidity ABI: unexpected output from `solc'

I was able to figure out that this error occurs if the contract uses a constructor, example:

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.16;

contract Calc {

    address public owner;
    uint256 public num;

    constructor() {
        owner = msg.sender;
        num = 0;
    }

    function multiply(int a, int b) public pure returns (int) {
        return a * b;
    }
}

This error also occurs if events are used in the contract, example:

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.16;

contract Calc {

    address public owner;
    uint256 public num;

    event Multiply(uint256 mul);

    function multiply(uint256 a, uint256 b) public returns (uint256) {

        uint256 c = a * b;

        emit Multiply(c);

        return c;
    }
}

Failed to compile Solidity code: could not parse `solc' output: invalid EVM opcode 0x5F

Hi all,

I have installed everything and am trying to execute the example in the README.

(base) sidsridhar@Siddarths-MacBook-Pro evm2near-master 2 % ./evm2near test/calc.sol -o calc.wasm -b wasi
Failed to compile Solidity code: could not parse `solc' output: invalid EVM opcode 0x5F

What do I need to do to clarify the above error - since the JUMP0 opcode is used in all .sol files in test before 0.8.20+?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.