# input-output-hk/rust-cardano

Initial version of the transaction builder.

```Current version of the transaction builder is designed to be
as simple as possible and contain only barebone API. So it can
be later improved to fit all the needs.
Builder only works with stable fee algorithms i.e. the ones that
can perfectly estimate the fee cost. The only provided fee
algorithm exactly matches that criterion. It sums up inputs
and outputs count and calculates the cost.```
qnikst committed Mar 13, 2019
1 parent 9aa89b3 commit ac4229dade08b3e4c28ace09148040304c0e499e
Showing with 259 additions and 0 deletions.
1. +67 −0 chain-impl-mockchain/src/fee.rs
2. +2 −0 chain-impl-mockchain/src/lib.rs
3. +190 −0 chain-impl-mockchain/src/txbuilder.rs
 @@ -0,0 +1,67 @@ use crate::transaction as tx; use crate::value::Value; use std::ops::{Add, Mul}; /// Linear fee using the basic affine formula `A * bytes(tx) + CONSTANT`. #[derive(PartialEq, Eq, PartialOrd, Debug, Clone, Copy)] pub struct LinearFee { pub constant: Milli, pub coefficient: Milli, } impl LinearFee { pub fn new(constant: Milli, coefficient: Milli) -> Self { LinearFee { constant, coefficient, } } } #[derive(PartialEq, Eq, PartialOrd, Debug, Clone, Copy)] pub struct Milli(u64); impl Milli { pub fn new(i: u64, f: u64) -> Self { Milli(i * 1000 + f % 1000) } pub fn integral(i: u64) -> Self { Milli(i * 1000) } pub fn to_integral(self) -> u64 { if self.0 % 1000 == 0 { self.0 / 1000 } else { (self.0 / 1000) + 1 } } } impl Add for Milli { type Output = Milli; fn add(self, other: Self) -> Self { Milli(self.0 + other.0) } } impl Mul for Milli { type Output = Milli; fn mul(self, other: Self) -> Self { let v = self.0 as u128 * other.0 as u128; Milli((v / 1000) as u64) } } pub trait FeeAlgorithm { fn calculate_for(&self, tx: &tx::Transaction) -> Value; } impl FeeAlgorithm for LinearFee { fn calculate_for(&self, tx: &tx::Transaction) -> Value { let msz = Milli::integral((tx.inputs.len() + tx.outputs.len()) as u64); let fee = self.coefficient * msz + self.constant; Value(fee.to_integral()) } }
 @@ -9,12 +9,14 @@ mod date; #[cfg(test)] pub mod environment; pub mod error; pub mod fee; pub mod key; pub mod leadership; pub mod ledger; pub mod setting; pub mod stake; pub mod transaction; pub mod txbuilder; pub mod update; pub mod value;