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

Merge master -> feat/unified #1535

Merged
merged 12 commits into from
Apr 3, 2024
58 changes: 3 additions & 55 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,11 @@ permissions:
jobs:
contracts:
name: Contracts
uses: multiversx/mx-sc-actions/.github/workflows/contracts.yml@v3.0.0
uses: multiversx/mx-sc-actions/.github/workflows/contracts.yml@v3.1.0
with:
rust-toolchain: nightly-2023-12-11
path-to-sc-meta: framework/meta
mx-scenario-go-version: v2.1.0-alpha
coverage-args: --ignore-filename-regex='meta/src' --ignore-filename-regex='wasm-adapter' --ignore-filename-regex='benchmarks/' --ignore-filename-regex='tests/' --output ./coverage.md
secrets:
token: ${{ secrets.GITHUB_TOKEN }}

test_coverage:
name: Test Coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Install rust
uses: actions-rust-lang/setup-rust-toolchain@v1
with:
toolchain: nightly-2023-12-11

- name: Install prerequisites
run: |
rustup component add llvm-tools-preview
dirname $(find ~/.rustup -name llvm-cov) >> $GITHUB_PATH

echo $(dirname $(find ~/.rustup -name llvm-cov))

- name: Run tests and generate report
env:
RUSTFLAGS: ""
run: |
cargo run --bin sc-meta test-coverage \
--ignore-filename-regex='meta/src' \
--ignore-filename-regex='wasm-adapter' \
--ignore-filename-regex='benchmarks/' \
--ignore-filename-regex='tests/' \
--output ./coverage.md

- name: Upload the report
uses: actions/upload-artifact@v3
with:
name: coverage
path: coverage.md

- name: Find the comment containing the report
id: fc
uses: peter-evans/find-comment@v2
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: "Coverage Summary"

- name: Create or update the report comment
uses: peter-evans/create-or-update-comment@v2
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body-file: ./coverage.md
edit-mode: replace
token: ${{ secrets.GITHUB_TOKEN }}
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 <[email protected]>"]
edition = "2021"
publish = false

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

[dependencies.multiversx-sc]
version = "0.48.0-alpha.1"
path = "../../../framework/base"

[dev-dependencies.multiversx-sc-scenario]
version = "0.48.0-alpha.1"
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.48.0-alpha.1"
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")),
);
}
Loading
Loading