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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use anyhow::{bail, Result};
use forc_pkg::{BuildOptions, Compiled, ManifestFile};
use fuel_crypto::Signature;
use fuel_gql_client::client::FuelClient;
use fuel_tx::{Output, Salt, StorageSlot, Transaction};
use fuel_vm::prelude::*;
use fuels_core::constants::{BASE_ASSET_ID, DEFAULT_SPENDABLE_COIN_AMOUNT};
use fuels_signers::{provider::Provider, wallet::Wallet};
use fuels_types::bech32::Bech32Address;
use std::{io::Write, path::PathBuf, str::FromStr};
use sway_core::TreeType;
use sway_utils::constants::DEFAULT_NODE_URL;
use tracing::info;
use crate::ops::{deploy::cmd::DeployCommand, parameters::TxParameters};
pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
let curr_dir = if let Some(ref path) = command.path {
PathBuf::from(path)
} else {
std::env::current_dir()?
};
let manifest = ManifestFile::from_dir(&curr_dir)?;
manifest.check_program_type(vec![TreeType::Contract])?;
let DeployCommand {
path,
print_ast,
print_finalized_asm,
print_intermediate_asm,
print_ir,
binary_outfile,
debug_outfile,
offline_mode,
silent_mode,
output_directory,
minify_json_abi,
minify_json_storage_slots,
locked,
url,
build_profile,
release,
time_phases,
unsigned,
gas_limit,
gas_price,
} = command;
let build_options = BuildOptions {
path,
print_ast,
print_finalized_asm,
print_intermediate_asm,
print_ir,
binary_outfile,
offline_mode,
debug_outfile,
silent_mode,
output_directory,
minify_json_abi,
minify_json_storage_slots,
locked,
build_profile,
release,
time_phases,
};
let compiled = forc_pkg::build_with_options(build_options)?;
let node_url = match &manifest.network {
Some(network) => &network.url,
_ => DEFAULT_NODE_URL,
};
let node_url = url.unwrap_or_else(|| node_url.to_string());
let client = FuelClient::new(node_url)?;
let (mut tx, contract_id) = if unsigned {
create_contract_tx(
compiled.bytecode,
Vec::<fuel_tx::Input>::new(),
Vec::<fuel_tx::Output>::new(),
compiled.storage_slots,
)
} else {
let mut wallet_address = String::new();
print!(
"Please provide the address of the wallet you are going to sign this transaction with:"
);
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut wallet_address)?;
let address = Bech32Address::from_str(wallet_address.trim())?;
let locked_wallet = Wallet::from_address(address, Some(Provider::new(client.clone())));
let tx_parameters = TxParameters::new(gas_limit, gas_price);
create_signed_contract_tx(compiled, locked_wallet, tx_parameters).await?
};
if !unsigned {
let mut signature = String::new();
print!("Please provide the signature for this transaction:");
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut signature)?;
let signature = Signature::from_str(signature.trim())?;
let witness = vec![Witness::from(signature.as_ref())];
let mut witnesses: Vec<Witness> = tx.witnesses().to_vec();
match witnesses.len() {
0 => tx.set_witnesses(witness),
_ => {
witnesses.extend(witness);
tx.set_witnesses(witnesses)
}
}
}
match client.submit(&tx).await {
Ok(logs) => {
info!("Logs:\n{:?}", logs);
Ok(contract_id)
}
Err(e) => bail!("{e}"),
}
}
async fn create_signed_contract_tx(
compiled_contract: Compiled,
signer_wallet: Wallet,
tx_parameters: TxParameters,
) -> Result<(Transaction, fuel_tx::ContractId)> {
let maturity = 0;
let bytecode_witness_index = 0;
let witnesses = vec![compiled_contract.bytecode.clone().into()];
let salt = Salt::new([0; 32]);
let contract = Contract::from(compiled_contract.bytecode);
let root = contract.root();
let mut storage_slots = compiled_contract.storage_slots;
storage_slots.sort();
let state_root = Contract::initial_state_root(storage_slots.iter());
let contract_id = contract.id(&salt, &root, &state_root);
info!("Contract id: 0x{}", hex::encode(contract_id));
let outputs: Vec<Output> = vec![
Output::contract_created(contract_id, state_root),
Output::change(signer_wallet.address().into(), 0, BASE_ASSET_ID),
];
let coin_witness_index = 1;
let inputs = signer_wallet
.get_asset_inputs_for_amount(
AssetId::default(),
DEFAULT_SPENDABLE_COIN_AMOUNT,
coin_witness_index,
)
.await?;
let tx = Transaction::create(
tx_parameters.gas_price,
tx_parameters.gas_limit,
maturity,
bytecode_witness_index,
salt,
storage_slots,
inputs,
outputs,
witnesses,
);
println!("Tx id to sign {}", tx.id());
Ok((tx, contract_id))
}
fn create_contract_tx(
compiled_contract: Vec<u8>,
inputs: Vec<Input>,
outputs: Vec<Output>,
storage_slots: Vec<StorageSlot>,
) -> (Transaction, fuel_tx::ContractId) {
let gas_price = 0;
let gas_limit = fuel_tx::ConsensusParameters::default().max_gas_per_tx;
let maturity = 0;
let bytecode_witness_index = 0;
let witnesses = vec![compiled_contract.clone().into()];
let salt = Salt::new([0; 32]);
let contract = Contract::from(compiled_contract);
let root = contract.root();
let mut storage_slots = storage_slots;
storage_slots.sort();
let state_root = Contract::initial_state_root(storage_slots.iter());
let id = contract.id(&salt, &root, &state_root);
info!("Contract id: 0x{}", hex::encode(id));
let outputs = [
&[Output::ContractCreated {
contract_id: id,
state_root,
}],
&outputs[..],
]
.concat();
(
Transaction::create(
gas_price,
gas_limit,
maturity,
bytecode_witness_index,
salt,
storage_slots,
inputs,
outputs,
witnesses,
),
id,
)
}