Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/cli/burn.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CommonOpt;
use crate::cli::utils::check_required_files;
use crate::cli::{get_swap_calldata, utils::check_required_files};
use alloy::primitives::utils::parse_ether;
use anyhow::Result;
use structopt::StructOpt;
Expand All @@ -24,6 +24,12 @@ impl BurnOpt {
let fee = parse_ether(&self.fee)?;
let spend = parse_ether(&self.spend)?;

// let receiver_hook = get_swap_calldata(
// parse_ether("0.0001").unwrap(),
// self.common_opt.private_key.address(),
// );
let receiver_hook = Vec::new();

let (
burn_key,
burn_addr,
Expand All @@ -32,7 +38,10 @@ impl BurnOpt {
remaining_coin_val,
remaining_coin_u256,
burn_extra_commit,
) = self.common_opt.prepare_inputs(amount, fee, spend).await?;
) = self
.common_opt
.prepare_inputs(amount, fee, spend, receiver_hook.clone().into())
.await?;

let (_tx_hash, _ok) = self.common_opt.send_burn_tx(burn_addr, amount).await?;
self.common_opt.persist_burn_data(
Expand Down Expand Up @@ -65,6 +74,7 @@ impl BurnOpt {
remaining_coin_u256,
fee,
spend,
receiver_hook.into(),
)
.await?;

Expand Down
99 changes: 85 additions & 14 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ mod info;
mod ls;
mod mine;
mod participate;
mod recover;
mod spend;
mod utils;
use crate::cli::utils::{append_new_entry, burn_file, coins_file, init_coins_file, next_id};
Expand All @@ -16,8 +15,10 @@ use crate::utils::{
compute_nullifier, compute_previous_coin, compute_remaining_coin, fetch_block_and_header_bytes,
find_burn_key, generate_burn_address, get_account_proof,
};
use alloy::primitives::Bytes;
use alloy::consensus::Receipt;
use alloy::primitives::{Bytes, U160, address};
use alloy::signers::local::PrivateKeySigner;
use alloy::sol_types::{SolCall, SolValue};
use alloy::{
hex::ToHexExt,
network::TransactionBuilder,
Expand Down Expand Up @@ -56,6 +57,51 @@ pub struct RuntimeContext<P: Provider> {
pub provider: P,
}

use alloy::dyn_abi::DynSolValue;
use alloy::primitives::I256;
use alloy::{hex, sol};

sol! {
interface IUniswapV3Pool {
/// swap function
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
}
}

/// Generate calldata for Uniswap V3 pool swap (BETH -> ETH)
fn get_swap_calldata(amount_in: U256, recipient: Address) -> Vec<u8> {
let zero_for_one = false;
let amount_specified = I256::from_raw(amount_in);

let sqrt_price_limit_x96 = if zero_for_one {
U160::from_str_radix("4295128741", 10).unwrap()
} else {
U160::from_str_radix("1461446703485210103287273052203988822378723970340", 10).unwrap()
};
let data = Vec::new();
let swap_call = IUniswapV3Pool::swapCall {
recipient,
zeroForOne: zero_for_one,
amountSpecified: amount_specified,
sqrtPriceLimitX96: sqrt_price_limit_x96,
data: data.into(),
}
.abi_encode();

(
address!("0x646b5eB499411390448b5e21838aCB8B2FF548dA"),
amount_in,
swap_call,
)
.abi_encode_params()
}

impl CommonOpt {
pub fn overridden_network(&self) -> Result<Network, anyhow::Error> {
let mut net = NETWORKS
Expand Down Expand Up @@ -97,6 +143,7 @@ impl CommonOpt {
remaining_coin: U256,
fee: U256,
spend: U256,
swap_calldata: Bytes,
) -> Result<()> {
let rt = self.setup().await?; // get provider, wallet, network from self
println!("Broadcasting mint transaction...");
Expand All @@ -105,7 +152,7 @@ impl CommonOpt {
let beth = BETH::new(net.beth, rt.provider);

// call the zk-proof mintCoin(...) method
let receipt = beth
let pending_tx = beth
.mintCoin(
// pi_a
[proof.proof.pi_a[0], proof.proof.pi_a[1]],
Expand All @@ -128,19 +175,39 @@ impl CommonOpt {
rt.wallet_address,
U256::ZERO,
rt.wallet_address,
Bytes::new(),
swap_calldata,
Bytes::new(),
)
.send()
.await?
.get_receipt()
.await?;

if receipt.status() {
println!("Success!");
} else {
println!("Transaction failed!");
.await;
match pending_tx {
Ok(pending) => {
// transaction mined successfully
let receipt = pending.get_receipt().await?;
if receipt.status() {
println!("Success!");
} else {
println!("Transaction failed!");
}
}
Err(err) => {
// transaction reverted, err may contain revert data
if let Some(revert_bytes) = err.as_revert_data() {
// revert_bytes: Vec<u8> — ABI encoded
println!("Revert data (raw): 0x{}", hex::encode(revert_bytes.clone()));

// decode revert reason as string
if let Ok(reason) =
ethers::abi::decode(&[ethers::abi::ParamType::String], &revert_bytes)
{
println!("Revert reason: {}", reason[0].to_string());
}
} else {
println!("Transaction failed without revert data: {:?}", err);
}
}
}

Ok(())
}
pub async fn broadcast_spend(
Expand Down Expand Up @@ -211,6 +278,7 @@ impl CommonOpt {
amount: U256,
fee: U256,
spend: U256,
receiver_hook: Bytes,
) -> Result<(Fp, Address, Fp, U256, Fp, U256, U256)> {
let rt = self.setup().await?;

Expand All @@ -225,7 +293,8 @@ impl CommonOpt {

// 1) burn_key
println!("Generating a burn-key...");
let extra_commit = generate_burn_extra_commit(rt.wallet_address, U256::ZERO, fee);
let extra_commit =
generate_burn_extra_commit(rt.wallet_address, U256::ZERO, fee, receiver_hook.clone());
let burn_key = find_burn_key(2, extra_commit, spend);
println!("Your burn_key: {:?}", burn_key);
println!(
Expand All @@ -242,6 +311,7 @@ impl CommonOpt {
U256::ZERO,
fee,
spend,
receiver_hook.clone(),
);

// 3) nullifier (Fp only needed by caller)
Expand All @@ -267,6 +337,7 @@ impl CommonOpt {
burn_key: Fp,
fee: U256,
reveal: U256,
receiver_hook: Bytes,
) -> anyhow::Result<(alloy::primitives::Address, Fp)> {
let rt = self.setup().await?; // wallet addr + provider + network
let burn_addr_prefix = crate::constants::poseidon_burn_address_prefix();
Expand All @@ -278,6 +349,7 @@ impl CommonOpt {
U256::ZERO,
fee,
reveal,
receiver_hook,
);
let (nullifier_fp, _nullifier_u256) = compute_nullifier(burn_key);
Ok((burn_addr, nullifier_fp))
Expand Down Expand Up @@ -521,5 +593,4 @@ pub use ls::LsCommand;
pub use ls::LsOpt;
pub use mine::MineOpt;
pub use participate::ParticipateOpt;
pub use recover::RecoverOpt;
pub use spend::SpendOpt;
164 changes: 0 additions & 164 deletions src/cli/recover.rs

This file was deleted.

Loading
Loading