Skip to content

Commit

Permalink
Basic flow
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph committed Dec 27, 2024
1 parent 76559d7 commit 7d7cff1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/subcommand/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use {
super::*,
crate::wallet::{batch, wallet_constructor::WalletConstructor, ListDescriptorsResult, Wallet},
base64::Engine,
bitcoin::Psbt,
shared_args::SharedArgs,
};

Expand Down
87 changes: 85 additions & 2 deletions src/subcommand/wallet/offer/create.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,96 @@
use super::*;

// TODO:
// - [ ] add a --dry-run flag that doesn't sign our inputs in the PSBT
// - [ ] pub fee: u64,

#[derive(Debug, Serialize, Deserialize)]
pub struct Output {
pub psbt: String,
pub seller_address: Address<NetworkUnchecked>,
pub inscription: InscriptionId,
}

#[derive(Debug, Parser)]
pub(crate) struct Create {
#[arg(short, long, help = "<INSCRIPTION> to make offer for.")]
#[arg(long, help = "<INSCRIPTION> to make offer for.")]
inscription: InscriptionId,
#[arg(long, help = "<AMOUNT> to offer.")]
amount: Amount,
#[arg(long, help = "<FEE_RATE> for finalized transaction.")]
fee_rate: FeeRate,
}

impl Create {
pub(crate) fn run(&self, wallet: Wallet) -> SubcommandResult {
todo!()
ensure!(
!wallet.inscription_info().contains_key(&self.inscription),
"{} in our wallet",
self.inscription
);

let inscription = wallet.get_inscription(self.inscription)?;

let utxo = inscription.satpoint.outpoint;

let Some(seller_address) = inscription.address else {
bail!("{} not owned by an address", self.inscription);
};

let Ok(seller_address) = Address::from_str(&seller_address) else {
bail!("{} not owned by an usable address", self.inscription);
};

let unsigned_transaction = Transaction {
version: Version(2),
lock_time: LockTime::ZERO,
input: vec![TxIn {
previous_output: utxo,
script_sig: ScriptBuf::new(),
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
witness: Witness::new(),
}],
output: vec![
TxOut {
value: Amount::from_sat(inscription.value.unwrap()),
script_pubkey: wallet.get_change_address()?.into(),
},
TxOut {
value: self.amount,
script_pubkey: seller_address
.clone()
.require_network(wallet.chain().network())
.unwrap()
.into(),
},
],
};

let unsigned_transaction_hex = fund_raw_transaction(
wallet.bitcoin_client(),
self.fee_rate,
&unsigned_transaction,
)?;

let unsigned_transaction =
Transaction::consensus_decode(&mut unsigned_transaction_hex.as_slice())?;

let unsigned_psbt = Psbt::from_unsigned_tx(unsigned_transaction)?;

let psbt = wallet
.bitcoin_client()
.wallet_process_psbt(
&base64::engine::general_purpose::STANDARD.encode(unsigned_psbt.serialize()),
Some(true),
None,
None,
)?
.psbt;

Ok(Some(Box::new(Output {
psbt,
inscription: self.inscription,
seller_address,
})))
}
}
16 changes: 16 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ impl Wallet {
self.inscription_info.clone()
}

pub(crate) fn get_inscription(&self, inscription_id: InscriptionId) -> Result<api::Inscription> {
let response = self
.ord_client
.get(
self
.rpc_url
.join(&format!("/inscription/{inscription_id}"))
.unwrap(),
)
.send()?;

let inscription: api::Inscription = serde_json::from_str(&response.text()?)?;

Ok(inscription)
}

pub(crate) fn inscription_exists(&self, inscription_id: InscriptionId) -> Result<bool> {
Ok(
!self
Expand Down

0 comments on commit 7d7cff1

Please sign in to comment.