-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from OffchainLabs/stylus-contract-calls
Contract Calls
- Loading branch information
Showing
43 changed files
with
1,188 additions
and
216 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,65 @@ | ||
// Copyright 2023, Offchain Labs, Inc. | ||
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE | ||
|
||
use super::util::{Bytes20, Bytes32}; | ||
|
||
#[derive(Copy, Clone)] | ||
#[repr(C)] | ||
struct RustVec { | ||
ptr: *mut u8, | ||
len: usize, | ||
cap: usize, | ||
} | ||
|
||
impl Default for RustVec { | ||
fn default() -> Self { | ||
Self { | ||
ptr: std::ptr::null_mut(), | ||
len: 0, | ||
cap: 0, | ||
} | ||
} | ||
} | ||
|
||
#[link(wasm_import_module = "forward")] | ||
extern "C" { | ||
fn call_contract( | ||
contract: *const u8, | ||
calldata: *const u8, | ||
calldata_len: usize, | ||
value: *const u8, | ||
gas: u64, | ||
return_data_len: *mut usize, | ||
) -> u8; | ||
|
||
/// A noop when there's never been a call | ||
fn read_return_data(dest: *mut u8); | ||
} | ||
|
||
/// Calls the contract at the given address, with options for passing value or limiting the amount of gas provided. | ||
/// On failure, the output consists of the call's revert data. | ||
pub fn call(contract: Bytes20, calldata: &[u8], value: Option<Bytes32>, gas: Option<u64>) -> Result<Vec<u8>, Vec<u8>> { | ||
let mut outs_len = 0; | ||
let value = value.unwrap_or_default(); | ||
let gas = gas.unwrap_or(u64::MAX); // will be clamped by 63/64 rule | ||
let status = unsafe { | ||
call_contract( | ||
contract.ptr(), | ||
calldata.as_ptr(), | ||
calldata.len(), | ||
value.ptr(), | ||
gas, | ||
&mut outs_len as *mut _, | ||
) | ||
}; | ||
let outs = unsafe { | ||
let mut outs = Vec::with_capacity(outs_len); | ||
read_return_data(outs.as_mut_ptr()); | ||
outs.set_len(outs_len); | ||
outs | ||
}; | ||
match status { | ||
0 => Ok(outs), | ||
_ => Err(outs), | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
pub use util::{Bytes20, Bytes32}; | ||
|
||
pub mod contract; | ||
pub mod debug; | ||
mod util; | ||
|
||
|
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
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
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
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
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
Oops, something went wrong.