-
Notifications
You must be signed in to change notification settings - Fork 86
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
test: deploy account on katana via beerus, scarb and starkli #827
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod constants; | ||
pub mod katana; | ||
pub mod scarb; | ||
pub mod starkli; | ||
pub mod utils; |
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,195 @@ | ||
use std::io::Write; | ||
|
||
use anyhow::{anyhow, Error}; | ||
use clap::Parser; | ||
use starkli::{ | ||
account::{ | ||
AccountConfig, AccountVariant, DeploymentStatus, OzAccountConfig, | ||
UndeployedStatus, | ||
}, | ||
signer::AnySigner, | ||
utils::{Cli, Subcommands}, | ||
}; | ||
use starknet::{ | ||
core::types::{contract::SierraClass, PriceUnit}, | ||
signers::{LocalWallet, Signer, SigningKey}, | ||
}; | ||
use starknet_crypto::Felt; | ||
|
||
#[allow(dead_code)] | ||
pub struct Starkli { | ||
pub rpc: String, | ||
account_folder: String, | ||
prefunded_account: String, | ||
persist_logger: bool, | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub enum PreFundedAccount { | ||
Katana, | ||
Sepolia, | ||
} | ||
|
||
const ACCOUNT: &str = "account.json"; | ||
const COMPILED_ACCOUNT: &str = "target/dev/account_Account.contract_class.json"; | ||
const KEY: &str = "key.json"; | ||
const PASSWORD: &str = "password"; | ||
|
||
#[allow(dead_code)] | ||
impl Starkli { | ||
pub fn new( | ||
rpc: &str, | ||
account_folder: &str, | ||
prefunded_account: PreFundedAccount, | ||
) -> Self { | ||
let prefunded_account = match prefunded_account { | ||
PreFundedAccount::Katana => "katana-0".to_string(), | ||
PreFundedAccount::Sepolia => "TODO".to_string(), | ||
}; | ||
Self { | ||
rpc: rpc.into(), | ||
account_folder: account_folder.into(), | ||
prefunded_account, | ||
persist_logger: false, | ||
} | ||
} | ||
|
||
pub fn create_keystore(&self) -> Result<SigningKey, Error> { | ||
let key = SigningKey::from_random(); | ||
let key_file = self.account_folder.clone() + KEY; | ||
key.save_as_keystore(key_file, PASSWORD)?; | ||
Ok(key) | ||
} | ||
|
||
pub fn extract_class_hash(&self) -> Result<Felt, Error> { | ||
let compiled = self.account_folder.clone() + COMPILED_ACCOUNT; | ||
let class = serde_json::from_reader::<_, SierraClass>( | ||
std::fs::File::open(compiled)?, | ||
)?; | ||
Ok(class.class_hash()?) | ||
} | ||
|
||
pub async fn create_account( | ||
&self, | ||
key: SigningKey, | ||
class_hash: Felt, | ||
) -> Result<Felt, Error> { | ||
let signer = AnySigner::LocalWallet(LocalWallet::from_signing_key(key)); | ||
let salt = SigningKey::from_random().secret_scalar(); | ||
let account_config = AccountConfig { | ||
version: 1, | ||
variant: AccountVariant::OpenZeppelin(OzAccountConfig { | ||
version: 1, | ||
public_key: signer.get_public_key().await?.scalar(), | ||
legacy: false, | ||
}), | ||
deployment: DeploymentStatus::Undeployed(UndeployedStatus { | ||
class_hash, | ||
salt, | ||
context: None, | ||
}), | ||
}; | ||
let target_deployment_address = | ||
account_config.deploy_account_address()?; | ||
let mut file = | ||
std::fs::File::create(self.account_folder.clone() + ACCOUNT)?; | ||
serde_json::to_writer_pretty(&mut file, &account_config)?; | ||
file.write_all(b"\n")?; | ||
Ok(target_deployment_address) | ||
} | ||
|
||
pub async fn declare_account(&mut self) -> Result<(), Error> { | ||
let compiled_contract = self.account_folder.clone() + COMPILED_ACCOUNT; | ||
let rpc = self.rpc.clone(); | ||
let account = self.prefunded_account.clone(); | ||
let input = vec![ | ||
"starkli", | ||
"declare", | ||
&compiled_contract, | ||
"--compiler-version", | ||
"2.8.2", | ||
"--rpc", | ||
&rpc, | ||
"--account", | ||
&account, | ||
]; | ||
self.run_command(input).await | ||
} | ||
|
||
pub async fn invoke_eth_transfer( | ||
&mut self, | ||
to_address: Felt, | ||
amount: u64, | ||
unit: PriceUnit, | ||
) -> Result<(), Error> { | ||
let address = &format!("{:#064x}", to_address); | ||
let amount = &format!("u256:{amount}"); | ||
let unit = match unit { | ||
PriceUnit::Wei => "--eth", | ||
PriceUnit::Fri => "--strk", | ||
}; | ||
let rpc = self.rpc.clone(); | ||
let account = self.prefunded_account.clone(); | ||
let input = vec![ | ||
"starkli", | ||
"invoke", | ||
unit, | ||
"eth", | ||
"transfer", | ||
address, | ||
amount, | ||
"--rpc", | ||
&rpc, | ||
"--account", | ||
&account, | ||
]; | ||
self.run_command(input).await | ||
} | ||
|
||
pub async fn deploy_account(&mut self) -> Result<(), Error> { | ||
let account = self.account_folder.clone() + "account.json"; | ||
let key = self.account_folder.clone() + "key.json"; | ||
let rpc = self.rpc.clone(); | ||
let input = vec![ | ||
"starkli", | ||
"account", | ||
"deploy", | ||
&account, | ||
"--rpc", | ||
&rpc, | ||
"--keystore", | ||
&key, | ||
"--keystore-password", | ||
"password", | ||
"--skip-manual-confirmation", | ||
]; | ||
self.run_command(input).await | ||
} | ||
|
||
async fn run_command(&mut self, mut input: Vec<&str>) -> Result<(), Error> { | ||
if !self.persist_logger { | ||
self.persist_logger = true; | ||
} else { | ||
input.push("--persist-logger"); | ||
} | ||
starkli::utils::run_command(Cli::parse_from(input)).await | ||
} | ||
|
||
pub async fn call( | ||
&self, | ||
address: Felt, | ||
func: &str, | ||
) -> Result<Vec<Felt>, Error> { | ||
let address = &format!("{:#064x}", address); | ||
let input = vec!["starkli", "call", address, func, "--rpc", &self.rpc]; | ||
let cli = Cli::parse_from(input); | ||
let cmd = match cli.command { | ||
Some(command) => match command { | ||
Subcommands::Call(cmd) => cmd, | ||
_ => return Err(anyhow!("Wrong subcommand")), | ||
}, | ||
None => return Err(anyhow!("Wrong command")), | ||
}; | ||
cmd.call().await | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this TODO is not being addressed in this PR: let's maybe fail fast here, e.g. with
unimplemented!()
.