From df9e5d74c6d86aec5ad764bfb5343618dd2d1623 Mon Sep 17 00:00:00 2001 From: Stefan Date: Mon, 2 Oct 2023 14:36:08 +0700 Subject: [PATCH] safe math (#340) --- Cargo.lock | 169 ++++++++++++------ game/pallet-game/src/features/common.rs | 3 +- game/pallet-game/src/features/loot_table.rs | 4 +- game/pallet-game/src/features/minting.rs | 2 +- game/pallet-game/src/features/upgrade_item.rs | 5 +- game/pallet-game/src/trades/auction.rs | 5 +- game/pallet-game/src/trades/retail.rs | 25 ++- game/randomness/src/features/randomness.rs | 4 +- game/randomness/src/lib.rs | 7 +- pallets/pallet-faucet/Cargo.toml | 2 + pallets/pallet-faucet/src/lib.rs | 43 +++-- support/src/common/currency.rs | 52 +++++- support/src/pool/pool_fee.rs | 4 +- 13 files changed, 228 insertions(+), 97 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cad4c633..93f81684 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,7 +82,7 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cipher 0.3.0", "cpufeatures", "opaque-debug 0.3.0", @@ -94,7 +94,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "433cfd6710c9986c576a25ca913c39d66a6474107b406f34f91d4a8923395241" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cipher 0.4.4", "cpufeatures", ] @@ -164,7 +164,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "getrandom 0.2.9", "once_cell", "version_check", @@ -398,7 +398,7 @@ checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af" dependencies = [ "async-lock", "autocfg", - "cfg-if", + "cfg-if 1.0.0", "concurrent-queue", "futures-lite", "log", @@ -485,7 +485,7 @@ checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca" dependencies = [ "addr2line 0.19.0", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide 0.6.2", "object 0.30.3", @@ -624,7 +624,7 @@ dependencies = [ "arrayref", "arrayvec 0.7.2", "cc", - "cfg-if", + "cfg-if 1.0.0", "constant_time_eq", ] @@ -837,6 +837,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -855,7 +861,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c80e5460aa66fe3b91d40bcbdab953a597b60053e34d684ac6903f863b680a6" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cipher 0.3.0", "cpufeatures", "zeroize", @@ -1061,7 +1067,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eeaa953eaad386a53111e47172c2fedba671e5684c8dd601a5f474f4f118710f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1193,7 +1199,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1202,7 +1208,7 @@ version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", ] @@ -1212,7 +1218,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-epoch", "crossbeam-utils", ] @@ -1224,7 +1230,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46bd5f3f85273295a9d14aedfb86f6aadbff6d8f5295c4a9edb08e819dcf5695" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", "memoffset 0.8.0", "scopeguard", @@ -1236,7 +1242,7 @@ version = "0.8.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1350,7 +1356,7 @@ version = "4.0.0-rc.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8d4ba9852b42210c7538b75484f9daa0655e9a3ac04f693747bb0f02cf3cfe16" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fiat-crypto", "packed_simd_2", "platforms 3.0.2", @@ -1616,7 +1622,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "339ee130d97a610ea5a5872d2bbb130fdf68884ff09d3028b81bec8a1ac23bbc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "dirs-sys-next", ] @@ -1979,7 +1985,7 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cbc844cecaee9d4443931972e1289c8ff485cb4cc2767cb03ca139ed6885153" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall 0.2.16", "windows-sys 0.48.0", @@ -2163,7 +2169,7 @@ version = "15.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "878babb0b136e731cc77ec2fd883ff02745ff21e6fb662729953d44923df009c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "parity-scale-codec", "scale-info", "serde", @@ -2529,7 +2535,7 @@ dependencies = [ "futures", "gafi-support", "game3-runtime", - "hex-literal", + "hex-literal 0.4.1", "jsonrpsee", "pallet-transaction-payment", "pallet-transaction-payment-rpc", @@ -2644,7 +2650,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi 0.9.0+wasi-snapshot-preview1", ] @@ -2655,7 +2661,7 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c85e1d9ab2eadba7e5040d4e09cbd6d072b76a557ad64e797c2cb9d4da21d7e4" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi 0.11.0+wasi-snapshot-preview1", ] @@ -2738,6 +2744,26 @@ dependencies = [ "subtle", ] +[[package]] +name = "gu-convertor" +version = "4.0.0-dev" +dependencies = [ + "frame-support", + "gafi-support", + "hex-literal 0.2.2", + "pallet-balances", + "parity-scale-codec", + "parity-secp256k1", + "rustc-hex", + "scale-info", + "serde", + "sha3", + "sp-core", + "sp-io", + "sp-runtime", + "sp-std", +] + [[package]] name = "h2" version = "0.3.18" @@ -2840,12 +2866,31 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d70693199b3cf4552f3fa720b54163927a3ebed2aef240efaf556033ab336a11" +dependencies = [ + "hex-literal-impl", + "proc-macro-hack", +] + [[package]] name = "hex-literal" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" +[[package]] +name = "hex-literal-impl" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59448fc2f82a5fb6907f78c3d69d843e82ff5b051923313cc4438cb0c7b745a8" +dependencies = [ + "proc-macro-hack", +] + [[package]] name = "hkdf" version = "0.12.3" @@ -3127,7 +3172,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -3388,7 +3433,7 @@ version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cadb76004ed8e97623117f3df85b17aaa6626ab0b0831e6573f104df16cd1bcc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "ecdsa 0.16.6", "elliptic-curve 0.13.4", "once_cell", @@ -3461,7 +3506,7 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "winapi", ] @@ -4263,7 +4308,7 @@ version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c84490118f2ee2d74570d114f3d0493cbf02790df303d2707606c3e14e07c96" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "downcast", "fragile", "lazy_static", @@ -4278,7 +4323,7 @@ version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ce75669015c4f47b289fd4d4f56e894e4c96003ffdf3ac51313126f94c6cbb" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "proc-macro2", "quote", "syn 1.0.109", @@ -4503,7 +4548,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fa52e972a9a719cecb6864fb88568781eb706bac2cd1d4f04a648542dbf78069" dependencies = [ "bitflags", - "cfg-if", + "cfg-if 1.0.0", "libc", "memoffset 0.6.5", ] @@ -4692,7 +4737,7 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1914cd452d8fccd6f9db48147b29fd4ae05bea9dc5d9ad578509f72415de282" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libm 0.1.4", ] @@ -4768,6 +4813,7 @@ dependencies = [ "frame-support", "frame-system", "gafi-support", + "gu-convertor", "pallet-balances", "pallet-cache", "pallet-timestamp", @@ -5013,6 +5059,17 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "parity-secp256k1" +version = "0.7.0" +source = "git+https://github.com/paritytech/rust-secp256k1#d05fd8e152f8d110b587906e3d854196b086e42a" +dependencies = [ + "arrayvec 0.5.2", + "cc", + "cfg-if 0.1.10", + "rand 0.7.3", +] + [[package]] name = "parity-send-wrapper" version = "0.1.0" @@ -5058,7 +5115,7 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60a2cfe6f0ad2bfc16aefa463b497d5c7a5ecd44a23efa72aa342d90177356dc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "instant", "libc", "redox_syscall 0.2.16", @@ -5072,7 +5129,7 @@ version = "0.9.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall 0.2.16", "smallvec", @@ -5271,7 +5328,7 @@ checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce" dependencies = [ "autocfg", "bitflags", - "cfg-if", + "cfg-if 1.0.0", "concurrent-queue", "libc", "log", @@ -5296,7 +5353,7 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "opaque-debug 0.3.0", "universal-hash 0.4.1", @@ -5308,7 +5365,7 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ef234e08c11dfcb2e56f79fd70f6f2eb7f025c0ce2333e82f4f0518ecad30c6" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "opaque-debug 0.3.0", "universal-hash 0.5.0", @@ -5407,6 +5464,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + [[package]] name = "proc-macro-warning" version = "0.3.1" @@ -5433,7 +5496,7 @@ version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "449811d15fbdf5ceb5c1144416066429cf82316e2ec8ce0c1f6f8a02e7bbcf8c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fnv", "lazy_static", "memchr", @@ -6550,7 +6613,7 @@ version = "0.10.0-dev" source = "git+https://github.com/grindytech/substrate?branch=gafi-polkadot-v0.9.42#b0de7e79a3cfba77a9e380f4be00fa98c23d498c" dependencies = [ "anyhow", - "cfg-if", + "cfg-if 1.0.0", "libc", "log", "once_cell", @@ -7160,7 +7223,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad560913365790f17cbf12479491169f01b9d46d29cfc7422bf8c64bdc61b731" dependencies = [ "bitvec", - "cfg-if", + "cfg-if 1.0.0", "derive_more", "parity-scale-codec", "scale-info-derive", @@ -7195,7 +7258,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "772575a524feeb803e5b0fcbc6dd9f367e579488197c94c6e4023aad2305774d" dependencies = [ "ahash 0.8.3", - "cfg-if", + "cfg-if 1.0.0", "hashbrown 0.13.2", ] @@ -7419,7 +7482,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug 0.3.0", @@ -7431,7 +7494,7 @@ version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.6", ] @@ -7455,7 +7518,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug 0.3.0", @@ -7467,7 +7530,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.6", ] @@ -8553,7 +8616,7 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9fbec84f381d5795b08656e4912bec604d162bff9291d6189a78f4c8ab87998" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "redox_syscall 0.3.5", "rustix 0.37.19", @@ -8607,7 +8670,7 @@ version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "once_cell", ] @@ -8870,7 +8933,7 @@ version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "log", "pin-project-lite 0.2.9", "tracing-attributes", @@ -8981,7 +9044,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f7f83d1e4a0e4358ac54c5c3681e5d7da5efc5a7a632c90bb6d6669ddd9bc26" dependencies = [ "async-trait", - "cfg-if", + "cfg-if 1.0.0", "data-encoding", "enum-as-inner", "futures-channel", @@ -9006,7 +9069,7 @@ version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aff21aa4dcefb0a1afbfac26deb0adc93888c7d295fb63ab273ef276ba2b7cfe" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "futures-util", "ipconfig", "lazy_static", @@ -9094,7 +9157,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "digest 0.10.6", "rand 0.8.5", "static_assertions", @@ -9304,7 +9367,7 @@ version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -9329,7 +9392,7 @@ version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -9481,7 +9544,7 @@ checksum = "76a222f5fa1e14b2cefc286f1b68494d7a965f4bf57ec04c59bb62673d639af6" dependencies = [ "anyhow", "bincode", - "cfg-if", + "cfg-if 1.0.0", "indexmap", "libc", "log", @@ -9507,7 +9570,7 @@ version = "6.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4407a7246e7d2f3d8fb1cf0c72fda8dbafdb6dd34d555ae8bea0e5ae031089cc" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -9579,7 +9642,7 @@ dependencies = [ "addr2line 0.17.0", "anyhow", "bincode", - "cfg-if", + "cfg-if 1.0.0", "cpp_demangle", "gimli 0.26.2", "log", @@ -9611,7 +9674,7 @@ version = "6.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43a28ae1e648461bfdbb79db3efdaee1bca5b940872e4175390f465593a2e54c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "windows-sys 0.42.0", ] @@ -9624,7 +9687,7 @@ checksum = "e704b126e4252788ccfc3526d4d4511d4b23c521bf123e447ac726c14545217b" dependencies = [ "anyhow", "cc", - "cfg-if", + "cfg-if 1.0.0", "indexmap", "libc", "log", diff --git a/game/pallet-game/src/features/common.rs b/game/pallet-game/src/features/common.rs index ed749aa0..6155ab16 100644 --- a/game/pallet-game/src/features/common.rs +++ b/game/pallet-game/src/features/common.rs @@ -1,6 +1,7 @@ /// Item module provides utility functions for pallet-game use crate::*; use frame_support::pallet_prelude::*; +use sp_runtime::traits::Saturating; impl, I: 'static> Pallet { /// Transfer an `amount` of `item` in `collection` from `from` to `to`. @@ -113,7 +114,7 @@ impl, I: 'static> Pallet { pub fn total_weight(table: &LootTable) -> u32 { let mut counter = 0; for package in table { - counter += package.weight; + counter = counter.saturating_add(package.weight); } counter } diff --git a/game/pallet-game/src/features/loot_table.rs b/game/pallet-game/src/features/loot_table.rs index 4f49ae19..ec474da1 100644 --- a/game/pallet-game/src/features/loot_table.rs +++ b/game/pallet-game/src/features/loot_table.rs @@ -28,7 +28,7 @@ impl, I: 'static> Pallet { while i < table.len() && positon > table[i].weight { positon -= table[i].weight; - i += 1; + i.saturating_inc(); } if i < table.len() { @@ -66,7 +66,7 @@ impl, I: 'static> Pallet { while i < table.len() && positon > table[i].weight { positon -= table[i].weight; - i += 1; + i.saturating_inc(); } if i < table.len() { diff --git a/game/pallet-game/src/features/minting.rs b/game/pallet-game/src/features/minting.rs index 20b690ba..57ef77b9 100644 --- a/game/pallet-game/src/features/minting.rs +++ b/game/pallet-game/src/features/minting.rs @@ -130,7 +130,7 @@ impl, I: 'static> let reserve = pool_details.mint_settings.price.saturating_mul(amount.into()); >::Currency::reserve(&who, reserve)?; - let execute_block = block_number + T::MintInterval::get(); + let execute_block = block_number.saturating_add(T::MintInterval::get()); let mint_request = MintRequest { miner: who.clone(), diff --git a/game/pallet-game/src/features/upgrade_item.rs b/game/pallet-game/src/features/upgrade_item.rs index 2a580487..61b9d065 100644 --- a/game/pallet-game/src/features/upgrade_item.rs +++ b/game/pallet-game/src/features/upgrade_item.rs @@ -1,6 +1,6 @@ use crate::*; use frame_support::{pallet_prelude::*, traits::ExistenceRequirement}; -use gafi_support::game::{Amount, UpgradeItem, Level}; +use gafi_support::game::{Amount, Level, UpgradeItem}; use sp_runtime::Saturating; impl, I: 'static> @@ -72,7 +72,8 @@ impl, I: 'static> ) -> DispatchResult { ensure!(amount > 0, Error::::InvalidAmount); - let next_level = LevelOf::::get(collection, item) + 1; + let mut next_level = LevelOf::::get(collection, item); + next_level.saturating_inc(); // get origin item let origin_item = match OriginItemOf::::get((collection, item)) { diff --git a/game/pallet-game/src/trades/auction.rs b/game/pallet-game/src/trades/auction.rs index 15b91e2f..3dc4e4f9 100644 --- a/game/pallet-game/src/trades/auction.rs +++ b/game/pallet-game/src/trades/auction.rs @@ -1,6 +1,7 @@ use crate::*; use frame_support::{pallet_prelude::*, traits::BalanceStatus}; use gafi_support::game::{Auction, Bundle}; +use sp_runtime::Saturating; impl, I: 'static> Auction, T::BlockNumber> @@ -70,7 +71,7 @@ impl, I: 'static> Error::::AuctionNotStarted ); ensure!( - block_number < config.start_block + config.duration, + block_number < config.start_block.saturating_add(config.duration), Error::::AuctionEnded ); @@ -101,7 +102,7 @@ impl, I: 'static> let block_number = >::block_number(); ensure!( - block_number >= (config.start_block + config.duration), + block_number >= (config.start_block.saturating_add(config.duration)), Error::::AuctionInProgress ); let maybe_bid = HighestBidOf::::get(trade); diff --git a/game/pallet-game/src/trades/retail.rs b/game/pallet-game/src/trades/retail.rs index 68f1f2bb..68403289 100644 --- a/game/pallet-game/src/trades/retail.rs +++ b/game/pallet-game/src/trades/retail.rs @@ -7,7 +7,8 @@ use gafi_support::game::{Amount, Package, Retail, TradeType}; use sp_runtime::Saturating; impl, I: 'static> - Retail, BlockNumber> for Pallet + Retail, BlockNumber> + for Pallet { fn do_set_price( trade: &T::TradeId, @@ -118,8 +119,11 @@ impl, I: 'static> ItemBalanceStatus::Free, )?; - let new_package = - Package::new(package.collection, package.item, package.amount - amount); + let new_package = Package::new( + package.collection, + package.item, + package.amount.saturating_sub(amount), + ); >::try_mutate(trade, |package_vec| -> DispatchResult { *package_vec = BundleFor::::try_from([new_package].to_vec()) @@ -206,7 +210,7 @@ impl, I: 'static> let new_package = Package::new( package.collection, package.item, - package.amount + supply.amount, + package.amount.saturating_add(supply.amount), ); >::try_mutate(trade, |package_vec| -> DispatchResult { @@ -238,7 +242,7 @@ impl, I: 'static> // ensure reserve deposit >::Currency::reserve(&who, T::BundleDeposit::get())?; - let deposit = unit_price * package.amount.into(); + let deposit = unit_price.saturating_mul(package.amount.into()); >::Currency::reserve(&who, deposit)?; >::try_mutate(trade, |package_vec| -> DispatchResult { @@ -280,7 +284,7 @@ impl, I: 'static> ) -> DispatchResult { if let Some(config) = TradeConfigOf::::get(trade) { ensure!(config.trade == TradeType::SetBuy, Error::::NotSetBuy); - + let block_number = >::block_number(); if let Some(start_block) = config.start_block { ensure!(block_number >= start_block, Error::::TradeNotStarted); @@ -315,12 +319,15 @@ impl, I: 'static> >::Currency::repatriate_reserved( &config.owner, &who, - price * amount.into(), + price.saturating_mul(amount.into()), BalanceStatus::Free, )?; - let new_package = - Package::new(package.collection, package.item, package.amount - amount); + let new_package = Package::new( + package.collection, + package.item, + package.amount.saturating_sub(amount), + ); >::try_mutate(trade, |package_vec| -> DispatchResult { *package_vec = BundleFor::::try_from([new_package].to_vec()) diff --git a/game/randomness/src/features/randomness.rs b/game/randomness/src/features/randomness.rs index fe6f1af7..6152bd12 100644 --- a/game/randomness/src/features/randomness.rs +++ b/game/randomness/src/features/randomness.rs @@ -40,12 +40,12 @@ impl GameRandomness for Pallet { } let mut random_number = Self::generate_random_number(seed); for _ in 1..T::RandomAttemps::get() { - if random_number < u32::MAX.saturating_sub(u32::MAX % total) { + if random_number < u32::MAX.saturating_sub(u32::MAX.wrapping_rem(total)) { break } random_number = Self::generate_random_number(seed); } - return Some((random_number % total).saturating_add(1)) + return Some((random_number.wrapping_rem(total)).saturating_add(1)) } None } diff --git a/game/randomness/src/lib.rs b/game/randomness/src/lib.rs index b252db28..32b370b9 100644 --- a/game/randomness/src/lib.rs +++ b/game/randomness/src/lib.rs @@ -15,6 +15,7 @@ use sp_core::{offchain::KeyTypeId, Get}; use sp_runtime::{ traits::TrailingZeroInput, transaction_validity::{InvalidTransaction, TransactionValidity, ValidTransaction}, + Saturating, }; mod features; @@ -54,7 +55,9 @@ pub mod crypto { } /// Payload used to hold seed data required to submit a transaction. -#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo, MaxEncodedLen)] +#[derive( + Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo, MaxEncodedLen, +)] pub struct SeedPayload { block_number: BlockNumber, seed: Seed, @@ -167,7 +170,7 @@ pub mod pallet { ensure_none(origin)?; RandomSeed::::put(SeedPayload { block_number, seed }); - >::put(block_number + T::UnsignedInterval::get()); + >::put(block_number.saturating_add(T::UnsignedInterval::get())); Self::deposit_event(Event::::NewSeed { block_number, seed }); Ok(()) } diff --git a/pallets/pallet-faucet/Cargo.toml b/pallets/pallet-faucet/Cargo.toml index 64e9fb77..f97ebde5 100644 --- a/pallets/pallet-faucet/Cargo.toml +++ b/pallets/pallet-faucet/Cargo.toml @@ -23,6 +23,7 @@ sp-std = {workspace = true} # Local gafi-support = {version = "4.0.0-dev", default-features = false, path = "../../support"} +gu-convertor = { default-features = false, version = "4.0.0-dev", path = "../../utils/convertor" } [dev-dependencies] pallet-balances = {workspace = true} @@ -38,6 +39,7 @@ std = [ "codec/std", "scale-info/std", "frame-support/std", + "gu-convertor/std", "frame-system/std", "sp-runtime/std", "pallet-balances/std", diff --git a/pallets/pallet-faucet/src/lib.rs b/pallets/pallet-faucet/src/lib.rs index 74c07e34..aa04c352 100644 --- a/pallets/pallet-faucet/src/lib.rs +++ b/pallets/pallet-faucet/src/lib.rs @@ -1,10 +1,11 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub use crate::weights::WeightInfo; use frame_support::traits::{Currency, ExistenceRequirement}; +use gafi_support::pallet::cache::Cache; +use gu_convertor::{balance_try_to_u128, u128_to_balance}; pub use pallet::*; -pub use crate::weights::WeightInfo; use sp_std::vec; -use gafi_support::pallet::cache::Cache; #[cfg(test)] mod mock; @@ -41,7 +42,7 @@ pub mod pallet { type WeightInfo: WeightInfo; /// Add Cache - type Cache: Cache ,u128> ; + type Cache: Cache, u128>; /// Faucet Amount type FaucetAmount: Get>; @@ -64,7 +65,9 @@ pub mod pallet { #[cfg(feature = "std")] impl Default for GenesisConfig { fn default() -> Self { - Self { genesis_accounts: vec![]} + Self { + genesis_accounts: vec![], + } } } @@ -110,10 +113,17 @@ pub mod pallet { let sender = ensure_signed(origin)?; let genesis_accounts = GenesisAccounts::::get(); let faucet_amount = T::FaucetAmount::get(); + let faucet_u128 = balance_try_to_u128::<::Currency, T::AccountId>( + faucet_amount, + )?; + + let amount = faucet_u128.saturating_div(10u128); + let faucet = u128_to_balance::<::Currency, T::AccountId>(amount); + ensure!(Self::get_cache(&sender) == None, >::PleaseWait); ensure!( - T::Currency::free_balance(&sender) < (faucet_amount / 10u128.try_into().ok().unwrap()), + T::Currency::free_balance(&sender) < faucet, >::DontBeGreedy ); @@ -144,13 +154,13 @@ pub mod pallet { /// Weight: `O(1)` #[pallet::call_index(1)] #[pallet::weight(::WeightInfo::donate(50u32))] - pub fn donate( - origin: OriginFor, - amount: BalanceOf, - ) -> DispatchResult { + pub fn donate(origin: OriginFor, amount: BalanceOf) -> DispatchResult { let from = ensure_signed(origin)?; - ensure!(T::Currency::free_balance(&from) > amount, >::NotEnoughBalance); + ensure!( + T::Currency::free_balance(&from) > amount, + >::NotEnoughBalance + ); let genesis_accounts = GenesisAccounts::::get(); ensure!(genesis_accounts[0] != from, >::SelfTransfer); @@ -161,24 +171,27 @@ pub mod pallet { ExistenceRequirement::KeepAlive, )?; - Self::deposit_event(Event::Transferred(from, genesis_accounts[0].clone(), amount)); + Self::deposit_event(Event::Transferred( + from, + genesis_accounts[0].clone(), + amount, + )); Ok(()) } } impl Pallet { - fn insert_cache(sender: T::AccountId, faucet_amount: BalanceOf)-> Option<()> { - match faucet_amount.try_into(){ + fn insert_cache(sender: T::AccountId, faucet_amount: BalanceOf) -> Option<()> { + match faucet_amount.try_into() { Ok(value) => Some(T::Cache::insert(&sender, sender.clone(), value)), Err(_) => None, } - } fn get_cache(sender: &T::AccountId) -> Option { if let Some(faucet_cache) = T::Cache::get(sender, sender.clone()) { - return Some(faucet_cache); + return Some(faucet_cache) } None } diff --git a/support/src/common/currency.rs b/support/src/common/currency.rs index 5dfcac07..7618cb6d 100644 --- a/support/src/common/currency.rs +++ b/support/src/common/currency.rs @@ -60,29 +60,69 @@ impl TokenInfo for GafiCurrency { /// assert_eq!(balance, 10_000_000_000_000_000_000); /// ``` pub fn unit(token: NativeToken) -> u128 { - 10u128.saturating_pow(GafiCurrency::token_info(token).decimals.into()) + 10u128.saturating_pow(GafiCurrency::token_info(token).decimals.into()) } /// 1 centi = 0.01 unit pub fn centi(token: NativeToken) -> u128 { - unit(token) / 100 + unit(token).saturating_div(100) } /// 1 milli = 0.001 unit pub fn milli(token: NativeToken) -> u128 { - unit(token) / 1000 + unit(token).saturating_div(1000) } /// 1 millicent = 0.00001 unit pub fn millicent(token: NativeToken) -> u128 { - centi(token) / 1000 + centi(token).saturating_div(1000) } /// 1 microcent = 0.000001 unit pub fn microcent(token: NativeToken) -> u128 { - milli(token) / 1000 + milli(token).saturating_div(1000) } pub fn deposit(items: u32, bytes: u32, token: NativeToken) -> Balance { - items as Balance * 20 * unit(token.clone()) + (bytes as Balance) * 100 * millicent(token) + let mut balance = items as Balance; + balance = balance + .saturating_mul(20) + .saturating_mul(unit(token.clone())) + .saturating_add((bytes as Balance).saturating_mul(100).saturating_mul(millicent(token))); + balance } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_unit() { + assert_eq!(unit(NativeToken::GAKI), 1_000_000_000_000_000_000); + } + + #[test] + fn test_centi() { + assert_eq!(centi(NativeToken::GAKI), 10_000_000_000_000_000); + } + + #[test] + fn test_milli() { + assert_eq!(milli(NativeToken::GAKI), 1_000_000_000_000_000); + } + + #[test] + fn test_millicent() { + assert_eq!(millicent(NativeToken::GAKI), 10_000_000_000_000); + } + + #[test] + fn test_microcent() { + assert_eq!(microcent(NativeToken::GAKI), 1_000_000_000_000); + } + + #[test] + fn test_deposit() { + assert_eq!(deposit(5, 100, NativeToken::GAKI), 100_100_000_000_000_000_000); + } +} \ No newline at end of file diff --git a/support/src/pool/pool_fee.rs b/support/src/pool/pool_fee.rs index 583745f4..0700aa9c 100644 --- a/support/src/pool/pool_fee.rs +++ b/support/src/pool/pool_fee.rs @@ -2,7 +2,7 @@ use frame_support::pallet_prelude::*; #[cfg(feature = "std")] use frame_support::serde::{Deserialize, Serialize}; use scale_info::TypeInfo; -use sp_runtime::{Permill, RuntimeDebug}; +use sp_runtime::{traits::Saturating, Permill, RuntimeDebug}; use sp_std::vec::Vec; use crate::common::ID; @@ -33,7 +33,7 @@ pub trait MasterPool { impl MasterPool for () { fn remove_player(_player: &AccountId, _pool_id: ID) {} fn get_timeservice() -> u128 { - 30 * 60_000u128 // 30 minutes + 30.saturating_mul(60_000u128) // 30 minutes } fn get_marktime() -> u128 { u128::default()