-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add web3::contract::deploy::execute_no_unlock
#252
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7cb9e68
Add `web3::contract::deploy::execute_no_unlock`
rockbmb c99e2bc
Refactor common parts of `execute` and `execute_no_unlock`
rockbmb 6ac5d7d
Revert "Refactor common parts of `execute` and `execute_no_unlock`"
rockbmb b5b2f56
Generalize field of `PendingContract` to accept `Future`s (WIP)
rockbmb 6f2f992
Extend `PendingContract` type to support `sign_and_execute`
rockbmb 5c45bcd
Refactor
tomusdrw 2204f5d
Apply cargo fmt
rockbmb 0589a9b
Update `Future` implementation for `PendingContract`
rockbmb 1df0720
Merge branch 'master' into execute-no-unlock
tomusdrw 2cee697
Remove unnecessary type parameter from function
rockbmb File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ use crate::api::{Eth, Namespace}; | |||||
use crate::confirm; | ||||||
use crate::contract::tokens::Tokenize; | ||||||
use crate::contract::{Contract, Options}; | ||||||
use crate::types::{Address, Bytes, TransactionRequest}; | ||||||
use crate::types::{Address, Bytes, TransactionReceipt, TransactionRequest}; | ||||||
use crate::Transport; | ||||||
|
||||||
pub use crate::contract::error::deploy::Error; | ||||||
|
@@ -102,13 +102,91 @@ impl<T: Transport> Builder<T> { | |||||
waiting, | ||||||
}) | ||||||
} | ||||||
|
||||||
/// Execute deployment passing code and contructor parameters. | ||||||
/// | ||||||
/// Unlike the above `execute`, this method uses | ||||||
/// `sign_raw_transaction_with_confirmation` instead of | ||||||
/// `sign_transaction_with_confirmation`, which requires the account from | ||||||
/// which the transaction is sent to be unlocked. | ||||||
pub fn sign_and_execute<P, V, Ft>( | ||||||
self, | ||||||
code: V, | ||||||
params: P, | ||||||
from: Address, | ||||||
password: &str, | ||||||
web3: crate::Web3<T>, | ||||||
) -> Result<PendingContract<T, Ft>, ethabi::Error> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Drop |
||||||
where | ||||||
P: Tokenize, | ||||||
V: AsRef<str>, | ||||||
T: Transport, | ||||||
Ft: Future<Item = TransactionReceipt, Error = crate::error::Error>, | ||||||
{ | ||||||
let options = self.options; | ||||||
let eth = self.eth; | ||||||
let abi = self.abi; | ||||||
|
||||||
let mut code_hex = code.as_ref().to_string(); | ||||||
|
||||||
for (lib, address) in self.linker { | ||||||
if lib.len() > 38 { | ||||||
return Err( | ||||||
ethabi::ErrorKind::Msg(String::from("The library name should be under 39 characters.")).into(), | ||||||
); | ||||||
} | ||||||
let replace = format!("__{:_<38}", lib); // This makes the required width 38 characters and will pad with `_` to match it. | ||||||
let address: String = address.as_ref().to_hex(); | ||||||
code_hex = code_hex.replacen(&replace, &address, 1); | ||||||
} | ||||||
code_hex = code_hex.replace("\"", "").replace("0x", ""); // This is to fix truffle + serde_json redundant `"` and `0x` | ||||||
let code = code_hex.from_hex().map_err(ethabi::ErrorKind::Hex)?; | ||||||
|
||||||
let params = params.into_tokens(); | ||||||
let data = match (abi.constructor(), params.is_empty()) { | ||||||
(None, false) => { | ||||||
return Err(ethabi::ErrorKind::Msg("Constructor is not defined in the ABI.".into()).into()); | ||||||
} | ||||||
(None, true) => code, | ||||||
(Some(constructor), _) => constructor.encode_input(code, ¶ms)?, | ||||||
}; | ||||||
|
||||||
let tx = TransactionRequest { | ||||||
from, | ||||||
to: None, | ||||||
gas: options.gas, | ||||||
gas_price: options.gas_price, | ||||||
value: options.value, | ||||||
nonce: options.nonce, | ||||||
data: Some(Bytes(data)), | ||||||
condition: options.condition, | ||||||
}; | ||||||
|
||||||
let waiting = web3.personal().sign_transaction(tx, password).and_then(|signed_tx| { | ||||||
confirm::send_raw_transaction_with_confirmation( | ||||||
eth.transport().clone(), | ||||||
signed_tx.raw, | ||||||
self.poll_interval, | ||||||
self.confirmations, | ||||||
) | ||||||
}); | ||||||
|
||||||
Ok(PendingContract { | ||||||
eth: Some(eth), | ||||||
abi: Some(abi), | ||||||
waiting, | ||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
/// Contract being deployed. | ||||||
pub struct PendingContract<T: Transport> { | ||||||
pub struct PendingContract< | ||||||
T: Transport, | ||||||
F: Future<Item = TransactionReceipt, Error = crate::error::Error> = confirm::SendTransactionWithConfirmation<T>, | ||||||
> { | ||||||
eth: Option<Eth<T>>, | ||||||
abi: Option<ethabi::Contract>, | ||||||
waiting: confirm::SendTransactionWithConfirmation<T>, | ||||||
waiting: F, | ||||||
} | ||||||
|
||||||
impl<T: Transport> Future for PendingContract<T> { | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Ft
parameter is unused. This is problematic for calling code because the compiler cannot infer the instantiation ofFt
. This leads to the calling code always requiring an explicit type annotation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's just remove it then. I guess I left it by mistake.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try this out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it solve the issues you were having?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It did. I get other errors now, but as far as this PR goes, it has what we need.
Do you still want me to add a test before we merge?