2020-05-30 20:04:08 +00:00
|
|
|
use crate::{Contract, ContractError};
|
2020-05-30 14:24:50 +00:00
|
|
|
|
2020-05-31 16:01:34 +00:00
|
|
|
use ethers_core::{
|
2021-12-16 22:01:00 +00:00
|
|
|
abi::{Abi, Token, Tokenize},
|
2022-02-05 14:37:09 +00:00
|
|
|
types::{
|
2022-05-22 00:48:41 +00:00
|
|
|
transaction::eip2718::TypedTransaction, Address, BlockNumber, Bytes, NameOrAddress,
|
|
|
|
TransactionReceipt, TransactionRequest, U256, U64,
|
2022-02-05 14:37:09 +00:00
|
|
|
},
|
2020-05-30 14:24:50 +00:00
|
|
|
};
|
2022-06-04 21:18:25 +00:00
|
|
|
use ethers_providers::{
|
|
|
|
call_raw::{CallBuilder, RawCall},
|
|
|
|
Middleware,
|
|
|
|
};
|
2020-05-30 14:24:50 +00:00
|
|
|
|
2021-08-09 00:50:38 +00:00
|
|
|
#[cfg(not(feature = "legacy"))]
|
|
|
|
use ethers_core::types::Eip1559TransactionRequest;
|
|
|
|
|
2022-06-04 21:18:25 +00:00
|
|
|
use std::{marker::PhantomData, sync::Arc};
|
2020-06-22 08:44:08 +00:00
|
|
|
|
2022-03-19 04:23:33 +00:00
|
|
|
/// Helper which manages the deployment transaction of a smart contract.
|
|
|
|
///
|
|
|
|
/// This is just a wrapper type for [Deployer] with an additional type to convert the [Contract]
|
|
|
|
/// that the deployer returns when sending the transaction.
|
2022-04-27 12:33:22 +00:00
|
|
|
#[derive(Debug)]
|
2022-03-19 04:23:33 +00:00
|
|
|
#[must_use = "Deployer does nothing unless you `send` it"]
|
|
|
|
pub struct ContractDeployer<M, C> {
|
2022-04-19 20:20:59 +00:00
|
|
|
/// the actual deployer, exposed for overriding the defaults
|
|
|
|
pub deployer: Deployer<M>,
|
2022-03-19 04:23:33 +00:00
|
|
|
/// marker for the `Contract` type to create afterwards
|
|
|
|
///
|
|
|
|
/// this type will be used to construct it via `From::from(Contract)`
|
|
|
|
_contract: PhantomData<C>,
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:33:22 +00:00
|
|
|
impl<M, C> Clone for ContractDeployer<M, C> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ContractDeployer { deployer: self.deployer.clone(), _contract: self._contract }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:23:33 +00:00
|
|
|
impl<M: Middleware, C: From<Contract<M>>> ContractDeployer<M, C> {
|
|
|
|
/// Create a new instance of this [ContractDeployer]
|
|
|
|
pub fn new(deployer: Deployer<M>) -> Self {
|
|
|
|
Self { deployer, _contract: Default::default() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the number of confirmations to wait for the contract deployment transaction
|
|
|
|
pub fn confirmations<T: Into<usize>>(mut self, confirmations: T) -> Self {
|
|
|
|
self.deployer.confs = confirmations.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
|
|
|
|
self.deployer.block = block.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Uses a Legacy transaction instead of an EIP-1559 one to do the deployment
|
|
|
|
pub fn legacy(mut self) -> Self {
|
|
|
|
self.deployer = self.deployer.legacy();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:48:41 +00:00
|
|
|
/// Sets the `from` field in the deploy transaction to the provided value
|
|
|
|
pub fn from<T: Into<Address>>(mut self, from: T) -> Self {
|
|
|
|
self.deployer.tx.set_from(from.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `to` field in the deploy transaction to the provided value
|
|
|
|
pub fn to<T: Into<NameOrAddress>>(mut self, to: T) -> Self {
|
|
|
|
self.deployer.tx.set_to(to.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `gas` field in the deploy transaction to the provided value
|
|
|
|
pub fn gas<T: Into<U256>>(mut self, gas: T) -> Self {
|
|
|
|
self.deployer.tx.set_gas(gas.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `gas_price` field in the deploy transaction to the provided value
|
|
|
|
pub fn gas_price<T: Into<U256>>(mut self, gas_price: T) -> Self {
|
|
|
|
self.deployer.tx.set_gas_price(gas_price.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `value` field in the deploy transaction to the provided value
|
|
|
|
pub fn value<T: Into<U256>>(mut self, value: T) -> Self {
|
|
|
|
self.deployer.tx.set_value(value.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `data` field in the deploy transaction to the provided value
|
|
|
|
pub fn data<T: Into<Bytes>>(mut self, data: T) -> Self {
|
|
|
|
self.deployer.tx.set_data(data.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `nonce` field in the deploy transaction to the provided value
|
|
|
|
pub fn nonce<T: Into<U256>>(mut self, nonce: T) -> Self {
|
|
|
|
self.deployer.tx.set_nonce(nonce.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the `chain_id` field in the deploy transaction to the provided value
|
|
|
|
pub fn chain_id<T: Into<U64>>(mut self, chain_id: T) -> Self {
|
|
|
|
self.deployer.tx.set_chain_id(chain_id.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:23:33 +00:00
|
|
|
/// Dry runs the deployment of the contract
|
|
|
|
///
|
|
|
|
/// Note: this function _does not_ send a transaction from your account
|
|
|
|
pub async fn call(&self) -> Result<(), ContractError<M>> {
|
|
|
|
self.deployer.call().await
|
|
|
|
}
|
|
|
|
|
2022-06-04 21:18:25 +00:00
|
|
|
/// Returns a CallBuilder, which when awaited executes the deployment of this contract via
|
|
|
|
/// `eth_call`. This call resolves to the returned data which would have been stored at the
|
|
|
|
/// destination address had the deploy transaction been executed via `send()`.
|
|
|
|
///
|
|
|
|
/// Note: this function _does not_ send a transaction from your account
|
|
|
|
pub fn call_raw(&self) -> CallBuilder<'_, M::Provider> {
|
|
|
|
self.deployer.call_raw()
|
|
|
|
}
|
|
|
|
|
2022-03-19 04:23:33 +00:00
|
|
|
/// Broadcasts the contract deployment transaction and after waiting for it to
|
|
|
|
/// be sufficiently confirmed (default: 1), it returns a new instance of the contract type at
|
|
|
|
/// the deployed contract's address.
|
|
|
|
pub async fn send(self) -> Result<C, ContractError<M>> {
|
|
|
|
let contract = self.deployer.send().await?;
|
|
|
|
Ok(C::from(contract))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Broadcasts the contract deployment transaction and after waiting for it to
|
|
|
|
/// be sufficiently confirmed (default: 1), it returns a new instance of the contract type at
|
|
|
|
/// the deployed contract's address and the corresponding
|
|
|
|
/// [`TransactionReceipt`](ethers_core::types::TransactionReceipt).
|
|
|
|
pub async fn send_with_receipt(self) -> Result<(C, TransactionReceipt), ContractError<M>> {
|
|
|
|
let (contract, receipt) = self.deployer.send_with_receipt().await?;
|
|
|
|
Ok((C::from(contract), receipt))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the deployer's ABI
|
|
|
|
pub fn abi(&self) -> &Abi {
|
|
|
|
self.deployer.abi()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the deployer's client
|
|
|
|
pub fn client(&self) -> &M {
|
|
|
|
self.deployer.client()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:20:47 +00:00
|
|
|
/// Helper which manages the deployment transaction of a smart contract
|
2022-04-27 12:33:22 +00:00
|
|
|
#[derive(Debug)]
|
2022-03-19 04:23:33 +00:00
|
|
|
#[must_use = "Deployer does nothing unless you `send` it"]
|
2020-09-24 21:33:09 +00:00
|
|
|
pub struct Deployer<M> {
|
2020-06-17 09:22:01 +00:00
|
|
|
/// The deployer's transaction, exposed for overriding the defaults
|
2021-08-09 00:50:38 +00:00
|
|
|
pub tx: TypedTransaction,
|
2020-06-15 08:46:07 +00:00
|
|
|
abi: Abi,
|
2020-09-24 21:33:09 +00:00
|
|
|
client: Arc<M>,
|
2020-05-30 20:04:08 +00:00
|
|
|
confs: usize,
|
2020-06-21 22:08:40 +00:00
|
|
|
block: BlockNumber,
|
2020-05-30 20:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:33:22 +00:00
|
|
|
impl<M> Clone for Deployer<M> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Deployer {
|
|
|
|
tx: self.tx.clone(),
|
|
|
|
abi: self.abi.clone(),
|
|
|
|
client: self.client.clone(),
|
|
|
|
confs: self.confs,
|
|
|
|
block: self.block,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
impl<M: Middleware> Deployer<M> {
|
2020-06-10 18:20:47 +00:00
|
|
|
/// Sets the number of confirmations to wait for the contract deployment transaction
|
2020-05-30 20:04:08 +00:00
|
|
|
pub fn confirmations<T: Into<usize>>(mut self, confirmations: T) -> Self {
|
|
|
|
self.confs = confirmations.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-06-21 22:08:40 +00:00
|
|
|
pub fn block<T: Into<BlockNumber>>(mut self, block: T) -> Self {
|
|
|
|
self.block = block.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-08-09 00:50:38 +00:00
|
|
|
/// Uses a Legacy transaction instead of an EIP-1559 one to do the deployment
|
|
|
|
pub fn legacy(mut self) -> Self {
|
|
|
|
self.tx = match self.tx {
|
|
|
|
TypedTransaction::Eip1559(inner) => {
|
|
|
|
let tx: TransactionRequest = inner.into();
|
|
|
|
TypedTransaction::Legacy(tx)
|
|
|
|
}
|
|
|
|
other => other,
|
|
|
|
};
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-11-04 13:02:05 +00:00
|
|
|
/// Dry runs the deployment of the contract
|
|
|
|
///
|
|
|
|
/// Note: this function _does not_ send a transaction from your account
|
|
|
|
pub async fn call(&self) -> Result<(), ContractError<M>> {
|
|
|
|
self.client
|
|
|
|
.call(&self.tx, Some(self.block.into()))
|
|
|
|
.await
|
|
|
|
.map_err(ContractError::MiddlewareError)?;
|
|
|
|
|
|
|
|
// TODO: It would be nice to handle reverts in a structured way.
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-06-04 21:18:25 +00:00
|
|
|
/// Returns a CallBuilder, which when awaited executes the deployment of this contract via
|
|
|
|
/// `eth_call`. This call resolves to the returned data which would have been stored at the
|
|
|
|
/// destination address had the deploy transaction been executed via `send()`.
|
|
|
|
///
|
|
|
|
/// Note: this function _does not_ send a transaction from your account
|
|
|
|
pub fn call_raw(&self) -> CallBuilder<'_, M::Provider> {
|
|
|
|
self.client.provider().call_raw(&self.tx).block(self.block.into())
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:20:47 +00:00
|
|
|
/// Broadcasts the contract deployment transaction and after waiting for it to
|
2020-06-20 13:55:07 +00:00
|
|
|
/// be sufficiently confirmed (default: 1), it returns a [`Contract`](crate::Contract)
|
2020-06-10 18:20:47 +00:00
|
|
|
/// struct at the deployed contract's address.
|
2020-09-24 21:33:09 +00:00
|
|
|
pub async fn send(self) -> Result<Contract<M>, ContractError<M>> {
|
2022-02-05 14:37:09 +00:00
|
|
|
let (contract, _) = self.send_with_receipt().await?;
|
|
|
|
Ok(contract)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Broadcasts the contract deployment transaction and after waiting for it to
|
|
|
|
/// be sufficiently confirmed (default: 1), it returns a tuple with
|
|
|
|
/// the [`Contract`](crate::Contract) struct at the deployed contract's address
|
|
|
|
/// and the corresponding [`TransactionReceipt`](ethers_core::types::TransactionReceipt).
|
|
|
|
pub async fn send_with_receipt(
|
|
|
|
self,
|
|
|
|
) -> Result<(Contract<M>, TransactionReceipt), ContractError<M>> {
|
2020-12-17 11:26:01 +00:00
|
|
|
let pending_tx = self
|
2020-06-21 22:08:40 +00:00
|
|
|
.client
|
2021-03-16 19:46:07 +00:00
|
|
|
.send_transaction(self.tx, Some(self.block.into()))
|
2020-09-24 21:33:09 +00:00
|
|
|
.await
|
|
|
|
.map_err(ContractError::MiddlewareError)?;
|
|
|
|
|
|
|
|
// TODO: Should this be calculated "optimistically" by address/nonce?
|
2020-12-17 11:26:01 +00:00
|
|
|
let receipt = pending_tx
|
2020-06-22 08:44:08 +00:00
|
|
|
.confirmations(self.confs)
|
2020-09-24 21:33:09 +00:00
|
|
|
.await
|
2021-07-06 08:06:18 +00:00
|
|
|
.map_err(|_| ContractError::ContractNotDeployed)?
|
|
|
|
.ok_or(ContractError::ContractNotDeployed)?;
|
2021-10-29 12:29:35 +00:00
|
|
|
let address = receipt.contract_address.ok_or(ContractError::ContractNotDeployed)?;
|
2020-05-30 20:04:08 +00:00
|
|
|
|
2020-06-15 08:46:07 +00:00
|
|
|
let contract = Contract::new(address, self.abi.clone(), self.client);
|
2022-02-05 14:37:09 +00:00
|
|
|
Ok((contract, receipt))
|
2020-05-30 20:04:08 +00:00
|
|
|
}
|
2020-06-02 10:36:02 +00:00
|
|
|
|
2020-06-10 18:20:47 +00:00
|
|
|
/// Returns a reference to the deployer's ABI
|
2020-06-02 10:36:02 +00:00
|
|
|
pub fn abi(&self) -> &Abi {
|
|
|
|
&self.abi
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:20:47 +00:00
|
|
|
/// Returns a reference to the deployer's client
|
2020-09-24 21:33:09 +00:00
|
|
|
pub fn client(&self) -> &M {
|
2020-06-02 10:36:02 +00:00
|
|
|
&self.client
|
|
|
|
}
|
2020-05-30 20:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 18:20:47 +00:00
|
|
|
/// To deploy a contract to the Ethereum network, a `ContractFactory` can be
|
|
|
|
/// created which manages the Contract bytecode and Application Binary Interface
|
|
|
|
/// (ABI), usually generated from the Solidity compiler.
|
|
|
|
///
|
|
|
|
/// Once the factory's deployment transaction is mined with sufficient confirmations,
|
2020-06-20 13:55:07 +00:00
|
|
|
/// the [`Contract`](crate::Contract) object is returned.
|
2020-06-10 18:20:47 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```no_run
|
2021-10-31 11:34:51 +00:00
|
|
|
/// use ethers_solc::Solc;
|
2021-08-28 21:06:29 +00:00
|
|
|
/// use ethers_contract::ContractFactory;
|
|
|
|
/// use ethers_providers::{Provider, Http};
|
|
|
|
/// use ethers_signers::Wallet;
|
2020-06-10 18:20:47 +00:00
|
|
|
/// use std::convert::TryFrom;
|
|
|
|
///
|
|
|
|
/// # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// // first we'll compile the contract (you can alternatively compile it yourself
|
|
|
|
/// // and pass the ABI/Bytecode
|
2021-10-31 11:34:51 +00:00
|
|
|
/// let compiled = Solc::default().compile_source("./tests/contract.sol").unwrap();
|
2020-06-10 18:20:47 +00:00
|
|
|
/// let contract = compiled
|
2021-10-31 11:34:51 +00:00
|
|
|
/// .get("./tests/contract.sol", "SimpleStorage")
|
2020-06-10 18:20:47 +00:00
|
|
|
/// .expect("could not find contract");
|
|
|
|
///
|
|
|
|
/// // connect to the network
|
2020-09-24 21:33:09 +00:00
|
|
|
/// let client = Provider::<Http>::try_from("http://localhost:8545").unwrap();
|
|
|
|
/// let client = std::sync::Arc::new(client);
|
2020-06-10 18:20:47 +00:00
|
|
|
///
|
|
|
|
/// // create a factory which will be used to deploy instances of the contract
|
2021-12-08 00:38:29 +00:00
|
|
|
/// let factory = ContractFactory::new(contract.abi.unwrap().clone(), contract.bytecode().unwrap().clone(), client);
|
2020-06-10 18:20:47 +00:00
|
|
|
///
|
|
|
|
/// // The deployer created by the `deploy` call exposes a builder which gets consumed
|
|
|
|
/// // by the async `send` call
|
|
|
|
/// let contract = factory
|
|
|
|
/// .deploy("initial value".to_string())?
|
|
|
|
/// .confirmations(0usize)
|
|
|
|
/// .send()
|
|
|
|
/// .await?;
|
|
|
|
/// println!("{}", contract.address());
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
2022-04-27 12:33:22 +00:00
|
|
|
#[derive(Debug)]
|
2020-09-24 21:33:09 +00:00
|
|
|
pub struct ContractFactory<M> {
|
|
|
|
client: Arc<M>,
|
2020-06-15 08:46:07 +00:00
|
|
|
abi: Abi,
|
|
|
|
bytecode: Bytes,
|
2020-05-30 14:24:50 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 12:33:22 +00:00
|
|
|
impl<M> Clone for ContractFactory<M> {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ContractFactory {
|
|
|
|
client: self.client.clone(),
|
|
|
|
abi: self.abi.clone(),
|
|
|
|
bytecode: self.bytecode.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
impl<M: Middleware> ContractFactory<M> {
|
2020-06-10 18:20:47 +00:00
|
|
|
/// Creates a factory for deployment of the Contract with bytecode, and the
|
|
|
|
/// constructor defined in the abi. The client will be used to send any deployment
|
|
|
|
/// transaction.
|
2020-09-24 21:33:09 +00:00
|
|
|
pub fn new(abi: Abi, bytecode: Bytes, client: Arc<M>) -> Self {
|
2021-10-29 12:29:35 +00:00
|
|
|
Self { client, abi, bytecode }
|
2020-05-30 14:24:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 22:01:00 +00:00
|
|
|
pub fn deploy_tokens(self, params: Vec<Token>) -> Result<Deployer<M>, ContractError<M>> {
|
2020-05-30 20:04:08 +00:00
|
|
|
// Encode the constructor args & concatenate with the bytecode if necessary
|
|
|
|
let data: Bytes = match (self.abi.constructor(), params.is_empty()) {
|
2021-10-29 12:29:35 +00:00
|
|
|
(None, false) => return Err(ContractError::ConstructorError),
|
2020-05-30 20:04:08 +00:00
|
|
|
(None, true) => self.bytecode.clone(),
|
2021-10-29 12:29:35 +00:00
|
|
|
(Some(constructor), _) => {
|
|
|
|
constructor.encode_input(self.bytecode.to_vec(), ¶ms)?.into()
|
|
|
|
}
|
2020-05-30 20:04:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// create the tx object. Since we're deploying a contract, `to` is `None`
|
2021-08-09 00:50:38 +00:00
|
|
|
// We default to EIP-1559 transactions, but the sender can convert it back
|
|
|
|
// to a legacy one
|
|
|
|
#[cfg(feature = "legacy")]
|
2021-10-29 12:29:35 +00:00
|
|
|
let tx = TransactionRequest { to: None, data: Some(data), ..Default::default() };
|
2021-08-09 00:50:38 +00:00
|
|
|
#[cfg(not(feature = "legacy"))]
|
2021-10-29 12:29:35 +00:00
|
|
|
let tx = Eip1559TransactionRequest { to: None, data: Some(data), ..Default::default() };
|
2021-08-09 00:50:38 +00:00
|
|
|
let tx = tx.into();
|
2020-05-30 20:04:08 +00:00
|
|
|
|
|
|
|
Ok(Deployer {
|
2020-06-22 08:44:08 +00:00
|
|
|
client: Arc::clone(&self.client), // cheap clone behind the arc
|
2020-05-30 20:04:08 +00:00
|
|
|
abi: self.abi,
|
|
|
|
tx,
|
|
|
|
confs: 1,
|
2020-06-21 22:08:40 +00:00
|
|
|
block: BlockNumber::Latest,
|
2020-05-30 20:04:08 +00:00
|
|
|
})
|
2020-05-30 14:24:50 +00:00
|
|
|
}
|
2021-12-16 22:01:00 +00:00
|
|
|
|
|
|
|
/// Constructs the deployment transaction based on the provided constructor
|
|
|
|
/// arguments and returns a `Deployer` instance. You must call `send()` in order
|
|
|
|
/// to actually deploy the contract.
|
|
|
|
///
|
|
|
|
/// Notes:
|
|
|
|
/// 1. If there are no constructor arguments, you should pass `()` as the argument.
|
|
|
|
/// 1. The default poll duration is 7 seconds.
|
|
|
|
/// 1. The default number of confirmations is 1 block.
|
|
|
|
pub fn deploy<T: Tokenize>(self, constructor_args: T) -> Result<Deployer<M>, ContractError<M>> {
|
|
|
|
self.deploy_tokens(constructor_args.into_tokens())
|
|
|
|
}
|
2020-05-30 14:24:50 +00:00
|
|
|
}
|