evm2near compiles Solidity contracts into NEAR WebAssembly contracts.

Overview

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 test.wasm -b wasi

Running the output in wasmtime:

wasmtime --allow-unknown-exports test.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 test.wasm -b near

Running the output using near-cli:

near --networkId testnet dev-deploy test.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

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

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
Comments
  • EVM Flashloan on Aurora. but get

    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?

    opened by cklai90 1
  • Empty result

    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

    opened by Scofield-04-21-076 1
  • bug when using ./evm2near (.sol to .wasm)

    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;
        }
    }
    
    opened by Scofield-04-21-076 1
  • Feat: give output as json from abi-based methods

    Feat: give output as json from abi-based methods

    This allows nice output when calling from the NEAR CLI. Example:

    $ near --networkId testnet --accountId birchmd.testnet call dev-1663014663747-27418521013742 multiply '{"a": 7, "b": 6}'
    
    Scheduling a call: dev-1663014663747-27418521013742.multiply({"a": 7, "b": 6})
    Doing account.functionCall()
    Transaction Id 97g3DMfcZMyipJ5QLzRw46d6SvnjsTdyn1B1ZVH5w7fg
    To see the transaction in the transaction explorer, please open this url in your browser
    https://explorer.testnet.near.org/transactions/97g3DMfcZMyipJ5QLzRw46d6SvnjsTdyn1B1ZVH5w7fg
    { output: 42, status: 'SUCCESS' }
    

    This feature is implemented by having environment functions like value_return and revert only update the internal EVM state (setting the exit status and return data), not call any external exit methods (like process::exit, or NEAR's host functions). Instead, the exit calls happen as part of a new function called post_exec. This gives a chance for the return data to be re-encoded from ABI into JSON before the value is returned.

    The main files to focus on in the review are: compiler.rs, api.rs and encode.rs. Note that the code for decoding JSON into ABI input was simply moved from api.rs to decode.rs with no changes.

    enhancement 
    opened by birchmd 0
  • Testing script

    Testing script

    This script automatically compile compiler, then compile test contracts, deploy them in testchain, call some functions and check that they return correct results.

    New tests can be added very easy, just add following string in All() function:

    TestOne(File.sol, method, args, expected_result)
    

    TODO : link this script to github actions (a bit difficult because it need some setup script)

    opened by MCJOHN974 6
  • "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

    opened by Scofield-04-21-076 3
Owner
Aurora
Scaling Solution for Ethereum - Built on NEAR
Aurora
Helpful functions and macros for developing smart contracts on NEAR Protocol.

near-contract-tools Helpful functions and macros for developing smart contracts on NEAR Protocol. This package is a collection of common tools and pat

Jacob 27 Dec 17, 2022
Helpful functions and macros for developing smart contracts on NEAR Protocol.

near-contract-tools Helpful functions and macros for developing smart contracts on NEAR Protocol. This package is a collection of common tools and pat

NEARFoundation 9 Aug 3, 2022
Generates Solidity code to verify EIP-712 style signatures

eip712 Generates Solidity code to verify EIP-712 style signatures. Usage First, create an abstract contract implementing the functionality you want: /

Sam Wilson 11 Dec 22, 2022
A tool to optimize your Solidity function signatures.

sigop A CLI tool to optimize your Solidity function signatures. I wanted to create this after seeing transmissions11's comment about this optimization

Quartz Technology 11 Nov 24, 2022
A Solidity static analyzer to identify contract vulnerabilities and gas efficiencies.

solstat A Solidity static analyzer to identify contract vulnerabilities and gas efficiencies. .------. .------. .------. .------. .------. .------. .-

null 345 Feb 18, 2023
Rust bindings for Solidity's AST and visitors

solc-ast solc-ast provides rust bindings for the solidity AST and visitors. The visitors were built to be 1-1 compatible with the visitors from solc.

Hari 35 May 7, 2023
Minimal Substrate node configured for smart contracts via pallet-contracts.

substrate-contracts-node This repository contains Substrate's node-template configured to include Substrate's pallet-contracts ‒ a smart contract modu

Parity Technologies 73 Dec 30, 2022
Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.

dlctix Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin. This project is part of the Backdrop Build

null 7 Feb 29, 2024
Reference client for NEAR Protocol

Reference implementation of NEAR Protocol About NEAR NEAR's purpose is to enable community-driven innovation to benefit people around the world. To ac

NEAR 2k Jan 3, 2023
A PoC backbone for NFT Marketplaces on NEAR Protocol

NFT Market Reference Implementation A PoC backbone for NFT Marketplaces on NEAR Protocol. Reference Changelog Changelog Progress: basic purchase of NF

null 9 May 26, 2022
Reference client for NEAR Protocol

Reference implementation of NEAR Protocol About NEAR NEAR's purpose is to enable community-driven innovation to benefit people around the world. To ac

NEAR 2k Jan 4, 2023
Near contract collection for story-arc.io

arc-near-contracts TBD Development Setup Make sure to format code using defaults before every commit or set up the environment to handle this for you.

null 2 Mar 14, 2022
A tutorial for an NFT Market Place Built with Near Protocol and React.js

nft-marketplace-part-1 A tutorial for an NFT Market Place built using Near Protocol and React.js. Preview To run this app locally, follow below steps:

Kohwo Orien 5 Jun 29, 2022
USN - the first NEAR-native stablecoin

USN USN is a NEAR-native USD stable coin. The contract implements fungible token API according to the following standards: NEP-141 (ERC-20 fashioned)

DecentralBank 52 Nov 2, 2022
A tool to help with estimating NEAR gas spent by transactions on Aurora.

Aurora Gas Estimator A tool to help with estimating NEAR gas spent by transactions on Aurora. Building from source Prerequisites Rust GNU Make (3.81+)

Michael Birch 4 Aug 23, 2022
Durudex Near Protocol Token.

Durudex Near Token ?? Prerequisites Rust Near CLI ⚙️ Build To build a token you will need to use this: cargo build --all --target wasm32-unknown-unkno

Durudex 2 May 31, 2022
Confidential credit scores and loan disbursal, powered by zkSNARKs and NEAR Protocol

zkLoans zkLoans brings confidential Credit Scores. It proves to a counterparty if you meet threshold requirements for a credit score without revealing

Anirudha Bose 2 Sep 13, 2022
Lesson 1 - Fundamental structure of smartcontract of near protocol

NEAR CONTRACT dev-1672293046853-49717456344735 pnpm run deploy > [email protected] deploy /Users/vudangquang/Courses/VBI-Courses/rs-contract > cd co

Dang Quang Vu 3 Jan 7, 2023
Smart contracts for Ref Finance

Ref Finance Contracts This mono repo contains the source code for the smart contracts of Ref Finance on NEAR. Contracts Contract Reference Description

Ref Finance 92 Dec 7, 2022