Implement fee calculator #81

Merged
pitmutt merged 1 commit from rav001 into dev040 2024-05-08 18:51:35 +00:00
3 changed files with 86 additions and 64 deletions

View file

@ -1643,6 +1643,7 @@ pub extern "C" fn rust_wrapper_create_transaction(
sapoutput_len: usize, sapoutput_len: usize,
net: bool, net: bool,
bl_height: u32, bl_height: u32,
build: bool,
out: *mut u8, out: *mut u8,
out_len: &mut usize){ out_len: &mut usize){
let sap_wit_in: Vec<u8> = marshall_from_haskell_var(sap_wit, sap_wit_len, RW); let sap_wit_in: Vec<u8> = marshall_from_haskell_var(sap_wit, sap_wit_len, RW);
@ -1794,71 +1795,89 @@ pub extern "C" fn rust_wrapper_create_transaction(
} }
} }
} }
let spend_params_in: Vec<u8> = marshall_from_haskell_var(sapspend, sapspend_len, RW); if build {
let spend_params_reader = Cursor::new(spend_params_in); let spend_params_in: Vec<u8> = marshall_from_haskell_var(sapspend, sapspend_len, RW);
let spend_prover = SpendParameters::read(spend_params_reader, false).unwrap(); let spend_params_reader = Cursor::new(spend_params_in);
let output_params_in: Vec<u8> = marshall_from_haskell_var(sapoutput, sapoutput_len, RW); let spend_prover = SpendParameters::read(spend_params_reader, false).unwrap();
let output_params_reader = Cursor::new(output_params_in); let output_params_in: Vec<u8> = marshall_from_haskell_var(sapoutput, sapoutput_len, RW);
let output_prover = OutputParameters::read(output_params_reader, false).unwrap(); let output_params_reader = Cursor::new(output_params_in);
let result = if net { let output_prover = OutputParameters::read(output_params_reader, false).unwrap();
main_builder.build(OsRng, &spend_prover, &output_prover, &FeeRule::standard()) let result = if net {
} else { main_builder.build(OsRng, &spend_prover, &output_prover, &FeeRule::standard())
test_builder.build(OsRng, &spend_prover, &output_prover, &FeeRule::standard()) } else {
}; test_builder.build(OsRng, &spend_prover, &output_prover, &FeeRule::standard())
match result { };
Ok(r) => { match result {
let mut out_bytes: Vec<u8> = Vec::new(); Ok(r) => {
let _t = r.transaction().write_v5(&mut out_bytes); let mut out_bytes: Vec<u8> = Vec::new();
let h = Hhex {bytes: out_bytes}; let _t = r.transaction().write_v5(&mut out_bytes);
marshall_to_haskell_var(&h, out, out_len, RW); let h = Hhex {bytes: out_bytes};
}, marshall_to_haskell_var(&h, out, out_len, RW);
Err(e) => { },
match e { Err(e) => {
Error::InsufficientFunds(y) => { match e {
let x = Hhex {bytes: vec![0]}; Error::InsufficientFunds(y) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![0]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::ChangeRequired(y1) => { },
let x = Hhex {bytes: vec![1]}; Error::ChangeRequired(y1) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![1]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::Fee(_y2) => { },
let x = Hhex {bytes: vec![2]}; Error::Fee(_y2) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![2]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::Balance(y3) => { },
let x = Hhex {bytes: vec![3]}; Error::Balance(y3) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![3]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::TransparentBuild(y4) => { },
let x = Hhex {bytes: vec![4]}; Error::TransparentBuild(y4) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![4]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::SaplingBuild(y5) => { },
let x = Hhex {bytes: vec![5]}; Error::SaplingBuild(y5) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![5]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::OrchardBuild(y7) => { },
let x = Hhex {bytes: vec![6]}; Error::OrchardBuild(y7) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![6]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::OrchardSpend(y8) => { },
let x = Hhex {bytes: vec![7]}; Error::OrchardSpend(y8) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![7]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::OrchardRecipient(y9) => { },
let x = Hhex {bytes: vec![8]}; Error::OrchardRecipient(y9) => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![8]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::SaplingBuilderNotAvailable => { },
let x = Hhex {bytes: vec![9]}; Error::SaplingBuilderNotAvailable => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![9]};
}, marshall_to_haskell_var(&x, out, out_len, RW);
Error::OrchardBuilderNotAvailable => { },
let x = Hhex {bytes: vec![10]}; Error::OrchardBuilderNotAvailable => {
marshall_to_haskell_var(&x, out, out_len, RW); let x = Hhex {bytes: vec![10]};
marshall_to_haskell_var(&x, out, out_len, RW);
}
} }
} }
} }
} else {
let result = if net {
main_builder.get_fee(&FeeRule::standard())
} else {
test_builder.get_fee(&FeeRule::standard())
};
match result {
Ok(r) => {
let x = Hhex {bytes: r.to_i64_le_bytes().to_vec()};
marshall_to_haskell_var(&x, out, out_len, RW);
},
Err(e) => {
let x = Hhex {bytes: vec![2]};
marshall_to_haskell_var(&x, out, out_len, RW);
}
}
} }
} }

View file

@ -287,6 +287,7 @@ import ZcashHaskell.Types
, toBorshVar* `BS.ByteString'& , toBorshVar* `BS.ByteString'&
, `Bool' , `Bool'
, `Word64' , `Word64'
, `Bool'
, getVarBuffer `Buffer HexString'& , getVarBuffer `Buffer HexString'&
} }
-> `()' -> `()'

View file

@ -133,8 +133,9 @@ createTransaction ::
-> SaplingOutputParams -- ^ the Sapling circuit output parameters -> SaplingOutputParams -- ^ the Sapling circuit output parameters
-> ZcashNet -- ^ the network to be used -> ZcashNet -- ^ the network to be used
-> Int -- ^ target block height -> Int -- ^ target block height
-> Bool -- ^ True to build, False to estimate fee
-> Either TxError HexString -> Either TxError HexString
createTransaction sapAnchor orchAnchor tSpend sSpend oSpend outgoing sParams oParams znet bh = createTransaction sapAnchor orchAnchor tSpend sSpend oSpend outgoing sParams oParams znet bh build =
if BS.length (hexBytes txResult) > 1 if BS.length (hexBytes txResult) > 1
then Right txResult then Right txResult
else case head (BS.unpack $ hexBytes txResult) of else case head (BS.unpack $ hexBytes txResult) of
@ -168,3 +169,4 @@ createTransaction sapAnchor orchAnchor tSpend sSpend oSpend outgoing sParams oPa
(sapOParams oParams) (sapOParams oParams)
(znet == MainNet) (znet == MainNet)
(fromIntegral bh) (fromIntegral bh)
build