-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathestimate_gas_consumption.rs
More file actions
61 lines (55 loc) · 1.9 KB
/
estimate_gas_consumption.rs
File metadata and controls
61 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use lib::{NewOperationGroup, NewTransactionParameters, Address};
use lib::api::{RunOperation, RunOperationError, RunOperationContents};
#[derive(PartialEq, Debug, Clone)]
pub struct OperationGroupGasConsumption {
pub reveal: Option<u64>,
pub transaction: Option<u64>,
pub delegation: Option<u64>,
}
impl OperationGroupGasConsumption {
pub fn total(&self) -> u64 {
self.reveal.unwrap_or(0)
+ self.transaction.unwrap_or(0)
+ self.delegation.unwrap_or(0)
}
}
fn find_consumed_gas_for_kind(
kind: &str,
run_op_contents: &RunOperationContents,
) -> Option<u64> {
run_op_contents.iter()
.find(|op| op.kind.as_str() == kind)
// Add 100 for safety
.map(|op| op.consumed_gas + 100)
}
pub fn estimate_gas_consumption<A>(
op: &NewOperationGroup,
api: &A,
) -> Result<OperationGroupGasConsumption, RunOperationError>
where A: RunOperation + ?Sized,
{
let op_results = api.run_operation(op)?;
// additional gas required when sending/delegating from Smart Contract (KT1).
let tx_additional_gas = op.transaction.as_ref()
.map(|op| {
use NewTransactionParameters::*;
match op.parameters.as_ref() {
Some(Transfer { to, .. }) => {
match to {
Address::Implicit(_) => 1427,
Address::Originated(_) => 2863,
}
}
Some(SetDelegate(_)) => 1000,
Some(CancelDelegate) => 1000,
None => 0,
}
})
.unwrap_or(0);
Ok(OperationGroupGasConsumption {
reveal: find_consumed_gas_for_kind("reveal", &op_results),
transaction: find_consumed_gas_for_kind("transaction", &op_results)
.map(|gas| gas + tx_additional_gas),
delegation: find_consumed_gas_for_kind("delegation", &op_results),
})
}