-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use napi::JsBigInt; | ||
use napi_derive::napi; | ||
use revm::primitives::{Address, BlobExcessGasAndPrice, BlockEnv, B256}; | ||
|
||
use crate::utils; | ||
|
||
#[napi(object)] | ||
pub struct JsBlockEnv { | ||
pub number: JsBigInt, | ||
pub timestamp: JsBigInt, | ||
pub gas_limit: JsBigInt, | ||
pub basefee: JsBigInt, | ||
pub difficulty: JsBigInt, | ||
} | ||
|
||
impl TryInto<BlockEnv> for JsBlockEnv { | ||
type Error = anyhow::Error; | ||
|
||
fn try_into(mut self) -> std::result::Result<BlockEnv, Self::Error> { | ||
Ok(BlockEnv { | ||
number: utils::convert_bigint_to_u256(&mut self.number)?, | ||
timestamp: utils::convert_bigint_to_u256(&mut self.timestamp)?, | ||
gas_limit: utils::convert_bigint_to_u256(&mut self.gas_limit)?, | ||
basefee: utils::convert_bigint_to_u256(&mut self.basefee)?, | ||
difficulty: utils::convert_bigint_to_u256(&mut self.difficulty)?, | ||
|
||
// TODO: double check | ||
coinbase: Address::ZERO, | ||
blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(0)), | ||
prevrandao: Some(B256::ZERO), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,45 @@ | ||
use std::str::FromStr; | ||
|
||
use anyhow; | ||
use napi::JsString; | ||
use revm::primitives::Address; | ||
use napi::{Env, JsBigInt, JsString}; | ||
use revm::primitives::{Address, U256}; | ||
|
||
pub(crate) fn create_address_from_js_string(js_str: JsString) -> anyhow::Result<Address> { | ||
let js_str = js_str.into_utf8()?; | ||
let slice = js_str.as_str()?; | ||
Ok(Address::from_str(slice)?) | ||
} | ||
|
||
pub(crate) fn convert_u256_to_bigint(node_env: Env, value: U256) -> JsBigInt { | ||
let slice = value.as_le_slice(); | ||
|
||
const WORD_SIZE: usize = 8; | ||
assert!(slice.len() % WORD_SIZE == 0); | ||
|
||
// https://nodejs.org/api/n-api.html#n_api_napi_create_bigint_words | ||
let mut words: Vec<u64> = Vec::with_capacity(slice.len() / WORD_SIZE); | ||
for chunk in slice.chunks_exact(WORD_SIZE) { | ||
let mut bytes = [0; 8]; | ||
bytes.copy_from_slice(chunk); | ||
words.push(u64::from_le_bytes(bytes)); | ||
} | ||
|
||
node_env.create_bigint_from_words(false, words).unwrap() | ||
} | ||
|
||
pub(crate) fn convert_bigint_to_u256(bigint: &mut JsBigInt) -> anyhow::Result<U256> { | ||
const WORD_SIZE: usize = 8; | ||
|
||
let words = bigint.get_words()?; | ||
|
||
let mut bytes: Vec<u8> = Vec::with_capacity(bigint.word_count * WORD_SIZE); | ||
|
||
for word in words.1 { | ||
let word_bytes = word.to_le_bytes(); | ||
bytes.extend_from_slice(&word_bytes); | ||
} | ||
|
||
assert!(bytes.len() % WORD_SIZE == 0); | ||
|
||
Ok(U256::from_le_slice(&bytes[..])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import { injectable } from "@mainsail/container"; | ||
import { Contracts } from "@mainsail/contracts"; | ||
|
||
import { Evm, JsTransactionContext, JsTransactionResult } from "./generated/bindings"; | ||
import { Evm } from "./generated/bindings"; | ||
|
||
@injectable() | ||
export class Instance { | ||
export class Instance implements Contracts.Evm.Instance { | ||
private readonly evm: Evm = new Evm(); | ||
|
||
public async transact(txContext: JsTransactionContext): Promise<JsTransactionResult> { | ||
public async configureBlockEnvironment(environment: Contracts.Evm.BlockEnvironment): Promise<void> { | ||
return this.evm.configureBlockEnv(environment); | ||
} | ||
|
||
public async transact(txContext: Contracts.Evm.TransactionContext): Promise<Contracts.Evm.TransactionResult> { | ||
return this.evm.transact(txContext); | ||
} | ||
|
||
public async view(txContext: JsTransactionContext): Promise<JsTransactionResult> { | ||
public async view(txContext: Contracts.Evm.TransactionContext): Promise<Contracts.Evm.TransactionResult> { | ||
return this.evm.view(txContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters