Skip to content

Commit

Permalink
Merge pull request #1512 from multiversx/import-traits-from-exchange
Browse files Browse the repository at this point in the history
import FixedSupplyToken & Mergeable traits from mx-exchange-rs
  • Loading branch information
alyn509 authored Apr 1, 2024
2 parents 7fdfbb6 + f5c4a4d commit 605aa85
Show file tree
Hide file tree
Showing 15 changed files with 542 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,7 @@ members = [
"contracts/feature-tests/rust-testing-framework-tester/meta",
"contracts/feature-tests/use-module",
"contracts/feature-tests/use-module/meta",
"contracts/feature-tests/exchange-features",
"contracts/feature-tests/exchange-features/meta",

]
17 changes: 17 additions & 0 deletions contracts/feature-tests/exchange-features/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "exchange-features"
version = "0.0.0"
authors = ["Alin-Marius Cruceat <alin.cruceat@multiversx.com>"]
edition = "2021"
publish = false

[lib]
path = "src/exchange_features.rs"

[dependencies.multiversx-sc]
version = "0.47.8"
path = "../../../framework/base"

[dev-dependencies.multiversx-sc-scenario]
version = "0.47.8"
path = "../../../framework/scenario"
13 changes: 13 additions & 0 deletions contracts/feature-tests/exchange-features/meta/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "exchange-features-meta"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.exchange-features]
path = ".."

[dependencies.multiversx-sc-meta]
version = "0.47.8"
path = "../../../../framework/meta"
default-features = false
3 changes: 3 additions & 0 deletions contracts/feature-tests/exchange-features/meta/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
multiversx_sc_meta::cli_main::<exchange_features::AbiProvider>();
}
3 changes: 3 additions & 0 deletions contracts/feature-tests/exchange-features/multiversx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"language": "rust"
}
72 changes: 72 additions & 0 deletions contracts/feature-tests/exchange-features/src/exchange_features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#![no_std]

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(
ManagedVecItem,
TopEncode,
TopDecode,
NestedEncode,
NestedDecode,
TypeAbi,
Clone,
PartialEq,
Debug,
)]
pub struct TokenAttributes<M: ManagedTypeApi> {
pub amount: BigUint<M>,
}

impl<M: ManagedTypeApi> FixedSupplyToken<M> for TokenAttributes<M> {
fn get_total_supply(&self) -> BigUint<M> {
self.amount.clone()
}

fn into_part(self, payment_amount: &BigUint<M>) -> Self {
let new_amount = self.rule_of_three_non_zero_result(payment_amount, &self.amount);
TokenAttributes { amount: new_amount }
}
}
impl<M: ManagedTypeApi> Mergeable<M> for TokenAttributes<M> {
#[inline]
fn can_merge_with(&self, _other: &Self) -> bool {
true
}

fn merge_with(&mut self, other: Self) {
self.error_if_not_mergeable(&other);

self.amount += other.amount
}
}

#[multiversx_sc::contract]
pub trait ExchangeFeatures {
#[storage_mapper("supply")]
fn supply(&self) -> SingleValueMapper<TokenAttributes<Self::Api>>;

#[init]
fn init(&self, initial_value: BigUint) {
self.supply().set(TokenAttributes {
amount: initial_value,
});
}

#[upgrade]
fn upgrade(&self, value: BigUint) {
let token = self.supply().get();
self.supply().set(token.into_part(&value));
}

#[endpoint]
fn merge(&self, value: BigUint) {
self.supply()
.update(|token| token.merge_with(TokenAttributes { amount: value }));
}

#[endpoint]
fn get_supply(&self) -> BigUint {
self.supply().get().get_total_supply()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use multiversx_sc_scenario::{scenario_model::*, *};

const EXCHANGE_FEATURES_PATH_EXPR: &str = "mxsc:output/exchange-features.mxsc.json";

fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();
blockchain.set_current_dir_from_workspace("contracts/feature-tests/exchange-features");

blockchain.register_contract(
EXCHANGE_FEATURES_PATH_EXPR,
exchange_features::ContractBuilder,
);
blockchain
}

#[test]
fn exchange_features_blackbox_raw() {
let mut world = world();
let exchange_features_code = world.code_expression(EXCHANGE_FEATURES_PATH_EXPR);

world
.set_state_step(
SetStateStep::new()
.put_account("address:owner", Account::new().nonce(1))
.new_address("address:owner", 1, "sc:exchange-features"),
)
.sc_deploy(
ScDeployStep::new()
.from("address:owner")
.code(&exchange_features_code)
.argument("5")
.expect(TxExpect::ok().no_result()),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:exchange-features")
.function("get_supply")
.expect(TxExpect::ok().result("5")),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:exchange-features")
.function("merge")
.argument("3")
.expect(TxExpect::ok().no_result()),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:exchange-features")
.function("get_supply")
.expect(TxExpect::ok().result("8")),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:exchange-features")
.function("upgradeContract")
.argument(&exchange_features_code)
.argument("0x0502") // codeMetadata
.argument("0") // contract argument
.expect(TxExpect::user_error("str:Zero amount")),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:exchange-features")
.function("upgradeContract")
.argument(exchange_features_code)
.argument("0x0502") // codeMetadata
.argument("3") // contract argument
.expect(TxExpect::ok().no_result()),
)
.sc_call(
ScCallStep::new()
.from("address:owner")
.to("sc:exchange-features")
.function("get_supply")
.expect(TxExpect::ok().result("3")),
);
}
170 changes: 170 additions & 0 deletions contracts/feature-tests/exchange-features/wasm/Cargo.lock

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

Loading

0 comments on commit 605aa85

Please sign in to comment.