-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add execution v1 or v3 as a parameter (#42)
* feat: add execution v1 or v3 as a parameter * fix: fix typo
- Loading branch information
Showing
10 changed files
with
218 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/// Execution version of Starknet transactions. | ||
/// The version of transaction to be executed. | ||
#[derive(Debug, Clone, Copy, Default)] | ||
pub enum ExecutionVersion { | ||
/// Execute the transaction using the `execute_v1` method, where fees are only payable in WEI. | ||
#[default] | ||
V1, | ||
/// Execute the transaction using the `execute_v3` method, where fees are payable in WEI or FRI. | ||
V3, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct ParseExecutionVersionError { | ||
invalid_value: String, | ||
} | ||
|
||
impl std::fmt::Display for ParseExecutionVersionError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"Invalid execution version '{}'. Supported values are 'v1', 'V1', 'v3', or 'V3'.", | ||
self.invalid_value | ||
) | ||
} | ||
} | ||
|
||
impl std::error::Error for ParseExecutionVersionError {} | ||
|
||
impl std::str::FromStr for ExecutionVersion { | ||
type Err = ParseExecutionVersionError; | ||
|
||
fn from_str(input: &str) -> Result<ExecutionVersion, Self::Err> { | ||
match input { | ||
"v1" | "V1" => Ok(ExecutionVersion::V1), | ||
"v3" | "V3" => Ok(ExecutionVersion::V3), | ||
_ => Err(ParseExecutionVersionError { | ||
invalid_value: input.to_string(), | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use cainome::rs::abigen; | ||
use starknet::{ | ||
accounts::{ExecutionEncoding, SingleOwnerAccount}, | ||
core::types::Felt, | ||
providers::{jsonrpc::HttpTransport, AnyProvider, JsonRpcClient}, | ||
signers::{LocalWallet, SigningKey}, | ||
}; | ||
use std::sync::Arc; | ||
use url::Url; | ||
|
||
// To run this example, please first run `make setup_simple_get_set` in the contracts directory with a Katana running. This will declare and deploy the testing contract. | ||
|
||
const CONTRACT_ADDRESS: &str = "0x007997dd654f2c079597a6c461489ee89981d0df733b8bcd3525153b0e700f98"; | ||
const KATANA_ACCOUNT_0: &str = "0x6162896d1d7ab204c7ccac6dd5f8e9e7c25ecd5ae4fcb4ad32e57786bb46e03"; | ||
const KATANA_PRIVKEY_0: &str = "0x1800000000300000180000000000030000000000003006001800006600"; | ||
const KATANA_CHAIN_ID: &str = "0x4b4154414e41"; | ||
|
||
// You can load of the sierra class entirely from the artifact. | ||
// Or you can use the extracted abi entries with jq in contracts/abi/. | ||
abigen!( | ||
MyContract, | ||
"./contracts/target/dev/contracts_simple_get_set.contract_class.json", | ||
execution_version("V3"), | ||
); | ||
//abigen!(MyContract, "./contracts/abi/simple_get_set.abi.json"); | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let rpc_url = Url::parse("http://0.0.0.0:5050").expect("Expecting Starknet RPC URL"); | ||
let provider = | ||
AnyProvider::JsonRpcHttp(JsonRpcClient::new(HttpTransport::new(rpc_url.clone()))); | ||
|
||
let contract_address = Felt::from_hex(CONTRACT_ADDRESS).unwrap(); | ||
|
||
// If you only plan to call views functions, you can use the `Reader`, which | ||
// only requires a provider along with your contract address. | ||
let contract = MyContractReader::new(contract_address, &provider); | ||
|
||
// To call a view, there is no need to initialize an account. You can directly | ||
// use the name of the method in the ABI and then use the `call()` method. | ||
let a = contract | ||
.get_a() | ||
.call() | ||
.await | ||
.expect("Call to `get_a` failed"); | ||
println!("a initial value: {:?}", a); | ||
|
||
// If you want to do some invoke for external functions, you must use an account. | ||
let signer = LocalWallet::from(SigningKey::from_secret_scalar( | ||
Felt::from_hex(KATANA_PRIVKEY_0).unwrap(), | ||
)); | ||
let address = Felt::from_hex(KATANA_ACCOUNT_0).unwrap(); | ||
|
||
let account = Arc::new(SingleOwnerAccount::new( | ||
provider, | ||
signer, | ||
address, | ||
Felt::from_hex(KATANA_CHAIN_ID).unwrap(), | ||
ExecutionEncoding::New, | ||
)); | ||
|
||
// A `Contract` exposes all the methods of the ABI, which includes the views (as the `ContractReader`) and | ||
// the externals (sending transaction). | ||
let contract = MyContract::new(contract_address, account); | ||
|
||
// The transaction is actually sent when `send()` is called. | ||
// You can before that configure the fees, or even only run an estimation of the | ||
// fees without actually sending the transaction. | ||
let _tx_res = contract | ||
.set_a(&(a + Felt::ONE)) | ||
.gas_estimate_multiplier(1.2) | ||
.send() | ||
.await | ||
.expect("Call to `set_a` failed"); | ||
|
||
// In production code, you want to poll the transaction status. | ||
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; | ||
|
||
let a = contract | ||
.get_a() | ||
.call() | ||
.await | ||
.expect("Call to `get_a` failed"); | ||
println!("a after invoke: {:?}", a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.