Skip to content

Commit

Permalink
Fix/option contructor (#21)
Browse files Browse the repository at this point in the history
* Add wasm constructor for send options

* lint && clippy
  • Loading branch information
fbsobreira authored Sep 11, 2023
1 parent 20b9a3e commit a2ac514
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/kos-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ rust-version = { workspace = true }
[features]

[dependencies]
kos-types = { workspace = true }
kos-types = { workspace = true, features = ["serde"]}

bytes = { workspace = true }
prost = { workspace = true }
Expand Down
214 changes: 209 additions & 5 deletions packages/kos-proto/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use wasm_bindgen::prelude::*;
#[derive(Default, Deserialize, Serialize, Clone, Debug)]
#[wasm_bindgen]
pub struct KLVOptions {
#[wasm_bindgen(skip)]
pub nonce: Option<u64>,
#[wasm_bindgen(skip)]
pub kda: Option<String>,
#[wasm_bindgen(js_name = kdaRoyalties)]
#[wasm_bindgen(skip)]
pub kda_royalties: Option<i64>,
#[wasm_bindgen(skip)]
pub kda_fee: Option<String>,
Expand All @@ -19,6 +20,21 @@ pub struct KLVOptions {

#[wasm_bindgen]
impl KLVOptions {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}

#[wasm_bindgen(js_name = setNonce)]
pub fn set_nonce(&mut self, nonce: i64) {
self.nonce = Some(nonce as u64);
}

#[wasm_bindgen(js_name = getNonce)]
pub fn get_nonce(&self) -> u64 {
self.nonce.unwrap_or_default()
}

#[wasm_bindgen(js_name = addMemo)]
pub fn add_memo(&mut self, data: &str) {
let mut memo = self.memo.clone().unwrap_or_default();
Expand All @@ -42,10 +58,30 @@ impl KLVOptions {
self.kda = Some(kda.to_owned());
}

#[wasm_bindgen(js_name = getKDA)]
pub fn get_kda(&self) -> String {
self.kda.clone().unwrap_or_default()
}

#[wasm_bindgen(js_name = setKDARoyalties)]
pub fn set_kda_royalties(&mut self, royalties: i64) {
self.kda_royalties = Some(royalties);
}

#[wasm_bindgen(js_name = getKDARoyalties)]
pub fn get_kda_royalties(&self) -> i64 {
self.kda_royalties.unwrap_or_default()
}

#[wasm_bindgen(js_name = setKDAFee)]
pub fn set_kda_fee(&mut self, fee: &str) {
self.kda_fee = Some(fee.to_owned());
}

#[wasm_bindgen(js_name = getKDAFee)]
pub fn get_kda_fee(&self) -> String {
self.kda_fee.clone().unwrap_or_default()
}
}

#[derive(Deserialize, Serialize, Clone, Debug)]
Expand All @@ -61,20 +97,40 @@ pub struct TRXOptions {

#[wasm_bindgen]
impl TRXOptions {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}

#[wasm_bindgen(js_name = setToken)]
pub fn set_token(&mut self, token: &str) {
self.token = Some(token.to_owned());
}

#[wasm_bindgen(js_name = getToken)]
pub fn get_token(&self) -> String {
self.token.clone().unwrap_or_default()
}

#[wasm_bindgen(js_name = setFeeLimit)]
pub fn set_fee_limit(&mut self, fee_limit: u64) {
self.fee_limit = Some(fee_limit as i64);
}

#[wasm_bindgen(js_name = getFeeLimit)]
pub fn get_fee_limit(&self) -> u64 {
self.fee_limit.unwrap_or_default() as u64
}

#[wasm_bindgen(js_name = setMemo)]
pub fn set_memo(&mut self, memo: &str) {
self.memo = Some(memo.to_owned());
}

#[wasm_bindgen(js_name = getMemo)]
pub fn get_memo(&self) -> String {
self.memo.clone().unwrap_or_default()
}
}

impl Default for TRXOptions {
Expand All @@ -90,10 +146,11 @@ impl Default for TRXOptions {
#[derive(Deserialize, Serialize, Default, Clone, Debug)]
#[wasm_bindgen]
pub struct ETHOptions {
#[wasm_bindgen(js_name = legacyType)]
#[wasm_bindgen(skip)]
pub legacy_type: Option<bool>,
#[wasm_bindgen(skip)]
pub nonce: Option<u64>,
#[wasm_bindgen(js_name = chainId)]
#[wasm_bindgen(skip)]
pub chain_id: Option<u64>,
#[wasm_bindgen(skip)]
pub token: Option<String>,
Expand All @@ -111,34 +168,94 @@ pub struct ETHOptions {

#[wasm_bindgen]
impl ETHOptions {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}

#[wasm_bindgen(js_name = setLegacyType)]
pub fn set_legacy_type(&mut self, legacy_type: bool) {
self.legacy_type = Some(legacy_type);
}

#[wasm_bindgen(js_name = getLegacyType)]
pub fn get_legacy_type(&self) -> bool {
self.legacy_type.unwrap_or_default()
}

#[wasm_bindgen(js_name = setNonce)]
pub fn set_nonce(&mut self, nonce: u64) {
self.nonce = Some(nonce);
}

#[wasm_bindgen(js_name = getNonce)]
pub fn get_nonce(&self) -> u64 {
self.nonce.unwrap_or_default()
}

#[wasm_bindgen(js_name = setChainId)]
pub fn set_chain_id(&mut self, chain_id: u64) {
self.chain_id = Some(chain_id);
}

#[wasm_bindgen(js_name = getChainId)]
pub fn get_chain_id(&self) -> u64 {
self.chain_id.unwrap_or_default()
}

#[wasm_bindgen(js_name = setToken)]
pub fn set_token(&mut self, token: &str) {
self.token = Some(token.to_owned());
}

#[wasm_bindgen(js_name = getToken)]
pub fn get_token(&self) -> String {
self.token.clone().unwrap_or_default()
}

#[wasm_bindgen(js_name = setGasLimit)]
pub fn set_gas_limit(&mut self, gas_limit: &str) -> Result<(), Error> {
self.gas_limit = Some(BigNumber::from_string(gas_limit)?);
Ok(())
}

#[wasm_bindgen(js_name = getGasLimit)]
pub fn get_gas_limit(&self) -> String {
self.gas_limit.clone().unwrap_or_default().to_string()
}

#[wasm_bindgen(js_name = setGasPrice)]
pub fn set_gas_price(&mut self, gas_price: &str) -> Result<(), Error> {
self.gas_price = Some(BigNumber::from_string(gas_price)?);
Ok(())
}

#[wasm_bindgen(js_name = getGasPrice)]
pub fn get_gas_price(&self) -> String {
self.gas_price.clone().unwrap_or_default().to_string()
}

#[wasm_bindgen(js_name = setContractData)]
pub fn set_contract_data(&mut self, contract_data: &[u8]) {
self.contract_data = Some(contract_data.to_owned());
}

#[wasm_bindgen(js_name = getContractData)]
pub fn get_contract_data(&self) -> Vec<u8> {
self.contract_data.clone().unwrap_or_default()
}

#[wasm_bindgen(js_name = setMaxFeePerGas)]
pub fn set_max_fee_per_gas(&mut self, max_fee_per_gas: &str) -> Result<(), Error> {
self.max_fee_per_gas = Some(BigNumber::from_string(max_fee_per_gas)?);
Ok(())
}

#[wasm_bindgen(js_name = getMaxFeePerGas)]
pub fn get_max_fee_per_gas(&self) -> String {
self.max_fee_per_gas.clone().unwrap_or_default().to_string()
}

#[wasm_bindgen(js_name = setMaxPriorityFeePerGas)]
pub fn set_max_priority_fee_per_gas(
&mut self,
Expand All @@ -147,6 +264,14 @@ impl ETHOptions {
self.max_priority_fee_per_gas = Some(BigNumber::from_string(max_priority_fee_per_gas)?);
Ok(())
}

#[wasm_bindgen(js_name = getMaxPriorityFeePerGas)]
pub fn get_max_priority_fee_per_gas(&self) -> String {
self.max_priority_fee_per_gas
.clone()
.unwrap_or_default()
.to_string()
}
}

#[derive(Deserialize, Serialize, Default, Clone, Debug)]
Expand All @@ -158,10 +283,20 @@ pub struct MATICOptions {

#[wasm_bindgen]
impl MATICOptions {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}

#[wasm_bindgen(js_name = setETHOptions)]
pub fn set_eth_options(&mut self, options: &ETHOptions) {
self.eth = options.clone();
}

#[wasm_bindgen(js_name = getETHOptions)]
pub fn get_eth_options(&self) -> ETHOptions {
self.eth.clone()
}
}

#[derive(Deserialize, Serialize, Default, Clone, Debug)]
Expand All @@ -170,37 +305,78 @@ pub struct BTCOptions {
/// hex magic from network (default is bitcoin mainnet)
#[wasm_bindgen(skip)]
pub network: Option<String>,
#[wasm_bindgen(js_name = satsPerBytes)]
#[wasm_bindgen(skip)]
pub sats_per_bytes: Option<u64>,
#[wasm_bindgen(skip)]
pub dust_value: Option<BigNumber>,
#[wasm_bindgen(js_name = sendAll)]
#[wasm_bindgen(skip)]
pub send_all: Option<bool>,
#[wasm_bindgen(skip)]
pub change_address: Option<String>,
#[wasm_bindgen(skip)]
pub receivers: Option<Vec<(String, BigNumber)>>,
#[wasm_bindgen(skip)]
pub rbf: Option<bool>,
}

#[wasm_bindgen]
impl BTCOptions {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}

#[wasm_bindgen(js_name = setNetwork)]
pub fn set_network(&mut self, network: &str) {
self.network = Some(network.to_owned());
}

#[wasm_bindgen(js_name = getNetwork)]
pub fn get_network(&self) -> String {
self.network.clone().unwrap_or_default()
}

#[wasm_bindgen(js_name = setSatsPerBytes)]
pub fn set_sats_per_bytes(&mut self, sats_per_bytes: u64) {
self.sats_per_bytes = Some(sats_per_bytes);
}

#[wasm_bindgen(js_name = getSatsPerBytes)]
pub fn get_sats_per_bytes(&self) -> u64 {
self.sats_per_bytes.unwrap_or_default()
}

#[wasm_bindgen(js_name = setDustValue)]
pub fn set_dust_value(&mut self, dust_value: &str) -> Result<(), Error> {
self.dust_value = Some(BigNumber::from_string(dust_value)?);
Ok(())
}

#[wasm_bindgen(js_name = getDustValue)]
pub fn get_dust_value(&self) -> String {
self.dust_value.clone().unwrap_or_default().to_string()
}

#[wasm_bindgen(js_name = setSendAll)]
pub fn set_send_all(&mut self, send_all: bool) {
self.send_all = Some(send_all);
}

#[wasm_bindgen(js_name = getSendAll)]
pub fn get_send_all(&self) -> bool {
self.send_all.unwrap_or_default()
}

#[wasm_bindgen(js_name = setChangeAddress)]
pub fn set_change_address(&mut self, change_address: &str) {
self.change_address = Some(change_address.to_owned());
}

#[wasm_bindgen(js_name = getChangeAddress)]
pub fn get_change_address(&self) -> String {
self.change_address.clone().unwrap_or_default()
}

#[wasm_bindgen(js_name = addReceiver)]
pub fn addr_receiver(&mut self, addr: &str, amount: &str) -> Result<(), Error> {
let amount = BigNumber::from_string(amount)?;
Expand All @@ -213,15 +389,43 @@ impl BTCOptions {

Ok(())
}

#[wasm_bindgen(js_name = getReceivers)]
pub fn get_receivers(&self) -> Vec<JsValue> {
self.receivers
.clone()
.unwrap_or_default()
.into_iter()
.map(|(addr, amount)| {
let obj = serde_json::json!({
"address": addr,
"amount": amount.to_string(),
});
obj.to_string().into()
})
.collect()
}

#[wasm_bindgen(js_name = setRBF)]
pub fn set_rbf(&mut self, rbf: bool) {
self.rbf = Some(rbf);
}

#[wasm_bindgen(js_name = getRBF)]
pub fn get_rbf(&self) -> bool {
self.rbf.unwrap_or_default()
}
}

impl BTCOptions {
pub fn dust_value(&self) -> BigNumber {
self.dust_value.clone().unwrap_or(BigNumber::from(546))
}

pub fn sats_per_bytes(&self) -> u64 {
self.sats_per_bytes.unwrap_or(1)
}

pub fn receivers(&self) -> Vec<(String, BigNumber)> {
self.receivers.clone().unwrap_or_default()
}
Expand Down

0 comments on commit a2ac514

Please sign in to comment.