Skip to content

Commit

Permalink
Merge pull request #3 from sander2/refactor/polkabtc-to-interbtc
Browse files Browse the repository at this point in the history
Refactor: polkabtc to interbtc
  • Loading branch information
sander2 authored Jun 8, 2021
2 parents 41d42e2 + 9c4da35 commit 6f6154b
Show file tree
Hide file tree
Showing 45 changed files with 629 additions and 647 deletions.
2 changes: 1 addition & 1 deletion .chglog/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ style: github
template: CHANGELOG.tpl.md
info:
title: CHANGELOG
repository_url: https://github.com/interlay/polkabtc-clients
repository_url: https://github.com/interlay/interbtc-clients
options:
commits:
# filters:
Expand Down
58 changes: 0 additions & 58 deletions .deploy/rust-builder-pod.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ release-github:
script:
- gh auth status
- git-chglog --output CHANGELOG.md $CI_COMMIT_TAG
- gh release -R https://github.com/interlay/polkabtc-clients create $CI_COMMIT_TAG --title $CI_COMMIT_TAG -F CHANGELOG.md -d target/x86_64-unknown-linux-gnu/release/*
- gh release -R https://github.com/interlay/interbtc-clients create $CI_COMMIT_TAG --title $CI_COMMIT_TAG -F CHANGELOG.md -d target/x86_64-unknown-linux-gnu/release/*
only:
- tags
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ARG PACKAGE=staked-relayer

RUN install_packages libssl-dev

COPY --from=builder /app/polkabtc-clients/target/$PROFILE/$PACKAGE /usr/local/bin
COPY --from=builder /app/interbtc-clients/target/$PROFILE/$PACKAGE /usr/local/bin

# Checks
RUN ldd /usr/local/bin/$PACKAGE && \
Expand Down
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pipeline {
environment {
PATH = "/busybox:$PATH"
REGISTRY = 'registry.gitlab.com'
REPOSITORY = 'interlay/polkabtc-clients'
REPOSITORY = 'interlay/interbtc-clients'
PROFILE = "x86_64-unknown-linux-gnu/release"
}
steps {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p align="center">
<a href="https://gitlab.com/interlay/polkabtc-clients">
<a href="https://gitlab.com/interlay/interbtc-clients">
<img src="media/polka_btc.png">
</a>

<h2 align="center">PolkaBTC Clients</h2>
<h2 align="center">InterBTC Clients</h2>

<p align="center">
Faucet, Oracle, Vault & Staked Relayer
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ authors = ["Interlay <[email protected]>"]
edition = "2018"

[features]
default = ["polkabtc"]
default = ["interbtc"]
regtest-mine-on-tx = []
regtest-manual-mining = []
cli = ["clap"]
polkabtc = ["polkabtc-bitcoin"]
interbtc = ["interbtc-bitcoin"]
uses-bitcoind = []

[dependencies]
Expand All @@ -31,7 +31,7 @@ hyper = "0.10"
# Substrate dependencies
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.1" }

[dependencies.polkabtc-bitcoin]
[dependencies.interbtc-bitcoin]
git = "https://github.com/interlay/btc-parachain"
branch = "master"
package = "bitcoin"
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub trait PartialAddress: Sized + Eq + PartialOrd {
fn encode_str(&self, network: Network) -> Result<String, ConversionError>;
}

#[cfg(feature = "polkabtc")]
impl PartialAddress for polkabtc_bitcoin::Address {
#[cfg(feature = "interbtc")]
impl PartialAddress for interbtc_bitcoin::Address {
fn from_payload(payload: Payload) -> Result<Self, ConversionError> {
match payload {
Payload::PubkeyHash(hash) => Ok(Self::P2PKH(H160::from(hash.as_hash().into_inner()))),
Expand Down
55 changes: 31 additions & 24 deletions faucet/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use jsonrpc_http_server::{
use kv::*;
use parity_scale_codec::{Decode, Encode};
use runtime::{
AccountId, CollateralBalancesPallet, Error as RuntimeError, PolkaBtcProvider, VaultRegistryPallet, PLANCK_PER_DOT,
AccountId, CollateralBalancesPallet, Error as RuntimeError, InterBtcParachain, VaultRegistryPallet, PLANCK_PER_DOT,
};
use serde::{Deserialize, Deserializer};
use std::{collections::HashMap, net::SocketAddr, time::Duration};
Expand Down Expand Up @@ -57,8 +57,8 @@ fn handle_resp<T: Encode>(resp: Result<T, Error>) -> Result<Value, JsonRpcError>
}
}

async fn _system_health(provider: &PolkaBtcProvider) -> Result<(), Error> {
match timeout(HEALTH_DURATION, provider.get_latest_block_hash()).await {
async fn _system_health(parachain_rpc: &InterBtcParachain) -> Result<(), Error> {
match timeout(HEALTH_DURATION, parachain_rpc.get_latest_block_hash()).await {
Err(err) => Err(Error::RuntimeError(RuntimeError::from(err))),
_ => Ok(()),
}
Expand All @@ -76,7 +76,7 @@ enum FundingRequestAccountType {
}

async fn _fund_account_raw(
provider: &PolkaBtcProvider,
parachain_rpc: &InterBtcParachain,
params: Params,
store: Store,
user_allowance: u128,
Expand All @@ -86,14 +86,14 @@ async fn _fund_account_raw(
let mut allowances = HashMap::new();
allowances.insert(FundingRequestAccountType::User, user_allowance);
allowances.insert(FundingRequestAccountType::Vault, vault_allowance);
fund_account(provider, req, store, allowances).await
fund_account(parachain_rpc, req, store, allowances).await
}

async fn get_account_type(
provider: &PolkaBtcProvider,
parachain_rpc: &InterBtcParachain,
account_id: AccountId,
) -> Result<FundingRequestAccountType, Error> {
if provider.get_vault(account_id.clone()).await.is_ok() {
if parachain_rpc.get_vault(account_id.clone()).await.is_ok() {
return Ok(FundingRequestAccountType::Vault);
}
Ok(FundingRequestAccountType::User)
Expand Down Expand Up @@ -143,13 +143,13 @@ fn has_request_expired(
}

async fn ensure_funding_allowed(
provider: &PolkaBtcProvider,
parachain_rpc: &InterBtcParachain,
account_id: AccountId,
last_request_json: Option<Json<FaucetRequest>>,
account_type: FundingRequestAccountType,
) -> Result<(), Error> {
let free_balance = provider.get_free_balance_for_id(account_id.clone()).await?;
let reserved_balance = provider.get_reserved_balance_for_id(account_id.clone()).await?;
let free_balance = parachain_rpc.get_free_balance_for_id(account_id.clone()).await?;
let reserved_balance = parachain_rpc.get_reserved_balance_for_id(account_id.clone()).await?;
if free_balance + reserved_balance > MAX_FUNDABLE_CLIENT_BALANCE {
log::warn!(
"User {} has enough funds: {:?}",
Expand Down Expand Up @@ -186,14 +186,20 @@ async fn ensure_funding_allowed(
}

async fn atomic_faucet_funding(
provider: &PolkaBtcProvider,
parachain_rpc: &InterBtcParachain,
kv: Bucket<'_, String, Json<FaucetRequest>>,
account_id: AccountId,
allowances: HashMap<FundingRequestAccountType, u128>,
) -> Result<(), Error> {
let last_request_json = kv.get(account_id.to_string())?;
let account_type = get_account_type(&provider, account_id.clone()).await?;
ensure_funding_allowed(provider, account_id.clone(), last_request_json, account_type.clone()).await?;
let account_type = get_account_type(&parachain_rpc, account_id.clone()).await?;
ensure_funding_allowed(
parachain_rpc,
account_id.clone(),
last_request_json,
account_type.clone(),
)
.await?;

let amount = allowances
.get(&account_type)
Expand All @@ -207,7 +213,7 @@ async fn atomic_faucet_funding(
account_type,
amount
);
provider.transfer_to(&account_id, amount).await?;
parachain_rpc.transfer_to(&account_id, amount).await?;

// Replace the previous (expired) claim datetime with the datetime of the current claim, only update
// this after successfully transferring funds to ensure that this can be called again on error
Expand All @@ -216,19 +222,19 @@ async fn atomic_faucet_funding(
}

async fn fund_account(
provider: &PolkaBtcProvider,
parachain_rpc: &InterBtcParachain,
req: FundAccountJsonRpcRequest,
store: Store,
allowances: HashMap<FundingRequestAccountType, u128>,
) -> Result<(), Error> {
let provider = provider.clone();
let parachain_rpc = parachain_rpc.clone();
let kv = open_kv_store(store)?;
atomic_faucet_funding(&provider, kv, req.account_id.clone(), allowances).await?;
atomic_faucet_funding(&parachain_rpc, kv, req.account_id.clone(), allowances).await?;
Ok(())
}

pub async fn start_http(
provider: PolkaBtcProvider,
parachain_rpc: InterBtcParachain,
addr: SocketAddr,
origin: String,
user_allowance: u128,
Expand All @@ -239,22 +245,23 @@ pub async fn start_http(
io.add_sync_method("user_allowance", move |_| handle_resp(Ok(user_allowance)));
io.add_sync_method("vault_allowance", move |_| handle_resp(Ok(vault_allowance)));
{
let provider = provider.clone();
let parachain_rpc = parachain_rpc.clone();
io.add_method("system_health", move |_| {
let provider = provider.clone();
async move { handle_resp(_system_health(&provider).await) }
let parachain_rpc = parachain_rpc.clone();
async move { handle_resp(_system_health(&parachain_rpc).await) }
});
}
{
let provider = provider;
let parachain_rpc = parachain_rpc;
let store = store;

// an async closure is only FnOnce, so we need this workaround
io.add_method("fund_account", move |params| {
let provider = provider.clone();
let parachain_rpc = parachain_rpc.clone();
let store = store.clone();
async move {
let result = _fund_account_raw(&provider.clone(), params, store, user_allowance, vault_allowance).await;
let result =
_fund_account_raw(&parachain_rpc.clone(), params, store, user_allowance, vault_allowance).await;
if let Err(ref err) = result {
log::debug!("Failed to fund account: {}", err);
}
Expand Down
4 changes: 2 additions & 2 deletions faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod http;
use clap::Clap;
use error::Error;
use git_version::git_version;
use runtime::{substrate_subxt::PairSigner, PolkaBtcRuntime};
use runtime::{substrate_subxt::PairSigner, InterBtcRuntime};
use service::{on_shutdown, wait_or_shutdown};
use std::net::SocketAddr;

Expand Down Expand Up @@ -56,7 +56,7 @@ async fn main() -> Result<(), Error> {
let opts: Opts = Opts::parse();

let (key_pair, _) = opts.account_info.get_key_pair()?;
let signer = PairSigner::<PolkaBtcRuntime, _>::new(key_pair);
let signer = PairSigner::<InterBtcRuntime, _>::new(key_pair);

let (shutdown_tx, _) = tokio::sync::broadcast::channel(16);

Expand Down
8 changes: 4 additions & 4 deletions oracle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use error::Error;
use git_version::git_version;
use log::{error, info};
use runtime::{
substrate_subxt::PairSigner, ExchangeRateOraclePallet, FixedPointNumber, FixedU128, PolkaBtcProvider,
PolkaBtcRuntime,
substrate_subxt::PairSigner, ExchangeRateOraclePallet, FixedPointNumber, FixedU128, InterBtcParachain,
InterBtcRuntime,
};
use std::{collections::HashMap, time::Duration};
use tokio::time::delay_for;
Expand Down Expand Up @@ -70,7 +70,7 @@ async fn main() -> Result<(), Error> {
let opts: Opts = Opts::parse();

let (key_pair, _) = opts.account_info.get_key_pair()?;
let signer = PairSigner::<PolkaBtcRuntime, _>::new(key_pair);
let signer = PairSigner::<InterBtcRuntime, _>::new(key_pair);

let timeout = Duration::from_millis(opts.timeout_ms);
let exchange_rate =
Expand Down Expand Up @@ -99,7 +99,7 @@ async fn main() -> Result<(), Error> {
chrono::offset::Local::now()
);

let result = PolkaBtcProvider::from_url_with_retry(
let result = InterBtcParachain::from_url_with_retry(
&opts.btc_parachain_url.clone(),
signer.clone(),
Duration::from_millis(opts.connection_timeout_ms),
Expand Down
6 changes: 3 additions & 3 deletions runtime/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
error::{Error, KeyLoadingError},
PolkaBtcProvider, PolkaBtcSigner,
InterBtcParachain, InterBtcSigner,
};
use clap::Clap;
use sp_core::{sr25519::Pair, Pair as _};
Expand Down Expand Up @@ -78,8 +78,8 @@ pub struct ConnectionOpts {
}

impl ConnectionOpts {
pub async fn try_connect(&self, signer: PolkaBtcSigner) -> Result<PolkaBtcProvider, Error> {
PolkaBtcProvider::from_url_and_config_with_retry(
pub async fn try_connect(&self, signer: InterBtcSigner) -> Result<InterBtcParachain, Error> {
InterBtcParachain::from_url_and_config_with_retry(
&self.btc_parachain_url,
signer,
self.max_concurrent_requests,
Expand Down
Loading

0 comments on commit 6f6154b

Please sign in to comment.