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

Created custom_utils module to replace min/max functions from std library #90 #117

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"sputnik-staking",
"sputnikdao2",
"sputnikdao-factory2",
"custom-utils",
"test-token"
]

Expand Down
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ cargo +stable build --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/sputnik_staking.wasm ./sputnik-staking/res/
cp target/wasm32-unknown-unknown/release/sputnikdao2.wasm ./sputnikdao2/res/
cp target/wasm32-unknown-unknown/release/sputnikdao_factory2.wasm ./sputnikdao-factory2/res/
cp target/wasm32-unknown-unknown/release/test_token.wasm ./test-token/res/
cp target/wasm32-unknown-unknown/release/test_token.wasm ./test-token/res/
cp target/wasm32-unknown-unknown/release/custom_utils.wasm ./custom-utils/res/
9 changes: 9 additions & 0 deletions custom-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "custom-utils"
version = "1.0.0"
authors = ["Nemanja Ninkovic <[email protected]>"]
edition = "2018"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]
5 changes: 5 additions & 0 deletions custom-utils/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -e

RUSTFLAGS='-C link-arg=-s' cargo +stable build --target wasm32-unknown-unknown --release
cp ../target/wasm32-unknown-unknown/release/custom_utils.wasm ./res/
Binary file added custom-utils/res/custom_utils.wasm
Binary file not shown.
28 changes: 28 additions & 0 deletions custom-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//Replacement for std::cmp::min/max
pub mod min_max {

pub fn get_max_u64(value1: u64, value2: u64) -> u64 {
if value1 > value2 {
return value1;
}
return value2;
}
pub fn get_min_u64(value1: u64, value2: u64) -> u64 {
if value1 < value2 {
return value1;
}
return value2;
}
pub fn get_min_u128(value1: u128, value2: u128) -> u128 {
if value1 < value2 {
return value1;
}
return value2;
}
pub fn get_max_u128(value1: u128, value2: u128) -> u128 {
if value1 > value2 {
return value1;
}
return value2;
}
}
1 change: 1 addition & 0 deletions sputnikdao-factory2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
near-sdk = { version = "4.0.0-pre.4", features = ["unstable"] }
custom-utils = { path = "../custom-utils" }
3 changes: 2 additions & 1 deletion sputnikdao-factory2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod factory_manager;

use custom_utils::min_max::get_min_u64;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{UnorderedMap, UnorderedSet};
use near_sdk::json_types::{Base58CryptoHash, Base64VecU8, U128};
Expand Down Expand Up @@ -272,7 +273,7 @@ impl SputnikDAOFactory {
/// Get daos in paginated view.
pub fn get_daos(&self, from_index: u64, limit: u64) -> Vec<AccountId> {
let elements = self.daos.as_vector();
(from_index..std::cmp::min(from_index + limit, elements.len()))
(from_index..get_min_u64(from_index + limit, elements.len()))
.filter_map(|index| elements.get(index))
.collect()
}
Expand Down
1 change: 1 addition & 0 deletions sputnikdao2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ crate-type = ["cdylib", "rlib"]
near-sdk = {version = "4.0.0-pre.4", features = ["unstable"]}
near-contract-standards = "4.0.0-pre.4"
hex = "0.4.2"
custom-utils = { path = "../custom-utils" }

[dependencies.serde_with]
version = "1.4.0"
Expand Down
11 changes: 5 additions & 6 deletions sputnikdao2/src/policy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::cmp::min;
use std::collections::{HashMap, HashSet};

use custom_utils::min_max::{get_max_u128, get_min_u128};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::{U128, U64};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, AccountId, Balance};
use std::collections::{HashMap, HashSet};

use crate::proposals::{PolicyParameters, Proposal, ProposalKind, ProposalStatus, Vote};
use crate::types::Action;
Expand Down Expand Up @@ -94,8 +93,8 @@ impl WeightOrRatio {
/// Convert weight or ratio to specific weight given total weight.
pub fn to_weight(&self, total_weight: Balance) -> Balance {
match self {
WeightOrRatio::Weight(weight) => min(weight.0, total_weight),
WeightOrRatio::Ratio(num, denom) => min(
WeightOrRatio::Weight(weight) => get_min_u128(weight.0, total_weight),
WeightOrRatio::Ratio(num, denom) => get_min_u128(
(*num as u128 * total_weight) / *denom as u128 + 1,
total_weight,
),
Expand Down Expand Up @@ -416,7 +415,7 @@ impl Policy {
}
RoleKind::Member(_) => total_supply,
};
let threshold = std::cmp::max(
let threshold = get_max_u128(
vote_policy.quorum.0,
vote_policy.threshold.to_weight(total_weight),
);
Expand Down
6 changes: 3 additions & 3 deletions sputnikdao2/src/views.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};

use std::cmp::min;
use custom_utils::min_max::get_max_u64;

use crate::*;

Expand Down Expand Up @@ -87,7 +87,7 @@ impl Contract {

/// Get proposals in paginated view.
pub fn get_proposals(&self, from_index: u64, limit: u64) -> Vec<ProposalOutput> {
(from_index..min(self.last_proposal_id, from_index + limit))
(from_index..get_max_u64(self.last_proposal_id, from_index + limit))
.filter_map(|id| {
self.proposals.get(&id).map(|proposal| ProposalOutput {
id,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Contract {

/// Get `limit` of bounties from given index.
pub fn get_bounties(&self, from_index: u64, limit: u64) -> Vec<BountyOutput> {
(from_index..std::cmp::min(from_index + limit, self.last_bounty_id))
(from_index..get_max_u64(from_index + limit, self.last_bounty_id))
.filter_map(|id| {
self.bounties.get(&id).map(|bounty| BountyOutput {
id,
Expand Down