From 98a87d5168b16d0a948d695ffd1574e7a8f9d961 Mon Sep 17 00:00:00 2001 From: nninkovicSQA Date: Fri, 18 Feb 2022 15:16:11 +0100 Subject: [PATCH] Added custom_utils module to replace min/max std --- Cargo.lock | 8 ++++++++ Cargo.toml | 1 + build.sh | 3 ++- custom-utils/Cargo.toml | 9 +++++++++ custom-utils/build.sh | 5 +++++ custom-utils/res/custom_utils.wasm | Bin 0 -> 86 bytes custom-utils/src/lib.rs | 28 ++++++++++++++++++++++++++++ sputnikdao-factory2/Cargo.toml | 1 + sputnikdao-factory2/src/lib.rs | 3 ++- sputnikdao2/Cargo.toml | 1 + sputnikdao2/src/policy.rs | 11 +++++------ sputnikdao2/src/views.rs | 6 +++--- 12 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 custom-utils/Cargo.toml create mode 100755 custom-utils/build.sh create mode 100755 custom-utils/res/custom_utils.wasm create mode 100644 custom-utils/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index f5db17fcc..d04a07d7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "Inflector" version = "0.11.4" @@ -708,6 +710,10 @@ dependencies = [ "zeroize", ] +[[package]] +name = "custom-utils" +version = "1.0.0" + [[package]] name = "darling" version = "0.13.1" @@ -2779,6 +2785,7 @@ dependencies = [ name = "sputnikdao-factory2" version = "0.2.0" dependencies = [ + "custom-utils", "near-sdk", ] @@ -2786,6 +2793,7 @@ dependencies = [ name = "sputnikdao2" version = "2.0.0" dependencies = [ + "custom-utils", "hex", "near-contract-standards", "near-sdk", diff --git a/Cargo.toml b/Cargo.toml index bb50970fa..40598363a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "sputnik-staking", "sputnikdao2", "sputnikdao-factory2", + "custom-utils", "test-token" ] diff --git a/build.sh b/build.sh index 72ebe95bc..65a428992 100755 --- a/build.sh +++ b/build.sh @@ -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/ \ No newline at end of file +cp target/wasm32-unknown-unknown/release/test_token.wasm ./test-token/res/ +cp target/wasm32-unknown-unknown/release/custom_utils.wasm ./custom-utils/res/ \ No newline at end of file diff --git a/custom-utils/Cargo.toml b/custom-utils/Cargo.toml new file mode 100644 index 000000000..f57b97e29 --- /dev/null +++ b/custom-utils/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "custom-utils" +version = "1.0.0" +authors = ["Nemanja Ninkovic "] +edition = "2018" +publish = false + +[lib] +crate-type = ["cdylib", "rlib"] \ No newline at end of file diff --git a/custom-utils/build.sh b/custom-utils/build.sh new file mode 100755 index 000000000..55c8e3c4e --- /dev/null +++ b/custom-utils/build.sh @@ -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/ diff --git a/custom-utils/res/custom_utils.wasm b/custom-utils/res/custom_utils.wasm new file mode 100755 index 0000000000000000000000000000000000000000..639540286e293cbf7f61295c9ffadee0d1a5867d GIT binary patch literal 86 zcmZQbEY4+QU|?WjWh`K1WMpM#WDsDJWUgm)Y-l*Zz+KOPO0%mnv*o7d<`-2mF>uAl drzDmn#;4|`Ff($;$7iG_7Q`nd7N;^Z0RaAq6wd$v literal 0 HcmV?d00001 diff --git a/custom-utils/src/lib.rs b/custom-utils/src/lib.rs new file mode 100644 index 000000000..145ecddc9 --- /dev/null +++ b/custom-utils/src/lib.rs @@ -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; + } +} diff --git a/sputnikdao-factory2/Cargo.toml b/sputnikdao-factory2/Cargo.toml index 64a713c72..d3028110c 100644 --- a/sputnikdao-factory2/Cargo.toml +++ b/sputnikdao-factory2/Cargo.toml @@ -10,3 +10,4 @@ crate-type = ["cdylib", "rlib"] [dependencies] near-sdk = { version = "4.0.0-pre.4", features = ["unstable"] } +custom-utils = { path = "../custom-utils" } diff --git a/sputnikdao-factory2/src/lib.rs b/sputnikdao-factory2/src/lib.rs index 7aceff63b..f708583e8 100644 --- a/sputnikdao-factory2/src/lib.rs +++ b/sputnikdao-factory2/src/lib.rs @@ -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}; @@ -150,7 +151,7 @@ impl SputnikDAOFactory { /// Get daos in paginated view. pub fn get_daos(&self, from_index: u64, limit: u64) -> Vec { 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() } diff --git a/sputnikdao2/Cargo.toml b/sputnikdao2/Cargo.toml index 8f8c9b5c1..96e42a9a5 100644 --- a/sputnikdao2/Cargo.toml +++ b/sputnikdao2/Cargo.toml @@ -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" diff --git a/sputnikdao2/src/policy.rs b/sputnikdao2/src/policy.rs index e376330c5..f808cbbd9 100644 --- a/sputnikdao2/src/policy.rs +++ b/sputnikdao2/src/policy.rs @@ -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; @@ -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, ), @@ -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), ); diff --git a/sputnikdao2/src/views.rs b/sputnikdao2/src/views.rs index efa84348b..49cce43c7 100644 --- a/sputnikdao2/src/views.rs +++ b/sputnikdao2/src/views.rs @@ -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::*; @@ -87,7 +87,7 @@ impl Contract { /// Get proposals in paginated view. pub fn get_proposals(&self, from_index: u64, limit: u64) -> Vec { - (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, @@ -122,7 +122,7 @@ impl Contract { /// Get `limit` of bounties from given index. pub fn get_bounties(&self, from_index: u64, limit: u64) -> Vec { - (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,