Skip to content

Commit d8bdf6f

Browse files
committed
More updates according to spec
1 parent 8280756 commit d8bdf6f

File tree

5 files changed

+112
-35
lines changed

5 files changed

+112
-35
lines changed

src/client/src/bin/http_server.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use options_rpc::rpc::OptionOps;
55
use options_rpc::ClientArgs;
66

77
use options_rpc::data_structures::{
8-
ContractArgs, ContractId, InfoResponse, InitArgs, NetworkParams,
8+
ContractArgs, ContractId, InfoResponse, InitArgs, NetworkParams, OptionsImportParams,
99
};
1010

1111
async fn info(item: web::Json<ContractId>, args: Data<ClientArgs>) -> HttpResponse {
@@ -17,11 +17,11 @@ async fn info(item: web::Json<ContractId>, args: Data<ClientArgs>) -> HttpRespon
1717
}
1818

1919
async fn list(args: Data<ClientArgs>) -> HttpResponse {
20-
let num_max_entries = 30;
20+
let num_max_entries = 1000;
2121
let mut res = Vec::with_capacity(num_max_entries);
2222
let db = args.read_options_db();
2323
let e_cli = args.elements_cli();
24-
for item in db.book.iter().take(100) {
24+
for item in db.book.iter().take(num_max_entries) {
2525
let (_id, contract) = item.unwrap();
2626
let contract = OptionsContract::from_slice(&contract);
2727
let info = InfoResponse::from_contract(&contract, &e_cli);
@@ -90,6 +90,29 @@ async fn settle(settle_args: web::Json<ContractArgs>, data: Data<ClientArgs>) ->
9090
HttpResponse::Ok().json(res) // <- send response
9191
}
9292

93+
async fn import(import_args: web::Json<OptionsImportParams>, data: Data<ClientArgs>) -> HttpResponse {
94+
let db = data.read_options_db();
95+
let contract = import_args.to_contract();
96+
db.insert(&contract);
97+
let res = ContractId {
98+
contract_id: contract.id(),
99+
};
100+
HttpResponse::Ok().json(res) // <- send response
101+
}
102+
103+
async fn export(export_args: web::Json<ContractId>, data: Data<ClientArgs>) -> HttpResponse {
104+
let db = data.read_options_db();
105+
let contract = db.get(&export_args.contract_id).unwrap();
106+
let res = OptionsImportParams::from_contract(contract);
107+
HttpResponse::Ok().json(res) // <- send response
108+
}
109+
110+
async fn remove(remove_args: web::Json<ContractId>, data: Data<ClientArgs>) -> HttpResponse {
111+
let db = data.read_options_db();
112+
db.remove(&remove_args.contract_id);
113+
HttpResponse::Ok().json(true) // <- send response
114+
}
115+
93116
#[actix_web::main]
94117
async fn main() -> std::io::Result<()> {
95118
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
@@ -113,6 +136,9 @@ async fn main() -> std::io::Result<()> {
113136
.service(web::resource("/expiry").route(web::post().to(expiry)))
114137
.service(web::resource("/exercise").route(web::post().to(exercise)))
115138
.service(web::resource("/settle").route(web::post().to(settle)))
139+
.service(web::resource("/import").route(web::post().to(import)))
140+
.service(web::resource("/export").route(web::post().to(export)))
141+
.service(web::resource("/remove").route(web::post().to(remove)))
116142
})
117143
.bind(("127.0.0.1", 8080))?
118144
.run()

src/client/src/bin/opt_cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn main() {
115115
}
116116
print_string(&res);
117117
}
118-
Some(Commands::Info(id)) => match book.get(&id.id) {
118+
Some(Commands::Info(id)) => match book.get(&id.contract_id) {
119119
None => panic!("Contract ID not found"),
120120
Some(x) => {
121121
let info = InfoResponse::from_contract(&x, &e_cli);

src/client/src/contract.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ impl OptionsBook {
3333
.insert(key.as_inner(), contract.serialize())
3434
.unwrap();
3535
}
36+
37+
/// Removes a contract from the book
38+
pub fn remove(&self, key: &sha256::Hash) {
39+
self.book.remove(&key).unwrap();
40+
}
3641
}

src/client/src/data_structures.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use bitcoin::hashes::hex::FromHex;
77
use clap::Args;
88
use elementsd::bitcoincore_rpc::bitcoin;
99
use elementsd::bitcoincore_rpc::Client;
10+
use options_lib::BaseParams;
1011
use options_lib::miniscript::elements::{self, AddressParams, AssetId};
1112
use options_lib::OptionsContract;
1213
use secp256k1::hashes::sha256;
@@ -84,6 +85,58 @@ pub struct InfoResponse {
8485
pub liquidity: u64,
8586
}
8687

88+
#[derive(Debug, Clone, Serialize, Deserialize)]
89+
pub struct OptionsImportParams {
90+
pub contract_size: u64,
91+
pub expiry: u32,
92+
pub start: u32,
93+
pub strike_price: u64,
94+
pub coll_asset: AssetId,
95+
pub settle_asset: AssetId,
96+
pub crt_rt_prevout_txid: elements::Txid,
97+
pub crt_rt_prevout_vout: u32,
98+
pub ort_rt_prevout_txid: elements::Txid,
99+
pub ort_rt_prevout_vout: u32,
100+
}
101+
102+
impl OptionsImportParams {
103+
104+
pub fn from_contract(contract: OptionsContract) -> Self {
105+
Self {
106+
contract_size: contract.params().contract_size,
107+
expiry: contract.params().expiry,
108+
start: contract.params().start,
109+
strike_price: contract.params().strike_price,
110+
coll_asset: contract.params().coll_asset,
111+
settle_asset: contract.params().settle_asset,
112+
crt_rt_prevout_txid: contract.crt_rt_prevout().txid,
113+
crt_rt_prevout_vout: contract.crt_rt_prevout().vout,
114+
ort_rt_prevout_txid: contract.ort_rt_prevout().txid,
115+
ort_rt_prevout_vout: contract.ort_rt_prevout().vout,
116+
}
117+
}
118+
119+
pub fn to_contract(&self) -> OptionsContract {
120+
let params = BaseParams {
121+
contract_size: self.contract_size,
122+
expiry: self.expiry,
123+
start: self.start,
124+
strike_price: self.strike_price,
125+
coll_asset: self.coll_asset,
126+
settle_asset: self.settle_asset,
127+
};
128+
let crt_prevout = elements::OutPoint {
129+
txid: self.crt_rt_prevout_txid,
130+
vout: self.crt_rt_prevout_vout,
131+
};
132+
let ort_prevout = elements::OutPoint {
133+
txid: self.ort_rt_prevout_txid,
134+
vout: self.ort_rt_prevout_vout,
135+
};
136+
OptionsContract::new(params, crt_prevout, ort_prevout)
137+
}
138+
}
139+
87140
impl InfoResponse {
88141
pub fn from_contract(contract: &OptionsContract, e_cli: &Client) -> Self {
89142
InfoResponse {
@@ -160,7 +213,6 @@ impl ClientArgs {
160213
pub fn read_options_db(&self) -> OptionsBook {
161214
let data_dir = utils::_data_dir(&self.data_dir);
162215
let db_path = utils::_options_db_file(data_dir);
163-
dbg!(&db_path);
164216
OptionsBook::new(&db_path)
165217
}
166218

src/options_lib/src/contract.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub struct OptionsContract {
8686
unspend_key: XOnlyPublicKey,
8787
/// The config for this contract
8888
params: BaseParams,
89+
/// Issuance CRT RT prevout
90+
crt_rt_prevout: OutPoint,
91+
/// Issuance ORT RT prevout
92+
ort_rt_prevout: OutPoint,
8993
}
9094

9195
/// Returns the [`ContractHash`] used in this contract
@@ -96,13 +100,13 @@ pub fn draft_contract_hash() -> ContractHash {
96100

97101
impl OptionsContract {
98102
/// Creates a new [`OptionsContract`].
99-
pub fn new(params: BaseParams, crt_prevout: OutPoint, ort_prevout: OutPoint) -> Self {
103+
pub fn new(params: BaseParams, crt_rt_prevout: OutPoint, ort_rt_prevout: OutPoint) -> Self {
100104
// Versioning incase we want to update the scripts
101105
let contract_hash = draft_contract_hash();
102106
let (crt_reissue_entropy, crt, crt_rt) =
103-
new_issuance(crt_prevout, contract_hash, /*confidential*/ false);
107+
new_issuance(crt_rt_prevout, contract_hash, /*confidential*/ false);
104108
let (ort_reissue_entropy, ort, ort_rt) =
105-
new_issuance(ort_prevout, contract_hash, /*confidential*/ false);
109+
new_issuance(ort_rt_prevout, contract_hash, /*confidential*/ false);
106110
// unspendable key = lift_x(Hash(ser(G)))
107111
let unspend_key = bitcoin::XOnlyPublicKey::from_str(
108112
"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0",
@@ -117,6 +121,8 @@ impl OptionsContract {
117121
ort_reissue_entropy,
118122
unspend_key,
119123
params,
124+
crt_rt_prevout,
125+
ort_rt_prevout,
120126
}
121127
}
122128

@@ -209,13 +215,8 @@ impl OptionsContract {
209215
/// Serializes the contract into a writer
210216
pub fn serialize_to_writer<W: std::io::Write>(&self, mut writer: W) -> Result<(), std::io::Error> {
211217
self.params.serialize_to_writer(&mut writer)?;
212-
self.crt_rt.consensus_encode(&mut writer).unwrap();
213-
self.ort_rt.consensus_encode(&mut writer).unwrap();
214-
self.crt.consensus_encode(&mut writer).unwrap();
215-
self.ort.consensus_encode(&mut writer).unwrap();
216-
self.crt_reissue_entropy.consensus_encode(&mut writer).unwrap();
217-
self.ort_reissue_entropy.consensus_encode(&mut writer).unwrap();
218-
self.unspend_key.serialize().consensus_encode(&mut writer).unwrap();
218+
self.crt_rt_prevout.consensus_encode(&mut writer).unwrap();
219+
self.ort_rt_prevout.consensus_encode(&mut writer).unwrap();
219220
Ok(())
220221
}
221222

@@ -231,33 +232,26 @@ impl OptionsContract {
231232
mut reader: R,
232233
) -> Result<Self, std::io::Error> {
233234
let params = BaseParams::deserialize_from_reader(&mut reader)?;
234-
let crt_rt = AssetId::consensus_decode(&mut reader).unwrap();
235-
let ort_rt = AssetId::consensus_decode(&mut reader).unwrap();
236-
let crt = AssetId::consensus_decode(&mut reader).unwrap();
237-
let ort = AssetId::consensus_decode(&mut reader).unwrap();
238-
let crt_reissue_entropy = sha256::Midstate::consensus_decode(&mut reader).unwrap();
239-
let ort_reissue_entropy = sha256::Midstate::consensus_decode(&mut reader).unwrap();
240-
let unspend_key = XOnlyPublicKey::from_slice(
241-
&<[u8; 32]>::consensus_decode(&mut reader).unwrap(),
242-
)
243-
.unwrap();
244-
Ok(Self {
245-
crt_rt,
246-
ort_rt,
247-
crt,
248-
ort,
249-
crt_reissue_entropy,
250-
ort_reissue_entropy,
251-
unspend_key,
252-
params,
253-
})
235+
let crt_rt_prevout = OutPoint::consensus_decode(&mut reader).unwrap();
236+
let ort_rt_prevout = OutPoint::consensus_decode(&mut reader).unwrap();
237+
Ok(Self::new(params, crt_rt_prevout, ort_rt_prevout))
254238
}
255239

256240
/// Deserialize from slice
257241
pub fn from_slice(slice: &[u8]) -> Self {
258242
Self::deserialize_from_reader(slice).unwrap()
259243
}
260244

245+
246+
/// Returns the crt rt prevout of this [`OptionsContract`].
247+
pub fn crt_rt_prevout(&self) -> OutPoint {
248+
self.crt_rt_prevout
249+
}
250+
251+
/// Returns the ort rt prevout of this [`OptionsContract`].
252+
pub fn ort_rt_prevout(&self) -> OutPoint {
253+
self.ort_rt_prevout
254+
}
261255
}
262256

263257
/// Parameters to be used when funding a new options contract

0 commit comments

Comments
 (0)