Skip to content

Commit

Permalink
Support eip1559 (#723)
Browse files Browse the repository at this point in the history
This is an update of #638 fixing the mergine conflicts.

Co-authored-by: sunce86 <[email protected]>
  • Loading branch information
vkgnosis and sunce86 authored Feb 18, 2022
1 parent 25e5258 commit 71548b2
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 364 deletions.
2 changes: 1 addition & 1 deletion ethcontract-mock/src/details/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl MockTransport {
root: None,
logs_bloom: Default::default(),
transaction_type: None,
effective_gas_price: Some(U256::from(1)),
effective_gas_price: Some(tx.gas_price),
};

state.receipts.insert(tx.hash, receipt);
Expand Down
3 changes: 3 additions & 0 deletions ethcontract-mock/src/details/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,8 @@ pub fn verify(raw_tx: &[u8], node_chain_id: u64) -> Transaction {
value,
data,
hash: signing::keccak256(raw_tx).into(),
transaction_type: 0,
max_fee_per_gas: Default::default(),
max_priority_fee_per_gas: Default::default(),
}
}
3 changes: 3 additions & 0 deletions ethcontract-mock/src/details/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub struct Transaction {
pub value: U256,
pub data: Vec<u8>,
pub hash: H256,
pub transaction_type: u64,
pub max_fee_per_gas: U256,
pub max_priority_fee_per_gas: U256,
}

/// Transaction execution result.
Expand Down
4 changes: 2 additions & 2 deletions ethcontract/src/contract/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ mod tests {
.expect("error creating deploy builder")
.from(Account::Local(from, None))
.gas(1.into())
.gas_price(2.into())
.gas_price(2.0.into())
.value(28.into())
.nonce(42.into())
.into_inner();

assert_eq!(tx.from.map(|a| a.address()), Some(from));
assert_eq!(tx.to, None);
assert_eq!(tx.gas, Some(1.into()));
assert_eq!(tx.gas_price, Some(2.into()));
assert_eq!(tx.gas_price, Some(2.0.into()));
assert_eq!(tx.value, Some(28.into()));
assert_eq!(tx.data, Some(bytecode.to_bytes().unwrap()));
assert_eq!(tx.nonce, Some(42.into()));
Expand Down
1 change: 1 addition & 0 deletions ethcontract/src/contract/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ mod tests {
"gasUsed": "0x1337",
"logsBloom": H2048::zero(),
"logs": [],
"effectiveGasPrice": "0x0",
}));
// get latest block
transport.add_response(json!(U64::from(20)));
Expand Down
24 changes: 15 additions & 9 deletions ethcontract/src/contract/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,25 @@ impl<T: Transport, R: Tokenize> ViewMethodBuilder<T, R> {
}

fn decompose(self) -> (Function, CallRequest, Option<BlockId>) {
let resolved_gas_price = self
.m
.tx
.gas_price
.map(|gas_price| gas_price.resolve_for_transaction())
.unwrap_or_default();
(
self.m.function,
CallRequest {
from: self.m.tx.from.map(|account| account.address()),
to: Some(self.m.tx.to.unwrap_or_default()),
gas: self.m.tx.gas,
gas_price: self.m.tx.gas_price.and_then(|gas_price| gas_price.value()),
gas_price: resolved_gas_price.gas_price,
value: self.m.tx.value,
data: self.m.tx.data,
transaction_type: None,
transaction_type: resolved_gas_price.transaction_type,
access_list: None,
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
},
self.block,
)
Expand Down Expand Up @@ -315,15 +321,15 @@ mod tests {
let tx = MethodBuilder::<_, U256>::new(web3, function, address, data.clone())
.from(Account::Local(from, None))
.gas(1.into())
.gas_price(2.into())
.gas_price(2.0.into())
.value(28.into())
.nonce(42.into())
.into_inner();

assert_eq!(tx.from.map(|a| a.address()), Some(from));
assert_eq!(tx.to, Some(address));
assert_eq!(tx.gas, Some(1.into()));
assert_eq!(tx.gas_price, Some(2.into()));
assert_eq!(tx.gas_price, Some(2.0.into()));
assert_eq!(tx.value, Some(28.into()));
assert_eq!(tx.data, Some(data));
assert_eq!(tx.nonce, Some(42.into()));
Expand All @@ -346,7 +352,7 @@ mod tests {
))
.from(from)
.gas(1.into())
.gas_price(2.into())
.gas_price(2.0.into())
.value(28.into())
.block(BlockId::Number(100.into()));

Expand Down Expand Up @@ -415,13 +421,13 @@ mod tests {
.with_defaults(&MethodDefaults {
from: Some(Account::Local(from, None)),
gas: Some(1.into()),
gas_price: Some(2.into()),
gas_price: Some(2.0.into()),
})
.into_inner();

assert_eq!(tx.from.map(|a| a.address()), Some(from));
assert_eq!(tx.gas, Some(1.into()));
assert_eq!(tx.gas_price, Some(2.into()));
assert_eq!(tx.gas_price, Some(2.0.into()));
transport.assert_no_more_requests();
}
}
25 changes: 10 additions & 15 deletions ethcontract/src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,16 @@ impl Debug for PrivateKey {
// Taken from rust-web3's signing.rs.
impl Key for &'_ PrivateKey {
fn sign(&self, message: &[u8], chain_id: Option<u64>) -> Result<Signature, SigningError> {
let message = Message::from_slice(message).map_err(|_| SigningError::InvalidMessage)?;
let (recovery_id, signature) = Secp256k1::signing_only()
.sign_ecdsa_recoverable(&message, self)
.serialize_compact();

let standard_v = recovery_id.to_i32() as u64;
let v = if let Some(chain_id) = chain_id {
standard_v + 35 + chain_id * 2
} else {
standard_v + 27
};
let r = H256::from_slice(&signature[..32]);
let s = H256::from_slice(&signature[32..]);

Ok(Signature { v, r, s })
let signature = self.sign_message(message)?;

Ok(Signature {
v: if let Some(chain_id) = chain_id {
signature.v + 35 + chain_id * 2
} else {
signature.v + 27
},
..signature
})
}

fn sign_message(&self, message: &[u8]) -> Result<Signature, SigningError> {
Expand Down
25 changes: 14 additions & 11 deletions ethcontract/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ pub struct TransactionBuilder<T: Transport> {
pub to: Option<Address>,
/// Optional gas amount to use for transaction. Defaults to estimated gas.
pub gas: Option<U256>,
/// Optional gas price to use for transaction. Defaults to estimated gas
/// price from the node (i.e. `GasPrice::Standard`).
/// Optional gas price to use for transaction. Defaults to None.
pub gas_price: Option<GasPrice>,
/// The ETH value to send with the transaction. Defaults to 0.
pub value: Option<U256>,
Expand Down Expand Up @@ -183,22 +182,24 @@ impl<T: Transport> TransactionBuilder<T> {
/// Estimate the gas required for this transaction.
pub async fn estimate_gas(self) -> Result<U256, ExecutionError> {
let from = self.from.map(|account| account.address());
let gas_price = self.gas_price.and_then(|gas_price| gas_price.value());

let resolved_gas_price = self
.gas_price
.map(|gas_price| gas_price.resolve_for_transaction())
.unwrap_or_default();
self.web3
.eth()
.estimate_gas(
CallRequest {
from,
to: self.to,
gas: None,
gas_price,
gas_price: resolved_gas_price.gas_price,
value: self.value,
data: self.data.clone(),
transaction_type: None,
transaction_type: resolved_gas_price.transaction_type,
access_list: None,
max_fee_per_gas: None,
max_priority_fee_per_gas: None,
max_fee_per_gas: resolved_gas_price.max_fee_per_gas,
max_priority_fee_per_gas: resolved_gas_price.max_priority_fee_per_gas,
},
None,
)
Expand Down Expand Up @@ -255,7 +256,7 @@ mod tests {
.from(Account::Local(from, Some(TransactionCondition::Block(100))))
.to(to)
.gas(1.into())
.gas_price(2.into())
.gas_price(2.0.into())
.value(28.into())
.data(Bytes(vec![0x13, 0x37]))
.nonce(42.into())
Expand Down Expand Up @@ -311,13 +312,14 @@ mod tests {
"logsBloom": H2048::zero(),
"logs": [],
"status": "0x1",
"effectiveGasPrice": "0x0",
}));

let builder = TransactionBuilder::new(web3)
.from(Account::Offline(key, Some(chain_id)))
.to(Address::zero())
.gas(0x1337.into())
.gas_price(0x00ba_b10c.into())
.gas_price(f64::from(0x00ba_b10c).into())
.nonce(0x42.into())
.confirmations(1);
let tx_raw = builder
Expand Down Expand Up @@ -364,13 +366,14 @@ mod tests {
"gasUsed": "0x1337",
"logsBloom": H2048::zero(),
"logs": [],
"effectiveGasPrice": "0x0",
}));

let builder = TransactionBuilder::new(web3)
.from(Account::Offline(key, Some(chain_id)))
.to(Address::zero())
.gas(0x1337.into())
.gas_price(0x00ba_b10c.into())
.gas_price(f64::from(0x00ba_b10c).into())
.nonce(0x42.into());
let tx_raw = builder
.clone()
Expand Down
Loading

0 comments on commit 71548b2

Please sign in to comment.