-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1512 from multiversx/import-traits-from-exchange
import FixedSupplyToken & Mergeable traits from mx-exchange-rs
- Loading branch information
Showing
15 changed files
with
542 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
multiversx_sc_meta::cli_main::<exchange_features::AbiProvider>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"language": "rust" | ||
} |
72 changes: 72 additions & 0 deletions
72
contracts/feature-tests/exchange-features/src/exchange_features.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
contracts/feature-tests/exchange-features/tests/exchange_features_blackbox_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
170
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.
Oops, something went wrong.
Oops, something went wrong.