Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ManagedDecimal #1461

Merged
merged 15 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,14 @@
{
"ticker": "ExplicitDiscriminantMixed",
"type": "ExplicitDiscriminantMixed"
},
{
"ticker": "ManagedDecimalVar",
"type": "ManagedDecimal<usize>"
},
{
"ticker": "ManagedDecimalConst",
"type": "ManagedDecimalWrapper"
}
],
"hasCallback": false,
Expand Down Expand Up @@ -796,6 +804,15 @@
}
]
},
"ManagedDecimalWrapper": {
"type": "struct",
"fields": [
{
"name": "field",
"type": "ManagedDecimal<2>"
}
]
},
"OnlyShowsUpAsNested01": {
"type": "struct",
"docs": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@
{
"ticker": "ExplicitDiscriminantMixed",
"type": "ExplicitDiscriminantMixed"
},
{
"ticker": "ManagedDecimalVar",
"type": "ManagedDecimal<usize>"
},
{
"ticker": "ManagedDecimalConst",
"type": "ManagedDecimalWrapper"
}
],
"hasCallback": false,
Expand Down Expand Up @@ -447,6 +455,15 @@
}
]
},
"ManagedDecimalWrapper": {
"type": "struct",
"fields": [
{
"name": "field",
"type": "ManagedDecimal<2>"
}
]
},
"OnlyShowsUpAsNested01": {
"type": "struct",
"docs": [
Expand Down
9 changes: 9 additions & 0 deletions contracts/feature-tests/abi-tester/src/abi_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,12 @@ pub enum ExplicitDiscriminantMixed {
b: u16,
},
}

#[type_abi]
#[derive(TopEncode, TopDecode)]
pub struct ManagedDecimalWrapper<Api>
where
Api: ManagedTypeApi,
{
pub field: ManagedDecimal<Api, ConstDecimals<2>>,
}
7 changes: 6 additions & 1 deletion contracts/feature-tests/abi-tester/src/abi_test_type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::only_nested::*;
use multiversx_sc::{
api::ManagedTypeApi,
types::{BigUint, Box, ManagedBuffer},
types::{BigUint, Box, ConstDecimals, ManagedBuffer, ManagedDecimal},
};
multiversx_sc::derive_imports!();

Expand Down Expand Up @@ -42,3 +42,8 @@
#[allow(dead_code)]
pub field: OnlyShowsUpAsNested10,
}

#[derive(TypeAbi)]
pub struct ManagedDecimalWrapper<M: ManagedTypeApi> {

Check warning on line 47 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/feature-tests/abi-tester/src/abi_test_type.rs#L47

warning: field `field` is never read --> contracts/feature-tests/abi-tester/src/abi_test_type.rs:48:9 | 47 | pub struct ManagedDecimalWrapper<M: ManagedTypeApi> { | --------------------- field in this struct 48 | pub field: ManagedDecimal<M, ConstDecimals<2>>, | ^^^^^ | = note: `#[warn(dead_code)]` on by default
Raw output
contracts/feature-tests/abi-tester/src/abi_test_type.rs:47:12:w:warning: field `field` is never read
  --> contracts/feature-tests/abi-tester/src/abi_test_type.rs:48:9
   |
47 | pub struct ManagedDecimalWrapper<M: ManagedTypeApi> {
   |            --------------------- field in this struct
48 |     pub field: ManagedDecimal<M, ConstDecimals<2>>,
   |         ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default


__END__

Check warning on line 47 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/feature-tests/abi-tester/src/abi_test_type.rs#L47

warning: field `field` is never read --> contracts/feature-tests/abi-tester/src/abi_test_type.rs:48:9 | 47 | pub struct ManagedDecimalWrapper<M: ManagedTypeApi> { | --------------------- field in this struct 48 | pub field: ManagedDecimal<M, ConstDecimals<2>>, | ^^^^^ | = note: `#[warn(dead_code)]` on by default
Raw output
contracts/feature-tests/abi-tester/src/abi_test_type.rs:47:12:w:warning: field `field` is never read
  --> contracts/feature-tests/abi-tester/src/abi_test_type.rs:48:9
   |
47 | pub struct ManagedDecimalWrapper<M: ManagedTypeApi> {
   |            --------------------- field in this struct
48 |     pub field: ManagedDecimal<M, ConstDecimals<2>>,
   |         ^^^^^
   |
   = note: `#[warn(dead_code)]` on by default


__END__
pub field: ManagedDecimal<M, ConstDecimals<2>>,

Check warning on line 48 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Test Coverage

field `field` is never read

Check warning on line 48 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Rust tests

field `field` is never read

Check warning on line 48 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

field `field` is never read

Check warning on line 48 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

field `field` is never read

Check warning on line 48 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

field `field` is never read

Check warning on line 48 in contracts/feature-tests/abi-tester/src/abi_test_type.rs

View workflow job for this annotation

GitHub Actions / Contracts (nightly) / Wasm tests

field `field` is never read
}
6 changes: 4 additions & 2 deletions contracts/feature-tests/abi-tester/src/abi_tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use only_nested::*;
#[esdt_attribute("STRUCT1", AbiEnum)]
#[esdt_attribute("STRUCT2", AbiManagedType<Self::Api>)]
#[esdt_attribute("OnlyInEsdt", OnlyShowsUpInEsdtAttr)]
#[esdt_attribute["ExplicitDiscriminant", ExplicitDiscriminant]]
#[esdt_attribute["ExplicitDiscriminantMixed", ExplicitDiscriminantMixed]]
#[esdt_attribute("ExplicitDiscriminant", ExplicitDiscriminant)]
#[esdt_attribute("ExplicitDiscriminantMixed", ExplicitDiscriminantMixed)]
#[esdt_attribute("ManagedDecimalVar", ManagedDecimal<Self::Api, NumDecimals>)]
#[esdt_attribute("ManagedDecimalConst", ManagedDecimalWrapper<Self::Api>)]
pub trait AbiTester {
/// Contract constructor.
#[init]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"steps": [
{
"step": "setState",
"accounts": {
"sc:basic-features": {
"nonce": "0",
"balance": "0",
"code": "mxsc:../output/basic-features.mxsc.json"
},
"address:an_account": {
"nonce": "0",
"balance": "0"
}
}
},
{
"step": "scCall",
"id": "1",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_addition",
"arguments": [
"4",
"5"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"9"
]
}
},
{
"step": "scCall",
"id": "1",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_subtraction",
"arguments": [
"9",
"4"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"5"
]
}
},
{
"step": "scCall",
"id": "2",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_subtraction",
"arguments": [
"2",
"8"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [],
"status": "4",
"message": "str:cannot subtract because result would be negative",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "3",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_eq",
"arguments": [
"13",
"13"
],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"0x01"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "4",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_trunc",
"arguments": [],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"313"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
},
{
"step": "scCall",
"id": "5",
"tx": {
"from": "address:an_account",
"to": "sc:basic-features",
"function": "managed_decimal_into_raw_units",
"arguments": [],
"gasLimit": "1,000,000,000,000",
"gasPrice": "0"
},
"expect": {
"out": [
"12345"
],
"status": "",
"message": "*",
"gas": "*",
"refund": "*"
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ pub mod codec_err_test;
pub mod crypto_features;
pub mod echo;
pub mod echo_managed;
pub mod egld_decimal;
pub mod elliptic_curve_features;
pub mod event_features;
pub mod macro_features;
pub mod managed_address_features;
pub mod managed_buffer_features;
pub mod managed_decimal_features;
pub mod managed_vec_features;
pub mod non_zero_features;
pub mod small_num_overflow_test_ops;
Expand Down Expand Up @@ -76,8 +78,10 @@ pub trait BasicFeatures:
+ small_num_overflow_test_ops::SmallIntOverflow
+ token_identifier_features::TokenIdentifierFeatures
+ non_zero_features::TypeFeatures
+ egld_decimal::EgldDecimal
+ multiversx_sc_modules::default_issue_callbacks::DefaultIssueCallbacksModule
+ storage_mapper_get_at_address::StorageMapperGetAtAddress
+ managed_decimal_features::ManagedDecimalFeatures
{
#[init]
fn init(&self) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ where
.argument(&bi)
.original_result()
}

pub fn returns_egld_decimal(
self,
) -> TxTypedCall<Env, From, To, (), Gas, ManagedDecimal<Env::Api, ConstDecimals<18>>> {
self.wrapped_tx
.raw_call("returns_egld_decimal")
.original_result()
}
}

#[type_abi]
Expand Down
11 changes: 11 additions & 0 deletions contracts/feature-tests/basic-features/src/egld_decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
multiversx_sc::imports!();

/// Used for testing the egld_decimal function return type
#[multiversx_sc::module]
pub trait EgldDecimal {
#[payable("EGLD")]
#[endpoint]
fn returns_egld_decimal(&self) -> ManagedDecimal<Self::Api, ConstDecimals<18>> {
self.call_value().egld_decimal()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
multiversx_sc::imports!();

#[multiversx_sc::module]
pub trait ManagedDecimalFeatures {
#[endpoint]
fn managed_decimal_addition(
&self,
first: ManagedDecimal<Self::Api, ConstDecimals<2>>,
second: ManagedDecimal<Self::Api, ConstDecimals<2>>,
) -> ManagedDecimal<Self::Api, ConstDecimals<2>> {
first + second
}

#[endpoint]
fn managed_decimal_subtraction(
&self,
first: ManagedDecimal<Self::Api, ConstDecimals<2>>,
second: ManagedDecimal<Self::Api, ConstDecimals<2>>,
) -> ManagedDecimal<Self::Api, ConstDecimals<2>> {
first - second
}

#[endpoint]
fn managed_decimal_eq(
&self,
first: ManagedDecimal<Self::Api, ConstDecimals<2>>,
second: ManagedDecimal<Self::Api, ConstDecimals<2>>,
) -> bool {
first.eq(&second)
}

#[endpoint]
fn managed_decimal_trunc(&self) -> BigUint {
let dec = ManagedDecimal::from_raw_units(BigUint::from(31332u64), 2usize);
dec.trunc()
}

#[endpoint]
fn managed_decimal_into_raw_units(&self) -> BigUint {
let dec = ManagedDecimal::from_raw_units(BigUint::from(12345u64), 2usize);
dec.into_raw_units().clone()
}
}
Loading
Loading