Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial interface for contract calls #9

Merged
merged 5 commits into from Nov 24, 2021
Merged

Conversation

digorithm
Copy link
Member

@digorithm digorithm commented Nov 23, 2021

Introduces an interface contract calls. Right now it only performs very basic calls to contracts and scripts deployment + execution (scripts not related to contracts).

Contract calls can be done like this:

let (_, contract_id) = Contract::launch_and_deploy(&compiled_contract, true)
      .await
      .unwrap();

  // Call contract
  let utxo_id = Bytes32::from([0u8; 32]);
  let balance_root = Bytes32::from([0u8; 32]);
  let state_root = Bytes32::from([0u8; 32]);
  let gas_price = 0;
  let gas_limit = 1_000_000;
  let maturity = 0;
  let input_index = 0;

  let receipts = Contract::call(
      contract_id,
      utxo_id,
      balance_root,
      state_root,
      input_index,
      gas_price,
      gas_limit,
      maturity,
  )
  .await
  .unwrap();

  assert_eq!(true, receipts.len() > 0);

And simple script execution from within the SDK, like this:

// Compile the script we will be calling
  let compiled =
      Script::compile_sway_script("../fuels-abigen-macro/tests/test_projects/simple_script")
          .unwrap();

  let tx = Transaction::Script {
      gas_price: 0,
      gas_limit: 1_000_000,
      maturity: 0,
      receipts_root: Default::default(),
      script: compiled.raw, // Here we pass the compiled script into the transaction
      script_data: vec![],
      inputs: vec![],
      outputs: vec![],
      witnesses: vec![vec![].into()],
      metadata: None,
  };

  let script = Script::new(tx, constants::DEFAULT_NODE_URL.to_string());

  let result = script.call().await.unwrap();

You can jump straight to fuels-rs/tests/calls.rs to see how it works.

The next step is to bind this interface to the functions generated by the abigen! and craft script_data on the fly in order to call specific functions from a deployed contract.

Closes #4.

@digorithm digorithm added the enhancement New feature or request label Nov 23, 2021
@digorithm digorithm self-assigned this Nov 23, 2021
@digorithm digorithm marked this pull request as ready for review November 23, 2021 23:52
fuels-rs/Cargo.toml Outdated Show resolved Hide resolved
fuels-rs/src/contract.rs Show resolved Hide resolved
fuels-rs/src/contract.rs Outdated Show resolved Hide resolved
AlicanC
AlicanC previously approved these changes Nov 24, 2021
Copy link
Contributor

@AlicanC AlicanC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

SilentCicero
SilentCicero previously approved these changes Nov 24, 2021
fuels-abigen-macro/tests/harness.rs Outdated Show resolved Hide resolved
fuels-abigen-macro/tests/harness.rs Outdated Show resolved Hide resolved
Comment on lines +657 to +658
let salt: [u8; 32] = rng.gen();
let salt = Salt::from(salt);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let salt: [u8; 32] = rng.gen();
let salt = Salt::from(salt);
let salt: Salt = rng.gen();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried that before doing the ugly way, was getting this:

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will figure this out on the next PR I'll be submitting soon, need to merge this 😅.

@digorithm digorithm merged commit 5f0c9ac into master Nov 24, 2021
@digorithm digorithm deleted the rodrigo/basic-calls branch November 24, 2021 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create interface to craft and send transactions to a deployed contract
5 participants