mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2026-01-08 20:59:35 +00:00
begin coordinator wallet implementation
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
ELECTRUM_BACKEND="127.0.0.1:50001"
|
||||
DATABASE_PATH="./db/trades.db"
|
||||
WALLET_XPRV="tprv8ZgxMBicQKsPdHuCSjhQuSZP1h6ZTeiRqREYS5guGPdtL7D1uNLpnJmb2oJep99Esq1NbNZKVJBNnD2ZhuXSK7G5eFmmcx73gsoa65e2U32"
|
||||
|
||||
@ -4,11 +4,12 @@ use super::*;
|
||||
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SqliteDB {
|
||||
pub struct CoordinatorDB {
|
||||
pub db_pool: Arc<Pool<Sqlite>>,
|
||||
}
|
||||
|
||||
impl SqliteDB {
|
||||
// is our implementation secure against sql injections?
|
||||
impl CoordinatorDB {
|
||||
// will either create a new db or load existing one. Will create according tables in new db
|
||||
pub async fn init() -> Result<Self> {
|
||||
let db_pool = SqlitePoolOptions::new()
|
||||
@ -1,13 +1,13 @@
|
||||
mod communication;
|
||||
mod coordinator;
|
||||
mod sqlite_db;
|
||||
mod database;
|
||||
mod wallet;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use bdk::{database::MemoryDatabase, Wallet};
|
||||
use communication::{api::*, api_server};
|
||||
use database::CoordinatorDB;
|
||||
use dotenv::dotenv;
|
||||
use sqlite_db::SqliteDB;
|
||||
use std::{env, sync::Arc};
|
||||
use wallet::CoordinatorWallet;
|
||||
|
||||
@ -16,8 +16,8 @@ use wallet::CoordinatorWallet;
|
||||
async fn main() -> Result<()> {
|
||||
dotenv().ok();
|
||||
// Initialize the database pool
|
||||
let coordinator_db = SqliteDB::init().await?;
|
||||
let wallet = CoordinatorWallet::init().await?;
|
||||
let coordinator_db = CoordinatorDB::init().await?;
|
||||
// let wallet = CoordinatorWallet::init().await?;
|
||||
|
||||
api_server(coordinator_db).await?;
|
||||
Ok(())
|
||||
|
||||
@ -1,7 +1,36 @@
|
||||
use super::*;
|
||||
use anyhow::Context;
|
||||
use bdk::{
|
||||
bitcoin::{self, bip32::ExtendedPrivKey},
|
||||
blockchain::ElectrumBlockchain,
|
||||
electrum_client::Client,
|
||||
template::Bip86,
|
||||
KeychainKind, SyncOptions, Wallet,
|
||||
};
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct CoordinatorWallet {
|
||||
pub wallet: Wallet<MemoryDatabase>,
|
||||
}
|
||||
|
||||
impl CoordinatorWallet {}
|
||||
impl CoordinatorWallet {
|
||||
pub async fn init() -> Result<Self> {
|
||||
let wallet_xprv = ExtendedPrivKey::from_str(
|
||||
&env::var("WALLET_XPRV").context("loading WALLET_XPRV from .env failed")?,
|
||||
)?;
|
||||
let backend = ElectrumBlockchain::from(Client::new(
|
||||
&env::var("ELECTRUM_BACKEND")
|
||||
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
|
||||
)?);
|
||||
let wallet = Wallet::new(
|
||||
Bip86(wallet_xprv, KeychainKind::External),
|
||||
Some(Bip86(wallet_xprv, KeychainKind::Internal)),
|
||||
bitcoin::Network::Testnet,
|
||||
MemoryDatabase::default(), // non-permanent storage
|
||||
)?;
|
||||
|
||||
wallet.sync(&backend, SyncOptions::default())?;
|
||||
dbg!("Balance: {} SAT", wallet.get_balance()?);
|
||||
Ok(TradingWallet { wallet, backend })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user