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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use anyhow::{anyhow, bail, Result};
use forc_pkg::{fuel_core_not_running, BuildOptions, ManifestFile};
use fuel_gql_client::client::FuelClient;
use fuel_tx::Transaction;
use futures::TryFutureExt;
use std::{path::PathBuf, str::FromStr};
use sway_core::TreeType;
use tracing::info;
use crate::ops::{parameters::TxParameters, run::cmd::RunCommand};
pub const NODE_URL: &str = "http://127.0.0.1:4000";
pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
let path_dir = if let Some(path) = &command.path {
PathBuf::from(path)
} else {
std::env::current_dir().map_err(|e| anyhow!("{:?}", e))?
};
let manifest = ManifestFile::from_dir(&path_dir)?;
manifest.check_program_type(vec![TreeType::Script])?;
let input_data = &command.data.unwrap_or_else(|| "".into());
let data = format_hex_data(input_data);
let script_data = hex::decode(data).expect("Invalid hex");
let build_options = BuildOptions {
path: command.path,
print_ast: command.print_ast,
print_finalized_asm: command.print_finalized_asm,
print_intermediate_asm: command.print_intermediate_asm,
print_ir: command.print_ir,
binary_outfile: command.binary_outfile,
debug_outfile: command.debug_outfile,
offline_mode: false,
silent_mode: command.silent_mode,
output_directory: command.output_directory,
minify_json_abi: command.minify_json_abi,
minify_json_storage_slots: command.minify_json_storage_slots,
locked: command.locked,
build_profile: None,
release: false,
time_phases: command.time_phases,
};
let compiled = forc_pkg::build_with_options(build_options)?;
let contracts = command.contract.unwrap_or_default();
let (inputs, outputs) = get_tx_inputs_and_outputs(contracts);
let tx = create_tx_with_script_and_data(
compiled.bytecode,
script_data,
inputs,
outputs,
TxParameters::new(command.gas_limit, command.gas_price),
);
let node_url = command.node_url.unwrap_or_else(|| match &manifest.network {
Some(network) => network.url.to_owned(),
None => NODE_URL.to_owned(),
});
if command.dry_run {
info!("{:?}", tx);
Ok(vec![])
} else {
try_send_tx(&node_url, &tx, command.pretty_print, command.simulate).await
}
}
async fn try_send_tx(
node_url: &str,
tx: &Transaction,
pretty_print: bool,
simulate: bool,
) -> Result<Vec<fuel_tx::Receipt>> {
let client = FuelClient::new(node_url)?;
match client.health().await {
Ok(_) => send_tx(&client, tx, pretty_print, simulate).await,
Err(_) => Err(fuel_core_not_running(node_url)),
}
}
async fn send_tx(
client: &FuelClient,
tx: &Transaction,
pretty_print: bool,
simulate: bool,
) -> Result<Vec<fuel_tx::Receipt>> {
let id = format!("{:#x}", tx.id());
let outputs = {
if !simulate {
client
.submit(tx)
.and_then(|_| client.receipts(id.as_str()))
.await
} else {
client
.dry_run(tx)
.and_then(|_| client.receipts(id.as_str()))
.await
}
};
match outputs {
Ok(logs) => {
print_receipt_output(&logs, pretty_print)?;
Ok(logs)
}
Err(e) => bail!("{e}"),
}
}
fn create_tx_with_script_and_data(
script: Vec<u8>,
script_data: Vec<u8>,
inputs: Vec<fuel_tx::Input>,
outputs: Vec<fuel_tx::Output>,
tx_params: TxParameters,
) -> Transaction {
let gas_price = tx_params.gas_price;
let gas_limit = tx_params.gas_limit;
let maturity = 0;
let witnesses = vec![];
Transaction::script(
gas_price,
gas_limit,
maturity,
script,
script_data,
inputs,
outputs,
witnesses,
)
}
fn format_hex_data(data: &str) -> &str {
data.strip_prefix("0x").unwrap_or(data)
}
fn construct_input_from_contract((_idx, contract): (usize, &String)) -> fuel_tx::Input {
fuel_tx::Input::Contract {
utxo_id: fuel_tx::UtxoId::new(fuel_tx::Bytes32::zeroed(), 0),
balance_root: fuel_tx::Bytes32::zeroed(),
state_root: fuel_tx::Bytes32::zeroed(),
tx_pointer: fuel_tx::TxPointer::new(0, 0),
contract_id: fuel_tx::ContractId::from_str(contract).unwrap(),
}
}
fn construct_output_from_contract((idx, _contract): (usize, &String)) -> fuel_tx::Output {
fuel_tx::Output::Contract {
input_index: idx as u8, balance_root: fuel_tx::Bytes32::zeroed(),
state_root: fuel_tx::Bytes32::zeroed(),
}
}
fn get_tx_inputs_and_outputs(
contracts: Vec<String>,
) -> (Vec<fuel_tx::Input>, Vec<fuel_tx::Output>) {
let inputs = contracts
.iter()
.enumerate()
.map(construct_input_from_contract)
.collect::<Vec<_>>();
let outputs = contracts
.iter()
.enumerate()
.map(construct_output_from_contract)
.collect::<Vec<_>>();
(inputs, outputs)
}
fn print_receipt_output(receipts: &Vec<fuel_tx::Receipt>, pretty_print: bool) -> Result<()> {
let mut receipt_to_json_array = serde_json::to_value(&receipts)?;
for (rec_index, receipt) in receipts.iter().enumerate() {
let rec_value = receipt_to_json_array.get_mut(rec_index).ok_or_else(|| {
anyhow!(
"Serialized receipts does not contain {} th index",
rec_index
)
})?;
match receipt {
fuel_tx::Receipt::LogData { data, .. } => {
if let Some(v) = rec_value.pointer_mut("/LogData/data") {
*v = hex::encode(data).into();
}
}
fuel_tx::Receipt::ReturnData { data, .. } => {
if let Some(v) = rec_value.pointer_mut("/ReturnData/data") {
*v = hex::encode(data).into();
}
}
_ => {}
}
}
if pretty_print {
info!("{}", serde_json::to_string_pretty(&receipt_to_json_array)?);
} else {
info!("{}", serde_json::to_string(&receipt_to_json_array)?);
}
Ok(())
}