Skip to content

Commit

Permalink
Merge branch 'main' into doc/redemption-restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen authored Sep 5, 2023
2 parents d8c6e96 + cbcc213 commit afa6be2
Show file tree
Hide file tree
Showing 149 changed files with 8,648 additions and 6,213 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/_02_retrieve-bins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: dawidd6/action-download-artifact@v2
with:
workflow: release-sisyphos.yml
name: chainflip-backend-runtime-ubuntu-${{ matrix.ubuntu_version }}
name: chainflip-node-runtime-ubuntu-${{ matrix.ubuntu_version }}
branch: release/${{ env.MAJOR_MINOR }}
github_token: ${{ secrets.CF_GITHUB_BOT_TOKEN }}
search_artifacts: true
Expand All @@ -40,6 +40,7 @@ jobs:
- name: Check Downloaded Binaries Version Matches Tag 🕵️‍♂️
shell: bash
run: |
chmod +x ./chainflip-*
# TODO: Make this look nicer once we have --version flag in all binaries
./ci/scripts/check_binary_version.sh ./chainflip-node ${{ github.ref_name }}
./ci/scripts/check_binary_version.sh ./chainflip-engine ${{ github.ref_name }}
Expand All @@ -51,6 +52,7 @@ jobs:
name: chainflip-backend-bin-ubuntu-${{ matrix.ubuntu_version }}
path: |
chainflip-broker-api
chainflip-btc-deposit-tracker
chainflip-cli
chainflip-engine
chainflip-lp-api
Expand All @@ -62,4 +64,4 @@ jobs:
with:
name: chainflip-node-runtime-ubuntu-${{ matrix.ubuntu_version }}
path: |
./state_chain_runtime*.wasm
./state_chain_runtime*.wasm
2 changes: 1 addition & 1 deletion .github/workflows/release-sisyphos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ jobs:
with:
version: "sisyphos/"
environment: dev
secrets: inherit
secrets: inherit
2 changes: 2 additions & 0 deletions Cargo.lock

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

92 changes: 71 additions & 21 deletions api/bin/chainflip-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(absolute_path)]
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use clap::Parser;
use futures::FutureExt;
use serde::Serialize;
Expand All @@ -10,8 +10,8 @@ use crate::settings::{
LiquidityProviderSubcommands,
};
use api::{
lp::LpApi, primitives::RedemptionAmount, AccountId32, BrokerApi, GovernanceApi, KeyPair,
OperatorApi, StateChainApi, SwapDepositAddress,
lp::LpApi, primitives::RedemptionAmount, queries::QueryApi, AccountId32, BrokerApi,
GovernanceApi, KeyPair, OperatorApi, SignedExtrinsicApi, StateChainApi, SwapDepositAddress,
};
use cf_chains::eth::Address as EthereumAddress;
use chainflip_api as api;
Expand Down Expand Up @@ -91,7 +91,13 @@ async fn run_cli() -> Result<()> {
println!("Emergency Withdrawal Address registered. Tx hash: {tx_hash}");
},
Redeem { amount, eth_address, executor } => {
request_redemption(api.operator_api(), amount, &eth_address, executor).await?;
request_redemption(api, amount, eth_address, executor).await?;
},
BindRedeemAddress { eth_address } => {
bind_redeem_address(api.operator_api(), &eth_address).await?;
},
GetBoundRedeemAddress {} => {
get_bound_redeem_address(api.query_api()).await?;
},
RegisterAccountRole { role } => {
println!(
Expand Down Expand Up @@ -129,36 +135,51 @@ async fn run_cli() -> Result<()> {
}

async fn request_redemption(
api: Arc<impl OperatorApi + Sync>,
api: StateChainApi,
amount: Option<f64>,
eth_address: &str,
supplied_address: Option<String>,
executor: Option<cf_chains::eth::Address>,
) -> Result<()> {
// Sanitise data
let eth_address = EthereumAddress::from_slice(
clean_hex_address::<[u8; 20]>(eth_address)
.context("Invalid ETH address supplied")?
.as_slice(),
);
let supplied_address = if let Some(address) = supplied_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 =
api.query_api().get_bound_redeem_address(None, Some(account_id.clone())).await?;

let redeem_address = match (supplied_address, bound_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:?}`.");
} else {
bound_address
},
(Some(supplied_address), None) => supplied_address,
(None, Some(bound_address)) => {
println!("Using bound redeem address.");
bound_address
},
(None, None) =>
bail!("No redeem address supplied and no bound redeem address found for your account {account_id}."),
};

let amount = match amount {
Some(amount_float) => {
let atomic_amount = (amount_float * 10_f64.powi(18)) as u128;

println!(
"Submitting redemption with amount `{}` FLIP (`{}` Flipperinos) to ETH address `0x{}`.",
amount_float,
atomic_amount,
hex::encode(eth_address)
"Submitting redemption with amount `{amount_float}` FLIP (`{atomic_amount}` Flipperinos) to ETH address `{redeem_address:?}`."
);

RedemptionAmount::Exact(atomic_amount)
},
None => {
println!(
"Submitting redemption with MAX amount to ETH address `0x{}`.",
hex::encode(eth_address)
);
println!("Submitting redemption with MAX amount to ETH address `{redeem_address:?}`.");

RedemptionAmount::Max
},
Expand All @@ -168,7 +189,7 @@ async fn request_redemption(
return Ok(())
}

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

println!(
"Your redemption request has transaction hash: `{tx_hash:#x}`. View your redemption's progress on the funding app."
Expand All @@ -177,6 +198,35 @@ async fn request_redemption(
Ok(())
}

async fn bind_redeem_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 a redemption address is irreversible. You will only ever be able to redeem to this address: {eth_address:?}.",
);
if !confirm_submit() {
return Ok(())
}

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

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

Ok(())
}

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

Ok(())
}

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

Expand Down
13 changes: 11 additions & 2 deletions api/bin/chainflip-cli/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,22 @@ pub enum CliCommand {
long = "exact"
)]
amount: Option<f64>,
#[clap(help = "The Ethereum address you wish to redeem your FLIP to")]
eth_address: String,
#[clap(
help = "The Ethereum address you wish to redeem your FLIP to. If not specified, the redeem address bound to your account will be used"
)]
eth_address: Option<String>,
#[clap(
help = "Optional executor. If specified, only this address will be able to execute the redemption."
)]
executor: Option<cf_chains::eth::Address>,
},
#[clap(about = "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 = "Shows the redeem address your account is bound to")]
GetBoundRedeemAddress,
#[clap(
about = "Submit an extrinsic to request generation of a redemption certificate (redeeming all available FLIP)"
)]
Expand Down
Loading

0 comments on commit afa6be2

Please sign in to comment.