Skip to content

Commit

Permalink
Merge branch 'main' into pla-807
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-chainflip authored Sep 27, 2023
2 parents 2732bbe + 25d2e4d commit a0a0ff6
Show file tree
Hide file tree
Showing 106 changed files with 3,532 additions and 1,886 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tree --no-default-features --depth 1 --edges=features,normal
# - RUSTSEC-2023-0053: This advisory comes from rustls-webpki, a dependency of ethers-rs. CPU denial of service in certificate path building.
# - RUSTSEC-2021-0060: This is a transitive dependency of libp2p and will be fixed in an upcoming release.
# - RUSTSEC-2021-0059: This is a transitive dependency of libp2p and will be fixed in an upcoming release.
# - RUSTSEC-2023-0063: This is a transitive dependency of libp2p and it is not used.
cf-audit = '''
audit --ignore RUSTSEC-2022-0061
--ignore RUSTSEC-2020-0071
Expand All @@ -47,4 +48,5 @@ audit --ignore RUSTSEC-2022-0061
--ignore RUSTSEC-2023-0053
--ignore RUSTSEC-2021-0060
--ignore RUSTSEC-2021-0059
--ignore RUSTSEC-2023-0063
'''
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions api/bin/chainflip-broker-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use tracing::log;
#[derive(Serialize, Deserialize, Clone)]
pub struct BrokerSwapDepositAddress {
pub address: String,
pub expiry_block: BlockNumber,
pub issued_block: BlockNumber,
pub channel_id: ChannelId,
}
Expand All @@ -33,7 +32,6 @@ impl From<chainflip_api::SwapDepositAddress> for BrokerSwapDepositAddress {
fn from(value: chainflip_api::SwapDepositAddress) -> Self {
Self {
address: value.address,
expiry_block: value.expiry_block,
issued_block: value.issued_block,
channel_id: value.channel_id,
}
Expand Down
93 changes: 80 additions & 13 deletions api/bin/chainflip-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn run_cli() -> Result<()> {
let api = StateChainApi::connect(scope, cli_settings.state_chain).await?;
match command_line_opts.cmd {
Broker(BrokerSubcommands::RequestSwapDepositAddress(params)) => {
let SwapDepositAddress { address, expiry_block, .. } = api
let SwapDepositAddress { address, .. } = api
.broker_api()
.request_swap_deposit_address(
params.source_asset,
Expand All @@ -71,7 +71,6 @@ async fn run_cli() -> Result<()> {
)
.await?;
println!("Deposit Address: {address}");
println!("Address expires at block {expiry_block}");
},
LiquidityProvider(
LiquidityProviderSubcommands::RequestLiquidityDepositAddress { asset },
Expand All @@ -87,15 +86,21 @@ async fn run_cli() -> Result<()> {
api.lp_api().register_liquidity_refund_address(lra_address).await?;
println!("Liquidity Refund address registered. Tx hash: {tx_hash}");
},
Redeem { amount, eth_address, executor } => {
request_redemption(api, amount, eth_address, executor).await?;
Redeem { amount, eth_address, executor_address } => {
request_redemption(api, amount, eth_address, executor_address).await?;
},
BindRedeemAddress { eth_address } => {
bind_redeem_address(api.operator_api(), &eth_address).await?;
},
BindExecutorAddress { eth_address } => {
bind_executor_address(api.operator_api(), &eth_address).await?;
},
GetBoundRedeemAddress {} => {
get_bound_redeem_address(api.query_api()).await?;
},
GetBoundExecutorAddress {} => {
get_bound_executor_address(api.query_api()).await?;
},
RegisterAccountRole { role } => {
println!(
"Submitting `register-account-role` with role: {role:?}. This cannot be reversed for your account.",
Expand Down Expand Up @@ -134,22 +139,23 @@ async fn run_cli() -> Result<()> {
async fn request_redemption(
api: StateChainApi,
amount: Option<f64>,
supplied_address: Option<String>,
executor: Option<cf_chains::eth::Address>,
supplied_redeem_address: Option<String>,
supplied_executor_address: Option<String>,
) -> Result<()> {
let supplied_address = if let Some(address) = supplied_address {
let account_id = api.state_chain_client.account_id();

// Check the bound redeem address for this account
let supplied_redeem_address = if let Some(address) = supplied_redeem_address {
Some(EthereumAddress::from(
clean_hex_address::<[u8; 20]>(&address).context("Invalid ETH address supplied")?,
))
} else {
None
};

let account_id = api.state_chain_client.account_id();
let bound_address =
let bound_redeem_address =
api.query_api().get_bound_redeem_address(None, Some(account_id.clone())).await?;

let redeem_address = match (supplied_address, bound_address) {
let redeem_address = match (supplied_redeem_address, bound_redeem_address) {
(Some(supplied_address), Some(bound_address)) =>
if supplied_address != bound_address {
bail!("Supplied ETH address `{supplied_address:?}` does not match bound address for this account `{bound_address:?}`.");
Expand All @@ -165,6 +171,35 @@ async fn request_redemption(
bail!("No redeem address supplied and no bound redeem address found for your account {account_id}."),
};

// Check the bound executor address for this account
let supplied_executor_address = if let Some(address) = supplied_executor_address {
Some(EthereumAddress::from(
clean_hex_address::<[u8; 20]>(&address).context("Invalid ETH address supplied")?,
))
} else {
None
};
let bound_executor_address = api
.query_api()
.get_bound_executor_address(None, Some(account_id.clone()))
.await?;

let executor_address = match (bound_executor_address, supplied_executor_address) {
(Some(bound_address), Some(supplied_address)) =>
if bound_address != supplied_address {
bail!("Supplied executor address `{supplied_address:?}` does not match bound address for this account `{bound_address:?}`.");
} else {
Some(supplied_address)
},
(Some(bound_address), None) => {
println!("Using bound executor address {bound_address}.");
Some(bound_address)
},
(None, Some(executor)) => Some(executor),
(None, None) => None,
};

// Calculate the redemption amount
let amount = match amount {
Some(amount_float) => {
let atomic_amount = (amount_float * 10_f64.powi(18)) as u128;
Expand All @@ -186,7 +221,10 @@ async fn request_redemption(
return Ok(())
}

let tx_hash = api.operator_api().request_redemption(amount, redeem_address, executor).await?;
let tx_hash = api
.operator_api()
.request_redemption(amount, redeem_address, executor_address)
.await?;

println!(
"Your redemption request has transaction hash: `{tx_hash:#x}`. View your redemption's progress on the funding app."
Expand All @@ -209,7 +247,26 @@ async fn bind_redeem_address(api: Arc<impl OperatorApi + Sync>, eth_address: &st

let tx_hash = api.bind_redeem_address(eth_address).await?;

println!("Account bound to address {eth_address}, transaction hash: `{tx_hash:#x}`.");
println!("Account bound to redeem address {eth_address}, transaction hash: `{tx_hash:#x}`.");

Ok(())
}

async fn bind_executor_address(api: Arc<impl OperatorApi + Sync>, eth_address: &str) -> Result<()> {
let eth_address = EthereumAddress::from(
clean_hex_address::<[u8; 20]>(eth_address).context("Invalid ETH address supplied")?,
);

println!(
"Binding your account to an executor address is irreversible. You will only ever be able to execute registered redemptions with this address: {eth_address:?}.",
);
if !confirm_submit() {
return Ok(())
}

let tx_hash = api.bind_executor_address(eth_address).await?;

println!("Account bound to executor address {eth_address}, transaction hash: `{tx_hash:#x}`.");

Ok(())
}
Expand All @@ -224,6 +281,16 @@ async fn get_bound_redeem_address(api: QueryApi) -> Result<()> {
Ok(())
}

async fn get_bound_executor_address(api: QueryApi) -> Result<()> {
if let Some(bound_address) = api.get_bound_executor_address(None, None).await? {
println!("Your account is bound to executor address: {bound_address:?}");
} else {
println!("Your account is not bound to any executor address.");
}

Ok(())
}

fn confirm_submit() -> bool {
use std::{io, io::*};

Expand Down
17 changes: 14 additions & 3 deletions api/bin/chainflip-cli/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,28 @@ pub enum CliCommand {
)]
eth_address: Option<String>,
#[clap(
help = "Optional executor. If specified, only this address will be able to execute the redemption."
help = "Optional executor address. If specified, only this address will be able to execute the redemption."
)]
executor: Option<cf_chains::eth::Address>,
executor_address: Option<String>,
},
#[clap(about = "Restricts your account to only be able to redeem to the specified address")]
#[clap(
about = "Irreversible action that restricts your account to only be able to redeem to the specified address"
)]
BindRedeemAddress {
#[clap(help = "The Ethereum address you wish to bind your account to")]
eth_address: String,
},
#[clap(
about = "Irreversible action that restricts your account to only be able to execute registered redemptions with the specified address"
)]
BindExecutorAddress {
#[clap(help = "The Ethereum address you wish to bind your account to")]
eth_address: String,
},
#[clap(about = "Shows the redeem address your account is bound to")]
GetBoundRedeemAddress,
#[clap(about = "Shows the executor address your account is bound to")]
GetBoundExecutorAddress,
#[clap(
about = "Submit an extrinsic to request generation of a redemption certificate (redeeming all available FLIP)"
)]
Expand Down
Loading

0 comments on commit a0a0ff6

Please sign in to comment.