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

Fee collector + unwrap fee #1

Merged
merged 8 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
29 changes: 29 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Coverage

on:
pull_request:
branches:
- main
- develop
jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
components: rustfmt, clippy
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Making coverage dir
run: mkdir coverage
- name: Generate code coverage
run: cargo llvm-cov --lcov --output-path coverage/lcov.info --ignore-filename-regex '(storage.rs|events.rs|macros|errors.rs|proxies)'
- name: Generating report
uses: vebr/jest-lcov-reporter@v0.2.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
lcov-file: ./coverage/lcov.info
22 changes: 21 additions & 1 deletion Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@ path = "src/lib.rs"
[dependencies.multiversx-sc]
version = "0.50.3"

[dependencies.multiversx-sc-modules]
version = "0.50.3"

[dev-dependencies]
num-bigint = "0.4"
multiversx-wegld-swap-sc = "0.50.3"


[dev-dependencies.multiversx-sc-scenario]
version = "0.50.3"



[workspace]
members = [
".",
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ See `devnet.snippets.sh` for list of available endpoints for user testing.

### Setting up dev environment (project development bootstrap) + how to build (and upgrade)

- Uses `multiversx-sc-* 0.49.0` (In v2.0.0, we used0.49.0) SDK libs (see Cargo.toml)
- Uses `multiversx-sc-* 0.50.3` (In v2.0.0, we used0.50.3) SDK libs (see Cargo.toml)
- Building requires minimum **mxpy 9.5.1** (In v2.0.0, we used mxpy 9.5.1). Check version using `mxpy --version`
- To build the project, requires minimum Rust version `1.78.0-nightly` (In v2.0.0, we used 1.78.0-nightly). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default nightly`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default nightly` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`.
- Uses `multiversx-sc-*0.49.0` (In v1.0.0, we used0.49.0) SDK libs (see Cargo.toml)
- To build the project, requires minimum Rust version `1.78.0` (In v2.0.0, we used 1.78.0). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default stable`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default stable` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`.
- Uses `multiversx-sc-*0.50.3` (In v1.0.0, we used0.50.3) SDK libs (see Cargo.toml)
- Building requires minimum **mxpy 9.5.1** (In v1.0.0, we used mxpy 9.5.1). Check version using `mxpy --version`
- To build the project, requires minimum Rust version `1.78.0-nightly` (In v1.0.0, we used 1.78.0-nightly). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default nightly`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default nightly` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`.
- To build the project, requires minimum Rust version `1.78.0` (In v1.0.0, we used 1.78.0-nightly). Check your Rust version by running `rustc --version`. To update your Rust, run `rustup update`. To set to nightly run `rustup default stable`. Note that `mxpy deps install rust --overwrite` also brings in it's own compatible rust version so running `rustup default nightly` might have a higher rust version than what is used via `mxpy deps install rust --overwrite`.

```
rustup default nightly
rustup default stable
mxpy deps install rust --overwrite
cargo clean
cargo build
Expand Down Expand Up @@ -58,7 +58,7 @@ Another way of running the tests is by using the rust-analyzer extension in Visu
Note: In order to run the tests, one has to use the rust nightly version. One can switch to the nightly version by using:

```shell
rustup default nightly
rustup default stable
```

### How to deploy
Expand Down
2 changes: 1 addition & 1 deletion coverage.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

cargo llvm-cov --ignore-filename-regex '(storage.rs|events.rs|macros|errors.rs)' --open
cargo llvm-cov --ignore-filename-regex '(storage.rs|events.rs|macros|errors.rs|proxies)' --open
20 changes: 20 additions & 0 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ pub trait AdminModule:
self.maximum_deposit(&token_identifier).set(maximum);
}

#[endpoint(setFeeCollector)]
fn set_fee_collector(&self, fee_collector: ManagedAddress) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.set_fee_collector_event(&fee_collector);
self.fee_collector().set(fee_collector);
}

#[endpoint(setFeeValue)]
fn set_fee_value(&self, fee_value: BigUint) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.fee_value().set(fee_value);
}

#[endpoint(setWegldContractAddress)]
fn set_wegld_contract_address(&self, wegld_contract_address: ManagedAddress) {
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.set_wegld_contract_address_event(&wegld_contract_address);
self.wegld_contract_address().set(wegld_contract_address);
}

#[endpoint(addTokensToWhitelist)]
fn add_tokens_to_whitelist(
&self,
Expand Down
9 changes: 9 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
if self.administrator().is_empty() {
is_ready = false;
}

if self.fee_collector().is_empty() {
is_ready = false;
}

if self.wegld_contract_address().is_empty() {
is_ready = false;
}

if self.relayer().is_empty() {
is_ready = false;
}
Expand Down
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub const ERR_NOT_WHOLE_NUMBER: &str = "Not a whole number";
pub const ERR_WRONG_VALUES: &str = "Wrong values";
pub const ERR_ADDRESS_NOT_WHITELISTED: &str = "Address not whitelisted";
pub const ERR_ADDRESS_ALREADY_WHITELISTED: &str = "Address already whitelisted";
pub const ERR_WRONG_FEE_TOKEN_IDENTIFIER: &str = "Wrong fee token identifier";
9 changes: 9 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,13 @@ pub trait EventsModule {
#[indexed] minimum: &BigUint,
#[indexed] maximum: &BigUint,
);

#[event("setFeeCollectorEvent")]
fn set_fee_collector_event(&self, #[indexed] fee_collector: &ManagedAddress);

#[event("setFeeValueEvent")]
fn set_fee_value_event(&self, #[indexed] fee_value: &BigUint);

#[event("setWegldContractAddressEvent")]
fn set_wegld_contract_address_event(&self, #[indexed] wegld_contract_address: &ManagedAddress);
}
123 changes: 96 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
use crate::errors::{
ERR_ADDRESS_NOT_WHITELISTED, ERR_CONTRACT_NOT_READY, ERR_NOT_ENOUGH_LIQUIDITY,
ERR_NOT_PRIVILEGED, ERR_NOT_WHOLE_NUMBER, ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE,
ERR_TOKEN_NOT_WHITELISTED,
ERR_TOKEN_NOT_WHITELISTED, ERR_WRONG_FEE_TOKEN_IDENTIFIER, ERR_WRONG_VALUES,
};

use crate::proxies::wegld_proxy;

multiversx_sc::imports!();

pub mod admin;
pub mod config;
pub mod errors;
pub mod events;
pub mod macros;
pub mod proxies;
pub mod storage;
pub mod utils;
#[multiversx_sc::contract]
Expand Down Expand Up @@ -41,37 +44,103 @@ pub trait CoreMxBridgeSc:
let caller = self.blockchain().get_caller();
require_contract_ready!(self, ERR_CONTRACT_NOT_READY);
check_whitelist!(self, &caller, ERR_ADDRESS_NOT_WHITELISTED);
let payment = self.call_value().single_esdt();

require!(
self.tokens_whitelist().contains(&payment.token_identifier),
ERR_TOKEN_NOT_WHITELISTED
);
let fee_value = self.fee_value().get();

require!(
self.check_amount(
&payment.amount,
self.token_decimals(&payment.token_identifier).get()
),
ERR_NOT_WHOLE_NUMBER
);
if fee_value == BigUint::zero() {
let deposit = self.call_value().single_esdt();

require!(
self.minimum_deposit(&payment.token_identifier).get() <= payment.amount
&& payment.amount <= self.maximum_deposit(&payment.token_identifier).get(),
ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE
);
require!(
self.tokens_whitelist().contains(&deposit.token_identifier),
ERR_TOKEN_NOT_WHITELISTED
);

self.send_to_liquidity_event(
&payment.token_identifier,
&payment.amount,
&caller,
&destination_address,
&destination_signature,
);
require!(
self.check_amount(
&deposit.amount,
self.token_decimals(&deposit.token_identifier).get()
),
ERR_NOT_WHOLE_NUMBER
);

require!(
self.minimum_deposit(&deposit.token_identifier).get() <= deposit.amount
&& deposit.amount <= self.maximum_deposit(&deposit.token_identifier).get(),
ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE
);

self.send_to_liquidity_event(
&deposit.token_identifier,
&deposit.amount,
&caller,
&destination_address,
&destination_signature,
);

self.liquidity(&deposit.token_identifier)
.update(|value| *value += deposit.amount);
} else {
let [deposit, fee] = self.call_value().multi_esdt();

require!(fee_value == fee.amount, ERR_WRONG_VALUES);

let wegld_token_identifier = self
.tx()
.to(&self.wegld_contract_address().get())
.typed(wegld_proxy::EgldEsdtSwapProxy)
.wrapped_egld_token_id()
.returns(ReturnsResult)
.sync_call();

require!(
fee.token_identifier == wegld_token_identifier,
ERR_WRONG_FEE_TOKEN_IDENTIFIER
);

require!(
self.tokens_whitelist().contains(&deposit.token_identifier),
ERR_TOKEN_NOT_WHITELISTED
);

require!(
self.check_amount(
&deposit.amount,
self.token_decimals(&deposit.token_identifier).get()
),
ERR_NOT_WHOLE_NUMBER
);

require!(
self.minimum_deposit(&deposit.token_identifier).get() <= deposit.amount
&& deposit.amount <= self.maximum_deposit(&deposit.token_identifier).get(),
ERR_PAYMENT_AMOUNT_NOT_IN_ACCEPTED_RANGE
);

let back_transfers = self
.tx()
.to(&self.wegld_contract_address().get())
.typed(wegld_proxy::EgldEsdtSwapProxy)
.unwrap_egld()
.with_esdt_transfer(fee)
.returns(ReturnsBackTransfers)
.sync_call();

self.send().direct_egld(
&self.fee_collector().get(),
&back_transfers.total_egld_amount,
);

self.send_to_liquidity_event(
&deposit.token_identifier,
&deposit.amount,
&caller,
&destination_address,
&destination_signature,
);

self.liquidity(&payment.token_identifier)
.update(|value| *value += payment.amount);
self.liquidity(&deposit.token_identifier)
.update(|value| *value += deposit.amount);
}
}

#[endpoint(sendFromLiquidity)]
Expand Down
1 change: 1 addition & 0 deletions src/proxies/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod wegld_proxy;
Loading
Loading