Skip to content

Commit 696cb57

Browse files
authored
minor: Migrate forc-call node_url (#6920)
## Description Migrated the `node_url` function; addressing the todo. Also refactored the function to ease readability and maintainability. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: z <zees-dev@users.noreply.github.com>
1 parent 8788257 commit 696cb57

File tree

8 files changed

+180
-167
lines changed

8 files changed

+180
-167
lines changed

docs/book/src/forc/plugins/forc_client/forc_call.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@ Here are a few examples of what you can do with `forc call`:
1010

1111
Call a simple addition function on a deployed contract (in dry-run mode):
1212

13+
```sway
14+
contract;
15+
16+
abi ContractABI {
17+
fn add(a: u64, b: u64) -> u64;
18+
}
19+
20+
impl ContractABI for Contract {
21+
fn add(a: u64, b: u64) -> u64 {
22+
a + b
23+
}
24+
}
25+
```
26+
1327
```bash
1428
forc call 0xe18de7c7c8c61a1c706dccb3533caa00ba5c11b5230da4428582abf1b6831b4d \
1529
--abi ./out/debug/counter-contract-abi.json \
1630
add 1 2
1731
```
1832

19-
Query the owner of a deployed DEX contract on testnet:
33+
Query the owner of a deployed [DEX contract](https://github.com/mira-amm/mira-v1-core) on testnet:
2034

2135
```bash
2236
forc call \
@@ -236,7 +250,7 @@ forc call <CONTRACT_ID> --abi <PATH> <FUNCTION> --max-fee 5000
236250

237251
### Common Use Cases
238252

239-
- 1. Contract State Queries
253+
#### Contract State Queries
240254

241255
```sh
242256
# Read contract state
@@ -246,7 +260,7 @@ forc call <CONTRACT_ID> --abi <PATH> get_balance
246260
forc call <CONTRACT_ID> --abi <PATH> get_user_info 0x1234...
247261
```
248262

249-
- 2. Token Operations
263+
#### Token Operations
250264

251265
```sh
252266
# Check token balance
@@ -256,7 +270,7 @@ forc call <CONTRACT_ID> --abi <PATH> balance_of 0x1234...
256270
forc call <CONTRACT_ID> --abi <PATH> transfer 0x1234... 100 --live
257271
```
258272

259-
- 3. Contract Administration
273+
#### Contract Administration
260274

261275
```sh
262276
# Check contract owner

forc-plugins/forc-client/src/lib.rs

+147
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod op;
44
pub mod util;
55

66
use clap::Parser;
7+
use forc_pkg::manifest::Network;
78
use serde::{Deserialize, Serialize};
89
use util::target::Target;
910

@@ -46,6 +47,40 @@ pub struct NodeTarget {
4647
}
4748

4849
impl NodeTarget {
50+
/// Returns the URL to use for connecting to Fuel Core node.
51+
pub fn get_node_url(&self, manifest_network: &Option<Network>) -> anyhow::Result<String> {
52+
let options_count = [
53+
self.mainnet,
54+
self.testnet,
55+
self.devnet,
56+
self.target.is_some(),
57+
self.node_url.is_some(),
58+
]
59+
.iter()
60+
.filter(|&&x| x)
61+
.count();
62+
63+
// ensure at most one option is specified
64+
if options_count > 1 {
65+
anyhow::bail!("Only one of `--mainnet`, `--testnet`, `--devnet`, `--target`, or `--node-url` should be specified");
66+
}
67+
68+
let node_url = match () {
69+
_ if self.mainnet => Target::mainnet().target_url(),
70+
_ if self.testnet => Target::testnet().target_url(),
71+
_ if self.devnet => Target::devnet().target_url(),
72+
_ if self.target.is_some() => self.target.as_ref().unwrap().target_url(),
73+
_ if self.node_url.is_some() => self.node_url.as_ref().unwrap().clone(),
74+
_ => manifest_network
75+
.as_ref()
76+
.map(|nw| &nw.url[..])
77+
.unwrap_or(crate::constants::NODE_URL)
78+
.to_string(),
79+
};
80+
81+
Ok(node_url)
82+
}
83+
4984
/// Returns the URL for explorer
5085
pub fn get_explorer_url(&self) -> Option<String> {
5186
match (
@@ -118,4 +153,116 @@ mod tests {
118153
let actual = node.get_explorer_url();
119154
assert_eq!(None, actual);
120155
}
156+
157+
#[test]
158+
fn test_get_node_url_testnet() {
159+
let node = NodeTarget {
160+
target: None,
161+
node_url: None,
162+
mainnet: false,
163+
testnet: true,
164+
devnet: false,
165+
};
166+
167+
let actual = node.get_node_url(&None).unwrap();
168+
assert_eq!("https://testnet.fuel.network", actual);
169+
}
170+
171+
#[test]
172+
fn test_get_node_url_mainnet() {
173+
let node = NodeTarget {
174+
target: None,
175+
node_url: None,
176+
mainnet: true,
177+
testnet: false,
178+
devnet: false,
179+
};
180+
181+
let actual = node.get_node_url(&None).unwrap();
182+
assert_eq!("https://mainnet.fuel.network", actual);
183+
}
184+
185+
#[test]
186+
fn test_get_node_url_target_mainnet() {
187+
let node = NodeTarget {
188+
target: Some(Target::Mainnet),
189+
node_url: None,
190+
mainnet: false,
191+
testnet: false,
192+
devnet: false,
193+
};
194+
let actual = node.get_node_url(&None).unwrap();
195+
assert_eq!("https://mainnet.fuel.network", actual);
196+
}
197+
198+
#[test]
199+
fn test_get_node_url_target_testnet() {
200+
let node = NodeTarget {
201+
target: Some(Target::Testnet),
202+
node_url: None,
203+
mainnet: false,
204+
testnet: false,
205+
devnet: false,
206+
};
207+
208+
let actual = node.get_node_url(&None).unwrap();
209+
assert_eq!("https://testnet.fuel.network", actual);
210+
}
211+
212+
#[test]
213+
fn test_get_node_url_default() {
214+
let node = NodeTarget {
215+
target: None,
216+
node_url: None,
217+
mainnet: false,
218+
testnet: false,
219+
devnet: false,
220+
};
221+
222+
let actual = node.get_node_url(&None).unwrap();
223+
assert_eq!("http://127.0.0.1:4000", actual);
224+
}
225+
226+
#[test]
227+
fn test_get_node_url_local() {
228+
let node = NodeTarget {
229+
target: Some(Target::Local),
230+
node_url: None,
231+
mainnet: false,
232+
testnet: false,
233+
devnet: false,
234+
};
235+
let actual = node.get_node_url(&None).unwrap();
236+
assert_eq!("http://127.0.0.1:4000", actual);
237+
}
238+
239+
#[test]
240+
#[should_panic(
241+
expected = "Only one of `--mainnet`, `--testnet`, `--devnet`, `--target`, or `--node-url` should be specified"
242+
)]
243+
fn test_get_node_url_local_testnet() {
244+
let node = NodeTarget {
245+
target: Some(Target::Local),
246+
node_url: None,
247+
mainnet: false,
248+
testnet: true,
249+
devnet: false,
250+
};
251+
node.get_node_url(&None).unwrap();
252+
}
253+
254+
#[test]
255+
#[should_panic(
256+
expected = "Only one of `--mainnet`, `--testnet`, `--devnet`, `--target`, or `--node-url` should be specified"
257+
)]
258+
fn test_get_node_url_same_url() {
259+
let node = NodeTarget {
260+
target: Some(Target::Testnet),
261+
node_url: Some("testnet.fuel.network".to_string()),
262+
mainnet: false,
263+
testnet: false,
264+
devnet: false,
265+
};
266+
node.get_node_url(&None).unwrap();
267+
}
121268
}

forc-plugins/forc-client/src/op/call/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ use crate::{
88
missing_contracts::get_missing_contracts,
99
parser::{param_type_val_to_token, token_to_string},
1010
},
11-
util::{
12-
node_url::get_node_url,
13-
tx::{prompt_forc_wallet_password, select_local_wallet_account},
14-
},
11+
util::tx::{prompt_forc_wallet_password, select_local_wallet_account},
1512
};
1613
use anyhow::{anyhow, bail, Result};
1714
use either::Either;
@@ -55,7 +52,7 @@ pub async fn call(cmd: cmd::Call) -> anyhow::Result<String> {
5552
external_contracts,
5653
output,
5754
} = cmd;
58-
let node_url = get_node_url(&node, &None)?;
55+
let node_url = node.get_node_url(&None)?;
5956
let provider: Provider = Provider::connect(node_url).await?;
6057

6158
let wallet = get_wallet(caller.signing_key, caller.wallet, provider).await?;

forc-plugins/forc-client/src/op/deploy.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::{
33
constants::TX_SUBMIT_TIMEOUT_MS,
44
util::{
55
account::ForcClientAccount,
6-
node_url::get_node_url,
76
pkg::{built_pkgs, create_proxy_contract, update_proxy_address_in_manifest},
87
target::Target,
98
tx::{
@@ -635,7 +634,9 @@ pub async fn deploy_contracts(
635634
let bytecode_size = pkg.bytecode.bytes.len();
636635
let deployed_contract_id = if bytecode_size > MAX_CONTRACT_SIZE {
637636
// Deploy chunked
638-
let node_url = get_node_url(&command.node, &pkg.descriptor.manifest_file.network)?;
637+
let node_url = command
638+
.node
639+
.get_node_url(&pkg.descriptor.manifest_file.network)?;
639640
let provider = Provider::connect(node_url).await?;
640641

641642
deploy_chunked(
@@ -980,9 +981,14 @@ async fn validate_and_get_node_url(
980981
command: &cmd::Deploy,
981982
packages: &[Arc<BuiltPackage>],
982983
) -> Result<String> {
983-
let node_url = get_node_url(&command.node, &packages[0].descriptor.manifest_file.network)?;
984+
let node_url = command
985+
.node
986+
.get_node_url(&packages[0].descriptor.manifest_file.network)?;
984987
if !packages.iter().all(|pkg| {
985-
get_node_url(&command.node, &pkg.descriptor.manifest_file.network).ok()
988+
command
989+
.node
990+
.get_node_url(&pkg.descriptor.manifest_file.network)
991+
.ok()
986992
== Some(node_url.clone())
987993
}) {
988994
bail!("All packages in a deployment should be deployed to the same node. Please ensure that the network specified in the Forc.toml files of all packages is the same.");

forc-plugins/forc-client/src/op/run/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::{
33
cmd,
44
constants::TX_SUBMIT_TIMEOUT_MS,
55
util::{
6-
node_url::get_node_url,
76
pkg::built_pkgs,
87
tx::{prompt_forc_wallet_password, select_account, SignerSelectionMode},
98
},
@@ -101,7 +100,7 @@ pub async fn run_pkg(
101100
compiled: &BuiltPackage,
102101
signer_mode: &SignerSelectionMode,
103102
) -> Result<RanScript> {
104-
let node_url = get_node_url(&command.node, &manifest.network)?;
103+
let node_url = command.node.get_node_url(&manifest.network)?;
105104
let provider = Provider::connect(node_url.clone()).await?;
106105
let tx_count = 1;
107106
let account = select_account(

forc-plugins/forc-client/src/op/submit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::{cmd, util::node_url::get_node_url};
1+
use crate::cmd;
22
use anyhow::Context;
33
use fuel_core_client::client::{types::TransactionStatus, FuelClient};
44
use fuel_crypto::fuel_types::canonical::Deserialize;
55

66
/// A command for submitting transactions to a Fuel network.
77
pub async fn submit(cmd: cmd::Submit) -> anyhow::Result<()> {
88
let tx = read_tx(&cmd.tx_path)?;
9-
let node_url = get_node_url(&cmd.network.node, &None)?;
9+
let node_url = cmd.network.node.get_node_url(&None)?;
1010
let client = FuelClient::new(node_url)?;
1111
if cmd.network.await_ {
1212
let status = client
-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub mod account;
22
pub mod aws;
33
pub(crate) mod encode;
4-
pub(crate) mod node_url;
54
pub(crate) mod pkg;
65
pub(crate) mod target;
76
pub mod tx;

0 commit comments

Comments
 (0)