Skip to content

Commit

Permalink
Add percentage to TransactionGuarantee (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbzurovski authored May 27, 2024
1 parent 81997d8 commit 5358f14
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sargon"
version = "1.0.2"
version = "1.0.3"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ final class ManifestBuildingTests: Test<TransactionManifest> {
var manifest = try rtm("transfer_1to2_multiple_nf_and_f_tokens")

let guarantee = TransactionGuarantee(
amount: 642,
amount: 642,
percentage: 0.9,
instructionIndex: 12,
resourceAddress: .sampleStokenetXRD,
resourceDivisibility: nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ class TransactionManifestTest : SampleTestable<TransactionManifest> {
TransactionGuarantee(
amount =
642.toDecimal192(),
percentage =
1.toDecimal192(),
instructionIndex =
12.toULong(),
resourceAddress =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ CALL_METHOD
let index = 2;
let resource = ResourceAddress::sample_mainnet_candy();
let added_guaranteed_amount: Decimal = "0.12344".parse().unwrap();
let percentage: Decimal = "0.95".parse().unwrap();
let divisibility = 4;
let rounded_guaranteed_amount: Decimal = "0.1234".parse().unwrap();
assert_eq!(
Expand All @@ -225,6 +226,7 @@ CALL_METHOD
.unwrap();
manifest = manifest.modify_add_guarantees([TransactionGuarantee::new(
added_guaranteed_amount,
percentage,
index,
resource,
divisibility,
Expand Down Expand Up @@ -352,6 +354,7 @@ CALL_METHOD
manifest_eq(
manifest.modify_add_guarantees([TransactionGuarantee::new(
1337,
0,
1,
ResourceAddress::sample(),
10,
Expand Down Expand Up @@ -389,6 +392,7 @@ CALL_METHOD
manifest_eq(
manifest.modify_add_guarantees([TransactionGuarantee::new(
1337,
0,
1,
ResourceAddress::sample(),
10,
Expand Down Expand Up @@ -461,6 +465,7 @@ CALL_METHOD
assert_eq!(
manifest.clone().modify_add_guarantees([
TransactionGuarantee::new(
0,
0,
4,
ResourceAddress::sample(),
Expand All @@ -484,6 +489,7 @@ CALL_METHOD
TransactionGuarantee::new(
i,
0,
0,
ResourceAddress::sample(),
None,
)
Expand All @@ -504,6 +510,7 @@ CALL_METHOD
modify_manifest_add_guarantees(
manifest.clone(),
vec![TransactionGuarantee::new(
0,
0,
5,
ResourceAddress::sample(),
Expand Down
1 change: 1 addition & 0 deletions src/wrapped_radix_engine_toolkit/high_level/ret_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ mod tests {
let modified = modify_manifest_add_guarantees(
manifest,
vec![TransactionGuarantee::new(
0,
0,
0,
ResourceAddress::sample(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use crate::prelude::*;

#[derive(Clone, Debug, PartialEq, Eq, Hash, uniffi::Record)]
pub struct TransactionGuarantee {
/// The guaranteed amount to be obtained on this transaction. For manifest & display purposes.
pub amount: Decimal192,

/// The percentage the user has selected, which generated the `amount`. For display purposes only.
pub percentage: Decimal192,
pub instruction_index: u64,
pub resource_address: ResourceAddress,
pub resource_divisibility: Option<u8>,
Expand All @@ -11,12 +15,14 @@ pub struct TransactionGuarantee {
impl TransactionGuarantee {
pub fn new(
amount: impl Into<Decimal192>,
percentage: impl Into<Decimal192>,
instruction_index: u64,
resource_address: ResourceAddress,
resource_divisibility: impl Into<Option<u8>>,
) -> Self {
Self {
amount: amount.into(),
percentage: percentage.into(),
instruction_index,
resource_address,
resource_divisibility: resource_divisibility.into(),
Expand All @@ -32,11 +38,23 @@ impl TransactionGuarantee {

impl HasSampleValues for TransactionGuarantee {
fn sample() -> Self {
TransactionGuarantee::new(1337, 3, ResourceAddress::sample(), Some(12))
TransactionGuarantee::new(
1337,
"0.95".parse::<Decimal192>().unwrap(),
3,
ResourceAddress::sample(),
Some(12),
)
}

fn sample_other() -> Self {
TransactionGuarantee::new(42, 12, ResourceAddress::sample_other(), None)
TransactionGuarantee::new(
42,
"0.90".parse::<Decimal192>().unwrap(),
12,
ResourceAddress::sample_other(),
None,
)
}
}

Expand All @@ -60,8 +78,13 @@ mod tests {

#[test]
fn rounding() {
let sut =
SUT::new("0.12344", 2, ResourceAddress::sample_mainnet_candy(), 4);
let sut = SUT::new(
"0.12344",
90,
2,
ResourceAddress::sample_mainnet_candy(),
4,
);

assert_eq!(sut.rounded_amount(), "0.1234".into());
}
Expand Down

0 comments on commit 5358f14

Please sign in to comment.