Skip to content
This repository has been archived by the owner on Feb 14, 2021. It is now read-only.

Add create2 under kip4 feature flag #12

Merged
merged 2 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ default-features = false

[features]
default = []
kip4 = []
std = ["pwasm-std/std", "parity-hash/std", "bigint/std", "byteorder/std"]
44 changes: 42 additions & 2 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,28 @@ mod external {

pub fn origin(dest: *mut u8);

pub fn elog(topic_ptr: *const u8, topic_count: u32, data_ptr: *const u8, data_len: u32);
pub fn elog(
topic_ptr: *const u8,
topic_count: u32,
data_ptr: *const u8,
data_len: u32
);

pub fn create(
endowment: *const u8,
code_ptr: *const u8,
code_len: u32,
result_ptr: *mut u8
) -> i32;

pub fn create(endowment: *const u8, code_ptr: *const u8, code_len: u32, result_ptr: *mut u8) -> i32;
#[cfg(feature = "kip4")]
pub fn create2(
endowment: *const u8,
salt: *const u8,
code_ptr: *const u8,
code_len: u32,
result_ptr: *mut u8
) -> i32;

pub fn suicide(refund: *const u8) -> !;

Expand Down Expand Up @@ -118,6 +137,27 @@ pub fn create(endowment: U256, code: &[u8]) -> Result<Address, Error> {
}
}

#[cfg(feature = "kip4")]
/// Create a new account with the given code and salt, requires KIP-4.
///
/// # Errors
///
/// Returns [`Error`] in case contract constructor failed.
///
/// [`Error`]: struct.Error.html
pub fn create2(endowment: U256, salt: H256, code: &[u8]) -> Result<Address, Error> {
let mut endowment_arr = [0u8; 32];
endowment.to_big_endian(&mut endowment_arr);
let mut result = Address::new();
unsafe {
if external::create2(endowment_arr.as_ptr(), salt.as_ptr(), code.as_ptr(), code.len() as u32, (&mut result).as_mut_ptr()) == 0 {
Ok(result)
} else {
Err(Error)
}
}
}

/// Message-call into an account
///
/// # Arguments:
Expand Down